--- jsr166/src/test/tck/AtomicBooleanTest.java 2003/09/20 18:20:07 1.4 +++ jsr166/src/test/tck/AtomicBooleanTest.java 2009/11/02 20:28:31 1.10 @@ -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 AtomicBooleanTest extends J } /** - * + * constructor initializes to given value */ public void testConstructor() { AtomicBoolean ai = new AtomicBoolean(true); @@ -26,7 +27,7 @@ public class AtomicBooleanTest extends J } /** - * + * default constructed initializes to false */ public void testConstructor2() { AtomicBoolean ai = new AtomicBoolean(); @@ -34,7 +35,7 @@ public class AtomicBooleanTest extends J } /** - * + * get returns the last value set */ public void testGetSet() { AtomicBoolean ai = new AtomicBoolean(true); @@ -43,10 +44,24 @@ public class AtomicBooleanTest extends J assertEquals(false,ai.get()); ai.set(true); assertEquals(true,ai.get()); - + + } + + /** + * get returns the last value lazySet in same thread + */ + public void testGetLazySet() { + AtomicBoolean ai = new AtomicBoolean(true); + assertEquals(true,ai.get()); + ai.lazySet(false); + assertEquals(false,ai.get()); + ai.lazySet(true); + assertEquals(true,ai.get()); + } + /** - * + * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet() { AtomicBoolean ai = new AtomicBoolean(true); @@ -61,7 +76,29 @@ public class AtomicBooleanTest extends J } /** - * + * compareAndSet in one thread enables another waiting for value + * to succeed + */ + public void testCompareAndSetInMultipleThreads() { + final AtomicBoolean ai = new AtomicBoolean(true); + Thread t = new Thread(new Runnable() { + public void run() { + while(!ai.compareAndSet(false, true)) Thread.yield(); + }}); + try { + t.start(); + assertTrue(ai.compareAndSet(true, false)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + } + catch(Exception e) { + unexpectedException(); + } + } + + /** + * repeated weakCompareAndSet succeeds in changing value when equal + * to expected */ public void testWeakCompareAndSet() { AtomicBoolean ai = new AtomicBoolean(true); @@ -74,7 +111,7 @@ public class AtomicBooleanTest extends J } /** - * + * getAndSet returns previous value and sets to given value */ public void testGetAndSet() { AtomicBoolean ai = new AtomicBoolean(true); @@ -85,7 +122,7 @@ public class AtomicBooleanTest extends J } /** - * + * a deserialized serialized atomic holds same value */ public void testSerialization() { AtomicBoolean l = new AtomicBoolean(); @@ -107,5 +144,14 @@ public class AtomicBooleanTest extends J } } + /** + * toString returns current value. + */ + public void testToString() { + AtomicBoolean ai = new AtomicBoolean(); + assertEquals(ai.toString(), Boolean.toString(false)); + ai.set(true); + assertEquals(ai.toString(), Boolean.toString(true)); + } }