-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathZipWithArraySpliteratorTest.java
More file actions
114 lines (80 loc) · 2.94 KB
/
ZipWithArraySpliteratorTest.java
File metadata and controls
114 lines (80 loc) · 2.94 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package spliterators.part3.exercise;
import org.junit.Test;
import java.util.Arrays;
import java.util.Spliterator;
import java.util.stream.DoubleStream;
import java.util.stream.StreamSupport;
import static org.junit.Assert.*;
/**
* Created by student on 7/18/17.
*/
public class ZipWithArraySpliteratorTest {
private int maxSize;
@Test
public void testPar() {
maxSize = 10000;
Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();
double res = 0.0;
assertEquals(sum, res, 0.1);
}
private Double[] generateDouble() {
Double[] doubles = new Double[maxSize];
for (int i = 0; i < maxSize; i++) {
doubles[i] = Math.random();
}
return doubles;
}
@Test
public void testSeq() {
maxSize = 10000;
Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();
double res = 0.0;
assertEquals(sum, res, 0.1);
}
@Test
public void testParDifSize() {
maxSize = 10000;
Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length/2)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();
double res = 0.0;
assertEquals(sum, res, 0.1);
}
@Test
public void testSeqDifSize() {
maxSize = 10000;
Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length/2)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();
double res = 0.0;
assertEquals(sum, res, 0.1);
}
@Test
public void emptyRight() {
maxSize = 10000;
Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, 0)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();
double res = 0.0;
assertEquals(sum, res, 0.1);
}
@Test
public void emptLeft() {
maxSize = 10000;
Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(Arrays.copyOf(array, 0)), array), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();
double res = 0.0;
assertEquals(sum, res, 0.1);
}
}