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