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 |
import java.util.concurrent.Executors; |
8 |
import java.util.concurrent.ExecutorService; |
9 |
import java.util.concurrent.Phaser; |
10 |
import java.util.concurrent.ThreadLocalRandom; |
11 |
import java.util.concurrent.atomic.DoubleAccumulator; |
12 |
|
13 |
import junit.framework.Test; |
14 |
import junit.framework.TestSuite; |
15 |
|
16 |
public class DoubleAccumulatorTest extends JSR166TestCase { |
17 |
public static void main(String[] args) { |
18 |
main(suite(), args); |
19 |
} |
20 |
public static Test suite() { |
21 |
return new TestSuite(DoubleAccumulatorTest.class); |
22 |
} |
23 |
|
24 |
/** |
25 |
* new instance initialized to supplied identity |
26 |
*/ |
27 |
public void testConstructor() { |
28 |
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 |
} |
38 |
|
39 |
/** |
40 |
* accumulate accumulates given value to current, and get returns current value |
41 |
*/ |
42 |
public void testAccumulateAndGet() { |
43 |
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 |
} |
51 |
|
52 |
/** |
53 |
* reset() causes subsequent get() to return zero |
54 |
*/ |
55 |
public void testReset() { |
56 |
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 |
} |
62 |
|
63 |
/** |
64 |
* getThenReset() returns current value; subsequent get() returns zero |
65 |
*/ |
66 |
public void testGetThenReset() { |
67 |
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 |
} |
73 |
|
74 |
/** |
75 |
* toString returns current value. |
76 |
*/ |
77 |
public void testToString() { |
78 |
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 |
} |
83 |
|
84 |
/** |
85 |
* intValue returns current value. |
86 |
*/ |
87 |
public void testIntValue() { |
88 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
89 |
assertEquals(0, acc.intValue()); |
90 |
acc.accumulate(1.0); |
91 |
assertEquals(1, acc.intValue()); |
92 |
} |
93 |
|
94 |
/** |
95 |
* longValue returns current value. |
96 |
*/ |
97 |
public void testLongValue() { |
98 |
DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0); |
99 |
assertEquals(0, acc.longValue()); |
100 |
acc.accumulate(1.0); |
101 |
assertEquals(1, acc.longValue()); |
102 |
} |
103 |
|
104 |
/** |
105 |
* floatValue returns current value. |
106 |
*/ |
107 |
public void testFloatValue() { |
108 |
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 |
} |
113 |
|
114 |
/** |
115 |
* doubleValue returns current value. |
116 |
*/ |
117 |
public void testDoubleValue() { |
118 |
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 |
} |
123 |
|
124 |
/** |
125 |
* accumulates by multiple threads produce correct result |
126 |
*/ |
127 |
public void testAccumulateAndGetMT() { |
128 |
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 = expensiveTests ? 1_000_000 : 100_000; |
133 |
final double total = nThreads * incs/2.0 * (incs - 1); // Gauss |
134 |
final Runnable task = () -> { |
135 |
phaser.arriveAndAwaitAdvance(); |
136 |
for (int i = 0; i < incs; i++) { |
137 |
acc.accumulate((double) i); |
138 |
assertTrue(acc.get() <= total); |
139 |
} |
140 |
phaser.arrive(); |
141 |
}; |
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 |
} |
150 |
} |
151 |
|
152 |
} |