author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 43522 | f9c6f543c4db |
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 and Martin Buchholz with assistance from |
|
30 |
* members of JCP JSR-166 Expert Group and released to the public |
|
31 |
* domain, as explained at |
|
32 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
33 |
*/ |
|
34 |
||
35 |
import java.util.concurrent.atomic.AtomicInteger; |
|
36 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
|
37 |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
|
38 |
import java.util.concurrent.atomic.AtomicLong; |
|
39 |
import java.util.concurrent.atomic.AtomicLongArray; |
|
40 |
import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
|
41 |
import java.util.concurrent.atomic.AtomicReference; |
|
42 |
import java.util.concurrent.atomic.AtomicReferenceArray; |
|
43 |
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
|
44 |
||
45 |
import junit.framework.Test; |
|
46 |
import junit.framework.TestSuite; |
|
47 |
||
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
48 |
/** |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
49 |
* Tests of atomic class methods accepting lambdas introduced in JDK8. |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
50 |
*/ |
35394 | 51 |
public class Atomic8Test extends JSR166TestCase { |
52 |
||
53 |
public static void main(String[] args) { |
|
54 |
main(suite(), args); |
|
55 |
} |
|
56 |
public static Test suite() { |
|
57 |
return new TestSuite(Atomic8Test.class); |
|
58 |
} |
|
59 |
||
60 |
static long addLong17(long x) { return x + 17; } |
|
61 |
static int addInt17(int x) { return x + 17; } |
|
62 |
static Integer addInteger17(Integer x) { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
63 |
return x.intValue() + 17; |
35394 | 64 |
} |
65 |
static Integer sumInteger(Integer x, Integer y) { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
66 |
return x.intValue() + y.intValue(); |
35394 | 67 |
} |
68 |
||
69 |
volatile long aLongField; |
|
70 |
volatile int anIntField; |
|
71 |
volatile Integer anIntegerField; |
|
72 |
||
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
73 |
AtomicLongFieldUpdater<Atomic8Test> aLongFieldUpdater() { |
35394 | 74 |
return AtomicLongFieldUpdater.newUpdater |
75 |
(Atomic8Test.class, "aLongField"); |
|
76 |
} |
|
77 |
||
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
78 |
AtomicIntegerFieldUpdater<Atomic8Test> anIntFieldUpdater() { |
35394 | 79 |
return AtomicIntegerFieldUpdater.newUpdater |
80 |
(Atomic8Test.class, "anIntField"); |
|
81 |
} |
|
82 |
||
83 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() { |
|
84 |
return AtomicReferenceFieldUpdater.newUpdater |
|
85 |
(Atomic8Test.class, Integer.class, "anIntegerField"); |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* AtomicLong getAndUpdate returns previous value and updates |
|
90 |
* result of supplied function |
|
91 |
*/ |
|
92 |
public void testLongGetAndUpdate() { |
|
93 |
AtomicLong a = new AtomicLong(1L); |
|
94 |
assertEquals(1L, a.getAndUpdate(Atomic8Test::addLong17)); |
|
95 |
assertEquals(18L, a.getAndUpdate(Atomic8Test::addLong17)); |
|
96 |
assertEquals(35L, a.get()); |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* AtomicLong updateAndGet updates with supplied function and |
|
101 |
* returns result. |
|
102 |
*/ |
|
103 |
public void testLongUpdateAndGet() { |
|
104 |
AtomicLong a = new AtomicLong(1L); |
|
105 |
assertEquals(18L, a.updateAndGet(Atomic8Test::addLong17)); |
|
106 |
assertEquals(35L, a.updateAndGet(Atomic8Test::addLong17)); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* AtomicLong getAndAccumulate returns previous value and updates |
|
111 |
* with supplied function. |
|
112 |
*/ |
|
113 |
public void testLongGetAndAccumulate() { |
|
114 |
AtomicLong a = new AtomicLong(1L); |
|
115 |
assertEquals(1L, a.getAndAccumulate(2L, Long::sum)); |
|
116 |
assertEquals(3L, a.getAndAccumulate(3L, Long::sum)); |
|
117 |
assertEquals(6L, a.get()); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* AtomicLong accumulateAndGet updates with supplied function and |
|
122 |
* returns result. |
|
123 |
*/ |
|
124 |
public void testLongAccumulateAndGet() { |
|
125 |
AtomicLong a = new AtomicLong(1L); |
|
126 |
assertEquals(7L, a.accumulateAndGet(6L, Long::sum)); |
|
127 |
assertEquals(10L, a.accumulateAndGet(3L, Long::sum)); |
|
128 |
assertEquals(10L, a.get()); |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* AtomicInteger getAndUpdate returns previous value and updates |
|
133 |
* result of supplied function |
|
134 |
*/ |
|
135 |
public void testIntGetAndUpdate() { |
|
136 |
AtomicInteger a = new AtomicInteger(1); |
|
137 |
assertEquals(1, a.getAndUpdate(Atomic8Test::addInt17)); |
|
138 |
assertEquals(18, a.getAndUpdate(Atomic8Test::addInt17)); |
|
139 |
assertEquals(35, a.get()); |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* AtomicInteger updateAndGet updates with supplied function and |
|
144 |
* returns result. |
|
145 |
*/ |
|
146 |
public void testIntUpdateAndGet() { |
|
147 |
AtomicInteger a = new AtomicInteger(1); |
|
148 |
assertEquals(18, a.updateAndGet(Atomic8Test::addInt17)); |
|
149 |
assertEquals(35, a.updateAndGet(Atomic8Test::addInt17)); |
|
150 |
assertEquals(35, a.get()); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* AtomicInteger getAndAccumulate returns previous value and updates |
|
155 |
* with supplied function. |
|
156 |
*/ |
|
157 |
public void testIntGetAndAccumulate() { |
|
158 |
AtomicInteger a = new AtomicInteger(1); |
|
159 |
assertEquals(1, a.getAndAccumulate(2, Integer::sum)); |
|
160 |
assertEquals(3, a.getAndAccumulate(3, Integer::sum)); |
|
161 |
assertEquals(6, a.get()); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* AtomicInteger accumulateAndGet updates with supplied function and |
|
166 |
* returns result. |
|
167 |
*/ |
|
168 |
public void testIntAccumulateAndGet() { |
|
169 |
AtomicInteger a = new AtomicInteger(1); |
|
170 |
assertEquals(7, a.accumulateAndGet(6, Integer::sum)); |
|
171 |
assertEquals(10, a.accumulateAndGet(3, Integer::sum)); |
|
172 |
assertEquals(10, a.get()); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* AtomicReference getAndUpdate returns previous value and updates |
|
177 |
* result of supplied function |
|
178 |
*/ |
|
179 |
public void testReferenceGetAndUpdate() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
35394
diff
changeset
|
180 |
AtomicReference<Integer> a = new AtomicReference<>(one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
181 |
assertEquals((Integer) 1, a.getAndUpdate(Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
182 |
assertEquals((Integer) 18, a.getAndUpdate(Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
183 |
assertEquals((Integer) 35, a.get()); |
35394 | 184 |
} |
185 |
||
186 |
/** |
|
187 |
* AtomicReference updateAndGet updates with supplied function and |
|
188 |
* returns result. |
|
189 |
*/ |
|
190 |
public void testReferenceUpdateAndGet() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
35394
diff
changeset
|
191 |
AtomicReference<Integer> a = new AtomicReference<>(one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
192 |
assertEquals((Integer) 18, a.updateAndGet(Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
193 |
assertEquals((Integer) 35, a.updateAndGet(Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
194 |
assertEquals((Integer) 35, a.get()); |
35394 | 195 |
} |
196 |
||
197 |
/** |
|
198 |
* AtomicReference getAndAccumulate returns previous value and updates |
|
199 |
* with supplied function. |
|
200 |
*/ |
|
201 |
public void testReferenceGetAndAccumulate() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
35394
diff
changeset
|
202 |
AtomicReference<Integer> a = new AtomicReference<>(one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
203 |
assertEquals((Integer) 1, a.getAndAccumulate(2, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
204 |
assertEquals((Integer) 3, a.getAndAccumulate(3, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
205 |
assertEquals((Integer) 6, a.get()); |
35394 | 206 |
} |
207 |
||
208 |
/** |
|
209 |
* AtomicReference accumulateAndGet updates with supplied function and |
|
210 |
* returns result. |
|
211 |
*/ |
|
212 |
public void testReferenceAccumulateAndGet() { |
|
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
35394
diff
changeset
|
213 |
AtomicReference<Integer> a = new AtomicReference<>(one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
214 |
assertEquals((Integer) 7, a.accumulateAndGet(6, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
215 |
assertEquals((Integer) 10, a.accumulateAndGet(3, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
216 |
assertEquals((Integer) 10, a.get()); |
35394 | 217 |
} |
218 |
||
219 |
/** |
|
220 |
* AtomicLongArray getAndUpdate returns previous value and updates |
|
221 |
* result of supplied function |
|
222 |
*/ |
|
223 |
public void testLongArrayGetAndUpdate() { |
|
224 |
AtomicLongArray a = new AtomicLongArray(1); |
|
225 |
a.set(0, 1); |
|
226 |
assertEquals(1L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
|
227 |
assertEquals(18L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
|
228 |
assertEquals(35L, a.get(0)); |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* AtomicLongArray updateAndGet updates with supplied function and |
|
233 |
* returns result. |
|
234 |
*/ |
|
235 |
public void testLongArrayUpdateAndGet() { |
|
236 |
AtomicLongArray a = new AtomicLongArray(1); |
|
237 |
a.set(0, 1); |
|
238 |
assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17)); |
|
239 |
assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17)); |
|
240 |
assertEquals(35L, a.get(0)); |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* AtomicLongArray getAndAccumulate returns previous value and updates |
|
245 |
* with supplied function. |
|
246 |
*/ |
|
247 |
public void testLongArrayGetAndAccumulate() { |
|
248 |
AtomicLongArray a = new AtomicLongArray(1); |
|
249 |
a.set(0, 1); |
|
250 |
assertEquals(1L, a.getAndAccumulate(0, 2L, Long::sum)); |
|
251 |
assertEquals(3L, a.getAndAccumulate(0, 3L, Long::sum)); |
|
252 |
assertEquals(6L, a.get(0)); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* AtomicLongArray accumulateAndGet updates with supplied function and |
|
257 |
* returns result. |
|
258 |
*/ |
|
259 |
public void testLongArrayAccumulateAndGet() { |
|
260 |
AtomicLongArray a = new AtomicLongArray(1); |
|
261 |
a.set(0, 1); |
|
262 |
assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum)); |
|
263 |
assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum)); |
|
264 |
assertEquals(10L, a.get(0)); |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* AtomicIntegerArray getAndUpdate returns previous value and updates |
|
269 |
* result of supplied function |
|
270 |
*/ |
|
271 |
public void testIntArrayGetAndUpdate() { |
|
272 |
AtomicIntegerArray a = new AtomicIntegerArray(1); |
|
273 |
a.set(0, 1); |
|
274 |
assertEquals(1, a.getAndUpdate(0, Atomic8Test::addInt17)); |
|
275 |
assertEquals(18, a.getAndUpdate(0, Atomic8Test::addInt17)); |
|
276 |
assertEquals(35, a.get(0)); |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* AtomicIntegerArray updateAndGet updates with supplied function and |
|
281 |
* returns result. |
|
282 |
*/ |
|
283 |
public void testIntArrayUpdateAndGet() { |
|
284 |
AtomicIntegerArray a = new AtomicIntegerArray(1); |
|
285 |
a.set(0, 1); |
|
286 |
assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17)); |
|
287 |
assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17)); |
|
288 |
assertEquals(35, a.get(0)); |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* AtomicIntegerArray getAndAccumulate returns previous value and updates |
|
293 |
* with supplied function. |
|
294 |
*/ |
|
295 |
public void testIntArrayGetAndAccumulate() { |
|
296 |
AtomicIntegerArray a = new AtomicIntegerArray(1); |
|
297 |
a.set(0, 1); |
|
298 |
assertEquals(1, a.getAndAccumulate(0, 2, Integer::sum)); |
|
299 |
assertEquals(3, a.getAndAccumulate(0, 3, Integer::sum)); |
|
300 |
assertEquals(6, a.get(0)); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* AtomicIntegerArray accumulateAndGet updates with supplied function and |
|
305 |
* returns result. |
|
306 |
*/ |
|
307 |
public void testIntArrayAccumulateAndGet() { |
|
308 |
AtomicIntegerArray a = new AtomicIntegerArray(1); |
|
309 |
a.set(0, 1); |
|
310 |
assertEquals(7, a.accumulateAndGet(0, 6, Integer::sum)); |
|
311 |
assertEquals(10, a.accumulateAndGet(0, 3, Integer::sum)); |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* AtomicReferenceArray getAndUpdate returns previous value and updates |
|
316 |
* result of supplied function |
|
317 |
*/ |
|
318 |
public void testReferenceArrayGetAndUpdate() { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
319 |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
35394 | 320 |
a.set(0, one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
321 |
assertEquals((Integer) 1, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
322 |
assertEquals((Integer) 18, a.getAndUpdate(0, Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
323 |
assertEquals((Integer) 35, a.get(0)); |
35394 | 324 |
} |
325 |
||
326 |
/** |
|
327 |
* AtomicReferenceArray updateAndGet updates with supplied function and |
|
328 |
* returns result. |
|
329 |
*/ |
|
330 |
public void testReferenceArrayUpdateAndGet() { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
331 |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
35394 | 332 |
a.set(0, one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
333 |
assertEquals((Integer) 18, a.updateAndGet(0, Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
334 |
assertEquals((Integer) 35, a.updateAndGet(0, Atomic8Test::addInteger17)); |
35394 | 335 |
} |
336 |
||
337 |
/** |
|
338 |
* AtomicReferenceArray getAndAccumulate returns previous value and updates |
|
339 |
* with supplied function. |
|
340 |
*/ |
|
341 |
public void testReferenceArrayGetAndAccumulate() { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
342 |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
35394 | 343 |
a.set(0, one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
344 |
assertEquals((Integer) 1, a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
345 |
assertEquals((Integer) 3, a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
346 |
assertEquals((Integer) 6, a.get(0)); |
35394 | 347 |
} |
348 |
||
349 |
/** |
|
350 |
* AtomicReferenceArray accumulateAndGet updates with supplied function and |
|
351 |
* returns result. |
|
352 |
*/ |
|
353 |
public void testReferenceArrayAccumulateAndGet() { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
354 |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<>(1); |
35394 | 355 |
a.set(0, one); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
356 |
assertEquals((Integer) 7, a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
357 |
assertEquals((Integer) 10, a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); |
35394 | 358 |
} |
359 |
||
360 |
/** |
|
361 |
* AtomicLongFieldUpdater getAndUpdate returns previous value and updates |
|
362 |
* result of supplied function |
|
363 |
*/ |
|
364 |
public void testLongFieldUpdaterGetAndUpdate() { |
|
365 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
|
366 |
a.set(this, 1); |
|
367 |
assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
|
368 |
assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
|
369 |
assertEquals(35L, a.get(this)); |
|
370 |
assertEquals(35L, aLongField); |
|
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* AtomicLongFieldUpdater updateAndGet updates with supplied function and |
|
375 |
* returns result. |
|
376 |
*/ |
|
377 |
public void testLongFieldUpdaterUpdateAndGet() { |
|
378 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
|
379 |
a.set(this, 1); |
|
380 |
assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17)); |
|
381 |
assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17)); |
|
382 |
assertEquals(35L, a.get(this)); |
|
383 |
assertEquals(35L, aLongField); |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* AtomicLongFieldUpdater getAndAccumulate returns previous value |
|
388 |
* and updates with supplied function. |
|
389 |
*/ |
|
390 |
public void testLongFieldUpdaterGetAndAccumulate() { |
|
391 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
|
392 |
a.set(this, 1); |
|
393 |
assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum)); |
|
394 |
assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum)); |
|
395 |
assertEquals(6L, a.get(this)); |
|
396 |
assertEquals(6L, aLongField); |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
* AtomicLongFieldUpdater accumulateAndGet updates with supplied |
|
401 |
* function and returns result. |
|
402 |
*/ |
|
403 |
public void testLongFieldUpdaterAccumulateAndGet() { |
|
404 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
|
405 |
a.set(this, 1); |
|
406 |
assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum)); |
|
407 |
assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum)); |
|
408 |
assertEquals(10L, a.get(this)); |
|
409 |
assertEquals(10L, aLongField); |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* AtomicIntegerFieldUpdater getAndUpdate returns previous value and updates |
|
414 |
* result of supplied function |
|
415 |
*/ |
|
416 |
public void testIntegerFieldUpdaterGetAndUpdate() { |
|
417 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
|
418 |
a.set(this, 1); |
|
419 |
assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17)); |
|
420 |
assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17)); |
|
421 |
assertEquals(35, a.get(this)); |
|
422 |
assertEquals(35, anIntField); |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* AtomicIntegerFieldUpdater updateAndGet updates with supplied function and |
|
427 |
* returns result. |
|
428 |
*/ |
|
429 |
public void testIntegerFieldUpdaterUpdateAndGet() { |
|
430 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
|
431 |
a.set(this, 1); |
|
432 |
assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17)); |
|
433 |
assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17)); |
|
434 |
assertEquals(35, a.get(this)); |
|
435 |
assertEquals(35, anIntField); |
|
436 |
} |
|
437 |
||
438 |
/** |
|
439 |
* AtomicIntegerFieldUpdater getAndAccumulate returns previous value |
|
440 |
* and updates with supplied function. |
|
441 |
*/ |
|
442 |
public void testIntegerFieldUpdaterGetAndAccumulate() { |
|
443 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
|
444 |
a.set(this, 1); |
|
445 |
assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum)); |
|
446 |
assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum)); |
|
447 |
assertEquals(6, a.get(this)); |
|
448 |
assertEquals(6, anIntField); |
|
449 |
} |
|
450 |
||
451 |
/** |
|
452 |
* AtomicIntegerFieldUpdater accumulateAndGet updates with supplied |
|
453 |
* function and returns result. |
|
454 |
*/ |
|
455 |
public void testIntegerFieldUpdaterAccumulateAndGet() { |
|
456 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
|
457 |
a.set(this, 1); |
|
458 |
assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum)); |
|
459 |
assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum)); |
|
460 |
assertEquals(10, a.get(this)); |
|
461 |
assertEquals(10, anIntField); |
|
462 |
} |
|
463 |
||
464 |
/** |
|
465 |
* AtomicReferenceFieldUpdater getAndUpdate returns previous value |
|
466 |
* and updates result of supplied function |
|
467 |
*/ |
|
468 |
public void testReferenceFieldUpdaterGetAndUpdate() { |
|
469 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
|
470 |
a.set(this, one); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
471 |
assertEquals((Integer) 1, a.getAndUpdate(this, Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
472 |
assertEquals((Integer) 18, a.getAndUpdate(this, Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
473 |
assertEquals((Integer) 35, a.get(this)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
474 |
assertEquals((Integer) 35, anIntegerField); |
35394 | 475 |
} |
476 |
||
477 |
/** |
|
478 |
* AtomicReferenceFieldUpdater updateAndGet updates with supplied |
|
479 |
* function and returns result. |
|
480 |
*/ |
|
481 |
public void testReferenceFieldUpdaterUpdateAndGet() { |
|
482 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
|
483 |
a.set(this, one); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
484 |
assertEquals((Integer) 18, a.updateAndGet(this, Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
485 |
assertEquals((Integer) 35, a.updateAndGet(this, Atomic8Test::addInteger17)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
486 |
assertEquals((Integer) 35, a.get(this)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
487 |
assertEquals((Integer) 35, anIntegerField); |
35394 | 488 |
} |
489 |
||
490 |
/** |
|
491 |
* AtomicReferenceFieldUpdater returns previous value and updates |
|
492 |
* with supplied function. |
|
493 |
*/ |
|
494 |
public void testReferenceFieldUpdaterGetAndAccumulate() { |
|
495 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
|
496 |
a.set(this, one); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
497 |
assertEquals((Integer) 1, a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
498 |
assertEquals((Integer) 3, a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
499 |
assertEquals((Integer) 6, a.get(this)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
500 |
assertEquals((Integer) 6, anIntegerField); |
35394 | 501 |
} |
502 |
||
503 |
/** |
|
504 |
* AtomicReferenceFieldUpdater accumulateAndGet updates with |
|
505 |
* supplied function and returns result. |
|
506 |
*/ |
|
507 |
public void testReferenceFieldUpdaterAccumulateAndGet() { |
|
508 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
|
509 |
a.set(this, one); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
510 |
assertEquals((Integer) 7, a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
511 |
assertEquals((Integer) 10, a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
512 |
assertEquals((Integer) 10, a.get(this)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
513 |
assertEquals((Integer) 10, anIntegerField); |
35394 | 514 |
} |
515 |
||
516 |
/** |
|
517 |
* All Atomic getAndUpdate methods throw NullPointerException on |
|
518 |
* null function argument |
|
519 |
*/ |
|
520 |
public void testGetAndUpdateNPE() { |
|
521 |
Runnable[] throwingActions = { |
|
522 |
() -> new AtomicLong().getAndUpdate(null), |
|
523 |
() -> new AtomicInteger().getAndUpdate(null), |
|
524 |
() -> new AtomicReference().getAndUpdate(null), |
|
525 |
() -> new AtomicLongArray(1).getAndUpdate(0, null), |
|
526 |
() -> new AtomicIntegerArray(1).getAndUpdate(0, null), |
|
527 |
() -> new AtomicReferenceArray(1).getAndUpdate(0, null), |
|
528 |
() -> aLongFieldUpdater().getAndUpdate(this, null), |
|
529 |
() -> anIntFieldUpdater().getAndUpdate(this, null), |
|
530 |
() -> anIntegerFieldUpdater().getAndUpdate(this, null), |
|
531 |
}; |
|
532 |
assertThrows(NullPointerException.class, throwingActions); |
|
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* All Atomic updateAndGet methods throw NullPointerException on null function argument |
|
537 |
*/ |
|
538 |
public void testUpdateAndGetNPE() { |
|
539 |
Runnable[] throwingActions = { |
|
540 |
() -> new AtomicLong().updateAndGet(null), |
|
541 |
() -> new AtomicInteger().updateAndGet(null), |
|
542 |
() -> new AtomicReference().updateAndGet(null), |
|
543 |
() -> new AtomicLongArray(1).updateAndGet(0, null), |
|
544 |
() -> new AtomicIntegerArray(1).updateAndGet(0, null), |
|
545 |
() -> new AtomicReferenceArray(1).updateAndGet(0, null), |
|
546 |
() -> aLongFieldUpdater().updateAndGet(this, null), |
|
547 |
() -> anIntFieldUpdater().updateAndGet(this, null), |
|
548 |
() -> anIntegerFieldUpdater().updateAndGet(this, null), |
|
549 |
}; |
|
550 |
assertThrows(NullPointerException.class, throwingActions); |
|
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* All Atomic getAndAccumulate methods throw NullPointerException |
|
555 |
* on null function argument |
|
556 |
*/ |
|
557 |
public void testGetAndAccumulateNPE() { |
|
558 |
Runnable[] throwingActions = { |
|
559 |
() -> new AtomicLong().getAndAccumulate(1L, null), |
|
560 |
() -> new AtomicInteger().getAndAccumulate(1, null), |
|
561 |
() -> new AtomicReference().getAndAccumulate(one, null), |
|
562 |
() -> new AtomicLongArray(1).getAndAccumulate(0, 1L, null), |
|
563 |
() -> new AtomicIntegerArray(1).getAndAccumulate(0, 1, null), |
|
564 |
() -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null), |
|
565 |
() -> aLongFieldUpdater().getAndAccumulate(this, 1L, null), |
|
566 |
() -> anIntFieldUpdater().getAndAccumulate(this, 1, null), |
|
567 |
() -> anIntegerFieldUpdater().getAndAccumulate(this, one, null), |
|
568 |
}; |
|
569 |
assertThrows(NullPointerException.class, throwingActions); |
|
570 |
} |
|
571 |
||
572 |
/** |
|
573 |
* All Atomic accumulateAndGet methods throw NullPointerException |
|
574 |
* on null function argument |
|
575 |
*/ |
|
576 |
public void testAccumulateAndGetNPE() { |
|
577 |
Runnable[] throwingActions = { |
|
578 |
() -> new AtomicLong().accumulateAndGet(1L, null), |
|
579 |
() -> new AtomicInteger().accumulateAndGet(1, null), |
|
580 |
() -> new AtomicReference().accumulateAndGet(one, null), |
|
581 |
() -> new AtomicLongArray(1).accumulateAndGet(0, 1L, null), |
|
582 |
() -> new AtomicIntegerArray(1).accumulateAndGet(0, 1, null), |
|
583 |
() -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null), |
|
584 |
() -> aLongFieldUpdater().accumulateAndGet(this, 1L, null), |
|
585 |
() -> anIntFieldUpdater().accumulateAndGet(this, 1, null), |
|
586 |
() -> anIntegerFieldUpdater().accumulateAndGet(this, one, null), |
|
587 |
}; |
|
588 |
assertThrows(NullPointerException.class, throwingActions); |
|
589 |
} |
|
590 |
||
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
591 |
/** |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
592 |
* Object arguments for parameters of type T that are not |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
593 |
* instances of the class passed to the newUpdater call will |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
594 |
* result in a ClassCastException being thrown. |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
595 |
*/ |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
596 |
public void testFieldUpdaters_ClassCastException() { |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
597 |
// Use raw types to allow passing wrong object type, provoking CCE |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
598 |
final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
599 |
final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
600 |
final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater(); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
601 |
final Object obj = new Object(); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
602 |
for (Object x : new Object[]{ new Object(), null }) { |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
603 |
Runnable[] throwingActions = { |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
604 |
() -> longUpdater.get(x), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
605 |
() -> intUpdater.get(x), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
606 |
() -> refUpdater.get(x), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
607 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
608 |
() -> longUpdater.set(x, 17L), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
609 |
() -> intUpdater.set(x, 17), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
610 |
() -> refUpdater.set(x, (Integer) 17), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
611 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
612 |
() -> longUpdater.addAndGet(x, 17L), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
613 |
() -> intUpdater.addAndGet(x, 17), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
614 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
615 |
() -> longUpdater.getAndUpdate(x, y -> y), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
616 |
() -> intUpdater.getAndUpdate(x, y -> y), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
617 |
() -> refUpdater.getAndUpdate(x, y -> y), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
618 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
619 |
() -> longUpdater.compareAndSet(x, 17L, 42L), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
620 |
() -> intUpdater.compareAndSet(x, 17, 42), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
621 |
() -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
622 |
}; |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
623 |
assertThrows(ClassCastException.class, throwingActions); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
624 |
} |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
625 |
} |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39725
diff
changeset
|
626 |
|
35394 | 627 |
} |