forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderTest.java
More file actions
116 lines (103 loc) · 3.68 KB
/
Copy pathReaderTest.java
File metadata and controls
116 lines (103 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package fj.data;
import fj.F;
import fj.data.test.PropertyAssert;
import fj.test.*;
import org.junit.Test;
import static fj.F1Functions.bind;
import static fj.F1Functions.map;
import static fj.test.Arbitrary.*;
import static fj.test.Coarbitrary.coarbInteger;
import static fj.test.Property.prop;
import static fj.test.Property.property;
import static org.junit.Assert.assertTrue;
/**
* Created by MarkPerry on 4/12/2014.
*/
public class ReaderTest {
@Test
public void testMap() {
// (3 + 8) * 11
// example taken from http://learnyouahaskell.com/for-a-few-monads-more
int x = Reader.unit((Integer i) -> i + 3).map(i -> i * 5).f(8);
assertTrue(x == 55);
// System.out.println(x); // 55
}
@Test
public void testFlatMap() {
// (3 * 2) + (3 + 10)
// example taken from http://learnyouahaskell.com/for-a-few-monads-more
int y = Reader.unit((Integer i) -> i * 2).flatMap(a -> Reader.unit((Integer i) -> i + 10).map(b -> a + b)).f(3);
// System.out.println(y); // 19
assertTrue(y == 19);
}
@Test
public void testMapProp() {
Property p = property(
arbF(coarbInteger, arbInteger),
arbF(coarbInteger, arbInteger),
arbInteger,
(f, g, i) -> {
int expected = map(f, g).f(i);
// System.out.println(String.format("input: %d, result: %d", i, expected));
return prop(expected == Reader.unit(f).map(g).f(i));
});
PropertyAssert.assertResult(p);
}
@Test
public void testFlatMapProp() {
Arbitrary<F<Integer, Reader<Integer, Integer>>> a = arbF(coarbInteger, arbReader());
Property p = property(
arbF(coarbInteger, arbInteger),
a,
arbInteger,
(f, g, i) -> {
int expected = bind(f, j -> g.f(j).getFunction()).f(i);
// System.out.println(String.format("input: %d, result: %d", i, expected));
return prop(expected == Reader.unit(f).flatMap(g).f(i));
}
);
PropertyAssert.assertResult(p);
}
// Left identity: return a >>= f == f a
@Test
public void testLeftIdentity() {
Property p = Property.property(
arbInteger,
arbInteger,
arbF(coarbInteger, arbReader()),
(i, j, f) -> {
int a = Reader.<Integer, Integer>constant(i).flatMap(f).f(j);
int b = f.f(i).f(j);
return prop(a == b);
});
PropertyAssert.assertResult(p);
}
// Right identity: m >>= return == m
@Test
public void testRightIdentity() {
Property p = Property.property(
arbInteger,
arbReader(),
(i, r2) -> {
return prop(r2.flatMap(a -> Reader.constant(a)).f(i) == r2.f(i));
});
PropertyAssert.assertResult(p);
}
// Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
@Test
public void testAssociativity() {
Property p = Property.property(
arbInteger,
arbReader(),
arbF(coarbInteger, arbReader()),
arbF(coarbInteger, arbReader()),
(i, r, f, g) -> {
boolean b2 = r.flatMap(f).flatMap(g).f(i) == r.flatMap(x -> f.f(x).flatMap(g)).f(i);
return prop(b2);
});
PropertyAssert.assertResult(p);
}
public Arbitrary<Reader<Integer, Integer>> arbReader() {
return Arbitrary.arbReader(coarbInteger, arbInteger);
}
}