0
|
1 package fj.data;
|
|
2
|
|
3 import fj.*;
|
|
4 import fj.test.Arbitrary;
|
|
5 import fj.test.Coarbitrary;
|
|
6 import fj.test.Gen;
|
|
7 import fj.test.Property;
|
|
8 import org.junit.Assert;
|
|
9 import org.junit.Test;
|
|
10
|
|
11 import static fj.data.Option.some;
|
|
12 import static fj.data.Stream.unfold;
|
|
13 import static fj.data.test.PropertyAssert.assertResult;
|
|
14 import static fj.test.Arbitrary.*;
|
|
15 import static fj.test.Coarbitrary.coarbInteger;
|
|
16 import static fj.test.Property.prop;
|
|
17 import static fj.test.Property.property;
|
|
18 import static fj.test.Variant.variant;
|
|
19
|
|
20 /**
|
|
21 * Created by mperry on 4/08/2014.
|
|
22 */
|
|
23 public class TestRngState {
|
|
24
|
|
25 static String expected1 = "<4,4,2,2,2,5,3,3,1,5>";
|
|
26 static int size = 10;
|
|
27
|
|
28 static Rng defaultRng() {
|
|
29 return new LcgRng(1);
|
|
30 }
|
|
31
|
|
32 static P2<Rng, Integer> num(Rng r) {
|
|
33 return r.range(1, 5);
|
|
34 }
|
|
35
|
|
36 static State<Rng, Integer> defaultState() {
|
|
37 return State.unit(s -> num(s));
|
|
38 }
|
|
39
|
|
40 static F<State<Rng, Integer>, State<Rng, Integer>> nextState() {
|
|
41 return s -> s.mapState(p2 -> num(p2._1()));
|
|
42 }
|
|
43
|
|
44 static P2<Rng, Integer> num(Rng r, int x) {
|
|
45 return r.range(x, x + 1);
|
|
46 }
|
|
47
|
|
48 @Test
|
|
49 public void testUnfold() {
|
|
50 Stream<Integer> s = unfold(r -> some(num(r).swap()), defaultRng());
|
|
51 Assert.assertTrue(s.take(size).toList().toString().equals(expected1));
|
|
52 }
|
|
53
|
|
54 @Test
|
|
55 public void testTransitions() {
|
|
56 P2<List<State<Rng, Integer>>, State<Rng, Integer>> p = List.replicate(size, nextState()).foldLeft(
|
|
57 (P2<List<State<Rng, Integer>>, State<Rng, Integer>> p2, F<State<Rng, Integer>, State<Rng, Integer>> f) -> {
|
|
58 State<Rng, Integer> s = f.f(p2._2());
|
|
59 return P.p(p2._1().snoc(p2._2()), s);
|
|
60 }
|
|
61 , P.p(List.nil(), defaultState())
|
|
62 );
|
|
63 List<Integer> ints = p._1().map(s -> s.eval(defaultRng()));
|
|
64 Assert.assertTrue(ints.toString().equals(expected1));
|
|
65 }
|
|
66
|
|
67 @Test
|
|
68 public void testSequence() {
|
|
69 List<Integer> list = State.sequence(List.replicate(size, defaultState())).eval(defaultRng());
|
|
70 Assert.assertTrue(list.toString().equals(expected1));
|
|
71 }
|
|
72
|
|
73 @Test
|
|
74 public void testTraverse() {
|
|
75 List<Integer> list = State.traverse(List.range(1, 10), a -> (State.unit((Rng s) -> num(s, a)))).eval(defaultRng());
|
|
76 // System.out.println(list.toString());
|
|
77 String expected = "<1,2,3,5,6,7,7,9,10>";
|
|
78 Assert.assertTrue(list.toString().equals(expected));
|
|
79 }
|
|
80
|
|
81 public static Arbitrary<State<LcgRng, Integer>> arbState() {
|
|
82 return Arbitrary.arbState(Arbitrary.arbLcgRng(), Coarbitrary.coarbLcgRng(), arbInteger);
|
|
83 }
|
|
84
|
|
85 public static Arbitrary<F<LcgRng, P2<LcgRng, Integer>>> arbStateF() {
|
|
86 return arbF(Coarbitrary.coarbLcgRng(), arbP2(arbLcgRng(), arbInteger));
|
|
87 }
|
|
88
|
|
89 public static Coarbitrary<State<LcgRng, Integer>> coarbState() {
|
|
90 return Coarbitrary.coarbState(Arbitrary.arbLcgRng(), (LcgRng s, Integer j) -> (long) (j >= 0 ? 2 * j : -2 * j + 1));
|
|
91 }
|
|
92
|
|
93 public static Arbitrary<F<Integer, State<LcgRng, Integer>>> arbBindable() {
|
|
94 return arbF(coarbInteger, arbState());
|
|
95 }
|
|
96
|
|
97 // Left identity: return i >>= f == f i
|
|
98 @Test
|
|
99 public void testLeftIdentity() {
|
|
100 Property p = property(
|
|
101 arbBindable(),
|
|
102 arbInteger,
|
|
103 arbLcgRng(),
|
|
104 (f, i, r) -> {
|
|
105 int a = State.<LcgRng, Integer>constant(i).flatMap(f).eval(r);
|
|
106 int b = f.f(i).eval(r);
|
|
107 // System.out.println(String.format("a=%d, b=%d", a, b));
|
|
108 return prop(a == b);
|
|
109 }
|
|
110 );
|
|
111 assertResult(p);
|
|
112 }
|
|
113
|
|
114
|
|
115 // Right identity: m >>= return == m
|
|
116 @Test
|
|
117 public void testRightIdentity() {
|
|
118 Property p = Property.property(
|
|
119 arbState(),
|
|
120 arbLcgRng(),
|
|
121 (s, r) -> {
|
|
122 int x = s.flatMap(a -> State.constant(a)).eval(r);
|
|
123 int y = s.eval(r);
|
|
124 // System.out.println(String.format("x=%d, y=%d", x, y));
|
|
125 return prop(x == y);
|
|
126 }
|
|
127 );
|
|
128 assertResult(p);
|
|
129 }
|
|
130
|
|
131 // Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
|
|
132 @Test
|
|
133 public void testAssociativity() {
|
|
134 Property p = Property.property(
|
|
135 arbState(),
|
|
136 arbBindable(),
|
|
137 arbBindable(),
|
|
138 arbLcgRng(),
|
|
139 (s, f, g, r) -> {
|
|
140 int t = s.flatMap(f).flatMap(g).eval(r);
|
|
141 int u = s.flatMap(x -> f.f(x).flatMap(g)).eval(r);
|
|
142 // System.out.println(String.format("x=%d, y=%d", t, u));
|
|
143 return prop(t == u);
|
|
144 });
|
|
145 assertResult(p);
|
|
146 }
|
|
147
|
|
148
|
|
149 }
|