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.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.commons.functor.BaseFunctorTest;
24  import org.apache.commons.functor.Predicate;
25  import org.apache.commons.functor.core.Constant;
26  import org.junit.Test;
27  
28  /**
29   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
30   * @author Rodney Waldhoff
31   */
32  public class TestOr extends BaseFunctorTest {
33  
34      // Functor Testing Framework
35      // ------------------------------------------------------------------------
36  
37      protected Object makeFunctor() {
38          return new Or(Constant.FALSE, Constant.TRUE);
39      }
40  
41      // Tests
42      // ------------------------------------------------------------------------
43  
44      @Test
45      public void testTrue() throws Exception {
46          assertTrue((new Or(Constant.TRUE)).test());
47          assertTrue((new Or(Constant.FALSE, Constant.TRUE)).test());
48          assertTrue((new Or(Constant.FALSE, Constant.FALSE, Constant.TRUE)).test());
49  
50          Or p = new Or(Constant.TRUE);
51          assertTrue(p.test());
52          for (int i=0;i<10;i++) {
53              p.or(Constant.of(i%2==0));
54              assertTrue(p.test());
55          }
56  
57          Or q = new Or(Constant.TRUE);
58          assertTrue(q.test());
59          for (int i=0;i<10;i++) {
60              q.or(Constant.of(i%2==0));
61              assertTrue(q.test());
62          }
63  
64          Or r = new Or(p,q);
65          assertTrue(r.test());
66      }
67  
68      @Test
69      public void testFalse() throws Exception {
70          assertFalse(new Or().test());
71          assertFalse(new Or(Constant.FALSE).test());
72          assertFalse(new Or(Constant.FALSE,Constant.FALSE).test());
73          assertFalse(new Or(Constant.FALSE,Constant.FALSE,Constant.FALSE).test());
74  
75          Or p = new Or(Constant.FALSE);
76          assertFalse(p.test());
77          for (int i=0;i<10;i++) {
78              p.or(Constant.FALSE);
79              assertFalse(p.test());
80          }
81  
82          Or q = new Or(Constant.FALSE);
83          assertFalse(q.test());
84          for (int i=0;i<10;i++) {
85              q.or(Constant.FALSE);
86              assertFalse(q.test());
87          }
88  
89          Or r = new Or(p,q);
90          assertTrue(!r.test());
91      }
92  
93      @Test
94      public void testDuplicateAdd() throws Exception {
95          Predicate p = Constant.TRUE;
96          Or q = new Or(p,p);
97          assertTrue(q.test());
98          for (int i=0;i<10;i++) {
99              q.or(p);
100             assertTrue(q.test());
101         }
102     }
103 
104     @Test
105     public void testEquals() throws Exception {
106         Or p = new Or();
107         assertEquals(p,p);
108 
109         Or q = new Or();
110         assertObjectsAreEqual(p,q);
111 
112         And r = new And();
113         assertObjectsAreNotEqual(p,r);
114 
115         for (int i=0;i<3;i++) {
116             p.or(Constant.TRUE);
117             assertObjectsAreNotEqual(p,q);
118             q.or(Constant.TRUE);
119             assertObjectsAreEqual(p,q);
120             r.and(Constant.TRUE);
121             assertObjectsAreNotEqual(p,r);
122 
123             p.or(new Or(Constant.TRUE,Constant.FALSE));
124             assertObjectsAreNotEqual(p,q);
125             q.or(new Or(Constant.TRUE,Constant.FALSE));
126             assertObjectsAreEqual(p,q);
127             r.and(new Or(Constant.TRUE,Constant.FALSE));
128             assertObjectsAreNotEqual(p,r);
129         }
130 
131         assertObjectsAreNotEqual(p,Constant.TRUE);
132     }
133 
134 }