--- jsr166/src/test/tck/AtomicLongArrayTest.java 2004/01/08 01:29:46 1.7 +++ jsr166/src/test/tck/AtomicLongArrayTest.java 2009/11/16 04:57:10 1.11 @@ -2,13 +2,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 - * 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.*; import java.util.concurrent.atomic.*; import java.io.*; +import java.util.*; public class AtomicLongArrayTest extends JSR166TestCase { public static void main (String[] args) { @@ -23,7 +24,7 @@ public class AtomicLongArrayTest extends */ 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)); } @@ -47,7 +48,7 @@ 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)); } @@ -58,19 +59,19 @@ public class AtomicLongArrayTest extends AtomicLongArray ai = new AtomicLongArray(SIZE); try { ai.get(SIZE); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success){ } try { ai.get(-1); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success){ } try { ai.set(SIZE, 0); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success){ } try { ai.set(-1, 0); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success){ } } @@ -78,7 +79,7 @@ public class AtomicLongArrayTest extends * get returns the last value set at index */ public void testGetSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.get(i)); @@ -90,10 +91,25 @@ public class AtomicLongArrayTest extends } /** + * get returns the last value lazySet at index by same thread + */ + public void testGetLazySet(){ + AtomicLongArray ai = new AtomicLongArray(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)); + } + } + + /** * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertTrue(ai.compareAndSet(i, 1,2)); @@ -115,7 +131,7 @@ public class AtomicLongArrayTest extends a.set(0, 1); Thread t = new Thread(new Runnable() { public void run() { - while(!a.compareAndSet(0, 2, 3)) Thread.yield(); + while (!a.compareAndSet(0, 2, 3)) Thread.yield(); }}); try { t.start(); @@ -124,23 +140,23 @@ public class AtomicLongArrayTest extends assertFalse(t.isAlive()); assertEquals(a.get(0), 3); } - catch(Exception e) { + catch (Exception e) { unexpectedException(); } } /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ public void testWeakCompareAndSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + 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)); } } @@ -149,7 +165,7 @@ public class AtomicLongArrayTest extends * getAndSet returns previous value and sets to given value at given index */ public void testGetAndSet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndSet(i,0)); @@ -162,7 +178,7 @@ public class AtomicLongArrayTest extends * getAndAdd returns previous value and adds given value */ public void testGetAndAdd(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndAdd(i,2)); @@ -176,7 +192,7 @@ public class AtomicLongArrayTest extends * getAndDecrement returns previous value and decrements */ public void testGetAndDecrement(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndDecrement(i)); @@ -189,7 +205,7 @@ public class AtomicLongArrayTest extends * getAndIncrement returns previous value and increments */ public void testGetAndIncrement(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndIncrement(i)); @@ -206,7 +222,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)); @@ -220,7 +236,7 @@ public class AtomicLongArrayTest extends * decrementAndGet decrements and returns current value */ public void testDecrementAndGet(){ - AtomicLongArray ai = new AtomicLongArray(SIZE); + AtomicLongArray ai = new AtomicLongArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(0,ai.decrementAndGet(i)); @@ -234,7 +250,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)); @@ -248,7 +264,7 @@ public class AtomicLongArrayTest extends } static final long COUNTDOWN = 100000; - + class Counter implements Runnable { final AtomicLongArray ai; volatile long counts; @@ -277,8 +293,8 @@ public class AtomicLongArrayTest extends */ public void testCountingInMultipleThreads() { try { - final AtomicLongArray ai = new AtomicLongArray(SIZE); - for (int i = 0; i < SIZE; ++i) + 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); @@ -290,7 +306,7 @@ public class AtomicLongArrayTest extends t2.join(); assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); } - catch(InterruptedException ie) { + catch (InterruptedException ie) { unexpectedException(); } } @@ -299,8 +315,8 @@ public class AtomicLongArrayTest extends * a deserialized serialized array holds same values */ public void testSerialization() { - AtomicLongArray l = new AtomicLongArray(SIZE); - for (int i = 0; i < SIZE; ++i) + AtomicLongArray l = new AtomicLongArray(SIZE); + for (int i = 0; i < SIZE; ++i) l.set(i, -i); try { @@ -315,10 +331,19 @@ public class AtomicLongArrayTest extends for (int i = 0; i < SIZE; ++i) { assertEquals(l.get(i), r.get(i)); } - } catch(Exception e){ + } catch (Exception e){ unexpectedException(); } } + /** + * toString returns current value. + */ + public void testToString() { + long[] a = { 17, 3, -42, 99, -7}; + AtomicLongArray ai = new AtomicLongArray(a); + assertEquals(Arrays.toString(a), ai.toString()); + } + }