ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceTest.java
Revision: 1.26
Committed: Fri Jun 17 01:38:28 2016 UTC (7 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +4 -6 lines
Log Message:
whitespace

File Contents

# Content
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 import java.util.concurrent.atomic.AtomicReference;
10
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13
14 public class AtomicReferenceTest extends JSR166TestCase {
15 public static void main(String[] args) {
16 main(suite(), args);
17 }
18 public static Test suite() {
19 return new TestSuite(AtomicReferenceTest.class);
20 }
21
22 /**
23 * constructor initializes to given value
24 */
25 public void testConstructor() {
26 AtomicReference ai = new AtomicReference(one);
27 assertSame(one, ai.get());
28 }
29
30 /**
31 * default constructed initializes to null
32 */
33 public void testConstructor2() {
34 AtomicReference ai = new AtomicReference();
35 assertNull(ai.get());
36 }
37
38 /**
39 * get returns the last value set
40 */
41 public void testGetSet() {
42 AtomicReference ai = new AtomicReference(one);
43 assertSame(one, ai.get());
44 ai.set(two);
45 assertSame(two, ai.get());
46 ai.set(m3);
47 assertSame(m3, ai.get());
48 }
49
50 /**
51 * get returns the last value lazySet in same thread
52 */
53 public void testGetLazySet() {
54 AtomicReference ai = new AtomicReference(one);
55 assertSame(one, ai.get());
56 ai.lazySet(two);
57 assertSame(two, ai.get());
58 ai.lazySet(m3);
59 assertSame(m3, ai.get());
60 }
61
62 /**
63 * compareAndSet succeeds in changing value if equal to expected else fails
64 */
65 public void testCompareAndSet() {
66 AtomicReference ai = new AtomicReference(one);
67 assertTrue(ai.compareAndSet(one, two));
68 assertTrue(ai.compareAndSet(two, m4));
69 assertSame(m4, ai.get());
70 assertFalse(ai.compareAndSet(m5, seven));
71 assertSame(m4, ai.get());
72 assertTrue(ai.compareAndSet(m4, seven));
73 assertSame(seven, ai.get());
74 }
75
76 /**
77 * compareAndSet in one thread enables another waiting for value
78 * to succeed
79 */
80 public void testCompareAndSetInMultipleThreads() throws Exception {
81 final AtomicReference ai = new AtomicReference(one);
82 Thread t = new Thread(new CheckedRunnable() {
83 public void realRun() {
84 while (!ai.compareAndSet(two, three))
85 Thread.yield();
86 }});
87
88 t.start();
89 assertTrue(ai.compareAndSet(one, two));
90 t.join(LONG_DELAY_MS);
91 assertFalse(t.isAlive());
92 assertSame(three, ai.get());
93 }
94
95 /**
96 * repeated weakCompareAndSet succeeds in changing value when equal
97 * to expected
98 */
99 public void testWeakCompareAndSet() {
100 AtomicReference ai = new AtomicReference(one);
101 do {} while (!ai.weakCompareAndSet(one, two));
102 do {} while (!ai.weakCompareAndSet(two, m4));
103 assertSame(m4, ai.get());
104 do {} while (!ai.weakCompareAndSet(m4, seven));
105 assertSame(seven, ai.get());
106 }
107
108 /**
109 * getAndSet returns previous value and sets to given value
110 */
111 public void testGetAndSet() {
112 AtomicReference ai = new AtomicReference(one);
113 assertSame(one, ai.getAndSet(zero));
114 assertSame(zero, ai.getAndSet(m10));
115 assertSame(m10, ai.getAndSet(one));
116 }
117
118 /**
119 * a deserialized serialized atomic holds same value
120 */
121 public void testSerialization() throws Exception {
122 AtomicReference x = new AtomicReference();
123 AtomicReference y = serialClone(x);
124 assertNotSame(x, y);
125 x.set(one);
126 AtomicReference z = serialClone(x);
127 assertNotSame(y, z);
128 assertEquals(one, x.get());
129 assertEquals(null, y.get());
130 assertEquals(one, z.get());
131 }
132
133 /**
134 * toString returns current value.
135 */
136 public void testToString() {
137 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
138 assertEquals(one.toString(), ai.toString());
139 ai.set(two);
140 assertEquals(two.toString(), ai.toString());
141 }
142
143 // jdk9
144
145 /**
146 * getPlain returns the last value set
147 */
148 public void testGetPlainSet() {
149 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
150 assertEquals(one, ai.getPlain());
151 ai.set(two);
152 assertEquals(two, ai.getPlain());
153 ai.set(m3);
154 assertEquals(m3, ai.getPlain());
155 }
156
157 /**
158 * getOpaque returns the last value set
159 */
160 public void testGetOpaqueSet() {
161 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
162 assertEquals(one, ai.getOpaque());
163 ai.set(two);
164 assertEquals(two, ai.getOpaque());
165 ai.set(m3);
166 assertEquals(m3, ai.getOpaque());
167 }
168
169 /**
170 * getAcquire returns the last value set
171 */
172 public void testGetAcquireSet() {
173 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
174 assertEquals(one, ai.getAcquire());
175 ai.set(two);
176 assertEquals(two, ai.getAcquire());
177 ai.set(m3);
178 assertEquals(m3, ai.getAcquire());
179 }
180
181 /**
182 * get returns the last value setPlain
183 */
184 public void testGetSetPlain() {
185 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
186 assertEquals(one, ai.get());
187 ai.setPlain(two);
188 assertEquals(two, ai.get());
189 ai.setPlain(m3);
190 assertEquals(m3, ai.get());
191 }
192
193 /**
194 * get returns the last value setOpaque
195 */
196 public void testGetSetOpaque() {
197 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
198 assertEquals(one, ai.get());
199 ai.setOpaque(two);
200 assertEquals(two, ai.get());
201 ai.setOpaque(m3);
202 assertEquals(m3, ai.get());
203 }
204
205 /**
206 * get returns the last value setRelease
207 */
208 public void testGetSetRelease() {
209 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
210 assertEquals(one, ai.get());
211 ai.setRelease(two);
212 assertEquals(two, ai.get());
213 ai.setRelease(m3);
214 assertEquals(m3, ai.get());
215 }
216
217 /**
218 * compareAndExchange succeeds in changing value if equal to
219 * expected else fails
220 */
221 public void testCompareAndExchange() {
222 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
223 assertEquals(one, ai.compareAndExchange(one, two));
224 assertEquals(two, ai.compareAndExchange(two, m4));
225 assertEquals(m4, ai.get());
226 assertEquals(m4, ai.compareAndExchange(m5, seven));
227 assertEquals(m4, ai.get());
228 assertEquals(m4, ai.compareAndExchange(m4, seven));
229 assertEquals(seven, ai.get());
230 }
231
232 /**
233 * compareAndExchangeAcquire succeeds in changing value if equal to
234 * expected else fails
235 */
236 public void testCompareAndExchangeAcquire() {
237 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
238 assertEquals(one, ai.compareAndExchangeAcquire(one, two));
239 assertEquals(two, ai.compareAndExchangeAcquire(two, m4));
240 assertEquals(m4, ai.get());
241 assertEquals(m4, ai.compareAndExchangeAcquire(m5, seven));
242 assertEquals(m4, ai.get());
243 assertEquals(m4, ai.compareAndExchangeAcquire(m4, seven));
244 assertEquals(seven, ai.get());
245 }
246
247 /**
248 * compareAndExchangeRelease succeeds in changing value if equal to
249 * expected else fails
250 */
251 public void testCompareAndExchangeRelease() {
252 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
253 assertEquals(one, ai.compareAndExchangeRelease(one, two));
254 assertEquals(two, ai.compareAndExchangeRelease(two, m4));
255 assertEquals(m4, ai.get());
256 assertEquals(m4, ai.compareAndExchangeRelease(m5, seven));
257 assertEquals(m4, ai.get());
258 assertEquals(m4, ai.compareAndExchangeRelease(m4, seven));
259 assertEquals(seven, ai.get());
260 }
261
262 /**
263 * repeated weakCompareAndSetVolatile succeeds in changing value when equal
264 * to expected
265 */
266 public void testWeakCompareAndSetVolatile() {
267 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
268 do {} while (!ai.weakCompareAndSetVolatile(one, two));
269 do {} while (!ai.weakCompareAndSetVolatile(two, m4));
270 assertEquals(m4, ai.get());
271 do {} while (!ai.weakCompareAndSetVolatile(m4, seven));
272 assertEquals(seven, ai.get());
273 }
274
275 /**
276 * repeated weakCompareAndSetAcquire succeeds in changing value when equal
277 * to expected
278 */
279 public void testWeakCompareAndSetAcquire() {
280 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
281 do {} while (!ai.weakCompareAndSetAcquire(one, two));
282 do {} while (!ai.weakCompareAndSetAcquire(two, m4));
283 assertEquals(m4, ai.get());
284 do {} while (!ai.weakCompareAndSetAcquire(m4, seven));
285 assertEquals(seven, ai.get());
286 }
287
288 /**
289 * repeated weakCompareAndSetRelease succeeds in changing value when equal
290 * to expected
291 */
292 public void testWeakCompareAndSetRelease() {
293 AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
294 do {} while (!ai.weakCompareAndSetRelease(one, two));
295 do {} while (!ai.weakCompareAndSetRelease(two, m4));
296 assertEquals(m4, ai.get());
297 do {} while (!ai.weakCompareAndSetRelease(m4, seven));
298 assertEquals(seven, ai.get());
299 }
300
301 }