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.4 by dl, Sat Sep 20 18:20:07 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.*;
11 < import java.io.*;
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  
21
24      /**
25 <     *
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      /**
34 <     *
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 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  
155      /**
156 <     *
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 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  
187      /**
188 <     *
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 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 <     *
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 <     *
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  
250      /**
251 <     *
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  
263      /**
264 <     *
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 <     *
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  
294      /**
295 <     *
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 <     *
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 <    
182 <    class Counter implements Runnable {
183 <        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 202 | Line 345 | public class AtomicIntegerArrayTest exte
345      }
346  
347      /**
348 <     *
348 >     * Multiple threads using same array of counters successfully
349 >     * update a number of times equal to total count
350       */
351 <    public void testCountingInMultipleThreads() {
352 <        try {
353 <            final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
354 <            for (int i = 0; i < SIZE; ++i)
355 <                ai.set(i, COUNTDOWN);
356 <            Counter c1 = new Counter(ai);
357 <            Counter c2 = new Counter(ai);
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);
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 <        catch(InterruptedException ie) {
381 <            unexpectedException();
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 <     *
424 >     * getAcquire returns the last value set
425       */
426 <    public void testSerialization() {
427 <        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
428 <        for (int i = 0; i < SIZE; ++i)
429 <            l.set(i, -i);
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 <        try {
439 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
440 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
441 <            out.writeObject(l);
442 <            out.close();
443 <
444 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
445 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
446 <            AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
447 <            for (int i = 0; i < SIZE; ++i) {
448 <                assertEquals(l.get(i), r.get(i));
449 <            }
248 <        } catch(Exception e){
249 <            e.printStackTrace();
250 <            unexpectedException();
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