001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.functor.core.composite;
018
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertTrue;
021
022import java.util.Arrays;
023
024import org.apache.commons.functor.BaseFunctorTest;
025import org.apache.commons.functor.NullaryPredicate;
026import org.apache.commons.functor.core.Constant;
027import org.junit.Test;
028
029/**
030 * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $
031 */
032public class TestNullaryAnd extends BaseFunctorTest {
033
034    // Functor Testing Framework
035    // ------------------------------------------------------------------------
036
037    @Override
038    protected Object makeFunctor() {
039        return new NullaryAnd(Constant.TRUE, Constant.TRUE);
040    }
041
042    // Tests
043    // ------------------------------------------------------------------------
044
045    @Test
046    public void testTrue() throws Exception {
047        assertTrue((new NullaryAnd()).test());
048        assertTrue((new NullaryAnd(Constant.TRUE)).test());
049        assertTrue((new NullaryAnd(Constant.TRUE,Constant.TRUE)).test());
050        assertTrue((new NullaryAnd(Constant.TRUE,Constant.TRUE,Constant.TRUE)).test());
051
052        NullaryAnd p = new NullaryAnd(Constant.TRUE);
053        assertTrue(p.test());
054        for (int i=0;i<10;i++) {
055            p.and(Constant.TRUE);
056            assertTrue(p.test());
057        }
058
059        NullaryAnd q = new NullaryAnd(Constant.TRUE);
060        assertTrue(q.test());
061        for (int i=0;i<10;i++) {
062            q.and(Constant.TRUE);
063            assertTrue(q.test());
064        }
065
066        NullaryAnd r = new NullaryAnd(p,q);
067        assertTrue(r.test());
068    }
069
070    @Test
071    public void testFalse() throws Exception {
072        assertTrue(!(new NullaryAnd(Constant.FALSE)).test());
073        assertTrue(!(new NullaryAnd(Constant.TRUE,Constant.FALSE)).test());
074        assertTrue(!(new NullaryAnd(Constant.TRUE,Constant.TRUE,Constant.FALSE)).test());
075
076        NullaryAnd p = new NullaryAnd(Constant.FALSE);
077        assertTrue(!p.test());
078        for (int i=0;i<10;i++) {
079            p.and(Constant.FALSE);
080            assertTrue(!p.test());
081        }
082
083        NullaryAnd q = new NullaryAnd(Constant.TRUE);
084        assertTrue(q.test());
085        for (int i=0;i<10;i++) {
086            q.and(Constant.TRUE);
087            assertTrue(q.test());
088        }
089
090        NullaryAnd r = new NullaryAnd(p,q);
091        assertTrue(!r.test());
092    }
093
094    @Test
095    public void testDuplicateAdd() throws Exception {
096        NullaryPredicate p = Constant.TRUE;
097        NullaryAnd q = new NullaryAnd(p,p);
098        assertTrue(q.test());
099        for (int i=0;i<10;i++) {
100            q.and(p);
101            assertTrue(q.test());
102        }
103    }
104
105    @Test
106    public void testEquals() throws Exception {
107        NullaryAnd p = new NullaryAnd();
108        assertEquals(p,p);
109        NullaryAnd q = new NullaryAnd();
110        assertObjectsAreEqual(p,q);
111
112        for (int i=0;i<3;i++) {
113            p.and(Constant.TRUE);
114            assertObjectsAreNotEqual(p,q);
115            q.and(Constant.truePredicate());
116            assertObjectsAreEqual(p,q);
117            p.and(new NullaryAnd(Constant.TRUE,Constant.FALSE));
118            assertObjectsAreNotEqual(p,q);
119            q.and(new NullaryAnd(Constant.TRUE,Constant.FALSE));
120            assertObjectsAreEqual(p,q);
121        }
122
123        assertObjectsAreNotEqual(p,Constant.TRUE);
124        Iterable<NullaryPredicate> iterable = Arrays.<NullaryPredicate>asList(
125            Constant.TRUE,
126            new NullaryAnd(Constant.TRUE, Constant.FALSE),
127            Constant.TRUE,
128            new NullaryAnd(Constant.TRUE, Constant.FALSE),
129            Constant.TRUE,
130            new NullaryAnd(Constant.TRUE, Constant.FALSE));
131        assertObjectsAreEqual(p,new NullaryAnd(iterable));
132
133        assertObjectsAreNotEqual(p,new NullaryAnd((Iterable<NullaryPredicate>)null));
134        assertObjectsAreNotEqual(p,new NullaryAnd((NullaryPredicate[])null));
135        assertObjectsAreNotEqual(p,new NullaryAnd((NullaryPredicate)null));
136
137        assertTrue(!p.equals(null));
138    }
139
140}