54 |
|
/** |
55 |
|
* get and set for out of bound indices throw IndexOutOfBoundsException |
56 |
|
*/ |
57 |
+ |
@SuppressWarnings("deprecation") |
58 |
|
public void testIndexing() { |
59 |
|
AtomicLongArray aa = new AtomicLongArray(SIZE); |
60 |
|
for (int index : new int[] { -1, SIZE }) { |
86 |
|
aa.addAndGet(index, 1); |
87 |
|
shouldThrow(); |
88 |
|
} catch (IndexOutOfBoundsException success) {} |
88 |
– |
try { |
89 |
– |
aa.getPlain(index); |
90 |
– |
shouldThrow(); |
91 |
– |
} catch (IndexOutOfBoundsException success) {} |
92 |
– |
try { |
93 |
– |
aa.getOpaque(index); |
94 |
– |
shouldThrow(); |
95 |
– |
} catch (IndexOutOfBoundsException success) {} |
96 |
– |
try { |
97 |
– |
aa.getAcquire(index); |
98 |
– |
shouldThrow(); |
99 |
– |
} catch (IndexOutOfBoundsException success) {} |
100 |
– |
try { |
101 |
– |
aa.setPlain(index, 1); |
102 |
– |
shouldThrow(); |
103 |
– |
} catch (IndexOutOfBoundsException success) {} |
104 |
– |
try { |
105 |
– |
aa.setOpaque(index, 1); |
106 |
– |
shouldThrow(); |
107 |
– |
} catch (IndexOutOfBoundsException success) {} |
108 |
– |
try { |
109 |
– |
aa.setRelease(index, 1); |
110 |
– |
shouldThrow(); |
111 |
– |
} catch (IndexOutOfBoundsException success) {} |
112 |
– |
try { |
113 |
– |
aa.compareAndExchange(index, 1, 2); |
114 |
– |
shouldThrow(); |
115 |
– |
} catch (IndexOutOfBoundsException success) {} |
116 |
– |
try { |
117 |
– |
aa.compareAndExchangeAcquire(index, 1, 2); |
118 |
– |
shouldThrow(); |
119 |
– |
} catch (IndexOutOfBoundsException success) {} |
120 |
– |
try { |
121 |
– |
aa.compareAndExchangeRelease(index, 1, 2); |
122 |
– |
shouldThrow(); |
123 |
– |
} catch (IndexOutOfBoundsException success) {} |
124 |
– |
try { |
125 |
– |
aa.weakCompareAndSetVolatile(index, 1, 2); |
126 |
– |
shouldThrow(); |
127 |
– |
} catch (IndexOutOfBoundsException success) {} |
128 |
– |
try { |
129 |
– |
aa.weakCompareAndSetAcquire(index, 1, 2); |
130 |
– |
shouldThrow(); |
131 |
– |
} catch (IndexOutOfBoundsException success) {} |
132 |
– |
try { |
133 |
– |
aa.weakCompareAndSetRelease(index, 1, 2); |
134 |
– |
shouldThrow(); |
135 |
– |
} catch (IndexOutOfBoundsException success) {} |
89 |
|
} |
90 |
|
} |
91 |
|
|
160 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
161 |
|
* to expected |
162 |
|
*/ |
163 |
+ |
@SuppressWarnings("deprecation") |
164 |
|
public void testWeakCompareAndSet() { |
165 |
|
AtomicLongArray aa = new AtomicLongArray(SIZE); |
166 |
|
for (int i = 0; i < SIZE; i++) { |
277 |
|
|
278 |
|
class Counter extends CheckedRunnable { |
279 |
|
final AtomicLongArray aa; |
280 |
< |
volatile long counts; |
280 |
> |
int decs; |
281 |
|
Counter(AtomicLongArray a) { aa = a; } |
282 |
|
public void realRun() { |
283 |
|
for (;;) { |
288 |
|
if (v != 0) { |
289 |
|
done = false; |
290 |
|
if (aa.compareAndSet(i, v, v - 1)) |
291 |
< |
++counts; |
291 |
> |
decs++; |
292 |
|
} |
293 |
|
} |
294 |
|
if (done) |
308 |
|
aa.set(i, countdown); |
309 |
|
Counter c1 = new Counter(aa); |
310 |
|
Counter c2 = new Counter(aa); |
311 |
< |
Thread t1 = new Thread(c1); |
312 |
< |
Thread t2 = new Thread(c2); |
359 |
< |
t1.start(); |
360 |
< |
t2.start(); |
311 |
> |
Thread t1 = newStartedThread(c1); |
312 |
> |
Thread t2 = newStartedThread(c2); |
313 |
|
t1.join(); |
314 |
|
t2.join(); |
315 |
< |
assertEquals(c1.counts+c2.counts, SIZE * countdown); |
315 |
> |
assertEquals(c1.decs + c2.decs, SIZE * countdown); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
< |
* a deserialized serialized array holds same values |
319 |
> |
* a deserialized/reserialized array holds same values in same order |
320 |
|
*/ |
321 |
|
public void testSerialization() throws Exception { |
322 |
|
AtomicLongArray x = new AtomicLongArray(SIZE); |
339 |
|
assertEquals(Arrays.toString(a), aa.toString()); |
340 |
|
} |
341 |
|
|
390 |
– |
// jdk9 |
391 |
– |
|
392 |
– |
/** |
393 |
– |
* getPlain returns the last value set |
394 |
– |
*/ |
395 |
– |
public void testGetPlainSet() { |
396 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
397 |
– |
for (int i = 0; i < SIZE; i++) { |
398 |
– |
aa.set(i, 1); |
399 |
– |
assertEquals(1, aa.getPlain(i)); |
400 |
– |
aa.set(i, 2); |
401 |
– |
assertEquals(2, aa.getPlain(i)); |
402 |
– |
aa.set(i, -3); |
403 |
– |
assertEquals(-3, aa.getPlain(i)); |
404 |
– |
} |
405 |
– |
} |
406 |
– |
|
407 |
– |
/** |
408 |
– |
* getOpaque returns the last value set |
409 |
– |
*/ |
410 |
– |
public void testGetOpaqueSet() { |
411 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
412 |
– |
for (int i = 0; i < SIZE; i++) { |
413 |
– |
aa.set(i, 1); |
414 |
– |
assertEquals(1, aa.getOpaque(i)); |
415 |
– |
aa.set(i, 2); |
416 |
– |
assertEquals(2, aa.getOpaque(i)); |
417 |
– |
aa.set(i, -3); |
418 |
– |
assertEquals(-3, aa.getOpaque(i)); |
419 |
– |
} |
420 |
– |
} |
421 |
– |
|
422 |
– |
/** |
423 |
– |
* getAcquire returns the last value set |
424 |
– |
*/ |
425 |
– |
public void testGetAcquireSet() { |
426 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
427 |
– |
for (int i = 0; i < SIZE; i++) { |
428 |
– |
aa.set(i, 1); |
429 |
– |
assertEquals(1, aa.getAcquire(i)); |
430 |
– |
aa.set(i, 2); |
431 |
– |
assertEquals(2, aa.getAcquire(i)); |
432 |
– |
aa.set(i, -3); |
433 |
– |
assertEquals(-3, aa.getAcquire(i)); |
434 |
– |
} |
435 |
– |
} |
436 |
– |
|
437 |
– |
/** |
438 |
– |
* get returns the last value setPlain |
439 |
– |
*/ |
440 |
– |
public void testGetSetPlain() { |
441 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
442 |
– |
for (int i = 0; i < SIZE; i++) { |
443 |
– |
aa.setPlain(i, 1); |
444 |
– |
assertEquals(1, aa.get(i)); |
445 |
– |
aa.setPlain(i, 2); |
446 |
– |
assertEquals(2, aa.get(i)); |
447 |
– |
aa.setPlain(i, -3); |
448 |
– |
assertEquals(-3, aa.get(i)); |
449 |
– |
} |
450 |
– |
} |
451 |
– |
|
452 |
– |
/** |
453 |
– |
* get returns the last value setOpaque |
454 |
– |
*/ |
455 |
– |
public void testGetSetOpaque() { |
456 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
457 |
– |
for (int i = 0; i < SIZE; i++) { |
458 |
– |
aa.setOpaque(i, 1); |
459 |
– |
assertEquals(1, aa.get(i)); |
460 |
– |
aa.setOpaque(i, 2); |
461 |
– |
assertEquals(2, aa.get(i)); |
462 |
– |
aa.setOpaque(i, -3); |
463 |
– |
assertEquals(-3, aa.get(i)); |
464 |
– |
} |
465 |
– |
} |
466 |
– |
|
467 |
– |
/** |
468 |
– |
* get returns the last value setRelease |
469 |
– |
*/ |
470 |
– |
public void testGetSetRelease() { |
471 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
472 |
– |
for (int i = 0; i < SIZE; i++) { |
473 |
– |
aa.setRelease(i, 1); |
474 |
– |
assertEquals(1, aa.get(i)); |
475 |
– |
aa.setRelease(i, 2); |
476 |
– |
assertEquals(2, aa.get(i)); |
477 |
– |
aa.setRelease(i, -3); |
478 |
– |
assertEquals(-3, aa.get(i)); |
479 |
– |
} |
480 |
– |
} |
481 |
– |
|
482 |
– |
/** |
483 |
– |
* compareAndExchange succeeds in changing value if equal to |
484 |
– |
* expected else fails |
485 |
– |
*/ |
486 |
– |
public void testCompareAndExchange() { |
487 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
488 |
– |
for (int i = 0; i < SIZE; i++) { |
489 |
– |
aa.set(i, 1); |
490 |
– |
assertEquals(1, aa.compareAndExchange(i, 1, 2)); |
491 |
– |
assertEquals(2, aa.compareAndExchange(i, 2, -4)); |
492 |
– |
assertEquals(-4, aa.get(i)); |
493 |
– |
assertEquals(-4, aa.compareAndExchange(i,-5, 7)); |
494 |
– |
assertEquals(-4, aa.get(i)); |
495 |
– |
assertEquals(-4, aa.compareAndExchange(i, -4, 7)); |
496 |
– |
assertEquals(7, aa.get(i)); |
497 |
– |
} |
498 |
– |
} |
499 |
– |
|
500 |
– |
/** |
501 |
– |
* compareAndExchangeAcquire succeeds in changing value if equal to |
502 |
– |
* expected else fails |
503 |
– |
*/ |
504 |
– |
public void testCompareAndExchangeAcquire() { |
505 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
506 |
– |
for (int i = 0; i < SIZE; i++) { |
507 |
– |
aa.set(i, 1); |
508 |
– |
assertEquals(1, aa.compareAndExchangeAcquire(i, 1, 2)); |
509 |
– |
assertEquals(2, aa.compareAndExchangeAcquire(i, 2, -4)); |
510 |
– |
assertEquals(-4, aa.get(i)); |
511 |
– |
assertEquals(-4, aa.compareAndExchangeAcquire(i,-5, 7)); |
512 |
– |
assertEquals(-4, aa.get(i)); |
513 |
– |
assertEquals(-4, aa.compareAndExchangeAcquire(i, -4, 7)); |
514 |
– |
assertEquals(7, aa.get(i)); |
515 |
– |
} |
516 |
– |
} |
517 |
– |
|
518 |
– |
/** |
519 |
– |
* compareAndExchangeRelease succeeds in changing value if equal to |
520 |
– |
* expected else fails |
521 |
– |
*/ |
522 |
– |
public void testCompareAndExchangeRelease() { |
523 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
524 |
– |
for (int i = 0; i < SIZE; i++) { |
525 |
– |
aa.set(i, 1); |
526 |
– |
assertEquals(1, aa.compareAndExchangeRelease(i, 1, 2)); |
527 |
– |
assertEquals(2, aa.compareAndExchangeRelease(i, 2, -4)); |
528 |
– |
assertEquals(-4, aa.get(i)); |
529 |
– |
assertEquals(-4, aa.compareAndExchangeRelease(i,-5, 7)); |
530 |
– |
assertEquals(-4, aa.get(i)); |
531 |
– |
assertEquals(-4, aa.compareAndExchangeRelease(i, -4, 7)); |
532 |
– |
assertEquals(7, aa.get(i)); |
533 |
– |
} |
534 |
– |
} |
535 |
– |
|
536 |
– |
/** |
537 |
– |
* repeated weakCompareAndSetVolatile succeeds in changing value when equal |
538 |
– |
* to expected |
539 |
– |
*/ |
540 |
– |
public void testWeakCompareAndSetVolatile() { |
541 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
542 |
– |
for (int i = 0; i < SIZE; i++) { |
543 |
– |
aa.set(i, 1); |
544 |
– |
do {} while (!aa.weakCompareAndSetVolatile(i, 1, 2)); |
545 |
– |
do {} while (!aa.weakCompareAndSetVolatile(i, 2, -4)); |
546 |
– |
assertEquals(-4, aa.get(i)); |
547 |
– |
do {} while (!aa.weakCompareAndSetVolatile(i, -4, 7)); |
548 |
– |
assertEquals(7, aa.get(i)); |
549 |
– |
} |
550 |
– |
} |
551 |
– |
|
552 |
– |
/** |
553 |
– |
* repeated weakCompareAndSetAcquire succeeds in changing value when equal |
554 |
– |
* to expected |
555 |
– |
*/ |
556 |
– |
public void testWeakCompareAndSetAcquire() { |
557 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
558 |
– |
for (int i = 0; i < SIZE; i++) { |
559 |
– |
aa.set(i, 1); |
560 |
– |
do {} while (!aa.weakCompareAndSetAcquire(i, 1, 2)); |
561 |
– |
do {} while (!aa.weakCompareAndSetAcquire(i, 2, -4)); |
562 |
– |
assertEquals(-4, aa.get(i)); |
563 |
– |
do {} while (!aa.weakCompareAndSetAcquire(i, -4, 7)); |
564 |
– |
assertEquals(7, aa.get(i)); |
565 |
– |
} |
566 |
– |
} |
567 |
– |
|
568 |
– |
/** |
569 |
– |
* repeated weakCompareAndSetRelease succeeds in changing value when equal |
570 |
– |
* to expected |
571 |
– |
*/ |
572 |
– |
public void testWeakCompareAndSetRelease() { |
573 |
– |
AtomicLongArray aa = new AtomicLongArray(SIZE); |
574 |
– |
for (int i = 0; i < SIZE; i++) { |
575 |
– |
aa.set(i, 1); |
576 |
– |
do {} while (!aa.weakCompareAndSetRelease(i, 1, 2)); |
577 |
– |
do {} while (!aa.weakCompareAndSetRelease(i, 2, -4)); |
578 |
– |
assertEquals(-4, aa.get(i)); |
579 |
– |
do {} while (!aa.weakCompareAndSetRelease(i, -4, 7)); |
580 |
– |
assertEquals(7, aa.get(i)); |
581 |
– |
} |
582 |
– |
} |
583 |
– |
|
342 |
|
} |