1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.core.composite;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  
22  import org.apache.commons.functor.BaseFunctorTest;
23  import org.apache.commons.functor.BinaryPredicate;
24  import org.apache.commons.functor.core.Constant;
25  import org.junit.Test;
26  
27  /**
28   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
29   * @author Rodney Waldhoff
30   */
31  public class TestBinaryAnd extends BaseFunctorTest {
32  
33      // Functor Testing Framework
34      // ------------------------------------------------------------------------
35  
36      protected Object makeFunctor() {
37          return new BinaryAnd<Object, Object>(Constant.TRUE, Constant.TRUE);
38      }
39  
40      // Tests
41      // ------------------------------------------------------------------------
42  
43      @Test
44      public void testTrue() throws Exception {
45          assertTrue((new BinaryAnd<String, Integer>()).test("xyzzy",3));
46          assertTrue((new BinaryAnd<String, Integer>(Constant.TRUE)).test("xyzzy",3));
47          assertTrue((new BinaryAnd<String, Integer>(Constant.TRUE,Constant.TRUE)).test("xyzzy",3));
48          assertTrue((new BinaryAnd<String, Integer>(Constant.TRUE,Constant.TRUE,Constant.TRUE)).test("xyzzy",3));
49  
50          BinaryAnd<String, Integer> p = new BinaryAnd<String, Integer>(Constant.TRUE);
51          assertTrue(p.test("xyzzy",3));
52          for (int i=0;i<10;i++) {
53              p.and(Constant.TRUE);
54              assertTrue(p.test("xyzzy",3));
55          }
56  
57          BinaryAnd<String, Integer> q = new BinaryAnd<String, Integer>(Constant.TRUE);
58          assertTrue(q.test("xyzzy",3));
59          for (int i=0;i<10;i++) {
60              q.and(Constant.TRUE);
61              assertTrue(q.test("xyzzy",3));
62          }
63  
64          BinaryAnd<String, Integer> r = new BinaryAnd<String, Integer>(p,q);
65          assertTrue(r.test("xyzzy",3));
66      }
67  
68      @Test
69      public void testFalse() throws Exception {
70          assertTrue(!(new BinaryAnd<String, Integer>(Constant.FALSE)).test("xyzzy",3));
71          assertTrue(!(new BinaryAnd<String, Integer>(Constant.TRUE,Constant.FALSE)).test("xyzzy",3));
72          assertTrue(!(new BinaryAnd<String, Integer>(Constant.TRUE,Constant.TRUE,Constant.FALSE)).test("xyzzy",3));
73  
74          BinaryAnd<String, Integer> p = new BinaryAnd<String, Integer>(Constant.FALSE);
75          assertTrue(!p.test("xyzzy",3));
76          for (int i=0;i<10;i++) {
77              p.and(Constant.FALSE);
78              assertTrue(!p.test("xyzzy",3));
79          }
80  
81          BinaryAnd<String, Integer> q = new BinaryAnd<String, Integer>(Constant.TRUE);
82          assertTrue(q.test("xyzzy",3));
83          for (int i=0;i<10;i++) {
84              q.and(Constant.TRUE);
85              assertTrue(q.test("xyzzy",3));
86          }
87  
88          BinaryAnd<String, Integer> r = new BinaryAnd<String, Integer>(p,q);
89          assertTrue(!r.test("xyzzy",3));
90      }
91  
92      @Test
93      public void testDuplicateAdd() throws Exception {
94          BinaryPredicate<Object, Object> p = Constant.TRUE;
95          BinaryAnd<String, Integer> q = new BinaryAnd<String, Integer>(p,p);
96          assertTrue(q.test("xyzzy",3));
97          for (int i=0;i<10;i++) {
98              q.and(p);
99              assertTrue(q.test("xyzzy",3));
100         }
101     }
102 
103     @Test
104     public void testEquals() throws Exception {
105         BinaryAnd<Object, Object> p = new BinaryAnd<Object, Object>();
106         assertEquals(p,p);
107         BinaryAnd<Object, Object> q = new BinaryAnd<Object, Object>();
108         assertObjectsAreEqual(p,q);
109         BinaryOr<Object, Object> r = new BinaryOr<Object, Object>();
110         assertObjectsAreNotEqual(p,r);
111 
112         for (int i=0;i<3;i++) {
113             p.and(Constant.truePredicate());
114             assertObjectsAreNotEqual(p,q);
115             q.and(Constant.truePredicate());
116             assertObjectsAreEqual(p,q);
117             p.and(new BinaryAnd<Object, Object>(Constant.truePredicate(),Constant.falsePredicate()));
118             assertObjectsAreNotEqual(p,q);
119             q.and(new BinaryAnd<Object, Object>(Constant.truePredicate(),Constant.falsePredicate()));
120             assertObjectsAreEqual(p,q);
121         }
122 
123         assertObjectsAreNotEqual(p,Constant.truePredicate());
124     }
125 
126 }