forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvalTest.java
More file actions
83 lines (68 loc) · 2.32 KB
/
EvalTest.java
File metadata and controls
83 lines (68 loc) · 2.32 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
package fj.data;
import fj.F0;
import fj.F2;
import org.junit.Assert;
import org.junit.Test;
import java.util.Iterator;
public class EvalTest {
@Test
public void testNow() {
Eval<Integer> eval = Eval.now(1);
Assert.assertEquals(eval.value().intValue(), 1);
Assert.assertEquals(eval.map(a -> a.toString()).value(), "1");
Assert.assertEquals(eval.bind(a -> Eval.now(a * 3)).value().intValue(), 3);
}
@Test
public void testLater() {
InvocationTrackingF<Integer> tracker = new InvocationTrackingF<>(1);
Eval<Integer> eval = Eval.later(tracker);
Assert.assertEquals(tracker.getInvocationCounter(), 0);
Assert.assertEquals(eval.value().intValue(), 1);
Assert.assertEquals(tracker.getInvocationCounter(), 1);
eval.value();
Assert.assertEquals(tracker.getInvocationCounter(), 1);
}
@Test
public void testAlways() {
InvocationTrackingF<Integer> tracker = new InvocationTrackingF<>(1);
Eval<Integer> eval = Eval.always(tracker);
Assert.assertEquals(tracker.getInvocationCounter(), 0);
Assert.assertEquals(eval.value().intValue(), 1);
Assert.assertEquals(tracker.getInvocationCounter(), 1);
eval.value();
eval.value();
Assert.assertEquals(tracker.getInvocationCounter(), 3);
}
@Test
public void testDefer() {
// Make sure that a recursive computation is actually stack-safe.
int targetValue = 200000;
Iterator<Integer> it = Enumerator.intEnumerator.toStream(0).iterator();
Eval<Boolean> result = foldRight(it, (v, acc) -> v == targetValue ? Eval.now(true) : acc, false);
Assert.assertTrue(result.value());
}
private static <A, T> Eval<A> foldRight(Iterator<T> iterator,
F2<T, Eval<A>, Eval<A>> f,
A zero) {
if (!iterator.hasNext()) {
return Eval.now(zero);
}
return f.f(iterator.next(), Eval.defer(() -> foldRight(iterator, f, zero)));
}
private static class InvocationTrackingF<A> implements F0<A> {
private final A value;
private int invocationCounter;
public InvocationTrackingF(A value) {
this.value = value;
this.invocationCounter = 0;
}
@Override
public A f() {
invocationCounter++;
return value;
}
public int getInvocationCounter() {
return invocationCounter;
}
}
}