|
1 | 1 | package fj.data; |
2 | 2 |
|
3 | 3 | import fj.F; |
4 | | -import fj.F2; |
| 4 | +import fj.data.test.PropertyAssert; |
5 | 5 | import fj.test.*; |
6 | 6 | import org.junit.Test; |
7 | 7 |
|
8 | 8 | import static fj.F1Functions.bind; |
9 | 9 | import static fj.F1Functions.map; |
10 | | -import static fj.F2Functions.curry; |
11 | | -import static fj.test.Arbitrary.arbF; |
12 | | -import static fj.test.Arbitrary.arbF2; |
13 | | -import static fj.test.Arbitrary.arbInteger; |
| 10 | +import static fj.test.Arbitrary.*; |
14 | 11 | import static fj.test.Coarbitrary.coarbInteger; |
15 | 12 | import static fj.test.Property.prop; |
16 | 13 | import static fj.test.Property.property; |
@@ -41,92 +38,79 @@ public void testFlatMap() { |
41 | 38 |
|
42 | 39 | @Test |
43 | 40 | public void testMapProp() { |
44 | | - CheckResult cr = property( |
| 41 | + Property p = property( |
45 | 42 | arbF(coarbInteger, arbInteger), |
46 | 43 | arbF(coarbInteger, arbInteger), |
47 | 44 | arbInteger, |
48 | 45 | (f, g, i) -> { |
49 | 46 | int expected = map(f, g).f(i); |
50 | 47 | // System.out.println(String.format("input: %d, result: %d", i, expected)); |
51 | 48 | return prop(expected == Reader.unit(f).map(g).f(i)); |
52 | | - }).check(); |
53 | | - CheckResult.summary.println(cr); |
54 | | - assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven()); |
| 49 | + }); |
| 50 | + PropertyAssert.assertResult(p); |
55 | 51 | } |
56 | 52 |
|
57 | 53 | @Test |
58 | 54 | public void testFlatMapProp() { |
59 | | - CheckResult cr = property( |
| 55 | + Arbitrary<F<Integer, Reader<Integer, Integer>>> a = arbF(coarbInteger, arbReader()); |
| 56 | + Property p = property( |
60 | 57 | arbF(coarbInteger, arbInteger), |
61 | | - arbF2(coarbInteger, coarbInteger, arbInteger), |
| 58 | + a, |
62 | 59 | arbInteger, |
63 | 60 | (f, g, i) -> { |
64 | | - int expected = bind(f, curry(g)).f(i); |
| 61 | + int expected = bind(f, j -> g.f(j).getFunction()).f(i); |
65 | 62 | // System.out.println(String.format("input: %d, result: %d", i, expected)); |
66 | | - return prop(expected == Reader.unit(f).flatMap(toBindable(g)).f(i)); |
| 63 | + return prop(expected == Reader.unit(f).flatMap(g).f(i)); |
67 | 64 | } |
68 | | - ).check(); |
69 | | - CheckResult.summary.println(cr); |
70 | | - assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven()); |
| 65 | + ); |
| 66 | + PropertyAssert.assertResult(p); |
71 | 67 | } |
72 | 68 |
|
| 69 | + // Left identity: return a >>= f == f a |
73 | 70 | @Test |
74 | 71 | public void testLeftIdentity() { |
75 | | - CheckResult cr = Property.property( |
| 72 | + Property p = Property.property( |
76 | 73 | arbInteger, |
77 | | - arbF(coarbInteger, arbInteger), |
78 | | - arbF2(coarbInteger, coarbInteger, arbInteger), |
79 | | - (i, f, g) -> { |
80 | | -// F<Integer, Reader<Integer, Integer>> h = convert(g); |
81 | | - int a = Reader.unit(f).flatMap(toBindable(g)).f(i); |
82 | | - int b = g.f(f.f(i), i); |
83 | | -// System.out.println(String.format("i=%d, a=%d, b=%d, truth=%b", i, a, b, a == b)); |
84 | | - return prop(a == b); |
85 | | - }).check(); |
86 | | - CheckResult.summary.println(cr); |
87 | | - assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven()); |
88 | | - } |
89 | | - |
90 | | - F<Integer, Reader<Integer, Integer>> toBindable(F2<Integer, Integer, Integer> f) { |
91 | | -// F<Integer, Reader<Integer, Integer>> h = map(curry(f), z -> Reader.unit(z)); |
92 | | - return map(curry(f), z -> Reader.unit(z)); |
| 74 | + arbInteger, |
| 75 | + arbF(coarbInteger, arbReader()), |
| 76 | + (i, j, f) -> { |
| 77 | + int a = Reader.<Integer, Integer>constant(i).flatMap(f).f(j); |
| 78 | + int b = f.f(i).f(j); |
| 79 | + return prop(a == b); |
| 80 | + }); |
| 81 | + PropertyAssert.assertResult(p); |
93 | 82 | } |
94 | 83 |
|
| 84 | + // Right identity: m >>= return == m |
95 | 85 | @Test |
96 | 86 | public void testRightIdentity() { |
97 | | - CheckResult cr = Property.property( |
| 87 | + Property p = Property.property( |
98 | 88 | arbInteger, |
99 | | - arbF(coarbInteger, arbInteger), |
100 | | - (i, f) -> { |
101 | | - Reader<Integer, Integer> r = Reader.unit(f); |
102 | | - boolean b = r.flatMap(a -> r).f(i) == r.f(i); |
103 | | -// System.out.println(String.format("i=%d, a=%d, b=%d, truth=%b", i, a, b, a == b)); |
104 | | - return prop(b); |
105 | | - }).check(); |
106 | | - CheckResult.summary.println(cr); |
107 | | - assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven()); |
| 89 | + arbReader(), |
| 90 | + (i, r2) -> { |
| 91 | + return prop(r2.flatMap(a -> Reader.constant(a)).f(i) == r2.f(i)); |
| 92 | + }); |
| 93 | + PropertyAssert.assertResult(p); |
108 | 94 | } |
109 | 95 |
|
110 | | - |
111 | | - |
| 96 | + // Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g) |
112 | 97 | @Test |
113 | 98 | public void testAssociativity() { |
114 | | - CheckResult cr = Property.property( |
| 99 | + Property p = Property.property( |
115 | 100 | arbInteger, |
116 | | - arbF(coarbInteger, arbInteger), |
117 | | - arbF2(coarbInteger, coarbInteger, arbInteger), |
118 | | - arbF2(coarbInteger, coarbInteger, arbInteger), |
119 | | - (i, f, g, h) -> { |
120 | | - Reader<Integer, Integer> r = Reader.unit(f); |
121 | | - int a = r.flatMap(toBindable(g)).flatMap(toBindable(h)).f(i); |
122 | | - int b = r.flatMap(x -> toBindable(g).f(x).flatMap(toBindable(h))).f(i); |
123 | | -// System.out.println(String.format("i=%d, a=%d, b=%d, truth=%b", i, a, b, a == b)); |
124 | | - return prop(a == b); |
125 | | - }).check(); |
126 | | - CheckResult.summary.println(cr); |
127 | | - assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven()); |
| 101 | + arbReader(), |
| 102 | + arbF(coarbInteger, arbReader()), |
| 103 | + arbF(coarbInteger, arbReader()), |
| 104 | + (i, r, f, g) -> { |
| 105 | + boolean b2 = r.flatMap(f).flatMap(g).f(i) == r.flatMap(x -> f.f(x).flatMap(g)).f(i); |
| 106 | + return prop(b2); |
| 107 | + }); |
| 108 | + PropertyAssert.assertResult(p); |
128 | 109 | } |
129 | 110 |
|
| 111 | + public Arbitrary<Reader<Integer, Integer>> arbReader() { |
| 112 | + return Arbitrary.arbReader(coarbInteger, arbInteger); |
| 113 | + } |
130 | 114 |
|
131 | 115 |
|
132 | 116 | } |
0 commit comments