|
| 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 | +} |
0 commit comments