-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRewindIteratorTest.java
67 lines (58 loc) · 1.95 KB
/
RewindIteratorTest.java
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
import org.junit.Test;
import java.util.Iterator;
import java.util.Random;
import java.util.zip.CRC32;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class RewindIteratorTest {
@Test public void testFirst1000() {
// Change false to true to see what your iterator generates.
massTest(1000, 876040768L);
}
@Test public void testFirstMillion() {
massTest(1_000_000, 1839975941L);
}
@Test public void testFirstHundredMillion() {
massTest(100_000_000, 2819947101L);
}
private void massTest(int n, long expected) {
CRC32 check = new CRC32();
Random rng = new Random(4444);
Iterator<Integer> ints = new Iterator<>() {
int v = 0;
public boolean hasNext() {
return true;
}
public Integer next() {
return v++;
}
};
RewindIterator<Integer> rwi = new RewindIterator<>(ints);
int marks = 0, count = 0, prev = -1;
for(int i = 0; i < n; i++) {
int v = rwi.next();
if(prev != -1) {
// If no rewind took place last round, elements must increase by one.
assertEquals(prev + 1, v);
}
prev = v;
count++;
check.update(v);
if(rng.nextInt(100 + i) < 20 || (marks == 0 && count > 10 + i / 10)) {
rwi.mark();
marks++;
count = 0;
}
if(marks > 0 && rng.nextInt(100 + i) < 30) {
rwi.rewind();
marks--;
prev = -1;
}
}
assertEquals(expected, check.getValue());
while(marks-- > 0) { rwi.rewind(); }
try { rwi.rewind(); } catch(IllegalStateException e) { return; }
// If this line is reached, your rewind method does not fail as specified.
fail();
}
}