-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathZipWithIndexDoubleSpliteratorTest.java
More file actions
57 lines (42 loc) · 1.4 KB
/
ZipWithIndexDoubleSpliteratorTest.java
File metadata and controls
57 lines (42 loc) · 1.4 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
package spliterators.part2.exercise;
import org.junit.Test;
import java.util.Spliterator;
import java.util.stream.DoubleStream;
import java.util.stream.StreamSupport;
import static org.junit.Assert.*;
/**
* Created by student on 7/17/17.
*/
public class ZipWithIndexDoubleSpliteratorTest {
private int maxSize;
@Test
public void testPar() {
maxSize = 100;
Spliterator.OfDouble spliterator = DoubleStream.iterate(1.0, i -> i + 0.1)
.limit(maxSize)
.spliterator();
double sum = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(spliterator), true)
.mapToDouble(z -> z.getIndex()*z.getValue())
.sum();
double res = 0.0;
for (int i = 0; i < maxSize; ++i) {
res += i * (1.0 + 0.1*i);
}
assertEquals(sum, res, 0.1);
}
@Test
public void testSeq() {
maxSize = 100;
Spliterator.OfDouble spliterator = DoubleStream.iterate(1.0, i -> i + 0.1)
.limit(maxSize)
.spliterator();
double sum = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(spliterator), false)
.mapToDouble(z -> z.getIndex()*z.getValue())
.sum();
double res = 0.0;
for (int i = 0; i < maxSize; ++i) {
res += i * (1.0 + 0.1*i);
}
assertEquals(sum, res, 0.1);
}
}