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