--- jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2003/09/25 11:02:41 1.4 +++ jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java 2009/11/17 06:58:50 1.14 @@ -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 java.util.concurrent.atomic.*; @@ -14,7 +15,7 @@ public class AtomicLongFieldUpdaterTest int z; long w; - public static void main(String[] args){ + public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { @@ -22,13 +23,13 @@ public class AtomicLongFieldUpdaterTest } /** - * Contruction with non-existent field throws RuntimeException + * Construction with non-existent field throws RuntimeException */ - public void testConstructor(){ - try{ - AtomicLongFieldUpdater + public void testConstructor() { + try { + AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater - (getClass(), "y"); + (AtomicLongFieldUpdaterTest.class, "y"); shouldThrow(); } catch (RuntimeException rt) {} @@ -37,11 +38,11 @@ public class AtomicLongFieldUpdaterTest /** * construction with field not of given type throws RuntimeException */ - public void testConstructor2(){ - try{ - AtomicLongFieldUpdater + public void testConstructor2() { + try { + AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater - (getClass(), "z"); + (AtomicLongFieldUpdaterTest.class, "z"); shouldThrow(); } catch (RuntimeException rt) {} @@ -50,11 +51,11 @@ public class AtomicLongFieldUpdaterTest /** * construction with non-volatile field throws RuntimeException */ - public void testConstructor3(){ - try{ - AtomicLongFieldUpdater + public void testConstructor3() { + try { + AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater - (getClass(), "w"); + (AtomicLongFieldUpdaterTest.class, "w"); shouldThrow(); } @@ -64,21 +65,50 @@ public class AtomicLongFieldUpdaterTest /** * get returns the last value set or assigned */ - public void testGetSet(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testGetSet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(1,a.get(this)); a.set(this,2); assertEquals(2,a.get(this)); a.set(this,-3); assertEquals(-3,a.get(this)); - } + + /** + * get returns the last value lazySet by same thread + */ + public void testGetLazySet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } + x = 1; + assertEquals(1,a.get(this)); + a.lazySet(this,2); + assertEquals(2,a.get(this)); + a.lazySet(this,-3); + 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"); + public void testCompareAndSet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertTrue(a.compareAndSet(this,1,2)); assertTrue(a.compareAndSet(this,2,-4)); @@ -94,45 +124,57 @@ public class AtomicLongFieldUpdaterTest * compareAndSet in one thread enables another waiting for value * to succeed */ - public void testCompareAndSetInMultipleThreads() { + public void testCompareAndSetInMultipleThreads() throws Exception { 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(); + final AtomicLongFieldUpdatera; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; } + + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) + Thread.yield(); + }}); + + t.start(); + assertTrue(a.compareAndSet(this, 1, 2)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(a.get(this), 3); } /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ - public void testWeakCompareAndSet(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testWeakCompareAndSet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; - while(!a.weakCompareAndSet(this,1,2)); - while(!a.weakCompareAndSet(this,2,-4)); + while (!a.weakCompareAndSet(this,1,2)); + while (!a.weakCompareAndSet(this,2,-4)); assertEquals(-4,a.get(this)); - while(!a.weakCompareAndSet(this,-4,7)); + while (!a.weakCompareAndSet(this,-4,7)); assertEquals(7,a.get(this)); } /** * getAndSet returns previous value and sets to given value */ - public void testGetAndSet(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testGetAndSet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(1,a.getAndSet(this, 0)); assertEquals(0,a.getAndSet(this,-10)); @@ -142,8 +184,13 @@ public class AtomicLongFieldUpdaterTest /** * getAndAdd returns previous value and adds given value */ - public void testGetAndAdd(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testGetAndAdd() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(1,a.getAndAdd(this,2)); assertEquals(3,a.get(this)); @@ -154,8 +201,13 @@ public class AtomicLongFieldUpdaterTest /** * getAndDecrement returns previous value and decrements */ - public void testGetAndDecrement(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testGetAndDecrement() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(1,a.getAndDecrement(this)); assertEquals(0,a.getAndDecrement(this)); @@ -165,8 +217,13 @@ public class AtomicLongFieldUpdaterTest /** * getAndIncrement returns previous value and increments */ - public void testGetAndIncrement(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testGetAndIncrement() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(1,a.getAndIncrement(this)); assertEquals(2,a.get(this)); @@ -180,8 +237,13 @@ public class AtomicLongFieldUpdaterTest /** * addAndGet adds given value to current, and returns current value */ - public void testAddAndGet(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testAddAndGet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(3,a.addAndGet(this,2)); assertEquals(3,a.get(this)); @@ -192,8 +254,13 @@ public class AtomicLongFieldUpdaterTest /** * decrementAndGet decrements and returns current value */ - public void testDecrementAndGet(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testDecrementAndGet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(0,a.decrementAndGet(this)); assertEquals(-1,a.decrementAndGet(this)); @@ -204,8 +271,13 @@ public class AtomicLongFieldUpdaterTest /** * incrementAndGet increments and returns current value */ - public void testIncrementAndGet(){ - AtomicLongFieldUpdater a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); + public void testIncrementAndGet() { + AtomicLongFieldUpdater a; + try { + a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); + } catch (RuntimeException ok) { + return; + } x = 1; assertEquals(2,a.incrementAndGet(this)); assertEquals(2,a.get(this));