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