--- jsr166/src/test/tck/AtomicIntegerArrayTest.java 2003/09/14 20:42:40 1.3 +++ jsr166/src/test/tck/AtomicIntegerArrayTest.java 2004/01/10 01:41:59 1.9 @@ -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 { @@ -19,13 +21,66 @@ public class AtomicIntegerArrayTest exte } - public void testConstructor(){ + /** + * 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)); } - public void testGetSet(){ + /** + * 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); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); @@ -37,7 +92,10 @@ public class AtomicIntegerArrayTest exte } } - public void testCompareAndSet(){ + /** + * 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); @@ -51,7 +109,34 @@ public class AtomicIntegerArrayTest exte } } - public void testWeakCompareAndSet(){ + /** + * 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); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); @@ -63,7 +148,10 @@ public class AtomicIntegerArrayTest exte } } - public void testGetAndSet(){ + /** + * 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); @@ -73,7 +161,10 @@ public class AtomicIntegerArrayTest exte } } - public void testGetAndAdd(){ + /** + * 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); @@ -84,7 +175,10 @@ public class AtomicIntegerArrayTest exte } } - public void testGetAndDecrement(){ + /** + * 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); @@ -94,7 +188,10 @@ public class AtomicIntegerArrayTest exte } } - public void testGetAndIncrement(){ + /** + * 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); @@ -108,6 +205,9 @@ public class AtomicIntegerArrayTest exte } } + /** + * 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) { @@ -119,7 +219,10 @@ public class AtomicIntegerArrayTest exte } } - public void testDecrementAndGet(){ + /** + * 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); @@ -130,6 +233,9 @@ public class AtomicIntegerArrayTest exte } } + /** + * incrementAndGet increments and returns current value + */ public void testIncrementAndGet() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { @@ -168,6 +274,10 @@ 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 { final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); @@ -184,11 +294,14 @@ public class AtomicIntegerArrayTest exte assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); } catch(InterruptedException ie) { - fail("unexpected exception"); + unexpectedException(); } } + /** + * a deserialized serialized array holds same values + */ public void testSerialization() { AtomicIntegerArray l = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) @@ -208,8 +321,18 @@ public class AtomicIntegerArrayTest exte } } catch(Exception e){ e.printStackTrace(); - fail("unexpected exception"); + unexpectedException(); } } + + /** + * 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()); + } + }