--- jsr166/src/test/tck/AtomicIntegerTest.java 2003/09/25 11:02:41 1.5 +++ jsr166/src/test/tck/AtomicIntegerTest.java 2009/11/16 04:57:10 1.13 @@ -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.*; @@ -26,7 +27,7 @@ public class AtomicIntegerTest extends J } /** - * default constructed intializes to zero + * default constructed initializes to zero */ public void testConstructor2(){ AtomicInteger ai = new AtomicInteger(); @@ -43,7 +44,20 @@ public class AtomicIntegerTest extends J assertEquals(2,ai.get()); ai.set(-3); 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 @@ -67,7 +81,7 @@ public class AtomicIntegerTest extends J final AtomicInteger ai = new AtomicInteger(1); Thread t = new Thread(new Runnable() { public void run() { - while(!ai.compareAndSet(2, 3)) Thread.yield(); + while (!ai.compareAndSet(2, 3)) Thread.yield(); }}); try { t.start(); @@ -76,21 +90,21 @@ public class AtomicIntegerTest extends J assertFalse(t.isAlive()); assertEquals(ai.get(), 3); } - catch(Exception e) { + catch (Exception e) { unexpectedException(); } } /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ public void testWeakCompareAndSet(){ AtomicInteger ai = new AtomicInteger(1); - while(!ai.weakCompareAndSet(1,2)); - while(!ai.weakCompareAndSet(2,-4)); + while (!ai.weakCompareAndSet(1,2)); + while (!ai.weakCompareAndSet(2,-4)); assertEquals(-4,ai.get()); - while(!ai.weakCompareAndSet(-4,7)); + while (!ai.weakCompareAndSet(-4,7)); assertEquals(7,ai.get()); } @@ -192,10 +206,67 @@ public class AtomicIntegerTest extends J ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); AtomicInteger r = (AtomicInteger) in.readObject(); assertEquals(l.get(), r.get()); - } catch(Exception e){ + } catch (Exception e){ unexpectedException(); } } + /** + * 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()); + } + } + + }