--- jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2003/09/14 20:42:40 1.2 +++ jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2003/09/25 11:02:41 1.4 @@ -11,51 +11,59 @@ import java.util.*; public class AtomicLongFieldUpdaterTest extends JSR166TestCase { volatile long x = 0; - long z; - int w; + int z; + long w; public static void main(String[] args){ junit.textui.TestRunner.run(suite()); } - - public static Test suite() { return new TestSuite(AtomicLongFieldUpdaterTest.class); } + /** + * Contruction with non-existent field throws RuntimeException + */ public void testConstructor(){ try{ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater (getClass(), "y"); - fail("Exception not thrown"); + shouldThrow(); } - catch (RuntimeException rt) {} } + /** + * construction with field not of given type throws RuntimeException + */ public void testConstructor2(){ try{ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater (getClass(), "z"); - fail("Exception not thrown"); + shouldThrow(); } - catch (RuntimeException rt) {} } + /** + * construction with non-volatile field throws RuntimeException + */ public void testConstructor3(){ try{ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater (getClass(), "w"); - fail("Exception not thrown"); + shouldThrow(); } catch (RuntimeException rt) {} } + /** + * get returns the last value set or assigned + */ public void testGetSet(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -66,6 +74,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(-3,a.get(this)); } + /** + * compareAndSet succeeds in changing value if equal to expected else fails + */ public void testCompareAndSet(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -78,6 +89,35 @@ public class AtomicLongFieldUpdaterTest assertEquals(7,a.get(this)); } + + /** + * compareAndSet in one thread enables another waiting for value + * to succeed + */ + public void testCompareAndSetInMultipleThreads() { + x = 1; + final AtomicLongFieldUpdatera = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + + Thread t = new Thread(new Runnable() { + public void run() { + while(!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield(); + }}); + try { + t.start(); + assertTrue(a.compareAndSet(this, 1, 2)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(a.get(this), 3); + } + catch(Exception e) { + unexpectedException(); + } + } + + /** + * repeated weakCompareAndSet succeeds in changing value when equal + * to expected + */ public void testWeakCompareAndSet(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -88,6 +128,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(7,a.get(this)); } + /** + * getAndSet returns previous value and sets to given value + */ public void testGetAndSet(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -96,6 +139,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(-10,a.getAndSet(this,1)); } + /** + * getAndAdd returns previous value and adds given value + */ public void testGetAndAdd(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -105,6 +151,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(-1,a.get(this)); } + /** + * getAndDecrement returns previous value and decrements + */ public void testGetAndDecrement(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -113,6 +162,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(-1,a.getAndDecrement(this)); } + /** + * getAndIncrement returns previous value and increments + */ public void testGetAndIncrement(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -125,6 +177,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(1,a.get(this)); } + /** + * addAndGet adds given value to current, and returns current value + */ public void testAddAndGet(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -134,6 +189,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(-1,a.get(this)); } + /** + * decrementAndGet decrements and returns current value + */ public void testDecrementAndGet(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1; @@ -143,6 +201,9 @@ public class AtomicLongFieldUpdaterTest assertEquals(-2,a.get(this)); } + /** + * incrementAndGet increments and returns current value + */ public void testIncrementAndGet(){ AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); x = 1;