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 |
dl |
1.1 |
import java.util.concurrent.atomic.DoubleAccumulator; |
11 |
|
|
|
12 |
jsr166 |
1.3 |
import junit.framework.Test; |
13 |
|
|
import junit.framework.TestSuite; |
14 |
|
|
|
15 |
dl |
1.1 |
public class DoubleAccumulatorTest extends JSR166TestCase { |
16 |
|
|
public static void main(String[] args) { |
17 |
jsr166 |
1.4 |
main(suite(), args); |
18 |
dl |
1.1 |
} |
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(DoubleAccumulatorTest.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
/** |
24 |
jsr166 |
1.6 |
* new instance initialized to supplied identity |
25 |
dl |
1.1 |
*/ |
26 |
|
|
public void testConstructor() { |
27 |
jsr166 |
1.6 |
for (double identity : new double[] { |
28 |
|
|
Double.NEGATIVE_INFINITY, |
29 |
|
|
Double.POSITIVE_INFINITY, |
30 |
|
|
Double.MIN_VALUE, |
31 |
|
|
Double.MAX_VALUE, |
32 |
|
|
0.0, |
33 |
|
|
}) |
34 |
|
|
assertEquals(identity, |
35 |
|
|
new DoubleAccumulator(Double::max, identity).get()); |
36 |
dl |
1.1 |
} |
37 |
|
|
|
38 |
|
|
/** |
39 |
|
|
* accumulate accumulates given value to current, and get returns current value |
40 |
|
|
*/ |
41 |
|
|
public void testAccumulateAndGet() { |
42 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
43 |
|
|
ai.accumulate(2.0); |
44 |
|
|
assertEquals(2.0, ai.get()); |
45 |
|
|
ai.accumulate(-4.0); |
46 |
|
|
assertEquals(2.0, ai.get()); |
47 |
|
|
ai.accumulate(4.0); |
48 |
|
|
assertEquals(4.0, ai.get()); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
/** |
52 |
jsr166 |
1.5 |
* reset() causes subsequent get() to return zero |
53 |
dl |
1.1 |
*/ |
54 |
|
|
public void testReset() { |
55 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
56 |
|
|
ai.accumulate(2.0); |
57 |
|
|
assertEquals(2.0, ai.get()); |
58 |
|
|
ai.reset(); |
59 |
|
|
assertEquals(0.0, ai.get()); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
jsr166 |
1.5 |
* getThenReset() returns current value; subsequent get() returns zero |
64 |
dl |
1.1 |
*/ |
65 |
|
|
public void testGetThenReset() { |
66 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
67 |
|
|
ai.accumulate(2.0); |
68 |
|
|
assertEquals(2.0, ai.get()); |
69 |
|
|
assertEquals(2.0, ai.getThenReset()); |
70 |
|
|
assertEquals(0.0, ai.get()); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* toString returns current value. |
75 |
|
|
*/ |
76 |
|
|
public void testToString() { |
77 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
78 |
|
|
assertEquals("0.0", ai.toString()); |
79 |
|
|
ai.accumulate(1.0); |
80 |
|
|
assertEquals(Double.toString(1.0), ai.toString()); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
/** |
84 |
|
|
* intValue returns current value. |
85 |
|
|
*/ |
86 |
|
|
public void testIntValue() { |
87 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
88 |
|
|
assertEquals(0, ai.intValue()); |
89 |
|
|
ai.accumulate(1.0); |
90 |
|
|
assertEquals(1, ai.intValue()); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* longValue returns current value. |
95 |
|
|
*/ |
96 |
|
|
public void testLongValue() { |
97 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
98 |
|
|
assertEquals(0, ai.longValue()); |
99 |
|
|
ai.accumulate(1.0); |
100 |
|
|
assertEquals(1, ai.longValue()); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* floatValue returns current value. |
105 |
|
|
*/ |
106 |
|
|
public void testFloatValue() { |
107 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
108 |
|
|
assertEquals(0.0f, ai.floatValue()); |
109 |
|
|
ai.accumulate(1.0); |
110 |
|
|
assertEquals(1.0f, ai.floatValue()); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* doubleValue returns current value. |
115 |
|
|
*/ |
116 |
|
|
public void testDoubleValue() { |
117 |
|
|
DoubleAccumulator ai = new DoubleAccumulator(Double::max, 0.0); |
118 |
|
|
assertEquals(0.0, ai.doubleValue()); |
119 |
|
|
ai.accumulate(1.0); |
120 |
|
|
assertEquals(1.0, ai.doubleValue()); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
/** |
124 |
|
|
* accumulates by multiple threads produce correct result |
125 |
|
|
*/ |
126 |
|
|
public void testAccumulateAndGetMT() { |
127 |
|
|
final int incs = 1000000; |
128 |
|
|
final int nthreads = 4; |
129 |
|
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
130 |
|
|
DoubleAccumulator a = new DoubleAccumulator(Double::max, 0.0); |
131 |
|
|
Phaser phaser = new Phaser(nthreads + 1); |
132 |
|
|
for (int i = 0; i < nthreads; ++i) |
133 |
|
|
pool.execute(new AccTask(a, phaser, incs)); |
134 |
|
|
phaser.arriveAndAwaitAdvance(); |
135 |
|
|
phaser.arriveAndAwaitAdvance(); |
136 |
|
|
double expected = incs - 1; |
137 |
|
|
double result = a.get(); |
138 |
|
|
assertEquals(expected, result); |
139 |
|
|
pool.shutdown(); |
140 |
|
|
} |
141 |
|
|
|
142 |
jsr166 |
1.2 |
static final class AccTask implements Runnable { |
143 |
dl |
1.1 |
final DoubleAccumulator acc; |
144 |
|
|
final Phaser phaser; |
145 |
|
|
final int incs; |
146 |
|
|
volatile double result; |
147 |
jsr166 |
1.2 |
AccTask(DoubleAccumulator acc, Phaser phaser, int incs) { |
148 |
|
|
this.acc = acc; |
149 |
dl |
1.1 |
this.phaser = phaser; |
150 |
|
|
this.incs = incs; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
public void run() { |
154 |
|
|
phaser.arriveAndAwaitAdvance(); |
155 |
|
|
DoubleAccumulator a = acc; |
156 |
|
|
for (int i = 0; i < incs; ++i) |
157 |
|
|
a.accumulate(i); |
158 |
|
|
result = a.get(); |
159 |
|
|
phaser.arrive(); |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
} |