--- jsr166/src/test/tck/AtomicLongArrayTest.java 2005/05/25 14:27:37 1.9 +++ jsr166/src/test/tck/AtomicLongArrayTest.java 2010/08/25 00:07:03 1.16 @@ -2,8 +2,8 @@ * 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 - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -12,8 +12,8 @@ import java.io.*; import java.util.*; public class AtomicLongArrayTest 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(AtomicLongArrayTest.class); @@ -22,9 +22,9 @@ public class AtomicLongArrayTest extends /** * constructor creates array of given size with all elements zero */ - public void testConstructor(){ + public void testConstructor() { AtomicLongArray ai = new AtomicLongArray(SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(0,ai.get(i)); } @@ -35,10 +35,8 @@ public class AtomicLongArrayTest extends try { long[] a = null; AtomicLongArray ai = new AtomicLongArray(a); - } catch (NullPointerException success) { - } catch (Exception ex) { - unexpectedException(); - } + shouldThrow(); + } catch (NullPointerException success) {} } /** @@ -48,38 +46,42 @@ public class AtomicLongArrayTest extends long[] a = { 17L, 3L, -42L, 99L, -7L}; AtomicLongArray ai = new AtomicLongArray(a); assertEquals(a.length, ai.length()); - for (int i = 0; i < a.length; ++i) + for (int i = 0; i < a.length; ++i) assertEquals(a[i], ai.get(i)); } /** * get and set for out of bound indices throw IndexOutOfBoundsException */ - public void testIndexing(){ + public void testIndexing() { AtomicLongArray ai = new AtomicLongArray(SIZE); try { ai.get(SIZE); - } catch(IndexOutOfBoundsException success){ + shouldThrow(); + } catch (IndexOutOfBoundsException success) { } try { ai.get(-1); - } catch(IndexOutOfBoundsException success){ + shouldThrow(); + } catch (IndexOutOfBoundsException success) { } try { ai.set(SIZE, 0); - } catch(IndexOutOfBoundsException success){ + shouldThrow(); + } catch (IndexOutOfBoundsException success) { } try { ai.set(-1, 0); - } catch(IndexOutOfBoundsException success){ + shouldThrow(); + } catch (IndexOutOfBoundsException success) { } } /** * get returns the last value set at index */ - public void testGetSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testGetSet() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.get(i)); @@ -93,8 +95,8 @@ public class AtomicLongArrayTest extends /** * get returns the last value lazySet at index by same thread */ - public void testGetLazySet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testGetLazySet() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.lazySet(i, 1); assertEquals(1,ai.get(i)); @@ -108,15 +110,15 @@ public class AtomicLongArrayTest extends /** * compareAndSet succeeds in changing value if equal to expected else fails */ - public void testCompareAndSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testCompareAndSet() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertTrue(ai.compareAndSet(i, 1,2)); 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)); } @@ -126,37 +128,34 @@ public class AtomicLongArrayTest extends * compareAndSet in one thread enables another waiting for value * to succeed */ - public void testCompareAndSetInMultipleThreads() { + public void testCompareAndSetInMultipleThreads() throws InterruptedException { final AtomicLongArray a = new AtomicLongArray(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); } /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ - public void testWeakCompareAndSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testWeakCompareAndSet() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); - while(!ai.weakCompareAndSet(i, 1,2)); - while(!ai.weakCompareAndSet(i, 2,-4)); + while (!ai.weakCompareAndSet(i, 1,2)); + while (!ai.weakCompareAndSet(i, 2,-4)); assertEquals(-4,ai.get(i)); - while(!ai.weakCompareAndSet(i, -4,7)); + while (!ai.weakCompareAndSet(i, -4,7)); assertEquals(7,ai.get(i)); } } @@ -164,8 +163,8 @@ public class AtomicLongArrayTest extends /** * getAndSet returns previous value and sets to given value at given index */ - public void testGetAndSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testGetAndSet() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndSet(i,0)); @@ -177,8 +176,8 @@ public class AtomicLongArrayTest extends /** * getAndAdd returns previous value and adds given value */ - public void testGetAndAdd(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testGetAndAdd() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndAdd(i,2)); @@ -191,8 +190,8 @@ public class AtomicLongArrayTest extends /** * getAndDecrement returns previous value and decrements */ - public void testGetAndDecrement(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testGetAndDecrement() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndDecrement(i)); @@ -204,8 +203,8 @@ public class AtomicLongArrayTest extends /** * getAndIncrement returns previous value and increments */ - public void testGetAndIncrement(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testGetAndIncrement() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndIncrement(i)); @@ -222,7 +221,7 @@ public class AtomicLongArrayTest extends * addAndGet adds given value to current, and returns current value */ public void testAddAndGet() { - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(3,ai.addAndGet(i,2)); @@ -235,8 +234,8 @@ public class AtomicLongArrayTest extends /** * decrementAndGet decrements and returns current value */ - public void testDecrementAndGet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + public void testDecrementAndGet() { + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(0,ai.decrementAndGet(i)); @@ -250,7 +249,7 @@ public class AtomicLongArrayTest extends * incrementAndGet increments and returns current value */ public void testIncrementAndGet() { - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(2,ai.incrementAndGet(i)); @@ -264,7 +263,7 @@ public class AtomicLongArrayTest extends } static final long COUNTDOWN = 100000; - + class Counter implements Runnable { final AtomicLongArray ai; volatile long counts; @@ -291,54 +290,45 @@ public class AtomicLongArrayTest extends * Multiple threads using same array of counters successfully * update a number of times equal to total count */ - public void testCountingInMultipleThreads() { - try { - final AtomicLongArray ai = new AtomicLongArray(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 AtomicLongArray ai = new AtomicLongArray(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() { - AtomicLongArray l = new AtomicLongArray(SIZE); - for (int i = 0; i < SIZE; ++i) + public void testSerialization() throws Exception { + AtomicLongArray l = new AtomicLongArray(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)); - AtomicLongArray r = (AtomicLongArray) in.readObject(); - for (int i = 0; i < SIZE; ++i) { - assertEquals(l.get(i), r.get(i)); - } - } catch(Exception e){ - unexpectedException(); + 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)); + AtomicLongArray r = (AtomicLongArray) in.readObject(); + for (int i = 0; i < SIZE; ++i) { + assertEquals(l.get(i), r.get(i)); } } /** * toString returns current value. - */ + */ public void testToString() { long[] a = { 17, 3, -42, 99, -7}; AtomicLongArray ai = new AtomicLongArray(a);