-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathNodeSpliteratorTest.java
More file actions
88 lines (66 loc) · 2.25 KB
/
NodeSpliteratorTest.java
File metadata and controls
88 lines (66 loc) · 2.25 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package spliterators.part4.data;
import org.junit.Before;
import org.junit.Test;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class NodeSpliteratorTest {
private Node<Integer> head;
private final static int LIMIT = 1000;
private Set<Integer> expected;
private Node<Integer> build(int left, int right) {
if (left > right) {
return null;
}
final int mid = (left + right) / 2;
final Node<Integer> l = build(left, mid - 1);
final Node<Integer> r = build(mid + 1, right);
return new Node<>(mid, l, r);
}
private int addRandom(Node<Integer> node) {
if ((node.getRight() == null) || (node.getLeft() == null)) {
final int add = ThreadLocalRandom.current().nextInt();
node.setNullChild(new Node<>(add, null, null));
return add;
}
final int res = LIMIT + ThreadLocalRandom.current().nextInt();
if (res > 0) {
return addRandom(node.getLeft());
} else {
return addRandom(node.getRight());
}
}
@Before
public void setUp() {
head = build(0, LIMIT);
expected = Stream.iterate(0, i -> i + 1).limit(LIMIT + 1).collect(Collectors.toSet());
}
@Test
public void testSeq() {
final Set<Integer> collect = head.stream(false).collect(Collectors.toSet());
assertEquals(collect, expected);
}
@Test
public void testSeqUnbalacnced() {
for (int i = 0; i < LIMIT; i++) {
expected.add(addRandom(head));
}
final Set<Integer> collect = head.stream(false).collect(Collectors.toSet());
assertEquals(collect, expected);
}
@Test
public void testPar() {
final Set<Integer> collect = head.stream(true).collect(Collectors.toSet());
assertEquals(collect, expected);
}
@Test
public void testParUnBalanced() {
for (int i = 0; i < LIMIT; i++) {
expected.add(addRandom(head));
}
final Set<Integer> collect = head.stream(true).collect(Collectors.toSet());
assertEquals(collect, expected);
}
}