Skip to content

Commit 1a4b4bc

Browse files
committed
Added tests to the reader monad
1 parent f762bf3 commit 1a4b4bc

2 files changed

Lines changed: 107 additions & 4 deletions

File tree

core/src/main/java/fj/data/Reader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import fj.F1Functions;
55

66
/**
7+
* The Reader monad (also called the function monad, so equivalent to the idea of F).
78
* Created by MarkPerry on 7/07/2014.
89
*/
910
public class Reader<A, B> {
Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,132 @@
11
package fj.data;
22

3-
import fj.data.Reader;
4-
import org.junit.Assert;
3+
import fj.F;
4+
import fj.F2;
5+
import fj.test.*;
56
import org.junit.Test;
67

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;
717
import static org.junit.Assert.assertTrue;
818

919
/**
1020
* Created by MarkPerry on 4/12/2014.
11-
*
12-
* Examples taken from http://learnyouahaskell.com/for-a-few-monads-more
1321
*/
1422
public class ReaderTest {
1523

1624
@Test
1725
public void testMap() {
26+
// (3 + 8) * 11
27+
// example taken from http://learnyouahaskell.com/for-a-few-monads-more
1828
int x = Reader.unit((Integer i) -> i + 3).map(i -> i * 5).f(8);
1929
assertTrue(x == 55);
2030
// System.out.println(x); // 55
2131
}
2232

2333
@Test
2434
public void testFlatMap() {
35+
// (3 * 2) + (3 + 10)
36+
// example taken from http://learnyouahaskell.com/for-a-few-monads-more
2537
int y = Reader.unit((Integer i) -> i * 2).flatMap(a -> Reader.unit((Integer i) -> i + 10).map(b -> a + b)).f(3);
2638
// System.out.println(y); // 19
2739
assertTrue(y == 19);
2840
}
2941

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+
30132
}

0 commit comments

Comments
 (0)