--- jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2011/05/31 16:16:23 1.21 +++ jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2013/04/02 04:11:28 1.25 @@ -20,40 +20,41 @@ public class AtomicIntegerFieldUpdaterTe return new TestSuite(AtomicIntegerFieldUpdaterTest.class); } + AtomicIntegerFieldUpdater updaterFor(String fieldName) { + return AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, fieldName); + } + /** * Construction with non-existent field throws RuntimeException */ public void testConstructor() { try { - AtomicIntegerFieldUpdater - a = AtomicIntegerFieldUpdater.newUpdater - (AtomicIntegerFieldUpdaterTest.class, "y"); + updaterFor("y"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (RuntimeException success) { + assertTrue(success.getCause() != null); + } } /** - * construction with field not of given type throws RuntimeException + * construction with field not of given type throws IllegalArgumentException */ public void testConstructor2() { try { - AtomicIntegerFieldUpdater - a = AtomicIntegerFieldUpdater.newUpdater - (AtomicIntegerFieldUpdaterTest.class, "z"); + updaterFor("z"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (IllegalArgumentException success) {} } /** - * construction with non-volatile field throws RuntimeException + * construction with non-volatile field throws IllegalArgumentException */ public void testConstructor3() { try { - AtomicIntegerFieldUpdater - a = AtomicIntegerFieldUpdater.newUpdater - (AtomicIntegerFieldUpdaterTest.class, "w"); + updaterFor("w"); shouldThrow(); - } catch (RuntimeException success) {} + } catch (IllegalArgumentException success) {} } /** @@ -61,17 +62,13 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetSet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); 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)); + assertEquals(1, a.get(this)); + a.set(this, 2); + assertEquals(2, a.get(this)); + a.set(this, -3); + assertEquals(-3, a.get(this)); } /** @@ -79,17 +76,13 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetLazySet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); 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)); + assertEquals(1, a.get(this)); + a.lazySet(this, 2); + assertEquals(2, a.get(this)); + a.lazySet(this, -3); + assertEquals(-3, a.get(this)); } /** @@ -97,19 +90,15 @@ public class AtomicIntegerFieldUpdaterTe */ public void testCompareAndSet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertTrue(a.compareAndSet(this,1,2)); - assertTrue(a.compareAndSet(this,2,-4)); - assertEquals(-4,a.get(this)); - assertFalse(a.compareAndSet(this,-5,7)); - assertEquals(-4,a.get(this)); - assertTrue(a.compareAndSet(this,-4,7)); - assertEquals(7,a.get(this)); + assertTrue(a.compareAndSet(this, 1, 2)); + assertTrue(a.compareAndSet(this, 2, -4)); + assertEquals(-4, a.get(this)); + assertFalse(a.compareAndSet(this, -5, 7)); + assertEquals(-4, a.get(this)); + assertTrue(a.compareAndSet(this, -4, 7)); + assertEquals(7, a.get(this)); } /** @@ -118,12 +107,8 @@ public class AtomicIntegerFieldUpdaterTe */ public void testCompareAndSetInMultipleThreads() throws Exception { x = 1; - final AtomicIntegerFieldUpdatera; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + final AtomicIntegerFieldUpdater a; + a = updaterFor("x"); Thread t = new Thread(new CheckedRunnable() { public void realRun() { @@ -135,7 +120,7 @@ public class AtomicIntegerFieldUpdaterTe assertTrue(a.compareAndSet(this, 1, 2)); t.join(LONG_DELAY_MS); assertFalse(t.isAlive()); - assertEquals(a.get(this), 3); + assertEquals(3, a.get(this)); } /** @@ -144,17 +129,13 @@ public class AtomicIntegerFieldUpdaterTe */ public void testWeakCompareAndSet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - while (!a.weakCompareAndSet(this,1,2)); - while (!a.weakCompareAndSet(this,2,-4)); - assertEquals(-4,a.get(this)); - while (!a.weakCompareAndSet(this,-4,7)); - assertEquals(7,a.get(this)); + while (!a.weakCompareAndSet(this, 1, 2)); + while (!a.weakCompareAndSet(this, 2, -4)); + assertEquals(-4, a.get(this)); + while (!a.weakCompareAndSet(this, -4, 7)); + assertEquals(7, a.get(this)); } /** @@ -162,15 +143,11 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetAndSet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndSet(this, 0)); - assertEquals(0,a.getAndSet(this,-10)); - assertEquals(-10,a.getAndSet(this,1)); + assertEquals(1, a.getAndSet(this, 0)); + assertEquals(0, a.getAndSet(this, -10)); + assertEquals(-10, a.getAndSet(this, 1)); } /** @@ -178,16 +155,12 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetAndAdd() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndAdd(this,2)); - assertEquals(3,a.get(this)); - assertEquals(3,a.getAndAdd(this,-4)); - assertEquals(-1,a.get(this)); + assertEquals(1, a.getAndAdd(this, 2)); + assertEquals(3, a.get(this)); + assertEquals(3, a.getAndAdd(this, -4)); + assertEquals(-1, a.get(this)); } /** @@ -195,15 +168,11 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetAndDecrement() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndDecrement(this)); - assertEquals(0,a.getAndDecrement(this)); - assertEquals(-1,a.getAndDecrement(this)); + assertEquals(1, a.getAndDecrement(this)); + assertEquals(0, a.getAndDecrement(this)); + assertEquals(-1, a.getAndDecrement(this)); } /** @@ -211,19 +180,15 @@ public class AtomicIntegerFieldUpdaterTe */ public void testGetAndIncrement() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(1,a.getAndIncrement(this)); - assertEquals(2,a.get(this)); - a.set(this,-2); - assertEquals(-2,a.getAndIncrement(this)); - assertEquals(-1,a.getAndIncrement(this)); - assertEquals(0,a.getAndIncrement(this)); - assertEquals(1,a.get(this)); + assertEquals(1, a.getAndIncrement(this)); + assertEquals(2, a.get(this)); + a.set(this, -2); + assertEquals(-2, a.getAndIncrement(this)); + assertEquals(-1, a.getAndIncrement(this)); + assertEquals(0, a.getAndIncrement(this)); + assertEquals(1, a.get(this)); } /** @@ -231,16 +196,12 @@ public class AtomicIntegerFieldUpdaterTe */ public void testAddAndGet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(3,a.addAndGet(this,2)); - assertEquals(3,a.get(this)); - assertEquals(-1,a.addAndGet(this,-4)); - assertEquals(-1,a.get(this)); + assertEquals(3, a.addAndGet(this, 2)); + assertEquals(3, a.get(this)); + assertEquals(-1, a.addAndGet(this, -4)); + assertEquals(-1, a.get(this)); } /** @@ -248,16 +209,12 @@ public class AtomicIntegerFieldUpdaterTe */ public void testDecrementAndGet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(0,a.decrementAndGet(this)); - assertEquals(-1,a.decrementAndGet(this)); - assertEquals(-2,a.decrementAndGet(this)); - assertEquals(-2,a.get(this)); + assertEquals(0, a.decrementAndGet(this)); + assertEquals(-1, a.decrementAndGet(this)); + assertEquals(-2, a.decrementAndGet(this)); + assertEquals(-2, a.get(this)); } /** @@ -265,19 +222,15 @@ public class AtomicIntegerFieldUpdaterTe */ public void testIncrementAndGet() { AtomicIntegerFieldUpdater a; - try { - a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); - } catch (RuntimeException ok) { - return; - } + a = updaterFor("x"); x = 1; - assertEquals(2,a.incrementAndGet(this)); - assertEquals(2,a.get(this)); - a.set(this,-2); - assertEquals(-1,a.incrementAndGet(this)); - assertEquals(0,a.incrementAndGet(this)); - assertEquals(1,a.incrementAndGet(this)); - assertEquals(1,a.get(this)); + assertEquals(2, a.incrementAndGet(this)); + assertEquals(2, a.get(this)); + a.set(this, -2); + assertEquals(-1, a.incrementAndGet(this)); + assertEquals(0, a.incrementAndGet(this)); + assertEquals(1, a.incrementAndGet(this)); + assertEquals(1, a.get(this)); } }