1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
jsr166 |
1.3 |
import java.util.concurrent.Executors; |
8 |
|
|
import java.util.concurrent.ExecutorService; |
9 |
|
|
import java.util.concurrent.Phaser; |
10 |
jsr166 |
1.8 |
import java.util.concurrent.ThreadLocalRandom; |
11 |
dl |
1.1 |
import java.util.concurrent.atomic.DoubleAccumulator; |
12 |
|
|
|
13 |
jsr166 |
1.3 |
import junit.framework.Test; |
14 |
|
|
import junit.framework.TestSuite; |
15 |
|
|
|
16 |
dl |
1.1 |
public class DoubleAccumulatorTest extends JSR166TestCase { |
17 |
|
|
public static void main(String[] args) { |
18 |
jsr166 |
1.4 |
main(suite(), args); |
19 |
dl |
1.1 |
} |
20 |
|
|
public static Test suite() { |
21 |
|
|
return new TestSuite(DoubleAccumulatorTest.class); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
/** |
25 |
jsr166 |
1.6 |
* new instance initialized to supplied identity |
26 |
dl |
1.1 |
*/ |
27 |
|
|
public void testConstructor() { |
28 |
jsr166 |
1.6 |
for (double identity : new double[] { |
29 |
|
|
Double.NEGATIVE_INFINITY, |
30 |
|
|
Double.POSITIVE_INFINITY, |
31 |
|
|
Double.MIN_VALUE, |
32 |
|
|
Double.MAX_VALUE, |
33 |
|
|
0.0, |
34 |
|
|
}) |
35 |
|
|
assertEquals(identity, |
36 |
|
|
new DoubleAccumulator(Double::max, identity).get()); |
37 |
dl |
1.1 |
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* accumulate accumulates given value to current, and get returns current value |
41 |
|
|
*/ |
42 |
|
|
public void testAccumulateAndGet() { |
43 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
44 |
|
|
acc.accumulate(2.0); |
45 |
|
|
assertEquals(2.0, acc.get()); |
46 |
|
|
acc.accumulate(-4.0); |
47 |
|
|
assertEquals(2.0, acc.get()); |
48 |
|
|
acc.accumulate(4.0); |
49 |
|
|
assertEquals(4.0, acc.get()); |
50 |
dl |
1.1 |
} |
51 |
|
|
|
52 |
|
|
/** |
53 |
jsr166 |
1.5 |
* reset() causes subsequent get() to return zero |
54 |
dl |
1.1 |
*/ |
55 |
|
|
public void testReset() { |
56 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
57 |
|
|
acc.accumulate(2.0); |
58 |
|
|
assertEquals(2.0, acc.get()); |
59 |
|
|
acc.reset(); |
60 |
|
|
assertEquals(0.0, acc.get()); |
61 |
dl |
1.1 |
} |
62 |
|
|
|
63 |
|
|
/** |
64 |
jsr166 |
1.5 |
* getThenReset() returns current value; subsequent get() returns zero |
65 |
dl |
1.1 |
*/ |
66 |
|
|
public void testGetThenReset() { |
67 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
68 |
|
|
acc.accumulate(2.0); |
69 |
|
|
assertEquals(2.0, acc.get()); |
70 |
|
|
assertEquals(2.0, acc.getThenReset()); |
71 |
|
|
assertEquals(0.0, acc.get()); |
72 |
dl |
1.1 |
} |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* toString returns current value. |
76 |
|
|
*/ |
77 |
|
|
public void testToString() { |
78 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
79 |
|
|
assertEquals("0.0", acc.toString()); |
80 |
|
|
acc.accumulate(1.0); |
81 |
|
|
assertEquals(Double.toString(1.0), acc.toString()); |
82 |
dl |
1.1 |
} |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* intValue returns current value. |
86 |
|
|
*/ |
87 |
|
|
public void testIntValue() { |
88 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
89 |
|
|
assertEquals(0, acc.intValue()); |
90 |
|
|
acc.accumulate(1.0); |
91 |
|
|
assertEquals(1, acc.intValue()); |
92 |
dl |
1.1 |
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* longValue returns current value. |
96 |
|
|
*/ |
97 |
|
|
public void testLongValue() { |
98 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
99 |
|
|
assertEquals(0, acc.longValue()); |
100 |
|
|
acc.accumulate(1.0); |
101 |
|
|
assertEquals(1, acc.longValue()); |
102 |
dl |
1.1 |
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* floatValue returns current value. |
106 |
|
|
*/ |
107 |
|
|
public void testFloatValue() { |
108 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
109 |
|
|
assertEquals(0.0f, acc.floatValue()); |
110 |
|
|
acc.accumulate(1.0); |
111 |
|
|
assertEquals(1.0f, acc.floatValue()); |
112 |
dl |
1.1 |
} |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* doubleValue returns current value. |
116 |
|
|
*/ |
117 |
|
|
public void testDoubleValue() { |
118 |
jsr166 |
1.7 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
119 |
|
|
assertEquals(0.0, acc.doubleValue()); |
120 |
|
|
acc.accumulate(1.0); |
121 |
|
|
assertEquals(1.0, acc.doubleValue()); |
122 |
dl |
1.1 |
} |
123 |
|
|
|
124 |
|
|
/** |
125 |
|
|
* accumulates by multiple threads produce correct result |
126 |
|
|
*/ |
127 |
|
|
public void testAccumulateAndGetMT() { |
128 |
jsr166 |
1.8 |
final DoubleAccumulator acc |
129 |
|
|
= new DoubleAccumulator((x, y) -> x + y, 0.0); |
130 |
|
|
final int nThreads = ThreadLocalRandom.current().nextInt(1, 5); |
131 |
|
|
final Phaser phaser = new Phaser(nThreads + 1); |
132 |
|
|
final int incs = 1_000_000; |
133 |
|
|
final double total = nThreads * incs/2.0 * (incs - 1); // Gauss |
134 |
|
|
final Runnable task = () -> { |
135 |
dl |
1.1 |
phaser.arriveAndAwaitAdvance(); |
136 |
jsr166 |
1.8 |
for (int i = 0; i < incs; i++) { |
137 |
|
|
acc.accumulate((double) i); |
138 |
|
|
assertTrue(acc.get() <= total); |
139 |
|
|
} |
140 |
dl |
1.1 |
phaser.arrive(); |
141 |
jsr166 |
1.8 |
}; |
142 |
|
|
final ExecutorService p = Executors.newCachedThreadPool(); |
143 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
144 |
|
|
for (int i = nThreads; i-->0; ) |
145 |
|
|
p.execute(task); |
146 |
|
|
phaser.arriveAndAwaitAdvance(); |
147 |
|
|
phaser.arriveAndAwaitAdvance(); |
148 |
|
|
assertEquals(total, acc.get()); |
149 |
dl |
1.1 |
} |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
} |