ScheduledFibonacci { public long get(final int index) { if (index <= 1) { return 1; } final ComputationResult<Long> x = scheduler.spawn(new Computation<Long>() { @Override public Long perform() { return new ScheduledFibonacci(schedulerFactory).get(index - 1); } }); final long y = new ScheduledFibonacci(schedulerFactory).get(index - 2); scheduler.syncAndEnd(); return x.get() + y; } ScheduledFibonacci(SchedulerFactory schedulerFactory); long get(final int index);  }
@Test public void testParallel() throws Exception { final ScheduledFibonacci fibonacci = new ScheduledFibonacci(new ContextAwareParallelSchedulerFactory()); long[] results = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89}; for (int i = 0; i < 11; i++) { assertThat(fibonacci.get(i), is(results[i])); } }
@Test public void testSerial() throws Exception { final ScheduledFibonacci fibonacci = new ScheduledFibonacci(new SerialSchedulerFactory()); long[] results = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89}; for (int i = 0; i < 11; i++) { assertThat(fibonacci.get(i), is(results[i])); } }
@Test(dependsOnMethods = {"testParallel", "testSerial"}) public void testParallelizationIntegrity() throws Exception { final ScheduledFibonacci first = new ScheduledFibonacci(new ContextAwareParallelSchedulerFactory()); final ScheduledFibonacci second = new ScheduledFibonacci(new SerialSchedulerFactory()); for (int i = 0; i < 10; i++) { assertThat(first.get(i), is(second.get(i))); } }