|
1 | 1 | package fj.data; |
2 | 2 |
|
3 | 3 | import fj.*; |
| 4 | +import fj.test.Arbitrary; |
| 5 | +import fj.test.Coarbitrary; |
| 6 | +import fj.test.Gen; |
| 7 | +import fj.test.Property; |
4 | 8 | import org.junit.Assert; |
5 | 9 | import org.junit.Test; |
6 | 10 |
|
7 | 11 | import static fj.data.Option.some; |
8 | 12 | import static fj.data.Stream.unfold; |
| 13 | +import static fj.data.test.PropertyAssert.assertResult; |
| 14 | +import static fj.test.Arbitrary.*; |
| 15 | +import static fj.test.Coarbitrary.coarbInteger; |
| 16 | +import static fj.test.Property.prop; |
| 17 | +import static fj.test.Property.property; |
| 18 | +import static fj.test.Variant.variant; |
9 | 19 |
|
10 | 20 | /** |
11 | 21 | * Created by mperry on 4/08/2014. |
@@ -68,4 +78,72 @@ public void testTraverse() { |
68 | 78 | Assert.assertTrue(list.toString().equals(expected)); |
69 | 79 | } |
70 | 80 |
|
| 81 | + public static Arbitrary<State<LcgRng, Integer>> arbState() { |
| 82 | + return Arbitrary.arbState(Arbitrary.arbLcgRng(), Coarbitrary.coarbLcgRng(), arbInteger); |
| 83 | + } |
| 84 | + |
| 85 | + public static Arbitrary<F<LcgRng, P2<LcgRng, Integer>>> arbStateF() { |
| 86 | + return arbF(Coarbitrary.coarbLcgRng(), arbP2(arbLcgRng(), arbInteger)); |
| 87 | + } |
| 88 | + |
| 89 | + public static Coarbitrary<State<LcgRng, Integer>> coarbState() { |
| 90 | + return Coarbitrary.coarbState(Arbitrary.arbLcgRng(), (LcgRng s, Integer j) -> (long) (j >= 0 ? 2 * j : -2 * j + 1)); |
| 91 | + } |
| 92 | + |
| 93 | + public static Arbitrary<F<Integer, State<LcgRng, Integer>>> arbBindable() { |
| 94 | + return arbF(coarbInteger, arbState()); |
| 95 | + } |
| 96 | + |
| 97 | + // Left identity: return i >>= f == f i |
| 98 | + @Test |
| 99 | + public void testLeftIdentity() { |
| 100 | + Property p = property( |
| 101 | + arbBindable(), |
| 102 | + arbInteger, |
| 103 | + arbLcgRng(), |
| 104 | + (f, i, r) -> { |
| 105 | + int a = State.<LcgRng, Integer>constant(i).flatMap(f).eval(r); |
| 106 | + int b = f.f(i).eval(r); |
| 107 | +// System.out.println(String.format("a=%d, b=%d", a, b)); |
| 108 | + return prop(a == b); |
| 109 | + } |
| 110 | + ); |
| 111 | + assertResult(p); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + // Right identity: m >>= return == m |
| 116 | + @Test |
| 117 | + public void testRightIdentity() { |
| 118 | + Property p = Property.property( |
| 119 | + arbState(), |
| 120 | + arbLcgRng(), |
| 121 | + (s, r) -> { |
| 122 | + int x = s.flatMap(a -> State.constant(a)).eval(r); |
| 123 | + int y = s.eval(r); |
| 124 | +// System.out.println(String.format("x=%d, y=%d", x, y)); |
| 125 | + return prop(x == y); |
| 126 | + } |
| 127 | + ); |
| 128 | + assertResult(p); |
| 129 | + } |
| 130 | + |
| 131 | + // Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g) |
| 132 | + @Test |
| 133 | + public void testAssociativity() { |
| 134 | + Property p = Property.property( |
| 135 | + arbState(), |
| 136 | + arbBindable(), |
| 137 | + arbBindable(), |
| 138 | + arbLcgRng(), |
| 139 | + (s, f, g, r) -> { |
| 140 | + int t = s.flatMap(f).flatMap(g).eval(r); |
| 141 | + int u = s.flatMap(x -> f.f(x).flatMap(g)).eval(r); |
| 142 | +// System.out.println(String.format("x=%d, y=%d", t, u)); |
| 143 | + return prop(t == u); |
| 144 | + }); |
| 145 | + assertResult(p); |
| 146 | + } |
| 147 | + |
| 148 | + |
71 | 149 | } |
0 commit comments