ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerArrayTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicIntegerArrayTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:52 2003 UTC vs.
Revision 1.34 by dl, Thu Jun 16 23:35:25 2016 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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 junit.framework.*;
10 < import java.util.concurrent.atomic.*;
9 > import java.util.Arrays;
10 > import java.util.concurrent.atomic.AtomicIntegerArray;
11  
12 < public class AtomicIntegerArrayTest extends TestCase
13 < {
13 <    static final int N = 10;
12 > import junit.framework.Test;
13 > import junit.framework.TestSuite;
14  
15 <    public static void main (String[] args) {
16 <        junit.textui.TestRunner.run (suite());
15 > public class AtomicIntegerArrayTest extends JSR166TestCase {
16 >
17 >    public static void main(String[] args) {
18 >        main(suite(), args);
19      }
20      public static Test suite() {
21          return new TestSuite(AtomicIntegerArrayTest.class);
22      }
23  
24 <    public void testConstructor(){
25 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
26 <        for (int i = 0; i < N; ++i)
27 <            assertEquals(0,ai.get(i));
28 <    }
29 <
30 <    public void testGetSet(){
31 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
32 <        for (int i = 0; i < N; ++i) {
33 <            ai.set(i, 1);
34 <            assertEquals(1,ai.get(i));
35 <            ai.set(i, 2);
36 <            assertEquals(2,ai.get(i));
37 <            ai.set(i, -3);
38 <            assertEquals(-3,ai.get(i));
39 <        }
40 <    }
41 <
42 <    public void testCompareAndSet(){
43 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
44 <        for (int i = 0; i < N; ++i) {
45 <            ai.set(i, 1);
46 <            assertTrue(ai.compareAndSet(i, 1,2));
47 <            assertTrue(ai.compareAndSet(i, 2,-4));
48 <            assertEquals(-4,ai.get(i));
49 <            assertFalse(ai.compareAndSet(i, -5,7));
50 <            assertFalse((7 == ai.get(i)));
51 <            assertTrue(ai.compareAndSet(i, -4,7));
52 <            assertEquals(7,ai.get(i));
53 <        }
54 <    }
55 <
56 <    public void testWeakCompareAndSet(){
57 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
58 <        for (int i = 0; i < N; ++i) {
59 <            ai.set(i, 1);
60 <            while(!ai.weakCompareAndSet(i, 1,2));
61 <            while(!ai.weakCompareAndSet(i, 2,-4));
62 <            assertEquals(-4,ai.get(i));
63 <            while(!ai.weakCompareAndSet(i, -4,7));
64 <            assertEquals(7,ai.get(i));
65 <        }
66 <    }
67 <
68 <    public void testGetAndSet(){
69 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
70 <        for (int i = 0; i < N; ++i) {
71 <            ai.set(i, 1);
72 <            assertEquals(1,ai.getAndSet(i,0));
73 <            assertEquals(0,ai.getAndSet(i,-10));
74 <            assertEquals(-10,ai.getAndSet(i,1));
75 <        }
76 <    }
77 <
78 <    public void testGetAndAdd(){
79 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
80 <        for (int i = 0; i < N; ++i) {
81 <            ai.set(i, 1);
82 <            assertEquals(1,ai.getAndAdd(i,2));
83 <            assertEquals(3,ai.get(i));
84 <            assertEquals(3,ai.getAndAdd(i,-4));
85 <            assertEquals(-1,ai.get(i));
86 <        }
87 <    }
88 <
89 <    public void testGetAndDecrement(){
90 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
91 <        for (int i = 0; i < N; ++i) {
92 <            ai.set(i, 1);
93 <            assertEquals(1,ai.getAndDecrement(i));
94 <            assertEquals(0,ai.getAndDecrement(i));
95 <            assertEquals(-1,ai.getAndDecrement(i));
96 <        }
97 <    }
98 <
99 <    public void testGetAndIncrement(){
100 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
101 <        for (int i = 0; i < N; ++i) {
102 <            ai.set(i, 1);
103 <            assertEquals(1,ai.getAndIncrement(i));
104 <            assertEquals(2,ai.get(i));
105 <            ai.set(i,-2);
106 <            assertEquals(-2,ai.getAndIncrement(i));
107 <            assertEquals(-1,ai.getAndIncrement(i));
108 <            assertEquals(0,ai.getAndIncrement(i));
109 <            assertEquals(1,ai.get(i));
24 >    /**
25 >     * constructor creates array of given size with all elements zero
26 >     */
27 >    public void testConstructor() {
28 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
29 >        for (int i = 0; i < SIZE; i++)
30 >            assertEquals(0, aa.get(i));
31 >    }
32 >
33 >    /**
34 >     * constructor with null array throws NPE
35 >     */
36 >    public void testConstructor2NPE() {
37 >        try {
38 >            int[] a = null;
39 >            new AtomicIntegerArray(a);
40 >            shouldThrow();
41 >        } catch (NullPointerException success) {}
42 >    }
43 >
44 >    /**
45 >     * constructor with array is of same size and has all elements
46 >     */
47 >    public void testConstructor2() {
48 >        int[] a = { 17, 3, -42, 99, -7 };
49 >        AtomicIntegerArray aa = new AtomicIntegerArray(a);
50 >        assertEquals(a.length, aa.length());
51 >        for (int i = 0; i < a.length; i++)
52 >            assertEquals(a[i], aa.get(i));
53 >    }
54 >
55 >    /**
56 >     * get and set for out of bound indices throw IndexOutOfBoundsException
57 >     */
58 >    public void testIndexing() {
59 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
60 >        for (int index : new int[] { -1, SIZE }) {
61 >            try {
62 >                aa.get(index);
63 >                shouldThrow();
64 >            } catch (IndexOutOfBoundsException success) {}
65 >            try {
66 >                aa.set(index, 1);
67 >                shouldThrow();
68 >            } catch (IndexOutOfBoundsException success) {}
69 >            try {
70 >                aa.lazySet(index, 1);
71 >                shouldThrow();
72 >            } catch (IndexOutOfBoundsException success) {}
73 >            try {
74 >                aa.compareAndSet(index, 1, 2);
75 >                shouldThrow();
76 >            } catch (IndexOutOfBoundsException success) {}
77 >            try {
78 >                aa.weakCompareAndSet(index, 1, 2);
79 >                shouldThrow();
80 >            } catch (IndexOutOfBoundsException success) {}
81 >            try {
82 >                aa.getAndAdd(index, 1);
83 >                shouldThrow();
84 >            } catch (IndexOutOfBoundsException success) {}
85 >            try {
86 >                aa.addAndGet(index, 1);
87 >                shouldThrow();
88 >            } catch (IndexOutOfBoundsException success) {}
89 >            try {
90 >                aa.getPlain(index);
91 >                shouldThrow();
92 >            } catch (IndexOutOfBoundsException success) {}
93 >            try {
94 >                aa.getOpaque(index);
95 >                shouldThrow();
96 >            } catch (IndexOutOfBoundsException success) {}
97 >            try {
98 >                aa.getAcquire(index);
99 >                shouldThrow();
100 >            } catch (IndexOutOfBoundsException success) {}
101 >            try {
102 >                aa.setPlain(index, 1);
103 >                shouldThrow();
104 >            } catch (IndexOutOfBoundsException success) {}
105 >            try {
106 >                aa.setOpaque(index, 1);
107 >                shouldThrow();
108 >            } catch (IndexOutOfBoundsException success) {}
109 >            try {
110 >                aa.setRelease(index, 1);
111 >                shouldThrow();
112 >            } catch (IndexOutOfBoundsException success) {}
113 >            try {
114 >                aa.compareAndExchange(index, 1, 2);
115 >                shouldThrow();
116 >            } catch (IndexOutOfBoundsException success) {}
117 >            try {
118 >                aa.compareAndExchangeAcquire(index, 1, 2);
119 >                shouldThrow();
120 >            } catch (IndexOutOfBoundsException success) {}
121 >            try {
122 >                aa.compareAndExchangeRelease(index, 1, 2);
123 >                shouldThrow();
124 >            } catch (IndexOutOfBoundsException success) {}
125 >            try {
126 >                aa.weakCompareAndSetVolatile(index, 1, 2);
127 >                shouldThrow();
128 >            } catch (IndexOutOfBoundsException success) {}
129 >            try {
130 >                aa.weakCompareAndSetAcquire(index, 1, 2);
131 >                shouldThrow();
132 >            } catch (IndexOutOfBoundsException success) {}
133 >            try {
134 >                aa.weakCompareAndSetRelease(index, 1, 2);
135 >                shouldThrow();
136 >            } catch (IndexOutOfBoundsException success) {}
137          }
138      }
139  
140 +    /**
141 +     * get returns the last value set at index
142 +     */
143 +    public void testGetSet() {
144 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
145 +        for (int i = 0; i < SIZE; i++) {
146 +            aa.set(i, 1);
147 +            assertEquals(1, aa.get(i));
148 +            aa.set(i, 2);
149 +            assertEquals(2, aa.get(i));
150 +            aa.set(i, -3);
151 +            assertEquals(-3, aa.get(i));
152 +        }
153 +    }
154 +
155 +    /**
156 +     * get returns the last value lazySet at index by same thread
157 +     */
158 +    public void testGetLazySet() {
159 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
160 +        for (int i = 0; i < SIZE; i++) {
161 +            aa.lazySet(i, 1);
162 +            assertEquals(1, aa.get(i));
163 +            aa.lazySet(i, 2);
164 +            assertEquals(2, aa.get(i));
165 +            aa.lazySet(i, -3);
166 +            assertEquals(-3, aa.get(i));
167 +        }
168 +    }
169 +
170 +    /**
171 +     * compareAndSet succeeds in changing value if equal to expected else fails
172 +     */
173 +    public void testCompareAndSet() {
174 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
175 +        for (int i = 0; i < SIZE; i++) {
176 +            aa.set(i, 1);
177 +            assertTrue(aa.compareAndSet(i, 1, 2));
178 +            assertTrue(aa.compareAndSet(i, 2, -4));
179 +            assertEquals(-4, aa.get(i));
180 +            assertFalse(aa.compareAndSet(i, -5, 7));
181 +            assertEquals(-4, aa.get(i));
182 +            assertTrue(aa.compareAndSet(i, -4, 7));
183 +            assertEquals(7, aa.get(i));
184 +        }
185 +    }
186 +
187 +    /**
188 +     * compareAndSet in one thread enables another waiting for value
189 +     * to succeed
190 +     */
191 +    public void testCompareAndSetInMultipleThreads() throws Exception {
192 +        final AtomicIntegerArray a = new AtomicIntegerArray(1);
193 +        a.set(0, 1);
194 +        Thread t = new Thread(new CheckedRunnable() {
195 +            public void realRun() {
196 +                while (!a.compareAndSet(0, 2, 3))
197 +                    Thread.yield();
198 +            }});
199 +
200 +        t.start();
201 +        assertTrue(a.compareAndSet(0, 1, 2));
202 +        t.join(LONG_DELAY_MS);
203 +        assertFalse(t.isAlive());
204 +        assertEquals(3, a.get(0));
205 +    }
206 +
207 +    /**
208 +     * repeated weakCompareAndSet succeeds in changing value when equal
209 +     * to expected
210 +     */
211 +    public void testWeakCompareAndSet() {
212 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
213 +        for (int i = 0; i < SIZE; i++) {
214 +            aa.set(i, 1);
215 +            do {} while (!aa.weakCompareAndSet(i, 1, 2));
216 +            do {} while (!aa.weakCompareAndSet(i, 2, -4));
217 +            assertEquals(-4, aa.get(i));
218 +            do {} while (!aa.weakCompareAndSet(i, -4, 7));
219 +            assertEquals(7, aa.get(i));
220 +        }
221 +    }
222 +
223 +    /**
224 +     * getAndSet returns previous value and sets to given value at given index
225 +     */
226 +    public void testGetAndSet() {
227 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
228 +        for (int i = 0; i < SIZE; i++) {
229 +            aa.set(i, 1);
230 +            assertEquals(1, aa.getAndSet(i, 0));
231 +            assertEquals(0, aa.getAndSet(i, -10));
232 +            assertEquals(-10, aa.getAndSet(i, 1));
233 +        }
234 +    }
235 +
236 +    /**
237 +     * getAndAdd returns previous value and adds given value
238 +     */
239 +    public void testGetAndAdd() {
240 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
241 +        for (int i = 0; i < SIZE; i++) {
242 +            aa.set(i, 1);
243 +            assertEquals(1, aa.getAndAdd(i, 2));
244 +            assertEquals(3, aa.get(i));
245 +            assertEquals(3, aa.getAndAdd(i, -4));
246 +            assertEquals(-1, aa.get(i));
247 +        }
248 +    }
249 +
250 +    /**
251 +     * getAndDecrement returns previous value and decrements
252 +     */
253 +    public void testGetAndDecrement() {
254 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
255 +        for (int i = 0; i < SIZE; i++) {
256 +            aa.set(i, 1);
257 +            assertEquals(1, aa.getAndDecrement(i));
258 +            assertEquals(0, aa.getAndDecrement(i));
259 +            assertEquals(-1, aa.getAndDecrement(i));
260 +        }
261 +    }
262 +
263 +    /**
264 +     * getAndIncrement returns previous value and increments
265 +     */
266 +    public void testGetAndIncrement() {
267 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
268 +        for (int i = 0; i < SIZE; i++) {
269 +            aa.set(i, 1);
270 +            assertEquals(1, aa.getAndIncrement(i));
271 +            assertEquals(2, aa.get(i));
272 +            aa.set(i, -2);
273 +            assertEquals(-2, aa.getAndIncrement(i));
274 +            assertEquals(-1, aa.getAndIncrement(i));
275 +            assertEquals(0, aa.getAndIncrement(i));
276 +            assertEquals(1, aa.get(i));
277 +        }
278 +    }
279 +
280 +    /**
281 +     * addAndGet adds given value to current, and returns current value
282 +     */
283      public void testAddAndGet() {
284 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
285 <        for (int i = 0; i < N; ++i) {
286 <            ai.set(i, 1);
287 <            assertEquals(3,ai.addAndGet(i,2));
288 <            assertEquals(3,ai.get(i));
289 <            assertEquals(-1,ai.addAndGet(i,-4));
290 <            assertEquals(-1,ai.get(i));
284 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
285 >        for (int i = 0; i < SIZE; i++) {
286 >            aa.set(i, 1);
287 >            assertEquals(3, aa.addAndGet(i, 2));
288 >            assertEquals(3, aa.get(i));
289 >            assertEquals(-1, aa.addAndGet(i, -4));
290 >            assertEquals(-1, aa.get(i));
291          }
292      }
293  
294 <    public void testDecrementAndGet(){
295 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
296 <        for (int i = 0; i < N; ++i) {
297 <            ai.set(i, 1);
298 <            assertEquals(0,ai.decrementAndGet(i));
299 <            assertEquals(-1,ai.decrementAndGet(i));
300 <            assertEquals(-2,ai.decrementAndGet(i));
301 <            assertEquals(-2,ai.get(i));
294 >    /**
295 >     * decrementAndGet decrements and returns current value
296 >     */
297 >    public void testDecrementAndGet() {
298 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
299 >        for (int i = 0; i < SIZE; i++) {
300 >            aa.set(i, 1);
301 >            assertEquals(0, aa.decrementAndGet(i));
302 >            assertEquals(-1, aa.decrementAndGet(i));
303 >            assertEquals(-2, aa.decrementAndGet(i));
304 >            assertEquals(-2, aa.get(i));
305          }
306      }
307  
308 +    /**
309 +     * incrementAndGet increments and returns current value
310 +     */
311      public void testIncrementAndGet() {
312 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
313 <        for (int i = 0; i < N; ++i) {
314 <            ai.set(i, 1);
315 <            assertEquals(2,ai.incrementAndGet(i));
316 <            assertEquals(2,ai.get(i));
317 <            ai.set(i, -2);
318 <            assertEquals(-1,ai.incrementAndGet(i));
319 <            assertEquals(0,ai.incrementAndGet(i));
320 <            assertEquals(1,ai.incrementAndGet(i));
321 <            assertEquals(1,ai.get(i));
312 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
313 >        for (int i = 0; i < SIZE; i++) {
314 >            aa.set(i, 1);
315 >            assertEquals(2, aa.incrementAndGet(i));
316 >            assertEquals(2, aa.get(i));
317 >            aa.set(i, -2);
318 >            assertEquals(-1, aa.incrementAndGet(i));
319 >            assertEquals(0, aa.incrementAndGet(i));
320 >            assertEquals(1, aa.incrementAndGet(i));
321 >            assertEquals(1, aa.get(i));
322 >        }
323 >    }
324 >
325 >    class Counter extends CheckedRunnable {
326 >        final AtomicIntegerArray aa;
327 >        volatile int counts;
328 >        Counter(AtomicIntegerArray a) { aa = a; }
329 >        public void realRun() {
330 >            for (;;) {
331 >                boolean done = true;
332 >                for (int i = 0; i < aa.length(); i++) {
333 >                    int v = aa.get(i);
334 >                    assertTrue(v >= 0);
335 >                    if (v != 0) {
336 >                        done = false;
337 >                        if (aa.compareAndSet(i, v, v - 1))
338 >                            ++counts;
339 >                    }
340 >                }
341 >                if (done)
342 >                    break;
343 >            }
344 >        }
345 >    }
346 >
347 >    /**
348 >     * Multiple threads using same array of counters successfully
349 >     * update a number of times equal to total count
350 >     */
351 >    public void testCountingInMultipleThreads() throws InterruptedException {
352 >        final AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
353 >        int countdown = 10000;
354 >        for (int i = 0; i < SIZE; i++)
355 >            aa.set(i, countdown);
356 >        Counter c1 = new Counter(aa);
357 >        Counter c2 = new Counter(aa);
358 >        Thread t1 = new Thread(c1);
359 >        Thread t2 = new Thread(c2);
360 >        t1.start();
361 >        t2.start();
362 >        t1.join();
363 >        t2.join();
364 >        assertEquals(c1.counts+c2.counts, SIZE * countdown);
365 >    }
366 >
367 >    /**
368 >     * a deserialized serialized array holds same values
369 >     */
370 >    public void testSerialization() throws Exception {
371 >        AtomicIntegerArray x = new AtomicIntegerArray(SIZE);
372 >        for (int i = 0; i < SIZE; i++)
373 >            x.set(i, -i);
374 >        AtomicIntegerArray y = serialClone(x);
375 >        assertNotSame(x, y);
376 >        assertEquals(x.length(), y.length());
377 >        for (int i = 0; i < SIZE; i++) {
378 >            assertEquals(x.get(i), y.get(i));
379 >        }
380 >    }
381 >
382 >    /**
383 >     * toString returns current value.
384 >     */
385 >    public void testToString() {
386 >        int[] a = { 17, 3, -42, 99, -7 };
387 >        AtomicIntegerArray aa = new AtomicIntegerArray(a);
388 >        assertEquals(Arrays.toString(a), aa.toString());
389 >    }
390 >
391 >    // jdk9
392 >    
393 >    /**
394 >     * getPlain returns the last value set
395 >     */
396 >    public void testGetPlainSet() {
397 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
398 >        for (int i = 0; i < SIZE; i++) {
399 >            aa.set(i, 1);
400 >            assertEquals(1, aa.getPlain(i));
401 >            aa.set(i, 2);
402 >            assertEquals(2, aa.getPlain(i));
403 >            aa.set(i, -3);
404 >            assertEquals(-3, aa.getPlain(i));
405          }
406      }
407  
408 +    /**
409 +     * getOpaque returns the last value set
410 +     */
411 +    public void testGetOpaqueSet() {
412 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
413 +        for (int i = 0; i < SIZE; i++) {
414 +            aa.set(i, 1);
415 +            assertEquals(1, aa.getOpaque(i));
416 +            aa.set(i, 2);
417 +            assertEquals(2, aa.getOpaque(i));
418 +            aa.set(i, -3);
419 +            assertEquals(-3, aa.getOpaque(i));
420 +        }
421 +    }
422 +
423 +    /**
424 +     * getAcquire returns the last value set
425 +     */
426 +    public void testGetAcquireSet() {
427 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
428 +        for (int i = 0; i < SIZE; i++) {
429 +            aa.set(i, 1);
430 +            assertEquals(1, aa.getAcquire(i));
431 +            aa.set(i, 2);
432 +            assertEquals(2, aa.getAcquire(i));
433 +            aa.set(i, -3);
434 +            assertEquals(-3, aa.getAcquire(i));
435 +        }
436 +    }
437 +
438 +    /**
439 +     * get returns the last value setPlain
440 +     */
441 +    public void testGetSetPlain() {
442 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
443 +        for (int i = 0; i < SIZE; i++) {
444 +            aa.setPlain(i, 1);
445 +            assertEquals(1, aa.get(i));
446 +            aa.setPlain(i, 2);
447 +            assertEquals(2, aa.get(i));
448 +            aa.setPlain(i, -3);
449 +            assertEquals(-3, aa.get(i));
450 +        }
451 +    }
452 +
453 +    /**
454 +     * get returns the last value setOpaque
455 +     */
456 +    public void testGetSetOpaque() {
457 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
458 +        for (int i = 0; i < SIZE; i++) {
459 +            aa.setOpaque(i, 1);
460 +            assertEquals(1, aa.get(i));
461 +            aa.setOpaque(i, 2);
462 +            assertEquals(2, aa.get(i));
463 +            aa.setOpaque(i, -3);
464 +            assertEquals(-3, aa.get(i));
465 +        }
466 +    }
467 +
468 +    /**
469 +     * get returns the last value setRelease
470 +     */
471 +    public void testGetSetRelease() {
472 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
473 +        for (int i = 0; i < SIZE; i++) {
474 +            aa.setRelease(i, 1);
475 +            assertEquals(1, aa.get(i));
476 +            aa.setRelease(i, 2);
477 +            assertEquals(2, aa.get(i));
478 +            aa.setRelease(i, -3);
479 +            assertEquals(-3, aa.get(i));
480 +        }
481 +    }
482 +
483 +    /**
484 +     * compareAndExchange succeeds in changing value if equal to
485 +     * expected else fails
486 +     */
487 +    public void testCompareAndExchange() {
488 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
489 +        for (int i = 0; i < SIZE; i++) {
490 +            aa.set(i, 1);
491 +            assertEquals(1, aa.compareAndExchange(i, 1, 2));
492 +            assertEquals(2, aa.compareAndExchange(i, 2, -4));
493 +            assertEquals(-4, aa.get(i));
494 +            assertEquals(-4, aa.compareAndExchange(i,-5, 7));
495 +            assertEquals(-4, aa.get(i));
496 +            assertEquals(-4, aa.compareAndExchange(i, -4, 7));
497 +            assertEquals(7, aa.get(i));
498 +        }
499 +    }
500 +
501 +    /**
502 +     * compareAndExchangeAcquire succeeds in changing value if equal to
503 +     * expected else fails
504 +     */
505 +    public void testCompareAndExchangeAcquire() {
506 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
507 +        for (int i = 0; i < SIZE; i++) {
508 +            aa.set(i, 1);
509 +            assertEquals(1, aa.compareAndExchangeAcquire(i, 1, 2));
510 +            assertEquals(2, aa.compareAndExchangeAcquire(i, 2, -4));
511 +            assertEquals(-4, aa.get(i));
512 +            assertEquals(-4, aa.compareAndExchangeAcquire(i,-5, 7));
513 +            assertEquals(-4, aa.get(i));
514 +            assertEquals(-4, aa.compareAndExchangeAcquire(i, -4, 7));
515 +            assertEquals(7, aa.get(i));
516 +        }
517 +    }
518 +
519 +    /**
520 +     * compareAndExchangeRelease succeeds in changing value if equal to
521 +     * expected else fails
522 +     */
523 +    public void testCompareAndExchangeRelease() {
524 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
525 +        for (int i = 0; i < SIZE; i++) {
526 +            aa.set(i, 1);
527 +            assertEquals(1, aa.compareAndExchangeRelease(i, 1, 2));
528 +            assertEquals(2, aa.compareAndExchangeRelease(i, 2, -4));
529 +            assertEquals(-4, aa.get(i));
530 +            assertEquals(-4, aa.compareAndExchangeRelease(i,-5, 7));
531 +            assertEquals(-4, aa.get(i));
532 +            assertEquals(-4, aa.compareAndExchangeRelease(i, -4, 7));
533 +            assertEquals(7, aa.get(i));
534 +        }
535 +    }
536 +
537 +    /**
538 +     * repeated weakCompareAndSetVolatile succeeds in changing value when equal
539 +     * to expected
540 +     */
541 +    public void testWeakCompareAndSetVolatile() {
542 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
543 +        for (int i = 0; i < SIZE; i++) {
544 +            aa.set(i, 1);
545 +            do {} while (!aa.weakCompareAndSetVolatile(i, 1, 2));
546 +            do {} while (!aa.weakCompareAndSetVolatile(i, 2, -4));
547 +            assertEquals(-4, aa.get(i));
548 +            do {} while (!aa.weakCompareAndSetVolatile(i, -4, 7));
549 +            assertEquals(7, aa.get(i));
550 +        }
551 +    }
552 +
553 +    /**
554 +     * repeated weakCompareAndSetAcquire succeeds in changing value when equal
555 +     * to expected
556 +     */
557 +    public void testWeakCompareAndSetAcquire() {
558 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
559 +        for (int i = 0; i < SIZE; i++) {
560 +            aa.set(i, 1);
561 +            do {} while (!aa.weakCompareAndSetAcquire(i, 1, 2));
562 +            do {} while (!aa.weakCompareAndSetAcquire(i, 2, -4));
563 +            assertEquals(-4, aa.get(i));
564 +            do {} while (!aa.weakCompareAndSetAcquire(i, -4, 7));
565 +            assertEquals(7, aa.get(i));
566 +        }
567 +    }
568 +
569 +    /**
570 +     * repeated weakCompareAndSetRelease succeeds in changing value when equal
571 +     * to expected
572 +     */
573 +    public void testWeakCompareAndSetRelease() {
574 +        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
575 +        for (int i = 0; i < SIZE; i++) {
576 +            aa.set(i, 1);
577 +            do {} while (!aa.weakCompareAndSetRelease(i, 1, 2));
578 +            do {} while (!aa.weakCompareAndSetRelease(i, 2, -4));
579 +            assertEquals(-4, aa.get(i));
580 +            do {} while (!aa.weakCompareAndSetRelease(i, -4, 7));
581 +            assertEquals(7, aa.get(i));
582 +        }
583 +    }
584 +    
585   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines