ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongArrayTest.java
Revision: 1.33
Committed: Thu Jun 16 23:35:25 2016 UTC (7 years, 10 months ago) by dl
Branch: MAIN
Changes since 1.32: +242 -0 lines
Log Message:
Add VarHandle atomic methods to Atomic classes

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.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 main(suite(), args);
18 }
19 public static Test suite() {
20 return new TestSuite(AtomicLongArrayTest.class);
21 }
22
23 /**
24 * constructor creates array of given size with all elements zero
25 */
26 public void testConstructor() {
27 AtomicLongArray aa = new AtomicLongArray(SIZE);
28 for (int i = 0; i < SIZE; i++)
29 assertEquals(0, aa.get(i));
30 }
31
32 /**
33 * constructor with null array throws NPE
34 */
35 public void testConstructor2NPE() {
36 try {
37 long[] a = null;
38 new AtomicLongArray(a);
39 shouldThrow();
40 } catch (NullPointerException success) {}
41 }
42
43 /**
44 * constructor with array is of same size and has all elements
45 */
46 public void testConstructor2() {
47 long[] a = { 17L, 3L, -42L, 99L, -7L };
48 AtomicLongArray aa = new AtomicLongArray(a);
49 assertEquals(a.length, aa.length());
50 for (int i = 0; i < a.length; i++)
51 assertEquals(a[i], aa.get(i));
52 }
53
54 /**
55 * get and set for out of bound indices throw IndexOutOfBoundsException
56 */
57 public void testIndexing() {
58 AtomicLongArray aa = new AtomicLongArray(SIZE);
59 for (int index : new int[] { -1, SIZE }) {
60 try {
61 aa.get(index);
62 shouldThrow();
63 } catch (IndexOutOfBoundsException success) {}
64 try {
65 aa.set(index, 1);
66 shouldThrow();
67 } catch (IndexOutOfBoundsException success) {}
68 try {
69 aa.lazySet(index, 1);
70 shouldThrow();
71 } catch (IndexOutOfBoundsException success) {}
72 try {
73 aa.compareAndSet(index, 1, 2);
74 shouldThrow();
75 } catch (IndexOutOfBoundsException success) {}
76 try {
77 aa.weakCompareAndSet(index, 1, 2);
78 shouldThrow();
79 } catch (IndexOutOfBoundsException success) {}
80 try {
81 aa.getAndAdd(index, 1);
82 shouldThrow();
83 } catch (IndexOutOfBoundsException success) {}
84 try {
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
139 /**
140 * get returns the last value set at index
141 */
142 public void testGetSet() {
143 AtomicLongArray aa = new AtomicLongArray(SIZE);
144 for (int i = 0; i < SIZE; i++) {
145 aa.set(i, 1);
146 assertEquals(1, aa.get(i));
147 aa.set(i, 2);
148 assertEquals(2, aa.get(i));
149 aa.set(i, -3);
150 assertEquals(-3, aa.get(i));
151 }
152 }
153
154 /**
155 * get returns the last value lazySet at index by same thread
156 */
157 public void testGetLazySet() {
158 AtomicLongArray aa = new AtomicLongArray(SIZE);
159 for (int i = 0; i < SIZE; i++) {
160 aa.lazySet(i, 1);
161 assertEquals(1, aa.get(i));
162 aa.lazySet(i, 2);
163 assertEquals(2, aa.get(i));
164 aa.lazySet(i, -3);
165 assertEquals(-3, aa.get(i));
166 }
167 }
168
169 /**
170 * compareAndSet succeeds in changing value if equal to expected else fails
171 */
172 public void testCompareAndSet() {
173 AtomicLongArray aa = new AtomicLongArray(SIZE);
174 for (int i = 0; i < SIZE; i++) {
175 aa.set(i, 1);
176 assertTrue(aa.compareAndSet(i, 1, 2));
177 assertTrue(aa.compareAndSet(i, 2, -4));
178 assertEquals(-4, aa.get(i));
179 assertFalse(aa.compareAndSet(i, -5, 7));
180 assertEquals(-4, aa.get(i));
181 assertTrue(aa.compareAndSet(i, -4, 7));
182 assertEquals(7, aa.get(i));
183 }
184 }
185
186 /**
187 * compareAndSet in one thread enables another waiting for value
188 * to succeed
189 */
190 public void testCompareAndSetInMultipleThreads() throws InterruptedException {
191 final AtomicLongArray a = new AtomicLongArray(1);
192 a.set(0, 1);
193 Thread t = new Thread(new CheckedRunnable() {
194 public void realRun() {
195 while (!a.compareAndSet(0, 2, 3))
196 Thread.yield();
197 }});
198
199 t.start();
200 assertTrue(a.compareAndSet(0, 1, 2));
201 t.join(LONG_DELAY_MS);
202 assertFalse(t.isAlive());
203 assertEquals(3, a.get(0));
204 }
205
206 /**
207 * repeated weakCompareAndSet succeeds in changing value when equal
208 * to expected
209 */
210 public void testWeakCompareAndSet() {
211 AtomicLongArray aa = new AtomicLongArray(SIZE);
212 for (int i = 0; i < SIZE; i++) {
213 aa.set(i, 1);
214 do {} while (!aa.weakCompareAndSet(i, 1, 2));
215 do {} while (!aa.weakCompareAndSet(i, 2, -4));
216 assertEquals(-4, aa.get(i));
217 do {} while (!aa.weakCompareAndSet(i, -4, 7));
218 assertEquals(7, aa.get(i));
219 }
220 }
221
222 /**
223 * getAndSet returns previous value and sets to given value at given index
224 */
225 public void testGetAndSet() {
226 AtomicLongArray aa = new AtomicLongArray(SIZE);
227 for (int i = 0; i < SIZE; i++) {
228 aa.set(i, 1);
229 assertEquals(1, aa.getAndSet(i, 0));
230 assertEquals(0, aa.getAndSet(i, -10));
231 assertEquals(-10, aa.getAndSet(i, 1));
232 }
233 }
234
235 /**
236 * getAndAdd returns previous value and adds given value
237 */
238 public void testGetAndAdd() {
239 AtomicLongArray aa = new AtomicLongArray(SIZE);
240 for (int i = 0; i < SIZE; i++) {
241 aa.set(i, 1);
242 assertEquals(1, aa.getAndAdd(i, 2));
243 assertEquals(3, aa.get(i));
244 assertEquals(3, aa.getAndAdd(i, -4));
245 assertEquals(-1, aa.get(i));
246 }
247 }
248
249 /**
250 * getAndDecrement returns previous value and decrements
251 */
252 public void testGetAndDecrement() {
253 AtomicLongArray aa = new AtomicLongArray(SIZE);
254 for (int i = 0; i < SIZE; i++) {
255 aa.set(i, 1);
256 assertEquals(1, aa.getAndDecrement(i));
257 assertEquals(0, aa.getAndDecrement(i));
258 assertEquals(-1, aa.getAndDecrement(i));
259 }
260 }
261
262 /**
263 * getAndIncrement returns previous value and increments
264 */
265 public void testGetAndIncrement() {
266 AtomicLongArray aa = new AtomicLongArray(SIZE);
267 for (int i = 0; i < SIZE; i++) {
268 aa.set(i, 1);
269 assertEquals(1, aa.getAndIncrement(i));
270 assertEquals(2, aa.get(i));
271 aa.set(i, -2);
272 assertEquals(-2, aa.getAndIncrement(i));
273 assertEquals(-1, aa.getAndIncrement(i));
274 assertEquals(0, aa.getAndIncrement(i));
275 assertEquals(1, aa.get(i));
276 }
277 }
278
279 /**
280 * addAndGet adds given value to current, and returns current value
281 */
282 public void testAddAndGet() {
283 AtomicLongArray aa = new AtomicLongArray(SIZE);
284 for (int i = 0; i < SIZE; i++) {
285 aa.set(i, 1);
286 assertEquals(3, aa.addAndGet(i, 2));
287 assertEquals(3, aa.get(i));
288 assertEquals(-1, aa.addAndGet(i, -4));
289 assertEquals(-1, aa.get(i));
290 }
291 }
292
293 /**
294 * decrementAndGet decrements and returns current value
295 */
296 public void testDecrementAndGet() {
297 AtomicLongArray aa = new AtomicLongArray(SIZE);
298 for (int i = 0; i < SIZE; i++) {
299 aa.set(i, 1);
300 assertEquals(0, aa.decrementAndGet(i));
301 assertEquals(-1, aa.decrementAndGet(i));
302 assertEquals(-2, aa.decrementAndGet(i));
303 assertEquals(-2, aa.get(i));
304 }
305 }
306
307 /**
308 * incrementAndGet increments and returns current value
309 */
310 public void testIncrementAndGet() {
311 AtomicLongArray aa = new AtomicLongArray(SIZE);
312 for (int i = 0; i < SIZE; i++) {
313 aa.set(i, 1);
314 assertEquals(2, aa.incrementAndGet(i));
315 assertEquals(2, aa.get(i));
316 aa.set(i, -2);
317 assertEquals(-1, aa.incrementAndGet(i));
318 assertEquals(0, aa.incrementAndGet(i));
319 assertEquals(1, aa.incrementAndGet(i));
320 assertEquals(1, aa.get(i));
321 }
322 }
323
324 class Counter extends CheckedRunnable {
325 final AtomicLongArray aa;
326 volatile long counts;
327 Counter(AtomicLongArray a) { aa = a; }
328 public void realRun() {
329 for (;;) {
330 boolean done = true;
331 for (int i = 0; i < aa.length(); i++) {
332 long v = aa.get(i);
333 assertTrue(v >= 0);
334 if (v != 0) {
335 done = false;
336 if (aa.compareAndSet(i, v, v - 1))
337 ++counts;
338 }
339 }
340 if (done)
341 break;
342 }
343 }
344 }
345
346 /**
347 * Multiple threads using same array of counters successfully
348 * update a number of times equal to total count
349 */
350 public void testCountingInMultipleThreads() throws InterruptedException {
351 final AtomicLongArray aa = new AtomicLongArray(SIZE);
352 long countdown = 10000;
353 for (int i = 0; i < SIZE; i++)
354 aa.set(i, countdown);
355 Counter c1 = new Counter(aa);
356 Counter c2 = new Counter(aa);
357 Thread t1 = new Thread(c1);
358 Thread t2 = new Thread(c2);
359 t1.start();
360 t2.start();
361 t1.join();
362 t2.join();
363 assertEquals(c1.counts+c2.counts, SIZE * countdown);
364 }
365
366 /**
367 * a deserialized serialized array holds same values
368 */
369 public void testSerialization() throws Exception {
370 AtomicLongArray x = new AtomicLongArray(SIZE);
371 for (int i = 0; i < SIZE; i++)
372 x.set(i, -i);
373 AtomicLongArray y = serialClone(x);
374 assertNotSame(x, y);
375 assertEquals(x.length(), y.length());
376 for (int i = 0; i < SIZE; i++) {
377 assertEquals(x.get(i), y.get(i));
378 }
379 }
380
381 /**
382 * toString returns current value.
383 */
384 public void testToString() {
385 long[] a = { 17, 3, -42, 99, -7 };
386 AtomicLongArray aa = new AtomicLongArray(a);
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 }