--- jsr166/src/test/tck/AtomicIntegerArrayTest.java 2009/11/16 05:30:07 1.13 +++ jsr166/src/test/tck/AtomicIntegerArrayTest.java 2011/06/10 19:45:01 1.23 @@ -1,26 +1,24 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ import junit.framework.*; -import java.util.concurrent.atomic.*; -import java.io.*; -import java.util.*; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicIntegerArray; public class AtomicIntegerArrayTest extends JSR166TestCase { - public static void main (String[] args) { - junit.textui.TestRunner.run (suite()); + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(AtomicIntegerArrayTest.class); } - /** * constructor creates array of given size with all elements zero */ @@ -37,17 +35,15 @@ public class AtomicIntegerArrayTest exte try { int[] a = null; AtomicIntegerArray ai = new AtomicIntegerArray(a); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); - } + shouldThrow(); + } catch (NullPointerException success) {} } /** * constructor with array is of same size and has all elements */ public void testConstructor2() { - int[] a = { 17, 3, -42, 99, -7}; + int[] a = { 17, 3, -42, 99, -7 }; AtomicIntegerArray ai = new AtomicIntegerArray(a); assertEquals(a.length, ai.length()); for (int i = 0; i < a.length; ++i) @@ -61,18 +57,22 @@ public class AtomicIntegerArrayTest exte AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); try { ai.get(SIZE); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } try { ai.get(-1); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } try { ai.set(SIZE, 0); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } try { ai.set(-1, 0); + shouldThrow(); } catch (IndexOutOfBoundsException success) { } } @@ -118,7 +118,7 @@ public class AtomicIntegerArrayTest exte assertTrue(ai.compareAndSet(i, 2,-4)); assertEquals(-4,ai.get(i)); assertFalse(ai.compareAndSet(i, -5,7)); - assertFalse((7 == ai.get(i))); + assertEquals(-4,ai.get(i)); assertTrue(ai.compareAndSet(i, -4,7)); assertEquals(7,ai.get(i)); } @@ -128,23 +128,20 @@ public class AtomicIntegerArrayTest exte * compareAndSet in one thread enables another waiting for value * to succeed */ - public void testCompareAndSetInMultipleThreads() { + public void testCompareAndSetInMultipleThreads() throws Exception { final AtomicIntegerArray a = new AtomicIntegerArray(1); a.set(0, 1); - Thread t = new Thread(new Runnable() { - public void run() { - while (!a.compareAndSet(0, 2, 3)) Thread.yield(); - }}); - try { - t.start(); - assertTrue(a.compareAndSet(0, 1, 2)); - t.join(LONG_DELAY_MS); - assertFalse(t.isAlive()); - assertEquals(a.get(0), 3); - } - catch (Exception e) { - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + while (!a.compareAndSet(0, 2, 3)) + Thread.yield(); + }}); + + t.start(); + assertTrue(a.compareAndSet(0, 1, 2)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(a.get(0), 3); } /** @@ -164,7 +161,7 @@ public class AtomicIntegerArrayTest exte } /** - * getAndSet returns previous value and sets to given value at given index + * getAndSet returns previous value and sets to given value at given index */ public void testGetAndSet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -177,7 +174,7 @@ public class AtomicIntegerArrayTest exte } /** - * getAndAdd returns previous value and adds given value + * getAndAdd returns previous value and adds given value */ public void testGetAndAdd() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -221,7 +218,7 @@ public class AtomicIntegerArrayTest exte } /** - * addAndGet adds given value to current, and returns current value + * addAndGet adds given value to current, and returns current value */ public void testAddAndGet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -249,7 +246,7 @@ public class AtomicIntegerArrayTest exte } /** - * incrementAndGet increments and returns current value + * incrementAndGet increments and returns current value */ public void testIncrementAndGet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -267,16 +264,16 @@ public class AtomicIntegerArrayTest exte static final int COUNTDOWN = 100000; - class Counter implements Runnable { + class Counter extends CheckedRunnable { final AtomicIntegerArray ai; volatile int counts; Counter(AtomicIntegerArray a) { ai = a; } - public void run() { + public void realRun() { for (;;) { boolean done = true; for (int i = 0; i < ai.length(); ++i) { int v = ai.get(i); - threadAssertTrue(v >= 0); + assertTrue(v >= 0); if (v != 0) { done = false; if (ai.compareAndSet(i, v, v-1)) @@ -293,59 +290,41 @@ public class AtomicIntegerArrayTest exte * Multiple threads using same array of counters successfully * update a number of times equal to total count */ - public void testCountingInMultipleThreads() { - try { - final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) - ai.set(i, COUNTDOWN); - Counter c1 = new Counter(ai); - Counter c2 = new Counter(ai); - Thread t1 = new Thread(c1); - Thread t2 = new Thread(c2); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testCountingInMultipleThreads() throws InterruptedException { + final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; ++i) + ai.set(i, COUNTDOWN); + Counter c1 = new Counter(ai); + Counter c2 = new Counter(ai); + Thread t1 = new Thread(c1); + Thread t2 = new Thread(c2); + t1.start(); + t2.start(); + t1.join(); + t2.join(); + assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); } - /** * a deserialized serialized array holds same values */ - public void testSerialization() { - AtomicIntegerArray l = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) - l.set(i, -i); - - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(l); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); - for (int i = 0; i < SIZE; ++i) { - assertEquals(l.get(i), r.get(i)); - } - } catch (Exception e) { - e.printStackTrace(); - unexpectedException(); + public void testSerialization() throws Exception { + AtomicIntegerArray x = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) + x.set(i, -i); + AtomicIntegerArray y = serialClone(x); + assertTrue(x != y); + assertEquals(x.length(), y.length()); + for (int i = 0; i < SIZE; i++) { + assertEquals(x.get(i), y.get(i)); } } - /** * toString returns current value. */ public void testToString() { - int[] a = { 17, 3, -42, 99, -7}; + int[] a = { 17, 3, -42, 99, -7 }; AtomicIntegerArray ai = new AtomicIntegerArray(a); assertEquals(Arrays.toString(a), ai.toString()); }