6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
9 |
|
import java.util.Arrays; |
10 |
|
import java.util.concurrent.atomic.AtomicLongArray; |
11 |
|
|
12 |
+ |
import junit.framework.Test; |
13 |
+ |
import junit.framework.TestSuite; |
14 |
+ |
|
15 |
|
public class AtomicLongArrayTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run(suite()); |
17 |
> |
main(suite(), args); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(AtomicLongArrayTest.class); |
35 |
|
public void testConstructor2NPE() { |
36 |
|
try { |
37 |
|
long[] a = null; |
38 |
< |
AtomicLongArray aa = new AtomicLongArray(a); |
38 |
> |
new AtomicLongArray(a); |
39 |
|
shouldThrow(); |
40 |
|
} catch (NullPointerException success) {} |
41 |
|
} |
85 |
|
aa.addAndGet(index, 1); |
86 |
|
shouldThrow(); |
87 |
|
} 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) {} |
136 |
|
} |
137 |
|
} |
138 |
|
|
211 |
|
AtomicLongArray aa = new AtomicLongArray(SIZE); |
212 |
|
for (int i = 0; i < SIZE; i++) { |
213 |
|
aa.set(i, 1); |
214 |
< |
while (!aa.weakCompareAndSet(i, 1, 2)); |
215 |
< |
while (!aa.weakCompareAndSet(i, 2, -4)); |
214 |
> |
do {} while (!aa.weakCompareAndSet(i, 1, 2)); |
215 |
> |
do {} while (!aa.weakCompareAndSet(i, 2, -4)); |
216 |
|
assertEquals(-4, aa.get(i)); |
217 |
< |
while (!aa.weakCompareAndSet(i, -4, 7)); |
217 |
> |
do {} while (!aa.weakCompareAndSet(i, -4, 7)); |
218 |
|
assertEquals(7, aa.get(i)); |
219 |
|
} |
220 |
|
} |
333 |
|
assertTrue(v >= 0); |
334 |
|
if (v != 0) { |
335 |
|
done = false; |
336 |
< |
if (aa.compareAndSet(i, v, v-1)) |
336 |
> |
if (aa.compareAndSet(i, v, v - 1)) |
337 |
|
++counts; |
338 |
|
} |
339 |
|
} |
387 |
|
assertEquals(Arrays.toString(a), aa.toString()); |
388 |
|
} |
389 |
|
|
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 |
+ |
|
584 |
|
} |