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