4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.concurrent.*; |
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 |
< |
junit.textui.TestRunner.run(suite()); |
17 |
> |
main(suite(), args); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(LongAccumulatorTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
/** |
24 |
< |
* default constructed initializes to zero |
24 |
> |
* new instance initialized to supplied identity |
25 |
|
*/ |
26 |
|
public void testConstructor() { |
27 |
< |
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
28 |
< |
assertEquals(0, ai.get()); |
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 |
|
/** |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* reset zeroes get |
46 |
> |
* reset() causes subsequent get() to return zero |
47 |
|
*/ |
48 |
|
public void testReset() { |
49 |
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* getThenReset returns get then zeros |
57 |
> |
* getThenReset() returns current value; subsequent get() returns zero |
58 |
|
*/ |
59 |
|
public void testGetThenReset() { |
60 |
|
LongAccumulator ai = new LongAccumulator(Long::max, 0L); |
133 |
|
pool.shutdown(); |
134 |
|
} |
135 |
|
|
136 |
< |
static final class AccTask implements Runnable { |
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; |
141 |
> |
AccTask(LongAccumulator acc, Phaser phaser, int incs) { |
142 |
> |
this.acc = acc; |
143 |
|
this.phaser = phaser; |
144 |
|
this.incs = incs; |
145 |
|
} |