-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathZipWithArraySpliteratorTest.java
More file actions
87 lines (68 loc) · 3.18 KB
/
ZipWithArraySpliteratorTest.java
File metadata and controls
87 lines (68 loc) · 3.18 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
package spliterators.part2.example;
import org.junit.Test;
import spliterators.part1.example.ArrayExample;
import spliterators.part3.exercise.Pair;
import spliterators.part3.exercise.ZipWithArraySpliterator;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.*;
public class ZipWithArraySpliteratorTest {
private Integer[] getRandomArray(int length) {
final Integer[] result = new Integer[length];
for (int i = 0; i < length; i++)
result[i] = ThreadLocalRandom.current().nextInt();
return result;
}
@Test
public void innerIsOfGreaterLengthSuccess() {
final Integer[] innerArray = getRandomArray(30);
final Integer[] array = getRandomArray(10);
final ZipWithArraySpliterator<Integer, Integer> spliterator = new ZipWithArraySpliterator<>(
Spliterators.spliterator(innerArray, 0), array);
final List<String> actual = StreamSupport.stream(spliterator, false)
.map(p -> new Pair<>(p.getA().hashCode() + 1, p.getB() + 1))
.map(Pair::toString)
.collect(Collectors.toList());
assertThat(actual.size(), is(10));
}
@Test
public void ArrayIsOfGreaterLengthSuccess() {
final Integer[] innerArray = getRandomArray(10);
final Integer[] array = getRandomArray(30);
final ZipWithArraySpliterator<Integer, Integer> spliterator = new ZipWithArraySpliterator<>(
Spliterators.spliterator(innerArray, 0), array);
final List<String> actual = StreamSupport.stream(spliterator, false)
.map(p -> new Pair<>(p.getA().hashCode() + 1, p.getB() + 1))
.map(Pair::toString)
.collect(Collectors.toList());
assertThat(actual.size(), is(10));
}
@Test
public void nonzeroLengthSuccess() {
final Integer[] innerArray = getRandomArray(10);
final Integer[] array = getRandomArray(10);
final ZipWithArraySpliterator<Integer, Integer> spliterator = new ZipWithArraySpliterator<>(
Spliterators.spliterator(innerArray, 0), array);
final List<String> actual = StreamSupport.stream(spliterator, false)
.map(p -> new Pair<>(p.getA().hashCode() + 1, p.getB() + 1))
.map(Pair::toString)
.collect(Collectors.toList());
assertThat(actual.size(), is(10));
}
@Test
public void zeroLengthSuccess() {
final Integer[] innerArray = getRandomArray(0);
final Integer[] array = getRandomArray(0);
final ZipWithArraySpliterator<Integer, Integer> spliterator = new ZipWithArraySpliterator<>(
Spliterators.spliterator(innerArray, 0), array);
final List<String> actual = StreamSupport.stream(spliterator, false)
.map(p -> new Pair<>(p.getA().hashCode() + 1, p.getB() + 1))
.map(Pair::toString)
.collect(Collectors.toList());
assertThat(actual.size(), is(0));
}
}