0
|
1 package fj.data;
|
|
2
|
|
3 import fj.F;
|
|
4 import fj.data.test.PropertyAssert;
|
|
5 import fj.test.*;
|
|
6 import org.junit.Test;
|
|
7
|
|
8 import static fj.F1Functions.bind;
|
|
9 import static fj.F1Functions.map;
|
|
10 import static fj.test.Arbitrary.*;
|
|
11 import static fj.test.Coarbitrary.coarbInteger;
|
|
12 import static fj.test.Property.prop;
|
|
13 import static fj.test.Property.property;
|
|
14 import static org.junit.Assert.assertTrue;
|
|
15
|
|
16 /**
|
|
17 * Created by MarkPerry on 4/12/2014.
|
|
18 */
|
|
19 public class ReaderTest {
|
|
20
|
|
21 @Test
|
|
22 public void testMap() {
|
|
23 // (3 + 8) * 11
|
|
24 // example taken from http://learnyouahaskell.com/for-a-few-monads-more
|
|
25 int x = Reader.unit((Integer i) -> i + 3).map(i -> i * 5).f(8);
|
|
26 assertTrue(x == 55);
|
|
27 // System.out.println(x); // 55
|
|
28 }
|
|
29
|
|
30 @Test
|
|
31 public void testFlatMap() {
|
|
32 // (3 * 2) + (3 + 10)
|
|
33 // example taken from http://learnyouahaskell.com/for-a-few-monads-more
|
|
34 int y = (int) Reader.unit((Integer i) -> i * 2).flatMap(a -> Reader.unit((Integer i) -> i + 10).map(b -> a + b)).f(3);
|
|
35 // System.out.println(y); // 19
|
|
36 assertTrue(y == 19);
|
|
37 }
|
|
38
|
|
39 @Test
|
|
40 public void testMapProp() {
|
|
41 Property p = property(
|
|
42 arbF(coarbInteger, arbInteger),
|
|
43 arbF(coarbInteger, arbInteger),
|
|
44 arbInteger,
|
|
45 (f, g, i) -> {
|
|
46 int expected = map(f, g).f(i);
|
|
47 // System.out.println(String.format("input: %d, result: %d", i, expected));
|
|
48 return prop(expected == Reader.unit(f).map(g).f(i));
|
|
49 });
|
|
50 PropertyAssert.assertResult(p);
|
|
51 }
|
|
52
|
|
53 @Test
|
|
54 public void testFlatMapProp() {
|
|
55 Arbitrary<F<Integer, Reader<Integer, Integer>>> a = arbF(coarbInteger, arbReader());
|
|
56 Property p = property(
|
|
57 arbF(coarbInteger, arbInteger),
|
|
58 a,
|
|
59 arbInteger,
|
|
60 (f, g, i) -> {
|
|
61 int expected = bind(f, j -> g.f(j).getFunction()).f(i);
|
|
62 // System.out.println(String.format("input: %d, result: %d", i, expected));
|
|
63 return prop(expected == Reader.unit(f).flatMap(g).f(i));
|
|
64 }
|
|
65 );
|
|
66 PropertyAssert.assertResult(p);
|
|
67 }
|
|
68
|
|
69 // Left identity: return a >>= f == f a
|
|
70 @Test
|
|
71 public void testLeftIdentity() {
|
|
72 Property p = Property.property(
|
|
73 arbInteger,
|
|
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);
|
|
82 }
|
|
83
|
|
84 // Right identity: m >>= return == m
|
|
85 @Test
|
|
86 public void testRightIdentity() {
|
|
87 Property p = Property.property(
|
|
88 arbInteger,
|
|
89 arbReader(),
|
|
90 (i, r2) -> {
|
|
91 return prop(r2.flatMap(a -> Reader.constant(a)).f(i) == r2.f(i));
|
|
92 });
|
|
93 PropertyAssert.assertResult(p);
|
|
94 }
|
|
95
|
|
96 // Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
|
|
97 @Test
|
|
98 public void testAssociativity() {
|
|
99 Property p = Property.property(
|
|
100 arbInteger,
|
|
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);
|
|
109 }
|
|
110
|
|
111 public Arbitrary<Reader<Integer, Integer>> arbReader() {
|
|
112 return Arbitrary.arbReader(coarbInteger, arbInteger);
|
|
113 }
|
|
114
|
|
115
|
|
116 }
|