ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java
Revision: 1.30
Committed: Sun Nov 8 15:34:01 2015 UTC (8 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.29: +88 -2 lines
Log Message:
Add tests checking the forms of thrown exceptions

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.AtomicLongFieldUpdater;
10
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13
14 public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
15 volatile long x = 0;
16 protected volatile long protectedField;
17 private volatile long privateField;
18 long w;
19 float z;
20 public static void main(String[] args) {
21 main(suite(), args);
22 }
23 public static Test suite() {
24 return new TestSuite(AtomicLongFieldUpdaterTest.class);
25 }
26
27 // for testing subclass access
28 static class AtomicLongFieldUpdaterTestSubclass extends AtomicLongFieldUpdaterTest {
29 public void checkPrivateAccess() {
30 try {
31 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
32 AtomicLongFieldUpdater.newUpdater
33 (AtomicLongFieldUpdaterTest.class, "privateField");
34 shouldThrow();
35 } catch (RuntimeException success) {
36 assertNotNull(success.getCause());
37 }
38 }
39
40 public void checkCompareAndSetProtectedSub() {
41 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
42 a = updaterFor("protectedField");
43 protectedField = 1;
44 assertTrue(a.compareAndSet(this, 1, 2));
45 assertTrue(a.compareAndSet(this, 2, -4));
46 assertEquals(-4, a.get(this));
47 assertFalse(a.compareAndSet(this, -5, 7));
48 assertEquals(-4, a.get(this));
49 assertTrue(a.compareAndSet(this, -4, 7));
50 assertEquals(7, a.get(this));
51 }
52 }
53
54 static class UnrelatedClass {
55 public void checkPrivateAccess() {
56 Exception ex = null;
57 try {
58 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
59 AtomicLongFieldUpdater.newUpdater
60 (AtomicLongFieldUpdaterTest.class, "x");
61 } catch (RuntimeException rex) {
62 ex = rex;
63 }
64 if (ex != null) throw new Error();
65 }
66 }
67
68 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
69 return AtomicLongFieldUpdater.newUpdater
70 (AtomicLongFieldUpdaterTest.class, fieldName);
71 }
72
73 /**
74 * Construction with non-existent field throws RuntimeException
75 */
76 public void testConstructor() {
77 try {
78 updaterFor("y");
79 shouldThrow();
80 } catch (RuntimeException success) {
81 assertNotNull(success.getCause());
82 }
83 }
84
85 /**
86 * construction with field not of given type throws IllegalArgumentException
87 */
88 public void testConstructor2() {
89 try {
90 updaterFor("z");
91 shouldThrow();
92 } catch (IllegalArgumentException success) {}
93 }
94
95 /**
96 * construction with non-volatile field throws IllegalArgumentException
97 */
98 public void testConstructor3() {
99 try {
100 updaterFor("w");
101 shouldThrow();
102 } catch (IllegalArgumentException success) {}
103 }
104
105 /**
106 * construction using private field from subclass throws RuntimeException
107 */
108 public void testPrivateFieldInSubclass() {
109 AtomicLongFieldUpdaterTestSubclass s =
110 new AtomicLongFieldUpdaterTestSubclass();
111 s.checkPrivateAccess();
112 }
113
114 /**
115 * construction from unrelated class throws RuntimeException
116 */
117 public void testUnrelatedClassAccess() {
118 UnrelatedClass s = new UnrelatedClass();
119 s.checkPrivateAccess();
120 }
121
122 /**
123 * get returns the last value set or assigned
124 */
125 public void testGetSet() {
126 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
127 a = updaterFor("x");
128 x = 1;
129 assertEquals(1, a.get(this));
130 a.set(this, 2);
131 assertEquals(2, a.get(this));
132 a.set(this, -3);
133 assertEquals(-3, a.get(this));
134 }
135
136 /**
137 * get returns the last value lazySet by same thread
138 */
139 public void testGetLazySet() {
140 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
141 a = updaterFor("x");
142 x = 1;
143 assertEquals(1, a.get(this));
144 a.lazySet(this, 2);
145 assertEquals(2, a.get(this));
146 a.lazySet(this, -3);
147 assertEquals(-3, a.get(this));
148 }
149
150 /**
151 * compareAndSet succeeds in changing value if equal to expected else fails
152 */
153 public void testCompareAndSet() {
154 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
155 a = updaterFor("x");
156 x = 1;
157 assertTrue(a.compareAndSet(this, 1, 2));
158 assertTrue(a.compareAndSet(this, 2, -4));
159 assertEquals(-4, a.get(this));
160 assertFalse(a.compareAndSet(this, -5, 7));
161 assertEquals(-4, a.get(this));
162 assertTrue(a.compareAndSet(this, -4, 7));
163 assertEquals(7, a.get(this));
164 }
165
166 /**
167 * compareAndSet succeeds in changing protected field value if
168 * equal to expected else fails
169 */
170 public void testCompareAndSetProtected() {
171 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
172 a = updaterFor("protectedField");
173 protectedField = 1;
174 assertTrue(a.compareAndSet(this, 1, 2));
175 assertTrue(a.compareAndSet(this, 2, -4));
176 assertEquals(-4, a.get(this));
177 assertFalse(a.compareAndSet(this, -5, 7));
178 assertEquals(-4, a.get(this));
179 assertTrue(a.compareAndSet(this, -4, 7));
180 assertEquals(7, a.get(this));
181 }
182
183 /**
184 * compareAndSet succeeds in changing protected field value if
185 * equal to expected else fails
186 */
187 public void testCompareAndSetProtectedInSubclass() {
188 AtomicLongFieldUpdaterTestSubclass s =
189 new AtomicLongFieldUpdaterTestSubclass();
190 s.checkCompareAndSetProtectedSub();
191 }
192
193 /**
194 * compareAndSet in one thread enables another waiting for value
195 * to succeed
196 */
197 public void testCompareAndSetInMultipleThreads() throws Exception {
198 x = 1;
199 final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
200 a = updaterFor("x");
201
202 Thread t = new Thread(new CheckedRunnable() {
203 public void realRun() {
204 while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
205 Thread.yield();
206 }});
207
208 t.start();
209 assertTrue(a.compareAndSet(this, 1, 2));
210 t.join(LONG_DELAY_MS);
211 assertFalse(t.isAlive());
212 assertEquals(3, a.get(this));
213 }
214
215 /**
216 * repeated weakCompareAndSet succeeds in changing value when equal
217 * to expected
218 */
219 public void testWeakCompareAndSet() {
220 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
221 a = updaterFor("x");
222 x = 1;
223 do {} while (!a.weakCompareAndSet(this, 1, 2));
224 do {} while (!a.weakCompareAndSet(this, 2, -4));
225 assertEquals(-4, a.get(this));
226 do {} while (!a.weakCompareAndSet(this, -4, 7));
227 assertEquals(7, a.get(this));
228 }
229
230 /**
231 * getAndSet returns previous value and sets to given value
232 */
233 public void testGetAndSet() {
234 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
235 a = updaterFor("x");
236 x = 1;
237 assertEquals(1, a.getAndSet(this, 0));
238 assertEquals(0, a.getAndSet(this, -10));
239 assertEquals(-10, a.getAndSet(this, 1));
240 }
241
242 /**
243 * getAndAdd returns previous value and adds given value
244 */
245 public void testGetAndAdd() {
246 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
247 a = updaterFor("x");
248 x = 1;
249 assertEquals(1, a.getAndAdd(this, 2));
250 assertEquals(3, a.get(this));
251 assertEquals(3, a.getAndAdd(this, -4));
252 assertEquals(-1, a.get(this));
253 }
254
255 /**
256 * getAndDecrement returns previous value and decrements
257 */
258 public void testGetAndDecrement() {
259 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
260 a = updaterFor("x");
261 x = 1;
262 assertEquals(1, a.getAndDecrement(this));
263 assertEquals(0, a.getAndDecrement(this));
264 assertEquals(-1, a.getAndDecrement(this));
265 }
266
267 /**
268 * getAndIncrement returns previous value and increments
269 */
270 public void testGetAndIncrement() {
271 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
272 a = updaterFor("x");
273 x = 1;
274 assertEquals(1, a.getAndIncrement(this));
275 assertEquals(2, a.get(this));
276 a.set(this, -2);
277 assertEquals(-2, a.getAndIncrement(this));
278 assertEquals(-1, a.getAndIncrement(this));
279 assertEquals(0, a.getAndIncrement(this));
280 assertEquals(1, a.get(this));
281 }
282
283 /**
284 * addAndGet adds given value to current, and returns current value
285 */
286 public void testAddAndGet() {
287 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
288 a = updaterFor("x");
289 x = 1;
290 assertEquals(3, a.addAndGet(this, 2));
291 assertEquals(3, a.get(this));
292 assertEquals(-1, a.addAndGet(this, -4));
293 assertEquals(-1, a.get(this));
294 }
295
296 /**
297 * decrementAndGet decrements and returns current value
298 */
299 public void testDecrementAndGet() {
300 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
301 a = updaterFor("x");
302 x = 1;
303 assertEquals(0, a.decrementAndGet(this));
304 assertEquals(-1, a.decrementAndGet(this));
305 assertEquals(-2, a.decrementAndGet(this));
306 assertEquals(-2, a.get(this));
307 }
308
309 /**
310 * incrementAndGet increments and returns current value
311 */
312 public void testIncrementAndGet() {
313 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
314 a = updaterFor("x");
315 x = 1;
316 assertEquals(2, a.incrementAndGet(this));
317 assertEquals(2, a.get(this));
318 a.set(this, -2);
319 assertEquals(-1, a.incrementAndGet(this));
320 assertEquals(0, a.incrementAndGet(this));
321 assertEquals(1, a.incrementAndGet(this));
322 assertEquals(1, a.get(this));
323 }
324
325 }