--- jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2009/11/17 03:12:51 1.13 +++ jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java 2015/11/08 15:34:00 1.30 @@ -1,101 +1,150 @@ /* * 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 + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ -import java.util.concurrent.atomic.*; -import junit.framework.*; -import java.util.*; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +import junit.framework.Test; +import junit.framework.TestSuite; public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { volatile int x = 0; + protected volatile int protectedField; + private volatile int privateField; int w; - long z; + float z; public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(AtomicIntegerFieldUpdaterTest.class); } + // for testing subclass access + static class AtomicIntegerFieldUpdaterTestSubclass extends AtomicIntegerFieldUpdaterTest { + public void checkPrivateAccess() { + try { + AtomicIntegerFieldUpdater a = + AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, "privateField"); + shouldThrow(); + } catch (RuntimeException success) { + assertNotNull(success.getCause()); + } + } + + public void checkCompareAndSetProtectedSub() { + AtomicIntegerFieldUpdater a; + a = updaterFor("protectedField"); + protectedField = 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)); + } + } + + static class UnrelatedClass { + public void checkPrivateAccess() { + Exception ex = null; + try { + AtomicIntegerFieldUpdater a = + AtomicIntegerFieldUpdater.newUpdater + (AtomicIntegerFieldUpdaterTest.class, "x"); + } catch (RuntimeException rex) { + ex = rex; + } + if (ex != null) throw new Error(); + } + } + + 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) { + assertNotNull(success.getCause()); } - catch (RuntimeException rt) {} } /** - * 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 rt) {} + } 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 rt) {} + } catch (IllegalArgumentException success) {} } /** - * get returns the last value set or assigned + * construction using private field from subclass throws RuntimeException + */ + public void testPrivateFieldInSubclass() { + AtomicIntegerFieldUpdaterTestSubclass s = + new AtomicIntegerFieldUpdaterTestSubclass(); + s.checkPrivateAccess(); + } + + /** + * construction from unrelated class throws RuntimeException + */ + public void testUnrelatedClassAccess() { + UnrelatedClass s = new UnrelatedClass(); + s.checkPrivateAccess(); + } + + /** + * get returns the last value set or assigned */ 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)); } /** - * get returns the last value lazySet by same thread + * get returns the last value lazySet by same thread */ 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)); } /** @@ -103,21 +152,43 @@ 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)); - assertFalse((7 == 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)); + } + + /** + * compareAndSet succeeds in changing protected field value if + * equal to expected else fails + */ + public void testCompareAndSetProtected() { + AtomicIntegerFieldUpdater a; + a = updaterFor("protectedField"); + protectedField = 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)); } + /** + * compareAndSet succeeds in changing protected field value if + * equal to expected else fails + */ + public void testCompareAndSetProtectedInSubclass() { + AtomicIntegerFieldUpdaterTestSubclass s = + new AtomicIntegerFieldUpdaterTestSubclass(); + s.checkCompareAndSetProtectedSub(); + } /** * compareAndSet in one thread enables another waiting for value @@ -125,23 +196,20 @@ 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 Runnable() { - public void run() { - while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield(); - }}); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() { + while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.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); + assertEquals(3, a.get(this)); } /** @@ -150,33 +218,25 @@ 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)); + do {} while (!a.weakCompareAndSet(this, 1, 2)); + do {} while (!a.weakCompareAndSet(this, 2, -4)); + assertEquals(-4, a.get(this)); + do {} while (!a.weakCompareAndSet(this, -4, 7)); + assertEquals(7, a.get(this)); } /** - * getAndSet returns previous value and sets to given value + * getAndSet returns previous value and sets to given value */ 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)); } /** @@ -184,16 +244,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)); } /** @@ -201,15 +257,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)); } /** @@ -217,19 +269,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)); } /** @@ -237,16 +285,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)); } /** @@ -254,16 +298,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)); } /** @@ -271,19 +311,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)); } }