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

Comparing jsr166/src/test/tck/AtomicLongArrayTest.java (file contents):
Revision 1.12 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.22 by jsr166, Fri Jun 10 19:45:01 2011 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.*;
10 > import java.util.Arrays;
11 > import java.util.concurrent.atomic.AtomicLongArray;
12  
13   public class AtomicLongArrayTest extends JSR166TestCase {
14 <    public static void main (String[] args) {
15 <        junit.textui.TestRunner.run (suite());
14 >    public static void main(String[] args) {
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18          return new TestSuite(AtomicLongArrayTest.class);
# Line 35 | Line 34 | public class AtomicLongArrayTest extends
34          try {
35              long[] a = null;
36              AtomicLongArray ai = new AtomicLongArray(a);
37 <        } catch (NullPointerException success) {
38 <        } catch (Exception ex) {
40 <            unexpectedException();
41 <        }
37 >            shouldThrow();
38 >        } catch (NullPointerException success) {}
39      }
40  
41      /**
42       * constructor with array is of same size and has all elements
43       */
44      public void testConstructor2() {
45 <        long[] a = { 17L, 3L, -42L, 99L, -7L};
45 >        long[] a = { 17L, 3L, -42L, 99L, -7L };
46          AtomicLongArray ai = new AtomicLongArray(a);
47          assertEquals(a.length, ai.length());
48          for (int i = 0; i < a.length; ++i)
# Line 59 | Line 56 | public class AtomicLongArrayTest extends
56          AtomicLongArray ai = new AtomicLongArray(SIZE);
57          try {
58              ai.get(SIZE);
59 +            shouldThrow();
60          } catch (IndexOutOfBoundsException success) {
61          }
62          try {
63              ai.get(-1);
64 +            shouldThrow();
65          } catch (IndexOutOfBoundsException success) {
66          }
67          try {
68              ai.set(SIZE, 0);
69 +            shouldThrow();
70          } catch (IndexOutOfBoundsException success) {
71          }
72          try {
73              ai.set(-1, 0);
74 +            shouldThrow();
75          } catch (IndexOutOfBoundsException success) {
76          }
77      }
# Line 116 | Line 117 | public class AtomicLongArrayTest extends
117              assertTrue(ai.compareAndSet(i, 2,-4));
118              assertEquals(-4,ai.get(i));
119              assertFalse(ai.compareAndSet(i, -5,7));
120 <            assertFalse((7 == ai.get(i)));
120 >            assertEquals(-4,ai.get(i));
121              assertTrue(ai.compareAndSet(i, -4,7));
122              assertEquals(7,ai.get(i));
123          }
# Line 126 | Line 127 | public class AtomicLongArrayTest extends
127       * compareAndSet in one thread enables another waiting for value
128       * to succeed
129       */
130 <    public void testCompareAndSetInMultipleThreads() {
130 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
131          final AtomicLongArray a = new AtomicLongArray(1);
132          a.set(0, 1);
133 <        Thread t = new Thread(new Runnable() {
134 <                public void run() {
135 <                    while (!a.compareAndSet(0, 2, 3)) Thread.yield();
136 <                }});
137 <        try {
138 <            t.start();
139 <            assertTrue(a.compareAndSet(0, 1, 2));
140 <            t.join(LONG_DELAY_MS);
141 <            assertFalse(t.isAlive());
142 <            assertEquals(a.get(0), 3);
143 <        }
143 <        catch (Exception e) {
144 <            unexpectedException();
145 <        }
133 >        Thread t = new Thread(new CheckedRunnable() {
134 >            public void realRun() {
135 >                while (!a.compareAndSet(0, 2, 3))
136 >                    Thread.yield();
137 >            }});
138 >
139 >        t.start();
140 >        assertTrue(a.compareAndSet(0, 1, 2));
141 >        t.join(LONG_DELAY_MS);
142 >        assertFalse(t.isAlive());
143 >        assertEquals(a.get(0), 3);
144      }
145  
146      /**
# Line 162 | Line 160 | public class AtomicLongArrayTest extends
160      }
161  
162      /**
163 <     *  getAndSet returns previous value and sets to given value at given index
163 >     * getAndSet returns previous value and sets to given value at given index
164       */
165      public void testGetAndSet() {
166          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 175 | Line 173 | public class AtomicLongArrayTest extends
173      }
174  
175      /**
176 <     *  getAndAdd returns previous value and adds given value
176 >     * getAndAdd returns previous value and adds given value
177       */
178      public void testGetAndAdd() {
179          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 219 | Line 217 | public class AtomicLongArrayTest extends
217      }
218  
219      /**
220 <     *  addAndGet adds given value to current, and returns current value
220 >     * addAndGet adds given value to current, and returns current value
221       */
222      public void testAddAndGet() {
223          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 265 | Line 263 | public class AtomicLongArrayTest extends
263  
264      static final long COUNTDOWN = 100000;
265  
266 <    class Counter implements Runnable {
266 >    class Counter extends CheckedRunnable {
267          final AtomicLongArray ai;
268          volatile long counts;
269          Counter(AtomicLongArray a) { ai = a; }
270 <        public void run() {
270 >        public void realRun() {
271              for (;;) {
272                  boolean done = true;
273                  for (int i = 0; i < ai.length(); ++i) {
274                      long v = ai.get(i);
275 <                    threadAssertTrue(v >= 0);
275 >                    assertTrue(v >= 0);
276                      if (v != 0) {
277                          done = false;
278                          if (ai.compareAndSet(i, v, v-1))
# Line 291 | Line 289 | public class AtomicLongArrayTest extends
289       * Multiple threads using same array of counters successfully
290       * update a number of times equal to total count
291       */
292 <    public void testCountingInMultipleThreads() {
293 <        try {
294 <            final AtomicLongArray ai = new AtomicLongArray(SIZE);
295 <            for (int i = 0; i < SIZE; ++i)
296 <                ai.set(i, COUNTDOWN);
297 <            Counter c1 = new Counter(ai);
298 <            Counter c2 = new Counter(ai);
299 <            Thread t1 = new Thread(c1);
300 <            Thread t2 = new Thread(c2);
301 <            t1.start();
302 <            t2.start();
303 <            t1.join();
304 <            t2.join();
307 <            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
308 <        }
309 <        catch (InterruptedException ie) {
310 <            unexpectedException();
311 <        }
292 >    public void testCountingInMultipleThreads() throws InterruptedException {
293 >        final AtomicLongArray ai = new AtomicLongArray(SIZE);
294 >        for (int i = 0; i < SIZE; ++i)
295 >            ai.set(i, COUNTDOWN);
296 >        Counter c1 = new Counter(ai);
297 >        Counter c2 = new Counter(ai);
298 >        Thread t1 = new Thread(c1);
299 >        Thread t2 = new Thread(c2);
300 >        t1.start();
301 >        t2.start();
302 >        t1.join();
303 >        t2.join();
304 >        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
305      }
306  
307      /**
308       * a deserialized serialized array holds same values
309       */
310 <    public void testSerialization() {
311 <        AtomicLongArray l = new AtomicLongArray(SIZE);
310 >    public void testSerialization() throws Exception {
311 >        AtomicLongArray x = new AtomicLongArray(SIZE);
312          for (int i = 0; i < SIZE; ++i)
313 <            l.set(i, -i);
314 <
315 <        try {
316 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
317 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
318 <            out.writeObject(l);
326 <            out.close();
327 <
328 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
329 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
330 <            AtomicLongArray r = (AtomicLongArray) in.readObject();
331 <            for (int i = 0; i < SIZE; ++i) {
332 <                assertEquals(l.get(i), r.get(i));
333 <            }
334 <        } catch (Exception e) {
335 <            unexpectedException();
313 >            x.set(i, -i);
314 >        AtomicLongArray y = serialClone(x);
315 >        assertTrue(x != y);
316 >        assertEquals(x.length(), y.length());
317 >        for (int i = 0; i < SIZE; ++i) {
318 >            assertEquals(x.get(i), y.get(i));
319          }
320      }
321  
# Line 340 | Line 323 | public class AtomicLongArrayTest extends
323       * toString returns current value.
324       */
325      public void testToString() {
326 <        long[] a = { 17, 3, -42, 99, -7};
326 >        long[] a = { 17, 3, -42, 99, -7 };
327          AtomicLongArray ai = new AtomicLongArray(a);
328          assertEquals(Arrays.toString(a), ai.toString());
329      }
330  
348
331   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines