|
1 | 1 | package fj.data; |
2 | 2 |
|
3 | | -import fj.data.Reader; |
4 | | -import org.junit.Assert; |
| 3 | +import fj.F; |
| 4 | +import fj.F2; |
| 5 | +import fj.test.*; |
5 | 6 | import org.junit.Test; |
6 | 7 |
|
| 8 | +import static fj.F1Functions.bind; |
| 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; |
| 14 | +import static fj.test.Coarbitrary.coarbInteger; |
| 15 | +import static fj.test.Property.prop; |
| 16 | +import static fj.test.Property.property; |
7 | 17 | import static org.junit.Assert.assertTrue; |
8 | 18 |
|
9 | 19 | /** |
10 | 20 | * Created by MarkPerry on 4/12/2014. |
11 | | - * |
12 | | - * Examples taken from http://learnyouahaskell.com/for-a-few-monads-more |
13 | 21 | */ |
14 | 22 | public class ReaderTest { |
15 | 23 |
|
16 | 24 | @Test |
17 | 25 | public void testMap() { |
| 26 | + // (3 + 8) * 11 |
| 27 | + // example taken from http://learnyouahaskell.com/for-a-few-monads-more |
18 | 28 | int x = Reader.unit((Integer i) -> i + 3).map(i -> i * 5).f(8); |
19 | 29 | assertTrue(x == 55); |
20 | 30 | // System.out.println(x); // 55 |
21 | 31 | } |
22 | 32 |
|
23 | 33 | @Test |
24 | 34 | public void testFlatMap() { |
| 35 | + // (3 * 2) + (3 + 10) |
| 36 | + // example taken from http://learnyouahaskell.com/for-a-few-monads-more |
25 | 37 | int y = Reader.unit((Integer i) -> i * 2).flatMap(a -> Reader.unit((Integer i) -> i + 10).map(b -> a + b)).f(3); |
26 | 38 | // System.out.println(y); // 19 |
27 | 39 | assertTrue(y == 19); |
28 | 40 | } |
29 | 41 |
|
| 42 | + @Test |
| 43 | + public void testMapProp() { |
| 44 | + CheckResult cr = property( |
| 45 | + arbF(coarbInteger, arbInteger), |
| 46 | + arbF(coarbInteger, arbInteger), |
| 47 | + arbInteger, |
| 48 | + (f, g, i) -> { |
| 49 | + int expected = map(f, g).f(i); |
| 50 | +// System.out.println(String.format("input: %d, result: %d", i, expected)); |
| 51 | + 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()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testFlatMapProp() { |
| 59 | + CheckResult cr = property( |
| 60 | + arbF(coarbInteger, arbInteger), |
| 61 | + arbF2(coarbInteger, coarbInteger, arbInteger), |
| 62 | + arbInteger, |
| 63 | + (f, g, i) -> { |
| 64 | + int expected = bind(f, curry(g)).f(i); |
| 65 | +// System.out.println(String.format("input: %d, result: %d", i, expected)); |
| 66 | + return prop(expected == Reader.unit(f).flatMap(toBindable(g)).f(i)); |
| 67 | + } |
| 68 | + ).check(); |
| 69 | + CheckResult.summary.println(cr); |
| 70 | + assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testLeftIdentity() { |
| 75 | + CheckResult cr = Property.property( |
| 76 | + 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)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testRightIdentity() { |
| 97 | + CheckResult cr = Property.property( |
| 98 | + 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()); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testAssociativity() { |
| 114 | + CheckResult cr = Property.property( |
| 115 | + 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()); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + |
30 | 132 | } |
0 commit comments