1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Predicate;
24 import org.apache.commons.functor.core.Constant;
25 import org.junit.Test;
26
27
28
29
30
31 public class TestAnd extends BaseFunctorTest {
32
33
34
35
36 protected Object makeFunctor() {
37 return new And(Constant.TRUE, Constant.TRUE);
38 }
39
40
41
42
43 @Test
44 public void testTrue() throws Exception {
45 assertTrue((new And()).test());
46 assertTrue((new And(Constant.TRUE)).test());
47 assertTrue((new And(Constant.TRUE,Constant.TRUE)).test());
48 assertTrue((new And(Constant.TRUE,Constant.TRUE,Constant.TRUE)).test());
49
50 And p = new And(Constant.TRUE);
51 assertTrue(p.test());
52 for (int i=0;i<10;i++) {
53 p.and(Constant.TRUE);
54 assertTrue(p.test());
55 }
56
57 And q = new And(Constant.TRUE);
58 assertTrue(q.test());
59 for (int i=0;i<10;i++) {
60 q.and(Constant.TRUE);
61 assertTrue(q.test());
62 }
63
64 And r = new And(p,q);
65 assertTrue(r.test());
66 }
67
68 @Test
69 public void testFalse() throws Exception {
70 assertTrue(!(new And(Constant.FALSE)).test());
71 assertTrue(!(new And(Constant.TRUE,Constant.FALSE)).test());
72 assertTrue(!(new And(Constant.TRUE,Constant.TRUE,Constant.FALSE)).test());
73
74 And p = new And(Constant.FALSE);
75 assertTrue(!p.test());
76 for (int i=0;i<10;i++) {
77 p.and(Constant.FALSE);
78 assertTrue(!p.test());
79 }
80
81 And q = new And(Constant.TRUE);
82 assertTrue(q.test());
83 for (int i=0;i<10;i++) {
84 q.and(Constant.TRUE);
85 assertTrue(q.test());
86 }
87
88 And r = new And(p,q);
89 assertTrue(!r.test());
90 }
91
92 @Test
93 public void testDuplicateAdd() throws Exception {
94 Predicate p = Constant.TRUE;
95 And q = new And(p,p);
96 assertTrue(q.test());
97 for (int i=0;i<10;i++) {
98 q.and(p);
99 assertTrue(q.test());
100 }
101 }
102
103 @Test
104 public void testEquals() throws Exception {
105 And p = new And();
106 assertEquals(p,p);
107 And q = new And();
108 assertObjectsAreEqual(p,q);
109
110 for (int i=0;i<3;i++) {
111 p.and(Constant.TRUE);
112 assertObjectsAreNotEqual(p,q);
113 q.and(Constant.truePredicate());
114 assertObjectsAreEqual(p,q);
115 p.and(new And(Constant.TRUE,Constant.FALSE));
116 assertObjectsAreNotEqual(p,q);
117 q.and(new And(Constant.TRUE,Constant.FALSE));
118 assertObjectsAreEqual(p,q);
119 }
120
121 assertObjectsAreNotEqual(p,Constant.TRUE);
122 }
123
124 }