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.LongAccumulator; |
10 |
|
|
|
11 |
|
|
public class LongAccumulatorTest 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(LongAccumulatorTest.class); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* default constructed initializes to zero |
21 |
|
|
*/ |
22 |
|
|
public void testConstructor() { |
23 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
24 |
|
|
assertEquals(0, ai.get()); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* accumulate accumulates given value to current, and get returns current value |
29 |
|
|
*/ |
30 |
|
|
public void testAccumulateAndGet() { |
31 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
32 |
|
|
ai.accumulate(2); |
33 |
|
|
assertEquals(2, ai.get()); |
34 |
|
|
ai.accumulate(-4); |
35 |
|
|
assertEquals(2, ai.get()); |
36 |
|
|
ai.accumulate(4); |
37 |
|
|
assertEquals(4, ai.get()); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
/** |
41 |
|
|
* reset zeroes get |
42 |
|
|
*/ |
43 |
|
|
public void testReset() { |
44 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
45 |
|
|
ai.accumulate(2); |
46 |
|
|
assertEquals(2, ai.get()); |
47 |
|
|
ai.reset(); |
48 |
|
|
assertEquals(0, ai.get()); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* getThenReset returns get then zeros |
53 |
|
|
*/ |
54 |
|
|
public void testGetThenReset() { |
55 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
56 |
|
|
ai.accumulate(2); |
57 |
|
|
assertEquals(2, ai.get()); |
58 |
|
|
assertEquals(2, ai.getThenReset()); |
59 |
|
|
assertEquals(0, ai.get()); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* toString returns current value. |
64 |
|
|
*/ |
65 |
|
|
public void testToString() { |
66 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
67 |
|
|
assertEquals("0", ai.toString()); |
68 |
|
|
ai.accumulate(1); |
69 |
|
|
assertEquals(Long.toString(1), ai.toString()); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
/** |
73 |
|
|
* intValue returns current value. |
74 |
|
|
*/ |
75 |
|
|
public void testIntValue() { |
76 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
77 |
|
|
assertEquals(0, ai.intValue()); |
78 |
|
|
ai.accumulate(1); |
79 |
|
|
assertEquals(1, ai.intValue()); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* longValue returns current value. |
84 |
|
|
*/ |
85 |
|
|
public void testLongValue() { |
86 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
87 |
|
|
assertEquals(0, ai.longValue()); |
88 |
|
|
ai.accumulate(1); |
89 |
|
|
assertEquals(1, ai.longValue()); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
/** |
93 |
|
|
* floatValue returns current value. |
94 |
|
|
*/ |
95 |
|
|
public void testFloatValue() { |
96 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
97 |
|
|
assertEquals(0.0f, ai.floatValue()); |
98 |
|
|
ai.accumulate(1); |
99 |
|
|
assertEquals(1.0f, ai.floatValue()); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* doubleValue returns current value. |
104 |
|
|
*/ |
105 |
|
|
public void testDoubleValue() { |
106 |
|
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
107 |
|
|
assertEquals(0.0, ai.doubleValue()); |
108 |
|
|
ai.accumulate(1); |
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 |
|
|
LongAccumulator a = new LongAccumulator(Long::max, 0L); |
120 |
|
|
Phaser phaser = new Phaser(nthreads + 1); |
121 |
|
|
for (int i = 0; i < nthreads; ++i) |
122 |
|
|
pool.execute(new AccumulateerTask(a, phaser, incs)); |
123 |
|
|
phaser.arriveAndAwaitAdvance(); |
124 |
|
|
phaser.arriveAndAwaitAdvance(); |
125 |
|
|
long expected = incs - 1; |
126 |
|
|
long result = a.get(); |
127 |
|
|
assertEquals(expected, result); |
128 |
|
|
pool.shutdown(); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
static final class AccumulateerTask implements Runnable { |
132 |
|
|
final LongAccumulator accumulateer; |
133 |
|
|
final Phaser phaser; |
134 |
|
|
final int incs; |
135 |
|
|
volatile long result; |
136 |
|
|
AccumulateerTask(LongAccumulator accumulateer, Phaser phaser, int incs) { |
137 |
|
|
this.accumulateer = accumulateer; |
138 |
|
|
this.phaser = phaser; |
139 |
|
|
this.incs = incs; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
public void run() { |
143 |
|
|
phaser.arriveAndAwaitAdvance(); |
144 |
|
|
LongAccumulator a = accumulateer; |
145 |
|
|
for (int i = 0; i < incs; ++i) |
146 |
|
|
a.accumulate(i); |
147 |
|
|
result = a.get(); |
148 |
|
|
phaser.arrive(); |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
} |