-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathRectangleSpliteratorTest.java
More file actions
73 lines (55 loc) · 1.75 KB
/
RectangleSpliteratorTest.java
File metadata and controls
73 lines (55 loc) · 1.75 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
package spliterators.part1.exercise;
import org.junit.Before;
import org.junit.Test;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.StreamSupport;
import static org.junit.Assert.*;
/**
* Created by student on 7/14/17.
*/
public class RectangleSpliteratorTest {
public int outerLength;
public int innerLength;
public int[][] array;
@Before
public void setup() {
outerLength = ThreadLocalRandom.current().nextInt(1000);
innerLength = ThreadLocalRandom.current().nextInt(1000);
array = new int[outerLength][];
for (int i = 0; i < array.length; i++) {
int[] inner = new int[innerLength];
array[i] = inner;
for (int j = 0; j < inner.length; j++) {
inner[j] = ThreadLocalRandom.current().nextInt();
}
}
}
@Test
public void sum() {
long l = rectangle_par();
long l1 = rectangle_seq();
long res = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
res += array[i][j];
}
}
assertEquals(res,l);
assertEquals(res,l1);
}
public long rectangle_seq() {
final boolean parallel = false;
return StreamSupport.intStream(new RectangleSpliterator(array), parallel)
.asLongStream()
.sum();
}
public long rectangle_par() {
final boolean parallel = true;
return StreamSupport.intStream(new RectangleSpliterator(array), parallel)
.asLongStream()
.sum();
}
}