--- jsr166/src/test/tck/AtomicIntegerArrayTest.java 2010/08/25 00:07:03 1.17 +++ jsr166/src/test/tck/AtomicIntegerArrayTest.java 2013/05/30 03:28:55 1.27 @@ -1,15 +1,14 @@ /* * 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 { @@ -20,14 +19,13 @@ public class AtomicIntegerArrayTest exte return new TestSuite(AtomicIntegerArrayTest.class); } - /** * constructor creates array of given size with all elements zero */ public void testConstructor() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) - assertEquals(0,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) + assertEquals(0, aa.get(i)); } /** @@ -36,7 +34,7 @@ public class AtomicIntegerArrayTest exte public void testConstructor2NPE() { try { int[] a = null; - AtomicIntegerArray ai = new AtomicIntegerArray(a); + AtomicIntegerArray aa = new AtomicIntegerArray(a); shouldThrow(); } catch (NullPointerException success) {} } @@ -45,37 +43,47 @@ public class AtomicIntegerArrayTest exte * constructor with array is of same size and has all elements */ public void testConstructor2() { - 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) - assertEquals(a[i], ai.get(i)); + int[] a = { 17, 3, -42, 99, -7 }; + AtomicIntegerArray aa = new AtomicIntegerArray(a); + assertEquals(a.length, aa.length()); + for (int i = 0; i < a.length; i++) + assertEquals(a[i], aa.get(i)); } /** * get and set for out of bound indices throw IndexOutOfBoundsException */ public void testIndexing() { - 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) { + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int index : new int[] { -1, SIZE }) { + try { + aa.get(index); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + try { + aa.set(index, 1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + try { + aa.lazySet(index, 1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + try { + aa.compareAndSet(index, 1, 2); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + try { + aa.weakCompareAndSet(index, 1, 2); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + try { + aa.getAndAdd(index, 1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} + try { + aa.addAndGet(index, 1); + shouldThrow(); + } catch (IndexOutOfBoundsException success) {} } } @@ -83,14 +91,14 @@ public class AtomicIntegerArrayTest exte * get returns the last value set at index */ public void testGetSet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(1,ai.get(i)); - ai.set(i, 2); - assertEquals(2,ai.get(i)); - ai.set(i, -3); - assertEquals(-3,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(1, aa.get(i)); + aa.set(i, 2); + assertEquals(2, aa.get(i)); + aa.set(i, -3); + assertEquals(-3, aa.get(i)); } } @@ -98,14 +106,14 @@ public class AtomicIntegerArrayTest exte * get returns the last value lazySet at index by same thread */ public void testGetLazySet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.lazySet(i, 1); - assertEquals(1,ai.get(i)); - ai.lazySet(i, 2); - assertEquals(2,ai.get(i)); - ai.lazySet(i, -3); - assertEquals(-3,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.lazySet(i, 1); + assertEquals(1, aa.get(i)); + aa.lazySet(i, 2); + assertEquals(2, aa.get(i)); + aa.lazySet(i, -3); + assertEquals(-3, aa.get(i)); } } @@ -113,16 +121,16 @@ public class AtomicIntegerArrayTest exte * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet() { - AtomicIntegerArray ai = new AtomicIntegerArray(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)); - assertEquals(-4,ai.get(i)); - assertTrue(ai.compareAndSet(i, -4,7)); - assertEquals(7,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertTrue(aa.compareAndSet(i, 1, 2)); + assertTrue(aa.compareAndSet(i, 2, -4)); + assertEquals(-4, aa.get(i)); + assertFalse(aa.compareAndSet(i, -5, 7)); + assertEquals(-4, aa.get(i)); + assertTrue(aa.compareAndSet(i, -4, 7)); + assertEquals(7, aa.get(i)); } } @@ -143,7 +151,7 @@ public class AtomicIntegerArrayTest exte assertTrue(a.compareAndSet(0, 1, 2)); t.join(LONG_DELAY_MS); assertFalse(t.isAlive()); - assertEquals(a.get(0), 3); + assertEquals(3, a.get(0)); } /** @@ -151,41 +159,41 @@ public class AtomicIntegerArrayTest exte * to expected */ public void testWeakCompareAndSet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - while (!ai.weakCompareAndSet(i, 1,2)); - while (!ai.weakCompareAndSet(i, 2,-4)); - assertEquals(-4,ai.get(i)); - while (!ai.weakCompareAndSet(i, -4,7)); - assertEquals(7,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + while (!aa.weakCompareAndSet(i, 1, 2)); + while (!aa.weakCompareAndSet(i, 2, -4)); + assertEquals(-4, aa.get(i)); + while (!aa.weakCompareAndSet(i, -4, 7)); + assertEquals(7, aa.get(i)); } } /** - * 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); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(1,ai.getAndSet(i,0)); - assertEquals(0,ai.getAndSet(i,-10)); - assertEquals(-10,ai.getAndSet(i,1)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(1, aa.getAndSet(i, 0)); + assertEquals(0, aa.getAndSet(i, -10)); + assertEquals(-10, aa.getAndSet(i, 1)); } } /** - * getAndAdd returns previous value and adds given value + * getAndAdd returns previous value and adds given value */ public void testGetAndAdd() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(1,ai.getAndAdd(i,2)); - assertEquals(3,ai.get(i)); - assertEquals(3,ai.getAndAdd(i,-4)); - assertEquals(-1,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(1, aa.getAndAdd(i, 2)); + assertEquals(3, aa.get(i)); + assertEquals(3, aa.getAndAdd(i, -4)); + assertEquals(-1, aa.get(i)); } } @@ -193,12 +201,12 @@ public class AtomicIntegerArrayTest exte * getAndDecrement returns previous value and decrements */ public void testGetAndDecrement() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(1,ai.getAndDecrement(i)); - assertEquals(0,ai.getAndDecrement(i)); - assertEquals(-1,ai.getAndDecrement(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(1, aa.getAndDecrement(i)); + assertEquals(0, aa.getAndDecrement(i)); + assertEquals(-1, aa.getAndDecrement(i)); } } @@ -206,30 +214,30 @@ public class AtomicIntegerArrayTest exte * getAndIncrement returns previous value and increments */ public void testGetAndIncrement() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(1,ai.getAndIncrement(i)); - assertEquals(2,ai.get(i)); - ai.set(i,-2); - assertEquals(-2,ai.getAndIncrement(i)); - assertEquals(-1,ai.getAndIncrement(i)); - assertEquals(0,ai.getAndIncrement(i)); - assertEquals(1,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(1, aa.getAndIncrement(i)); + assertEquals(2, aa.get(i)); + aa.set(i, -2); + assertEquals(-2, aa.getAndIncrement(i)); + assertEquals(-1, aa.getAndIncrement(i)); + assertEquals(0, aa.getAndIncrement(i)); + assertEquals(1, aa.get(i)); } } /** - * 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); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(3,ai.addAndGet(i,2)); - assertEquals(3,ai.get(i)); - assertEquals(-1,ai.addAndGet(i,-4)); - assertEquals(-1,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(3, aa.addAndGet(i, 2)); + assertEquals(3, aa.get(i)); + assertEquals(-1, aa.addAndGet(i, -4)); + assertEquals(-1, aa.get(i)); } } @@ -237,48 +245,48 @@ public class AtomicIntegerArrayTest exte * decrementAndGet decrements and returns current value */ public void testDecrementAndGet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(0,ai.decrementAndGet(i)); - assertEquals(-1,ai.decrementAndGet(i)); - assertEquals(-2,ai.decrementAndGet(i)); - assertEquals(-2,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(0, aa.decrementAndGet(i)); + assertEquals(-1, aa.decrementAndGet(i)); + assertEquals(-2, aa.decrementAndGet(i)); + assertEquals(-2, aa.get(i)); } } /** - * incrementAndGet increments and returns current value + * incrementAndGet increments and returns current value */ public void testIncrementAndGet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) { - ai.set(i, 1); - assertEquals(2,ai.incrementAndGet(i)); - assertEquals(2,ai.get(i)); - ai.set(i, -2); - assertEquals(-1,ai.incrementAndGet(i)); - assertEquals(0,ai.incrementAndGet(i)); - assertEquals(1,ai.incrementAndGet(i)); - assertEquals(1,ai.get(i)); + AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) { + aa.set(i, 1); + assertEquals(2, aa.incrementAndGet(i)); + assertEquals(2, aa.get(i)); + aa.set(i, -2); + assertEquals(-1, aa.incrementAndGet(i)); + assertEquals(0, aa.incrementAndGet(i)); + assertEquals(1, aa.incrementAndGet(i)); + assertEquals(1, aa.get(i)); } } static final int COUNTDOWN = 100000; - class Counter implements Runnable { - final AtomicIntegerArray ai; + class Counter extends CheckedRunnable { + final AtomicIntegerArray aa; volatile int counts; - Counter(AtomicIntegerArray a) { ai = a; } - public void run() { + Counter(AtomicIntegerArray a) { aa = a; } + public void realRun() { for (;;) { boolean done = true; - for (int i = 0; i < ai.length(); ++i) { - int v = ai.get(i); - threadAssertTrue(v >= 0); + for (int i = 0; i < aa.length(); i++) { + int v = aa.get(i); + assertTrue(v >= 0); if (v != 0) { done = false; - if (ai.compareAndSet(i, v, v-1)) + if (aa.compareAndSet(i, v, v-1)) ++counts; } } @@ -293,11 +301,11 @@ public class AtomicIntegerArrayTest exte * update a number of times equal to total count */ 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); + final AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; i++) + aa.set(i, COUNTDOWN); + Counter c1 = new Counter(aa); + Counter c2 = new Counter(aa); Thread t1 = new Thread(c1); Thread t2 = new Thread(c2); t1.start(); @@ -307,36 +315,28 @@ 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); + assertNotSame(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}; - AtomicIntegerArray ai = new AtomicIntegerArray(a); - assertEquals(Arrays.toString(a), ai.toString()); + int[] a = { 17, 3, -42, 99, -7 }; + AtomicIntegerArray aa = new AtomicIntegerArray(a); + assertEquals(Arrays.toString(a), aa.toString()); } }