forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRngState.java
More file actions
150 lines (127 loc) · 4.66 KB
/
Copy pathTestRngState.java
File metadata and controls
150 lines (127 loc) · 4.66 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package fj.data;
import fj.*;
import fj.test.Arbitrary;
import fj.test.Coarbitrary;
import fj.test.Gen;
import fj.test.Property;
import org.junit.Assert;
import org.junit.Test;
import static fj.data.Option.some;
import static fj.data.Stream.unfold;
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 fj.test.Variant.variant;
/**
* Created by mperry on 4/08/2014.
*/
public class TestRngState {
static List<Integer> expected1 = List.list(4,4,2,2,2,5,3,3,1,5);
static int size = 10;
static final Equal<List<Integer>> listIntEqual = Equal.listEqual(Equal.intEqual);
static Rng defaultRng() {
return new LcgRng(1);
}
static P2<Rng, Integer> num(Rng r) {
return r.range(1, 5);
}
static State<Rng, Integer> defaultState() {
return State.unit(s -> num(s));
}
static F<State<Rng, Integer>, State<Rng, Integer>> nextState() {
return s -> s.mapState(p2 -> num(p2._1()));
}
static P2<Rng, Integer> num(Rng r, int x) {
return r.range(x, x + 1);
}
@Test
public void testUnfold() {
Stream<Integer> s = unfold(r -> some(num(r).swap()), defaultRng());
Assert.assertTrue(listIntEqual.eq(s.take(size).toList(), expected1));
}
@Test
public void testTransitions() {
P2<List<State<Rng, Integer>>, State<Rng, Integer>> p = List.replicate(size, nextState()).foldLeft(
(P2<List<State<Rng, Integer>>, State<Rng, Integer>> p2, F<State<Rng, Integer>, State<Rng, Integer>> f) -> {
State<Rng, Integer> s = f.f(p2._2());
return P.p(p2._1().snoc(p2._2()), s);
}
, P.p(List.nil(), defaultState())
);
List<Integer> ints = p._1().map(s -> s.eval(defaultRng()));
Assert.assertTrue(listIntEqual.eq(ints, expected1));
}
@Test
public void testSequence() {
List<Integer> list = State.sequence(List.replicate(size, defaultState())).eval(defaultRng());
Assert.assertTrue(listIntEqual.eq(list, expected1));
}
@Test
public void testTraverse() {
List<Integer> list = State.traverse(List.range(1, 10), a -> (State.unit((Rng s) -> num(s, a)))).eval(defaultRng());
// System.out.println(list.toString());
List<Integer> expected = List.list(1,2,3,5,6,7,7,9,10);
Assert.assertTrue(listIntEqual.eq(list, expected));
}
public static Arbitrary<State<LcgRng, Integer>> arbState() {
return Arbitrary.arbState(Arbitrary.arbLcgRng(), Coarbitrary.coarbLcgRng(), arbInteger);
}
public static Arbitrary<F<LcgRng, P2<LcgRng, Integer>>> arbStateF() {
return arbF(Coarbitrary.coarbLcgRng(), arbP2(arbLcgRng(), arbInteger));
}
public static Coarbitrary<State<LcgRng, Integer>> coarbState() {
return Coarbitrary.coarbState(Arbitrary.arbLcgRng(), (LcgRng s, Integer j) -> (long) (j >= 0 ? 2 * j : -2 * j + 1));
}
public static Arbitrary<F<Integer, State<LcgRng, Integer>>> arbBindable() {
return arbF(coarbInteger, arbState());
}
// Left identity: return i >>= f == f i
@Test
public void testLeftIdentity() {
Property p = property(
arbBindable(),
arbInteger,
arbLcgRng(),
(f, i, r) -> {
int a = State.<LcgRng, Integer>constant(i).flatMap(f).eval(r);
int b = f.f(i).eval(r);
// System.out.println(String.format("a=%d, b=%d", a, b));
return prop(a == b);
}
);
assertResult(p);
}
// Right identity: m >>= return == m
@Test
public void testRightIdentity() {
Property p = Property.property(
arbState(),
arbLcgRng(),
(s, r) -> {
int x = s.flatMap(a -> State.constant(a)).eval(r);
int y = s.eval(r);
// System.out.println(String.format("x=%d, y=%d", x, y));
return prop(x == y);
}
);
assertResult(p);
}
// Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
@Test
public void testAssociativity() {
Property p = Property.property(
arbState(),
arbBindable(),
arbBindable(),
arbLcgRng(),
(s, f, g, r) -> {
int t = s.flatMap(f).flatMap(g).eval(r);
int u = s.flatMap(x -> f.f(x).flatMap(g)).eval(r);
// System.out.println(String.format("x=%d, y=%d", t, u));
return prop(t == u);
});
assertResult(p);
}
}