forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriterTest.java
More file actions
112 lines (93 loc) · 3.34 KB
/
Copy pathWriterTest.java
File metadata and controls
112 lines (93 loc) · 3.34 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
package fj.data;
import fj.Equal;
import fj.F;
import fj.data.test.PropertyAssert;
import fj.test.Arbitrary;
import fj.test.Property;
import org.junit.Assert;
import org.junit.Test;
import static fj.data.test.PropertyAssert.assertResult;
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 17/12/2014.
*/
public class WriterTest {
@Test
public void base() {
Assert.assertTrue(tellTruth("a", "b", 0));
}
boolean tellTruth(String s1, String s2, int i) {
Writer<String, Integer> w = defaultWriter.f(i);
Writer<String, Integer> w1 = w.tell(s1).tell(s2);
Writer<String, Integer> w2 = w.tell(w.monoid().sum(s1, s2));
boolean b = eq.eq(w1, w2);
// System.out.println(String.format("p1: %s, p2: %s, b: %s", w1, w2, b));
return b;
}
final Equal<Writer<String, Integer>> eq = Equal.writerEqual(Equal.stringEqual, Equal.intEqual);
final F<Integer, Writer<String, Integer>> defaultWriter = Writer.<Integer>stringLogger();
@Test
public void testTellProp() {
Property p = property(arbString, arbString, arbInteger, (s1, s2, i) -> prop(tellTruth(s1, s2, i)));
assertResult(p);
}
@Test
public void testMap() {
Property p = property(arbInteger, arbF(coarbInteger, arbInteger), (i, f) -> {
boolean b = eq.eq(defaultWriter.f(i).map(f), defaultWriter.f(f.f(i)));
return prop(b);
});
assertResult(p);
}
@Test
public void testFlatMap() {
Property p = property(arbInteger,arbF(coarbInteger, arbWriterStringInt()), (i, f) -> {
boolean b = eq.eq(Writer.<Integer>stringLogger().f(i).flatMap(f), f.f(i));
return prop(b);
});
assertResult(p);
}
public Arbitrary<Writer<String, Integer>> arbWriterStringInt() {
return arbWriterString(arbInteger);
}
public <A> Arbitrary<Writer<String, A>> arbWriterString(Arbitrary<A> arb) {
return Arbitrary.arbitrary(arb.gen.map(a -> Writer.<A>stringLogger().f(a)));
}
// Left identity: return a >>= f == f a
@Test
public void testLeftIdentity() {
Property p = Property.property(
arbInteger,
arbF(coarbInteger, arbWriterStringInt()),
(i, f) -> {
return prop(eq.eq(defaultWriter.f(i).flatMap(f), f.f(i)));
});
assertResult(p);
}
// Right identity: m >>= return == m
@Test
public void testRightIdentity() {
Property p = Property.property(
arbWriterStringInt(),
(w) -> prop(eq.eq(w.flatMap(a -> defaultWriter.f(a)), w))
);
assertResult(p);
}
// Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
@Test
public void testAssociativity() {
Property p = Property.property(
arbWriterStringInt(),
arbF(coarbInteger, arbWriterStringInt()),
arbF(coarbInteger, arbWriterStringInt()),
(w, f, g) -> {
boolean t = eq.eq(w.flatMap(f).flatMap(g), w.flatMap(x -> f.f(x).flatMap(g)));
return prop(t);
});
assertResult(p);
}
}