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