--- jsr166/src/test/tck/AtomicBooleanTest.java 2003/09/14 20:42:40 1.3 +++ jsr166/src/test/tck/AtomicBooleanTest.java 2004/01/09 20:07:36 1.8 @@ -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.*; @@ -17,17 +18,26 @@ public class AtomicBooleanTest extends J return new TestSuite(AtomicBooleanTest.class); } - public void testConstructor(){ + /** + * constructor initializes to given value + */ + public void testConstructor() { AtomicBoolean ai = new AtomicBoolean(true); assertEquals(true,ai.get()); } - public void testConstructor2(){ + /** + * default constructed initializes to false + */ + public void testConstructor2() { AtomicBoolean ai = new AtomicBoolean(); assertEquals(false,ai.get()); } - public void testGetSet(){ + /** + * get returns the last value set + */ + public void testGetSet() { AtomicBoolean ai = new AtomicBoolean(true); assertEquals(true,ai.get()); ai.set(false); @@ -36,7 +46,11 @@ public class AtomicBooleanTest extends J assertEquals(true,ai.get()); } - public void testCompareAndSet(){ + + /** + * compareAndSet succeeds in changing value if equal to expected else fails + */ + public void testCompareAndSet() { AtomicBoolean ai = new AtomicBoolean(true); assertTrue(ai.compareAndSet(true,false)); assertEquals(false,ai.get()); @@ -48,7 +62,32 @@ public class AtomicBooleanTest extends J assertEquals(true,ai.get()); } - public void testWeakCompareAndSet(){ + /** + * 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); while(!ai.weakCompareAndSet(true,false)); assertEquals(false,ai.get()); @@ -58,7 +97,10 @@ public class AtomicBooleanTest extends J assertEquals(true,ai.get()); } - public void testGetAndSet(){ + /** + * getAndSet returns previous value and sets to given value + */ + public void testGetAndSet() { AtomicBoolean ai = new AtomicBoolean(true); assertEquals(true,ai.getAndSet(false)); assertEquals(false,ai.getAndSet(false)); @@ -66,6 +108,9 @@ public class AtomicBooleanTest extends J assertEquals(true,ai.get()); } + /** + * a deserialized serialized atomic holds same value + */ public void testSerialization() { AtomicBoolean l = new AtomicBoolean(); @@ -82,9 +127,18 @@ public class AtomicBooleanTest extends J assertEquals(l.get(), r.get()); } catch(Exception e){ e.printStackTrace(); - fail("unexpected exception"); + unexpectedException(); } } + /** + * 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)); + } }