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 TestBinaryOr extends BaseFunctorTest {
32  
33      // Functor Testing Framework
34      // ------------------------------------------------------------------------
35  
36      protected Object makeFunctor() {
37          return new BinaryOr<Object, Object>(Constant.FALSE, Constant.TRUE);
38      }
39  
40      // Tests
41      // ------------------------------------------------------------------------
42  
43      @Test
44      public void testTrue() throws Exception {
45          assertTrue((new BinaryOr<Object, Object>(Constant.TRUE)).test("xyzzy",new Integer(3)));
46          assertTrue((new BinaryOr<Object, Object>(Constant.FALSE,Constant.TRUE)).test("xyzzy",new Integer(3)));
47          assertTrue((new BinaryOr<Object, Object>(Constant.FALSE, Constant.FALSE, Constant.TRUE)).test("xyzzy",new Integer(3)));
48  
49          BinaryOr<Object, Object> p = new BinaryOr<Object, Object>(Constant.TRUE);
50          assertTrue(p.test("xyzzy",new Integer(3)));
51          for (int i=0;i<10;i++) {
52              p.or(Constant.of(i%2==0));
53              assertTrue(p.test("xyzzy",new Integer(3)));
54          }
55  
56          BinaryOr<Object, Object> q = new BinaryOr<Object, Object>(Constant.TRUE);
57          assertTrue(q.test("xyzzy",new Integer(3)));
58          for (int i=0;i<10;i++) {
59              q.or(Constant.of(i%2==0));
60              assertTrue(q.test("xyzzy",new Integer(3)));
61          }
62  
63          BinaryOr<Object, Object> r = new BinaryOr<Object, Object>(p,q);
64          assertTrue(r.test("xyzzy",new Integer(3)));
65      }
66  
67      @Test
68      public void testFalse() throws Exception {
69          assertTrue(!(new BinaryOr<Object, Object>()).test("xyzzy",new Integer(3)));
70          assertTrue(!(new BinaryOr<Object, Object>(Constant.FALSE)).test("xyzzy",new Integer(3)));
71          assertTrue(!(new BinaryOr<Object, Object>(Constant.FALSE,Constant.FALSE)).test("xyzzy",new Integer(3)));
72          assertTrue(!(new BinaryOr<Object, Object>(Constant.FALSE,Constant.FALSE,Constant.FALSE)).test("xyzzy",new Integer(3)));
73  
74          BinaryOr<Object, Object> p = new BinaryOr<Object, Object>(Constant.FALSE);
75          assertTrue(!p.test("xyzzy",new Integer(3)));
76          for (int i=0;i<10;i++) {
77              p.or(Constant.FALSE);
78              assertTrue(!p.test("xyzzy",new Integer(3)));
79          }
80  
81          BinaryOr<Object, Object> q = new BinaryOr<Object, Object>(Constant.FALSE);
82          assertTrue(!q.test("xyzzy",new Integer(3)));
83          for (int i=0;i<10;i++) {
84              q.or(Constant.FALSE);
85              assertTrue(!q.test("xyzzy",new Integer(3)));
86          }
87  
88          BinaryOr<Object, Object> r = new BinaryOr<Object, Object>(p,q);
89          assertTrue(!r.test("xyzzy",new Integer(3)));
90      }
91  
92      @Test
93      public void testDuplicateAdd() throws Exception {
94          BinaryPredicate<Object, Object> p = Constant.TRUE;
95          BinaryOr<Object, Object> q = new BinaryOr<Object, Object>(p,p);
96          assertTrue(q.test("xyzzy",new Integer(3)));
97          for (int i=0;i<10;i++) {
98              q.or(p);
99              assertTrue(q.test("xyzzy",new Integer(3)));
100         }
101     }
102 
103     @Test
104     public void testEquals() throws Exception {
105         BinaryOr<Object, Object> p = new BinaryOr<Object, Object>();
106         assertEquals(p,p);
107 
108         BinaryOr<Object, Object> q = new BinaryOr<Object, Object>();
109         assertObjectsAreEqual(p,q);
110 
111         BinaryAnd<Object, Object> r = new BinaryAnd<Object, Object>();
112         assertObjectsAreNotEqual(p,r);
113 
114         for (int i=0;i<3;i++) {
115             p.or(Constant.TRUE);
116             assertObjectsAreNotEqual(p,q);
117             q.or(Constant.TRUE);
118             assertObjectsAreEqual(p,q);
119             r.and(Constant.TRUE);
120             assertObjectsAreNotEqual(p,r);
121 
122             p.or(new BinaryOr<Object, Object>(Constant.truePredicate(),Constant.FALSE));
123             assertObjectsAreNotEqual(p,q);
124             q.or(new BinaryOr<Object, Object>(Constant.truePredicate(),Constant.FALSE));
125             assertObjectsAreEqual(p,q);
126             r.and(new BinaryOr<Object, Object>(Constant.truePredicate(),Constant.FALSE));
127             assertObjectsAreNotEqual(p,r);
128         }
129 
130         assertObjectsAreNotEqual(p,Constant.TRUE);
131     }
132 
133 }