blob: 47289707d52e7775e828fab05c0842ee3bfe677e [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
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
9package jsr166;
10
Calin Juravle8f0d92b2013-08-01 17:26:00 +010011import java.util.concurrent.atomic.AtomicReference;
12
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010013import junit.framework.Test;
14import junit.framework.TestSuite;
15
Calin Juravle8f0d92b2013-08-01 17:26:00 +010016public class AtomicReferenceTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010017 // 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() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000024 // return new TestSuite(AtomicReferenceTest.class);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010025 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010026
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);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100106 do {} while (!ai.weakCompareAndSet(one, two));
107 do {} while (!ai.weakCompareAndSet(two, m4));
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100108 assertSame(m4, ai.get());
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100109 do {} while (!ai.weakCompareAndSet(m4, seven));
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100110 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}