author | dl |
Sat, 14 Sep 2019 11:26:26 -0700 | |
changeset 58138 | 1e4270f875ee |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
34 |
import java.util.concurrent.Executors; |
|
35 |
import java.util.concurrent.ExecutorService; |
|
36 |
import java.util.concurrent.Phaser; |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
37 |
import java.util.concurrent.ThreadLocalRandom; |
35394 | 38 |
import java.util.concurrent.atomic.LongAccumulator; |
39 |
||
40 |
import junit.framework.Test; |
|
41 |
import junit.framework.TestSuite; |
|
42 |
||
43 |
public class LongAccumulatorTest extends JSR166TestCase { |
|
44 |
public static void main(String[] args) { |
|
45 |
main(suite(), args); |
|
46 |
} |
|
47 |
public static Test suite() { |
|
48 |
return new TestSuite(LongAccumulatorTest.class); |
|
49 |
} |
|
50 |
||
51 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
52 |
* new instance initialized to supplied identity |
35394 | 53 |
*/ |
54 |
public void testConstructor() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
55 |
for (long identity : new long[] { Long.MIN_VALUE, 0, Long.MAX_VALUE }) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
56 |
assertEquals(identity, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
57 |
new LongAccumulator(Long::max, identity).get()); |
35394 | 58 |
} |
59 |
||
60 |
/** |
|
61 |
* accumulate accumulates given value to current, and get returns current value |
|
62 |
*/ |
|
63 |
public void testAccumulateAndGet() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
64 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
65 |
acc.accumulate(2); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
66 |
assertEquals(2, acc.get()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
67 |
acc.accumulate(-4); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
68 |
assertEquals(2, acc.get()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
69 |
acc.accumulate(4); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
70 |
assertEquals(4, acc.get()); |
35394 | 71 |
} |
72 |
||
73 |
/** |
|
74 |
* reset() causes subsequent get() to return zero |
|
75 |
*/ |
|
76 |
public void testReset() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
77 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
78 |
acc.accumulate(2); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
79 |
assertEquals(2, acc.get()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
80 |
acc.reset(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
81 |
assertEquals(0, acc.get()); |
35394 | 82 |
} |
83 |
||
84 |
/** |
|
85 |
* getThenReset() returns current value; subsequent get() returns zero |
|
86 |
*/ |
|
87 |
public void testGetThenReset() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
88 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
89 |
acc.accumulate(2); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
90 |
assertEquals(2, acc.get()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
91 |
assertEquals(2, acc.getThenReset()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
92 |
assertEquals(0, acc.get()); |
35394 | 93 |
} |
94 |
||
95 |
/** |
|
96 |
* toString returns current value. |
|
97 |
*/ |
|
98 |
public void testToString() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
99 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
100 |
assertEquals("0", acc.toString()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
101 |
acc.accumulate(1); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
102 |
assertEquals(Long.toString(1), acc.toString()); |
35394 | 103 |
} |
104 |
||
105 |
/** |
|
106 |
* intValue returns current value. |
|
107 |
*/ |
|
108 |
public void testIntValue() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
109 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
110 |
assertEquals(0, acc.intValue()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
111 |
acc.accumulate(1); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
112 |
assertEquals(1, acc.intValue()); |
35394 | 113 |
} |
114 |
||
115 |
/** |
|
116 |
* longValue returns current value. |
|
117 |
*/ |
|
118 |
public void testLongValue() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
119 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
120 |
assertEquals(0, acc.longValue()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
121 |
acc.accumulate(1); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
122 |
assertEquals(1, acc.longValue()); |
35394 | 123 |
} |
124 |
||
125 |
/** |
|
126 |
* floatValue returns current value. |
|
127 |
*/ |
|
128 |
public void testFloatValue() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
129 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
130 |
assertEquals(0.0f, acc.floatValue()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
131 |
acc.accumulate(1); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
132 |
assertEquals(1.0f, acc.floatValue()); |
35394 | 133 |
} |
134 |
||
135 |
/** |
|
136 |
* doubleValue returns current value. |
|
137 |
*/ |
|
138 |
public void testDoubleValue() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
139 |
LongAccumulator acc = new LongAccumulator(Long::max, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
140 |
assertEquals(0.0, acc.doubleValue()); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
141 |
acc.accumulate(1); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
142 |
assertEquals(1.0, acc.doubleValue()); |
35394 | 143 |
} |
144 |
||
145 |
/** |
|
146 |
* accumulates by multiple threads produce correct result |
|
147 |
*/ |
|
148 |
public void testAccumulateAndGetMT() { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
149 |
final LongAccumulator acc |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
150 |
= new LongAccumulator((x, y) -> x + y, 0L); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
151 |
final int nThreads = ThreadLocalRandom.current().nextInt(1, 5); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
152 |
final Phaser phaser = new Phaser(nThreads + 1); |
58138
1e4270f875ee
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents:
47216
diff
changeset
|
153 |
final int incs = expensiveTests ? 1_000_000 : 100_000; |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
154 |
final long total = nThreads * incs/2L * (incs - 1); // Gauss |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
155 |
final Runnable task = () -> { |
35394 | 156 |
phaser.arriveAndAwaitAdvance(); |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
157 |
for (int i = 0; i < incs; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
158 |
acc.accumulate((long) i); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
159 |
assertTrue(acc.get() <= total); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
160 |
} |
35394 | 161 |
phaser.arrive(); |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
162 |
}; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
163 |
final ExecutorService p = Executors.newCachedThreadPool(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
164 |
try (PoolCleaner cleaner = cleaner(p)) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
165 |
for (int i = nThreads; i-->0; ) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
166 |
p.execute(task); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
167 |
phaser.arriveAndAwaitAdvance(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
168 |
phaser.arriveAndAwaitAdvance(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
35394
diff
changeset
|
169 |
assertEquals(total, acc.get()); |
35394 | 170 |
} |
171 |
} |
|
172 |
||
173 |
} |