Skip to content

Commit e1a0983

Browse files
gliptakjbgi
authored andcommitted
Add Strategy tests
Signed-off-by: Gábor Lipták <gliptak@gmail.com> (cherry picked from commit 2e1f2c5)
1 parent abb3263 commit e1a0983

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package fj.control.parallel;
2+
3+
import fj.Ord;
4+
import fj.P;
5+
import fj.P1;
6+
import fj.data.Enumerator;
7+
import fj.data.List;
8+
import fj.data.Stream;
9+
import org.junit.Test;
10+
11+
import java.util.concurrent.*;
12+
13+
import static fj.control.parallel.Callables.callable;
14+
import static fj.control.parallel.Strategy.*;
15+
import static fj.data.Stream.range;
16+
import static org.hamcrest.core.Is.is;
17+
import static org.junit.Assert.assertThat;
18+
19+
public class StrategyTest {
20+
21+
@Test
22+
public void testStrategySeq() {
23+
final Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
24+
assertThat(s.sort(Ord.intOrd, seqStrategy()), is(s.sort(Ord.intOrd)));
25+
}
26+
27+
@Test
28+
public void testStrategyThread() {
29+
final Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
30+
assertThat(s.sort(Ord.intOrd, simpleThreadStrategy()), is(s.sort(Ord.intOrd)));
31+
}
32+
33+
@Test
34+
public void testStrategyExecutor() {
35+
final Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
36+
final ExecutorService es = Executors.newFixedThreadPool(10);
37+
assertThat(s.sort(Ord.intOrd, executorStrategy(es)), is(s.sort(Ord.intOrd)));
38+
}
39+
40+
@Test
41+
public void testStrategyCompletion() {
42+
final Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
43+
final ExecutorService es = Executors.newFixedThreadPool(10);
44+
final CompletionService cs = new ExecutorCompletionService(es);
45+
assertThat(s.sort(Ord.intOrd, completionStrategy(cs)), is(s.sort(Ord.intOrd)));
46+
}
47+
48+
@Test
49+
public void testStrategyMergeAll() {
50+
final List<Integer> l = List.range(0, 100);
51+
final List<P1<Integer>> p1s = mergeAll(l.map(x -> CompletableFuture.supplyAsync(() -> x)));
52+
assertThat(P1.sequence(p1s)._1(), is(l));
53+
}
54+
55+
@Test
56+
public void testStrategyCallables() throws Exception {
57+
final Strategy<Callable<Integer>> s = strategy(c -> c);
58+
final Strategy<Callable<Integer>> cs = callableStrategy(s);
59+
assertThat(callableStrategy(s).par(P.p(callable(1)))._1().call(), is(1));
60+
}
61+
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package fj.data;
22

33
import fj.Equal;
4-
import fj.Ord;
54
import fj.P2;
6-
import fj.control.parallel.Strategy;
75
import org.junit.Test;
86

97
import java.util.ConcurrentModificationException;
@@ -105,18 +103,4 @@ public void testMinus() {
105103
assertThat(s1.minus(Equal.charEqual, s2),
106104
is(stream(new Character[]{'a', 'c', 'd'})));
107105
}
108-
109-
@Test
110-
public void testSortSeq() {
111-
Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
112-
assertThat(s.sort(Ord.intOrd, Strategy.seqStrategy()),
113-
is(s.sort(Ord.intOrd)));
114-
}
115-
116-
@Test
117-
public void testSortThread() {
118-
Stream<Integer> s = range(Enumerator.intEnumerator, 99, -99, -1);
119-
assertThat(s.sort(Ord.intOrd, Strategy.simpleThreadStrategy()),
120-
is(s.sort(Ord.intOrd)));
121-
}
122106
}

0 commit comments

Comments
 (0)