Skip to content

Commit 5bec258

Browse files
committed
Clean up reader tests
1 parent aa9a683 commit 5bec258

6 files changed

Lines changed: 88 additions & 91 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public Reader(F<A, B> f) {
1515
function = f;
1616
}
1717

18+
public F<A, B> getFunction() {
19+
return function;
20+
}
21+
1822
public static <A, B> Reader<A, B> unit(F<A, B> f) {
1923
return new Reader<A, B>(f);
2024
}

core/src/main/java/fj/test/Arbitrary.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
import fj.P6;
2020
import fj.P7;
2121
import fj.P8;
22-
import fj.data.Array;
23-
import fj.data.Either;
22+
import fj.data.*;
23+
2424
import static fj.data.Either.left;
2525
import static fj.data.Either.right;
2626
import static fj.data.Enumerator.charEnumerator;
27-
import fj.data.List;
2827
import static fj.data.List.asString;
2928
import static fj.data.List.list;
30-
import fj.data.Option;
3129
import static fj.data.Option.some;
32-
import fj.data.Stream;
30+
3331
import fj.function.Effect1;
3432

3533
import static fj.data.Stream.range;
@@ -125,6 +123,10 @@ public Gen<B> f(final A x) {
125123
}));
126124
}
127125

126+
public static <A, B> Arbitrary<Reader<A, B>> arbReader(Coarbitrary<A> aa, Arbitrary<B> ab) {
127+
return arbitrary(Arbitrary.arbF(aa, ab).gen.map(f -> Reader.unit(f)));
128+
}
129+
128130
/**
129131
* An arbitrary for functions.
130132
*

core/src/main/java/fj/test/Property.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@
33
import static fj.Function.curry;
44
import static fj.Function.compose2;
55
import static fj.P.p;
6-
import fj.F;
7-
import fj.F2;
8-
import fj.F3;
9-
import fj.F4;
10-
import fj.F5;
11-
import fj.F6;
12-
import fj.F7;
13-
import fj.F8;
14-
import fj.P;
15-
import fj.P1;
16-
import fj.P2;
6+
7+
import fj.*;
8+
179
import static fj.P2.__2;
1810
import fj.data.List;
1911
import fj.data.Option;
@@ -1663,4 +1655,7 @@ public Result f(final Rand r) {
16631655
});
16641656
}
16651657
}
1658+
1659+
1660+
16661661
}
Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package fj.data;
22

33
import fj.F;
4-
import fj.F2;
4+
import fj.data.test.PropertyAssert;
55
import fj.test.*;
66
import org.junit.Test;
77

88
import static fj.F1Functions.bind;
99
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;
10+
import static fj.test.Arbitrary.*;
1411
import static fj.test.Coarbitrary.coarbInteger;
1512
import static fj.test.Property.prop;
1613
import static fj.test.Property.property;
@@ -41,92 +38,79 @@ public void testFlatMap() {
4138

4239
@Test
4340
public void testMapProp() {
44-
CheckResult cr = property(
41+
Property p = property(
4542
arbF(coarbInteger, arbInteger),
4643
arbF(coarbInteger, arbInteger),
4744
arbInteger,
4845
(f, g, i) -> {
4946
int expected = map(f, g).f(i);
5047
// System.out.println(String.format("input: %d, result: %d", i, expected));
5148
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());
49+
});
50+
PropertyAssert.assertResult(p);
5551
}
5652

5753
@Test
5854
public void testFlatMapProp() {
59-
CheckResult cr = property(
55+
Arbitrary<F<Integer, Reader<Integer, Integer>>> a = arbF(coarbInteger, arbReader());
56+
Property p = property(
6057
arbF(coarbInteger, arbInteger),
61-
arbF2(coarbInteger, coarbInteger, arbInteger),
58+
a,
6259
arbInteger,
6360
(f, g, i) -> {
64-
int expected = bind(f, curry(g)).f(i);
61+
int expected = bind(f, j -> g.f(j).getFunction()).f(i);
6562
// System.out.println(String.format("input: %d, result: %d", i, expected));
66-
return prop(expected == Reader.unit(f).flatMap(toBindable(g)).f(i));
63+
return prop(expected == Reader.unit(f).flatMap(g).f(i));
6764
}
68-
).check();
69-
CheckResult.summary.println(cr);
70-
assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven());
65+
);
66+
PropertyAssert.assertResult(p);
7167
}
7268

69+
// Left identity: return a >>= f == f a
7370
@Test
7471
public void testLeftIdentity() {
75-
CheckResult cr = Property.property(
72+
Property p = Property.property(
7673
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));
74+
arbInteger,
75+
arbF(coarbInteger, arbReader()),
76+
(i, j, f) -> {
77+
int a = Reader.<Integer, Integer>constant(i).flatMap(f).f(j);
78+
int b = f.f(i).f(j);
79+
return prop(a == b);
80+
});
81+
PropertyAssert.assertResult(p);
9382
}
9483

84+
// Right identity: m >>= return == m
9585
@Test
9686
public void testRightIdentity() {
97-
CheckResult cr = Property.property(
87+
Property p = Property.property(
9888
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());
89+
arbReader(),
90+
(i, r2) -> {
91+
return prop(r2.flatMap(a -> Reader.constant(a)).f(i) == r2.f(i));
92+
});
93+
PropertyAssert.assertResult(p);
10894
}
10995

110-
111-
96+
// Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
11297
@Test
11398
public void testAssociativity() {
114-
CheckResult cr = Property.property(
99+
Property p = Property.property(
115100
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());
101+
arbReader(),
102+
arbF(coarbInteger, arbReader()),
103+
arbF(coarbInteger, arbReader()),
104+
(i, r, f, g) -> {
105+
boolean b2 = r.flatMap(f).flatMap(g).f(i) == r.flatMap(x -> f.f(x).flatMap(g)).f(i);
106+
return prop(b2);
107+
});
108+
PropertyAssert.assertResult(p);
128109
}
129110

111+
public Arbitrary<Reader<Integer, Integer>> arbReader() {
112+
return Arbitrary.arbReader(coarbInteger, arbInteger);
113+
}
130114

131115

132116
}

core/src/test/java/fj/data/WriterTest.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import fj.Equal;
44
import fj.F;
5-
import fj.F2;
5+
import fj.data.test.PropertyAssert;
66
import fj.test.Arbitrary;
7-
import fj.test.CheckResult;
87
import fj.test.Property;
98
import org.junit.Assert;
109
import org.junit.Test;
1110

12-
import static fj.F1Functions.map;
13-
import static fj.F2Functions.curry;
11+
import static fj.data.test.PropertyAssert.assertResult;
1412
import static fj.test.Arbitrary.*;
1513
import static fj.test.Coarbitrary.coarbInteger;
1614
import static fj.test.Property.prop;
@@ -37,18 +35,12 @@ boolean tellTruth(String s1, String s2, int i) {
3735
}
3836

3937
final Equal<Writer<String, Integer>> eq = Equal.writerEqual(Equal.stringEqual, Equal.intEqual);
40-
final F<Integer, Writer<String, Integer>> defaultWriter = Writer.<Integer>stringLogger();
41-
42-
void assertProperty(Property p) {
43-
CheckResult cr = p.check();
44-
CheckResult.summary.println(cr);
45-
assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven());
46-
}
38+
final F<Integer, Writer<String, Integer>> defaultWriter = Writer.<Integer>stringLogger();git
4739

4840
@Test
4941
public void testTellProp() {
5042
Property p = property(arbString, arbString, arbInteger, (s1, s2, i) -> prop(tellTruth(s1, s2, i)));
51-
assertProperty(p);
43+
assertResult(p);
5244
}
5345

5446
@Test
@@ -57,7 +49,7 @@ public void testMap() {
5749
boolean b = eq.eq(defaultWriter.f(i).map(f), defaultWriter.f(f.f(i)));
5850
return prop(b);
5951
});
60-
assertProperty(p);
52+
assertResult(p);
6153
}
6254

6355
@Test
@@ -66,7 +58,7 @@ public void testFlatMap() {
6658
boolean b = eq.eq(Writer.<Integer>stringLogger().f(i).flatMap(f), f.f(i));
6759
return prop(b);
6860
});
69-
assertProperty(p);
61+
assertResult(p);
7062

7163
}
7264

@@ -87,7 +79,7 @@ public void testLeftIdentity() {
8779
(i, f) -> {
8880
return prop(eq.eq(defaultWriter.f(i).flatMap(f), f.f(i)));
8981
});
90-
assertProperty(p);
82+
assertResult(p);
9183
}
9284

9385
// Right identity: m >>= return == m
@@ -97,7 +89,7 @@ public void testRightIdentity() {
9789
arbWriterStringInt(),
9890
(w) -> prop(eq.eq(w.flatMap(a -> defaultWriter.f(a)), w))
9991
);
100-
assertProperty(p);
92+
assertResult(p);
10193
}
10294

10395
// Associativity: (m >>= f) >>= g == m >>= (\x -> f x >>= g)
@@ -111,7 +103,7 @@ public void testAssociativity() {
111103
boolean t = eq.eq(w.flatMap(f).flatMap(g), w.flatMap(x -> f.f(x).flatMap(g)));
112104
return prop(t);
113105
});
114-
assertProperty(p);
106+
assertResult(p);
115107
}
116108

117109

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fj.data.test;
2+
3+
import fj.Unit;
4+
import fj.test.CheckResult;
5+
import fj.test.Property;
6+
import org.junit.Assert;
7+
8+
/**
9+
* Created by MarkPerry on 18/12/2014.
10+
*/
11+
public class PropertyAssert {
12+
13+
public static Unit assertResult(Property p) {
14+
CheckResult cr = p.check();
15+
CheckResult.summary.println(cr);
16+
Assert.assertTrue(cr.isExhausted() || cr.isPassed() || cr.isProven());
17+
return Unit.unit();
18+
}
19+
20+
}

0 commit comments

Comments
 (0)