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.26 by jsr166, Wed Aug 10 07:14:48 2011 UTC vs.
Revision 1.39 by dl, Tue Jan 26 13:33:05 2021 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
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());
18 >        main(suite(), args);
19      }
20      public static Test suite() {
21          return new TestSuite(AtomicIntegerArrayTest.class);
# Line 34 | Line 36 | public class AtomicIntegerArrayTest exte
36      public void testConstructor2NPE() {
37          try {
38              int[] a = null;
39 <            AtomicIntegerArray aa = new AtomicIntegerArray(a);
39 >            new AtomicIntegerArray(a);
40              shouldThrow();
41          } catch (NullPointerException success) {}
42      }
# Line 53 | Line 55 | public class AtomicIntegerArrayTest exte
55      /**
56       * get and set for out of bound indices throw IndexOutOfBoundsException
57       */
58 +    @SuppressWarnings("deprecation")
59      public void testIndexing() {
60          AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
61          for (int index : new int[] { -1, SIZE }) {
# Line 158 | Line 161 | public class AtomicIntegerArrayTest exte
161       * repeated weakCompareAndSet succeeds in changing value when equal
162       * to expected
163       */
164 +    @SuppressWarnings("deprecation")
165      public void testWeakCompareAndSet() {
166          AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
167          for (int i = 0; i < SIZE; i++) {
168              aa.set(i, 1);
169 <            while (!aa.weakCompareAndSet(i, 1, 2));
170 <            while (!aa.weakCompareAndSet(i, 2, -4));
169 >            do {} while (!aa.weakCompareAndSet(i, 1, 2));
170 >            do {} while (!aa.weakCompareAndSet(i, 2, -4));
171              assertEquals(-4, aa.get(i));
172 <            while (!aa.weakCompareAndSet(i, -4, 7));
172 >            do {} while (!aa.weakCompareAndSet(i, -4, 7));
173              assertEquals(7, aa.get(i));
174          }
175      }
# Line 272 | Line 276 | public class AtomicIntegerArrayTest exte
276          }
277      }
278  
275    static final int COUNTDOWN = 100000;
276
279      class Counter extends CheckedRunnable {
280          final AtomicIntegerArray aa;
281 <        volatile int counts;
281 >        int decs;
282          Counter(AtomicIntegerArray a) { aa = a; }
283          public void realRun() {
284              for (;;) {
# Line 286 | Line 288 | public class AtomicIntegerArrayTest exte
288                      assertTrue(v >= 0);
289                      if (v != 0) {
290                          done = false;
291 <                        if (aa.compareAndSet(i, v, v-1))
292 <                            ++counts;
291 >                        if (aa.compareAndSet(i, v, v - 1))
292 >                            decs++;
293                      }
294                  }
295                  if (done)
# Line 302 | Line 304 | public class AtomicIntegerArrayTest exte
304       */
305      public void testCountingInMultipleThreads() throws InterruptedException {
306          final AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
307 +        int countdown = 10000;
308          for (int i = 0; i < SIZE; i++)
309 <            aa.set(i, COUNTDOWN);
309 >            aa.set(i, countdown);
310          Counter c1 = new Counter(aa);
311          Counter c2 = new Counter(aa);
312 <        Thread t1 = new Thread(c1);
313 <        Thread t2 = new Thread(c2);
311 <        t1.start();
312 <        t2.start();
312 >        Thread t1 = newStartedThread(c1);
313 >        Thread t2 = newStartedThread(c2);
314          t1.join();
315          t2.join();
316 <        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
316 >        assertEquals(c1.decs + c2.decs, SIZE * countdown);
317      }
318  
319      /**
320 <     * a deserialized serialized array holds same values
320 >     * a deserialized/reserialized array holds same values in same order
321       */
322      public void testSerialization() throws Exception {
323          AtomicIntegerArray x = new AtomicIntegerArray(SIZE);
324          for (int i = 0; i < SIZE; i++)
325              x.set(i, -i);
326          AtomicIntegerArray y = serialClone(x);
327 <        assertTrue(x != y);
327 >        assertNotSame(x, y);
328          assertEquals(x.length(), y.length());
329          for (int i = 0; i < SIZE; i++) {
330              assertEquals(x.get(i), y.get(i));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines