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
|
import org.junit.Test;
import java.util.PrimitiveIterator;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
public class TestSuite {
@Test
public void testThatTheFirstTwentyElementsAreCorrect() {
assertArrayEquals("The first twenty elements are incorrect!",
new int[] {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765},
Utility.generateFibonacciSequence().limit(20).toArray());
}
@Test(timeout = 30)
public void testRecursivePropertyInStreamByRandomLeaps() {
for (int i = 0; i < 3; i++) { // Repeat three times
final PrimitiveIterator.OfInt iterator =
Utility.generateFibonacciSequence()
.skip((int) (Math.random() * 900)) // Begin leap
.limit(20) // End leap
.iterator();
int previous = iterator.nextInt(),
current = iterator.nextInt();
while (iterator.hasNext()) {
final int next = iterator.next();
if (next != previous + current)
fail(String.format("Elements %s, %s are followed by %s!", previous, current, next));
previous = current;
current = next;
}
}
}
}
|