Justin Klaassen | 10d07c8 | 2017-09-15 17:58:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Written by Doug Lea with assistance from members of JCP JSR-166 |
| 3 | * Expert Group and released to the public domain, as explained at |
| 4 | * http://creativecommons.org/publicdomain/zero/1.0/ |
| 5 | * Other contributors include Andrew Wright, Jeffrey Hayes, |
| 6 | * Pat Fisher, Mike Judd. |
| 7 | */ |
| 8 | |
| 9 | package jsr166; |
| 10 | |
| 11 | import java.util.concurrent.atomic.AtomicReference; |
| 12 | |
| 13 | import junit.framework.Test; |
| 14 | import junit.framework.TestSuite; |
| 15 | |
| 16 | public class AtomicReferenceTest extends JSR166TestCase { |
| 17 | // android-note: Removed because the CTS runner does a bad job of |
| 18 | // retrying tests that have suite() declarations. |
| 19 | // |
| 20 | // public static void main(String[] args) { |
| 21 | // main(suite(), args); |
| 22 | // } |
| 23 | // public static Test suite() { |
| 24 | // return new TestSuite(AtomicReferenceTest.class); |
| 25 | // } |
| 26 | |
| 27 | /** |
| 28 | * constructor initializes to given value |
| 29 | */ |
| 30 | public void testConstructor() { |
| 31 | AtomicReference ai = new AtomicReference(one); |
| 32 | assertSame(one, ai.get()); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * default constructed initializes to null |
| 37 | */ |
| 38 | public void testConstructor2() { |
| 39 | AtomicReference ai = new AtomicReference(); |
| 40 | assertNull(ai.get()); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * get returns the last value set |
| 45 | */ |
| 46 | public void testGetSet() { |
| 47 | AtomicReference ai = new AtomicReference(one); |
| 48 | assertSame(one, ai.get()); |
| 49 | ai.set(two); |
| 50 | assertSame(two, ai.get()); |
| 51 | ai.set(m3); |
| 52 | assertSame(m3, ai.get()); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * get returns the last value lazySet in same thread |
| 57 | */ |
| 58 | public void testGetLazySet() { |
| 59 | AtomicReference ai = new AtomicReference(one); |
| 60 | assertSame(one, ai.get()); |
| 61 | ai.lazySet(two); |
| 62 | assertSame(two, ai.get()); |
| 63 | ai.lazySet(m3); |
| 64 | assertSame(m3, ai.get()); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * compareAndSet succeeds in changing value if equal to expected else fails |
| 69 | */ |
| 70 | public void testCompareAndSet() { |
| 71 | AtomicReference ai = new AtomicReference(one); |
| 72 | assertTrue(ai.compareAndSet(one, two)); |
| 73 | assertTrue(ai.compareAndSet(two, m4)); |
| 74 | assertSame(m4, ai.get()); |
| 75 | assertFalse(ai.compareAndSet(m5, seven)); |
| 76 | assertSame(m4, ai.get()); |
| 77 | assertTrue(ai.compareAndSet(m4, seven)); |
| 78 | assertSame(seven, ai.get()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * compareAndSet in one thread enables another waiting for value |
| 83 | * to succeed |
| 84 | */ |
| 85 | public void testCompareAndSetInMultipleThreads() throws Exception { |
| 86 | final AtomicReference ai = new AtomicReference(one); |
| 87 | Thread t = new Thread(new CheckedRunnable() { |
| 88 | public void realRun() { |
| 89 | while (!ai.compareAndSet(two, three)) |
| 90 | Thread.yield(); |
| 91 | }}); |
| 92 | |
| 93 | t.start(); |
| 94 | assertTrue(ai.compareAndSet(one, two)); |
| 95 | t.join(LONG_DELAY_MS); |
| 96 | assertFalse(t.isAlive()); |
| 97 | assertSame(three, ai.get()); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * repeated weakCompareAndSet succeeds in changing value when equal |
| 102 | * to expected |
| 103 | */ |
| 104 | public void testWeakCompareAndSet() { |
| 105 | AtomicReference ai = new AtomicReference(one); |
| 106 | do {} while (!ai.weakCompareAndSet(one, two)); |
| 107 | do {} while (!ai.weakCompareAndSet(two, m4)); |
| 108 | assertSame(m4, ai.get()); |
| 109 | do {} while (!ai.weakCompareAndSet(m4, seven)); |
| 110 | assertSame(seven, ai.get()); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * getAndSet returns previous value and sets to given value |
| 115 | */ |
| 116 | public void testGetAndSet() { |
| 117 | AtomicReference ai = new AtomicReference(one); |
| 118 | assertSame(one, ai.getAndSet(zero)); |
| 119 | assertSame(zero, ai.getAndSet(m10)); |
| 120 | assertSame(m10, ai.getAndSet(one)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * a deserialized serialized atomic holds same value |
| 125 | */ |
| 126 | public void testSerialization() throws Exception { |
| 127 | AtomicReference x = new AtomicReference(); |
| 128 | AtomicReference y = serialClone(x); |
| 129 | assertNotSame(x, y); |
| 130 | x.set(one); |
| 131 | AtomicReference z = serialClone(x); |
| 132 | assertNotSame(y, z); |
| 133 | assertEquals(one, x.get()); |
| 134 | assertEquals(null, y.get()); |
| 135 | assertEquals(one, z.get()); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * toString returns current value. |
| 140 | */ |
| 141 | public void testToString() { |
| 142 | AtomicReference<Integer> ai = new AtomicReference<Integer>(one); |
| 143 | assertEquals(one.toString(), ai.toString()); |
| 144 | ai.set(two); |
| 145 | assertEquals(two.toString(), ai.toString()); |
| 146 | } |
| 147 | |
| 148 | } |