--- jsr166/src/test/tck/AtomicIntegerArrayTest.java 2003/09/20 18:20:07 1.4 +++ jsr166/src/test/tck/AtomicIntegerArrayTest.java 2005/05/25 14:27:37 1.10 @@ -1,13 +1,15 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * 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. */ import junit.framework.*; import java.util.concurrent.atomic.*; import java.io.*; +import java.util.*; public class AtomicIntegerArrayTest extends JSR166TestCase { @@ -20,7 +22,7 @@ public class AtomicIntegerArrayTest exte /** - * + * constructor creates array of given size with all elements zero */ public void testConstructor() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -29,7 +31,54 @@ public class AtomicIntegerArrayTest exte } /** - * + * constructor with null array throws NPE + */ + public void testConstructor2NPE() { + try { + int[] a = null; + AtomicIntegerArray ai = new AtomicIntegerArray(a); + } catch (NullPointerException success) { + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * 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)); + } + + /** + * get and set for out of bound indices throw IndexOutOfBoundsException + */ + public void testIndexing(){ + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + try { + ai.get(SIZE); + } catch(IndexOutOfBoundsException success){ + } + try { + ai.get(-1); + } catch(IndexOutOfBoundsException success){ + } + try { + ai.set(SIZE, 0); + } catch(IndexOutOfBoundsException success){ + } + try { + ai.set(-1, 0); + } catch(IndexOutOfBoundsException success){ + } + } + + /** + * get returns the last value set at index */ public void testGetSet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -44,7 +93,22 @@ 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)); + } + } + + /** + * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -61,7 +125,31 @@ public class AtomicIntegerArrayTest exte } /** - * + * compareAndSet in one thread enables another waiting for value + * to succeed + */ + public void testCompareAndSetInMultipleThreads() { + 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(); + } + } + + /** + * repeated weakCompareAndSet succeeds in changing value when equal + * to expected */ public void testWeakCompareAndSet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -76,7 +164,7 @@ public class AtomicIntegerArrayTest exte } /** - * + * getAndSet returns previous value and sets to given value at given index */ public void testGetAndSet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -89,7 +177,7 @@ public class AtomicIntegerArrayTest exte } /** - * + * getAndAdd returns previous value and adds given value */ public void testGetAndAdd() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -103,7 +191,7 @@ public class AtomicIntegerArrayTest exte } /** - * + * getAndDecrement returns previous value and decrements */ public void testGetAndDecrement() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -116,7 +204,7 @@ public class AtomicIntegerArrayTest exte } /** - * + * getAndIncrement returns previous value and increments */ public void testGetAndIncrement() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -133,7 +221,7 @@ public class AtomicIntegerArrayTest exte } /** - * + * addAndGet adds given value to current, and returns current value */ public void testAddAndGet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -147,7 +235,7 @@ public class AtomicIntegerArrayTest exte } /** - * + * decrementAndGet decrements and returns current value */ public void testDecrementAndGet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -161,7 +249,7 @@ public class AtomicIntegerArrayTest exte } /** - * + * incrementAndGet increments and returns current value */ public void testIncrementAndGet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -202,7 +290,8 @@ 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 { @@ -226,7 +315,7 @@ public class AtomicIntegerArrayTest exte /** - * + * a deserialized serialized array holds same values */ public void testSerialization() { AtomicIntegerArray l = new AtomicIntegerArray(SIZE); @@ -251,4 +340,14 @@ public class AtomicIntegerArrayTest exte } } + + /** + * 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()); + } + }