--- jsr166/src/test/tck/AtomicIntegerArrayTest.java 2009/12/01 09:56:28 1.16 +++ jsr166/src/test/tck/AtomicIntegerArrayTest.java 2011/05/31 16:16:23 1.22 @@ -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 */ @@ -163,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); @@ -176,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); @@ -220,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); @@ -248,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); @@ -266,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)) @@ -307,29 +305,21 @@ public class AtomicIntegerArrayTest exte assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); } - /** * a deserialized serialized array holds same values */ public void testSerialization() throws Exception { - AtomicIntegerArray l = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) - l.set(i, -i); - - 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)); + 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. */