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.15 by jsr166, Tue Nov 17 06:58:50 2009 UTC vs.
Revision 1.35 by jsr166, Fri Jun 17 01:38:28 2016 UTC

# Line 1 | Line 1
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/licenses/publicdomain
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.*;
11 < import java.io.*;
12 < import java.util.*;
9 > import java.util.Arrays;
10 > import java.util.concurrent.atomic.AtomicIntegerArray;
11 >
12 > import junit.framework.Test;
13 > import junit.framework.TestSuite;
14  
15   public class AtomicIntegerArrayTest extends JSR166TestCase {
16  
17 <    public static void main (String[] args) {
18 <        junit.textui.TestRunner.run (suite());
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  
23
24      /**
25       * constructor creates array of given size with all elements zero
26       */
27      public void testConstructor() {
28 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
29 <        for (int i = 0; i < SIZE; ++i)
30 <            assertEquals(0,ai.get(i));
28 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
29 >        for (int i = 0; i < SIZE; i++)
30 >            assertEquals(0, aa.get(i));
31      }
32  
33      /**
# Line 36 | Line 36 | public class AtomicIntegerArrayTest exte
36      public void testConstructor2NPE() {
37          try {
38              int[] a = null;
39 <            AtomicIntegerArray ai = new AtomicIntegerArray(a);
39 >            new AtomicIntegerArray(a);
40              shouldThrow();
41          } catch (NullPointerException success) {}
42      }
# Line 45 | Line 45 | public class AtomicIntegerArrayTest exte
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 ai = new AtomicIntegerArray(a);
50 <        assertEquals(a.length, ai.length());
51 <        for (int i = 0; i < a.length; ++i)
52 <            assertEquals(a[i], ai.get(i));
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 ai = new AtomicIntegerArray(SIZE);
60 <        try {
61 <            ai.get(SIZE);
62 <            shouldThrow();
63 <        } catch (IndexOutOfBoundsException success) {
64 <        }
65 <        try {
66 <            ai.get(-1);
67 <            shouldThrow();
68 <        } catch (IndexOutOfBoundsException success) {
69 <        }
70 <        try {
71 <            ai.set(SIZE, 0);
72 <            shouldThrow();
73 <        } catch (IndexOutOfBoundsException success) {
74 <        }
75 <        try {
76 <            ai.set(-1, 0);
77 <            shouldThrow();
78 <        } catch (IndexOutOfBoundsException success) {
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  
# Line 83 | Line 141 | public class AtomicIntegerArrayTest exte
141       * get returns the last value set at index
142       */
143      public void testGetSet() {
144 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
145 <        for (int i = 0; i < SIZE; ++i) {
146 <            ai.set(i, 1);
147 <            assertEquals(1,ai.get(i));
148 <            ai.set(i, 2);
149 <            assertEquals(2,ai.get(i));
150 <            ai.set(i, -3);
151 <            assertEquals(-3,ai.get(i));
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  
# Line 98 | Line 156 | public class AtomicIntegerArrayTest exte
156       * get returns the last value lazySet at index by same thread
157       */
158      public void testGetLazySet() {
159 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
160 <        for (int i = 0; i < SIZE; ++i) {
161 <            ai.lazySet(i, 1);
162 <            assertEquals(1,ai.get(i));
163 <            ai.lazySet(i, 2);
164 <            assertEquals(2,ai.get(i));
165 <            ai.lazySet(i, -3);
166 <            assertEquals(-3,ai.get(i));
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  
# Line 113 | Line 171 | public class AtomicIntegerArrayTest exte
171       * compareAndSet succeeds in changing value if equal to expected else fails
172       */
173      public void testCompareAndSet() {
174 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
175 <        for (int i = 0; i < SIZE; ++i) {
176 <            ai.set(i, 1);
177 <            assertTrue(ai.compareAndSet(i, 1,2));
178 <            assertTrue(ai.compareAndSet(i, 2,-4));
179 <            assertEquals(-4,ai.get(i));
180 <            assertFalse(ai.compareAndSet(i, -5,7));
181 <            assertFalse((7 == ai.get(i)));
182 <            assertTrue(ai.compareAndSet(i, -4,7));
183 <            assertEquals(7,ai.get(i));
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  
# Line 143 | Line 201 | public class AtomicIntegerArrayTest exte
201          assertTrue(a.compareAndSet(0, 1, 2));
202          t.join(LONG_DELAY_MS);
203          assertFalse(t.isAlive());
204 <        assertEquals(a.get(0), 3);
204 >        assertEquals(3, a.get(0));
205      }
206  
207      /**
# Line 151 | Line 209 | public class AtomicIntegerArrayTest exte
209       * to expected
210       */
211      public void testWeakCompareAndSet() {
212 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
213 <        for (int i = 0; i < SIZE; ++i) {
214 <            ai.set(i, 1);
215 <            while (!ai.weakCompareAndSet(i, 1,2));
216 <            while (!ai.weakCompareAndSet(i, 2,-4));
217 <            assertEquals(-4,ai.get(i));
218 <            while (!ai.weakCompareAndSet(i, -4,7));
219 <            assertEquals(7,ai.get(i));
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
224 >     * getAndSet returns previous value and sets to given value at given index
225       */
226      public void testGetAndSet() {
227 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
228 <        for (int i = 0; i < SIZE; ++i) {
229 <            ai.set(i, 1);
230 <            assertEquals(1,ai.getAndSet(i,0));
231 <            assertEquals(0,ai.getAndSet(i,-10));
232 <            assertEquals(-10,ai.getAndSet(i,1));
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
237 >     * getAndAdd returns previous value and adds given value
238       */
239      public void testGetAndAdd() {
240 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
241 <        for (int i = 0; i < SIZE; ++i) {
242 <            ai.set(i, 1);
243 <            assertEquals(1,ai.getAndAdd(i,2));
244 <            assertEquals(3,ai.get(i));
245 <            assertEquals(3,ai.getAndAdd(i,-4));
246 <            assertEquals(-1,ai.get(i));
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  
# Line 193 | Line 251 | public class AtomicIntegerArrayTest exte
251       * getAndDecrement returns previous value and decrements
252       */
253      public void testGetAndDecrement() {
254 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
255 <        for (int i = 0; i < SIZE; ++i) {
256 <            ai.set(i, 1);
257 <            assertEquals(1,ai.getAndDecrement(i));
258 <            assertEquals(0,ai.getAndDecrement(i));
259 <            assertEquals(-1,ai.getAndDecrement(i));
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  
# Line 206 | Line 264 | public class AtomicIntegerArrayTest exte
264       * getAndIncrement returns previous value and increments
265       */
266      public void testGetAndIncrement() {
267 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
268 <        for (int i = 0; i < SIZE; ++i) {
269 <            ai.set(i, 1);
270 <            assertEquals(1,ai.getAndIncrement(i));
271 <            assertEquals(2,ai.get(i));
272 <            ai.set(i,-2);
273 <            assertEquals(-2,ai.getAndIncrement(i));
274 <            assertEquals(-1,ai.getAndIncrement(i));
275 <            assertEquals(0,ai.getAndIncrement(i));
276 <            assertEquals(1,ai.get(i));
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
281 >     * addAndGet adds given value to current, and returns current value
282       */
283      public void testAddAndGet() {
284 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
285 <        for (int i = 0; i < SIZE; ++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  
# Line 237 | Line 295 | public class AtomicIntegerArrayTest exte
295       * decrementAndGet decrements and returns current value
296       */
297      public void testDecrementAndGet() {
298 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
299 <        for (int i = 0; i < SIZE; ++i) {
300 <            ai.set(i, 1);
301 <            assertEquals(0,ai.decrementAndGet(i));
302 <            assertEquals(-1,ai.decrementAndGet(i));
303 <            assertEquals(-2,ai.decrementAndGet(i));
304 <            assertEquals(-2,ai.get(i));
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
309 >     * incrementAndGet increments and returns current value
310       */
311      public void testIncrementAndGet() {
312 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
313 <        for (int i = 0; i < SIZE; ++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 <    static final int COUNTDOWN = 100000;
326 <
269 <    class Counter implements Runnable {
270 <        final AtomicIntegerArray ai;
325 >    class Counter extends CheckedRunnable {
326 >        final AtomicIntegerArray aa;
327          volatile int counts;
328 <        Counter(AtomicIntegerArray a) { ai = a; }
329 <        public void run() {
328 >        Counter(AtomicIntegerArray a) { aa = a; }
329 >        public void realRun() {
330              for (;;) {
331                  boolean done = true;
332 <                for (int i = 0; i < ai.length(); ++i) {
333 <                    int v = ai.get(i);
334 <                    threadAssertTrue(v >= 0);
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 (ai.compareAndSet(i, v, v-1))
337 >                        if (aa.compareAndSet(i, v, v - 1))
338                              ++counts;
339                      }
340                  }
# Line 293 | Line 349 | public class AtomicIntegerArrayTest exte
349       * update a number of times equal to total count
350       */
351      public void testCountingInMultipleThreads() throws InterruptedException {
352 <        final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
353 <        for (int i = 0; i < SIZE; ++i)
354 <            ai.set(i, COUNTDOWN);
355 <        Counter c1 = new Counter(ai);
356 <        Counter c2 = new Counter(ai);
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);
364 >        assertEquals(c1.counts+c2.counts, SIZE * countdown);
365      }
366  
310
367      /**
368       * a deserialized serialized array holds same values
369       */
370      public void testSerialization() throws Exception {
371 <        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
372 <        for (int i = 0; i < SIZE; ++i)
373 <            l.set(i, -i);
374 <
375 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
376 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
377 <        out.writeObject(l);
378 <        out.close();
323 <
324 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
325 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
326 <        AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
327 <        for (int i = 0; i < SIZE; ++i) {
328 <            assertEquals(l.get(i), r.get(i));
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  
332
382      /**
383       * toString returns current value.
384       */
385      public void testToString() {
386 <        int[] a = { 17, 3, -42, 99, -7};
387 <        AtomicIntegerArray ai = new AtomicIntegerArray(a);
388 <        assertEquals(Arrays.toString(a), ai.toString());
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