--- jsr166/src/test/tck/AtomicIntegerTest.java 2003/09/20 18:20:07 1.4 +++ jsr166/src/test/tck/AtomicIntegerTest.java 2005/10/07 14:58:35 1.11 @@ -1,8 +1,9 @@ /* - * 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.*; @@ -18,7 +19,7 @@ public class AtomicIntegerTest extends J } /** - * + * constructor initializes to given value */ public void testConstructor(){ AtomicInteger ai = new AtomicInteger(1); @@ -26,7 +27,7 @@ public class AtomicIntegerTest extends J } /** - * + * default constructed initializes to zero */ public void testConstructor2(){ AtomicInteger ai = new AtomicInteger(); @@ -34,7 +35,7 @@ public class AtomicIntegerTest extends J } /** - * + * get returns the last value set */ public void testGetSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -45,8 +46,21 @@ public class AtomicIntegerTest extends J assertEquals(-3,ai.get()); } + /** - * + * get returns the last value lazySet in same thread + */ + public void testGetLazySet(){ + AtomicInteger ai = new AtomicInteger(1); + assertEquals(1,ai.get()); + ai.lazySet(2); + assertEquals(2,ai.get()); + ai.lazySet(-3); + assertEquals(-3,ai.get()); + + } + /** + * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -60,7 +74,30 @@ public class AtomicIntegerTest extends J } /** - * + * compareAndSet in one thread enables another waiting for value + * to succeed + */ + public void testCompareAndSetInMultipleThreads() { + final AtomicInteger ai = new AtomicInteger(1); + Thread t = new Thread(new Runnable() { + public void run() { + while(!ai.compareAndSet(2, 3)) Thread.yield(); + }}); + try { + t.start(); + assertTrue(ai.compareAndSet(1, 2)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(ai.get(), 3); + } + catch(Exception e) { + unexpectedException(); + } + } + + /** + * repeated weakCompareAndSet succeeds in changing value when equal + * to expected */ public void testWeakCompareAndSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -72,7 +109,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndSet returns previous value and sets to given value */ public void testGetAndSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -82,7 +119,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndAdd returns previous value and adds given value */ public void testGetAndAdd(){ AtomicInteger ai = new AtomicInteger(1); @@ -93,7 +130,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndDecrement returns previous value and decrements */ public void testGetAndDecrement(){ AtomicInteger ai = new AtomicInteger(1); @@ -103,7 +140,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndIncrement returns previous value and increments */ public void testGetAndIncrement(){ AtomicInteger ai = new AtomicInteger(1); @@ -117,7 +154,7 @@ public class AtomicIntegerTest extends J } /** - * + * addAndGet adds given value to current, and returns current value */ public void testAddAndGet(){ AtomicInteger ai = new AtomicInteger(1); @@ -128,7 +165,7 @@ public class AtomicIntegerTest extends J } /** - * + * decrementAndGet decrements and returns current value */ public void testDecrementAndGet(){ AtomicInteger ai = new AtomicInteger(1); @@ -139,7 +176,7 @@ public class AtomicIntegerTest extends J } /** - * + * incrementAndGet increments and returns current value */ public void testIncrementAndGet(){ AtomicInteger ai = new AtomicInteger(1); @@ -153,7 +190,7 @@ public class AtomicIntegerTest extends J } /** - * + * a deserialized serialized atomic holds same value */ public void testSerialization() { AtomicInteger l = new AtomicInteger(); @@ -174,5 +211,62 @@ public class AtomicIntegerTest extends J } } + /** + * toString returns current value. + */ + public void testToString() { + AtomicInteger ai = new AtomicInteger(); + for (int i = -12; i < 6; ++i) { + ai.set(i); + assertEquals(ai.toString(), Integer.toString(i)); + } + } + + /** + * intValue returns current value. + */ + public void testIntValue() { + AtomicInteger ai = new AtomicInteger(); + for (int i = -12; i < 6; ++i) { + ai.set(i); + assertEquals(i, ai.intValue()); + } + } + + + /** + * longValue returns current value. + */ + public void testLongValue() { + AtomicInteger ai = new AtomicInteger(); + for (int i = -12; i < 6; ++i) { + ai.set(i); + assertEquals((long)i, ai.longValue()); + } + } + + /** + * floatValue returns current value. + */ + public void testFloatValue() { + AtomicInteger ai = new AtomicInteger(); + for (int i = -12; i < 6; ++i) { + ai.set(i); + assertEquals((float)i, ai.floatValue()); + } + } + + /** + * doubleValue returns current value. + */ + public void testDoubleValue() { + AtomicInteger ai = new AtomicInteger(); + for (int i = -12; i < 6; ++i) { + ai.set(i); + assertEquals((double)i, ai.doubleValue()); + } + } + + }