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