--- jsr166/src/test/tck/Atomic8Test.java 2013/09/08 23:00:36 1.1 +++ jsr166/src/test/tck/Atomic8Test.java 2013/09/09 06:23:16 1.2 @@ -37,6 +37,21 @@ public class Atomic8Test extends JSR166T volatile int anIntField; volatile Integer anIntegerField; + AtomicLongFieldUpdater aLongFieldUpdater() { + return AtomicLongFieldUpdater.newUpdater + (Atomic8Test.class, "aLongField"); + } + + AtomicIntegerFieldUpdater anIntFieldUpdater() { + return AtomicIntegerFieldUpdater.newUpdater + (Atomic8Test.class, "anIntField"); + } + + AtomicReferenceFieldUpdater anIntegerFieldUpdater() { + return AtomicReferenceFieldUpdater.newUpdater + (Atomic8Test.class, Integer.class, "anIntegerField"); + } + /** * AtomicLong getAndUpdate returns previous value and updates * result of supplied function @@ -77,6 +92,7 @@ public class Atomic8Test extends JSR166T AtomicLong a = new AtomicLong(1L); assertEquals(7L, a.accumulateAndGet(6L, Long::sum)); assertEquals(10L, a.accumulateAndGet(3L, Long::sum)); + assertEquals(10L, a.get()); } /** @@ -98,6 +114,7 @@ public class Atomic8Test extends JSR166T AtomicInteger a = new AtomicInteger(1); assertEquals(18, a.updateAndGet(Atomic8Test::addInt17)); assertEquals(35, a.updateAndGet(Atomic8Test::addInt17)); + assertEquals(35, a.get()); } /** @@ -119,6 +136,7 @@ public class Atomic8Test extends JSR166T AtomicInteger a = new AtomicInteger(1); assertEquals(7, a.accumulateAndGet(6, Integer::sum)); assertEquals(10, a.accumulateAndGet(3, Integer::sum)); + assertEquals(10, a.get()); } /** @@ -140,6 +158,7 @@ public class Atomic8Test extends JSR166T AtomicReference a = new AtomicReference(one); assertEquals(new Integer(18), a.updateAndGet(Atomic8Test::addInteger17)); assertEquals(new Integer(35), a.updateAndGet(Atomic8Test::addInteger17)); + assertEquals(new Integer(35), a.get()); } /** @@ -161,6 +180,7 @@ public class Atomic8Test extends JSR166T AtomicReference a = new AtomicReference(one); assertEquals(new Integer(7), a.accumulateAndGet(6, Atomic8Test::sumInteger)); assertEquals(new Integer(10), a.accumulateAndGet(3, Atomic8Test::sumInteger)); + assertEquals(new Integer(10), a.get()); } @@ -185,6 +205,7 @@ public class Atomic8Test extends JSR166T a.set(0, 1); assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17)); assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17)); + assertEquals(35L, a.get(0)); } /** @@ -208,6 +229,7 @@ public class Atomic8Test extends JSR166T a.set(0, 1); assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum)); assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum)); + assertEquals(10L, a.get(0)); } /** @@ -231,6 +253,7 @@ public class Atomic8Test extends JSR166T a.set(0, 1); assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17)); assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17)); + assertEquals(35, a.get(0)); } /** @@ -307,12 +330,12 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testLongFieldUpdaterGetAndUpdate() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); assertEquals(35L, a.get(this)); + assertEquals(35L, aLongField); } /** @@ -320,11 +343,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testLongFieldUpdaterUpdateAndGet() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17)); assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17)); + assertEquals(35L, a.get(this)); + assertEquals(35L, aLongField); } /** @@ -332,12 +356,12 @@ public class Atomic8Test extends JSR166T * and updates with supplied function. */ public void testLongFieldUpdaterGetAndAccumulate() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum)); assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum)); assertEquals(6L, a.get(this)); + assertEquals(6L, aLongField); } /** @@ -345,11 +369,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testLongFieldUpdaterAccumulateAndGet() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); + AtomicLongFieldUpdater a = aLongFieldUpdater(); a.set(this, 1); assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum)); assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum)); + assertEquals(10L, a.get(this)); + assertEquals(10L, aLongField); } /** @@ -357,12 +382,12 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testIntegerFieldUpdaterGetAndUpdate() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17)); assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17)); assertEquals(35, a.get(this)); + assertEquals(35, anIntField); } /** @@ -370,11 +395,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testIntegerFieldUpdaterUpdateAndGet() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17)); assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17)); + assertEquals(35, a.get(this)); + assertEquals(35, anIntField); } /** @@ -382,12 +408,12 @@ public class Atomic8Test extends JSR166T * and updates with supplied function. */ public void testIntegerFieldUpdaterGetAndAccumulate() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum)); assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum)); assertEquals(6, a.get(this)); + assertEquals(6, anIntField); } /** @@ -395,11 +421,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testIntegerFieldUpdaterAccumulateAndGet() { - AtomicIntegerFieldUpdater a = AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"); + AtomicIntegerFieldUpdater a = anIntFieldUpdater(); a.set(this, 1); assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum)); assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum)); + assertEquals(10, a.get(this)); + assertEquals(10, anIntField); } @@ -408,12 +435,12 @@ public class Atomic8Test extends JSR166T * and updates result of supplied function */ public void testReferenceFieldUpdaterGetAndUpdate() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); assertEquals(new Integer(1), a.getAndUpdate(this, Atomic8Test::addInteger17)); assertEquals(new Integer(18), a.getAndUpdate(this, Atomic8Test::addInteger17)); assertEquals(new Integer(35), a.get(this)); + assertEquals(new Integer(35), anIntegerField); } /** @@ -421,11 +448,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testReferenceFieldUpdaterUpdateAndGet() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17)); assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17)); + assertEquals(new Integer(35), a.get(this)); + assertEquals(new Integer(35), anIntegerField); } /** @@ -433,12 +461,12 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceFieldUpdaterGetAndAccumulate() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); assertEquals(new Integer(1), a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); assertEquals(new Integer(3), a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); assertEquals(new Integer(6), a.get(this)); + assertEquals(new Integer(6), anIntegerField); } /** @@ -446,144 +474,90 @@ public class Atomic8Test extends JSR166T * supplied function and returns result. */ public void testReferenceFieldUpdaterAccumulateAndGet() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); a.set(this, one); assertEquals(new Integer(7), a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); assertEquals(new Integer(10), a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); + assertEquals(new Integer(10), a.get(this)); + assertEquals(new Integer(10), anIntegerField); } /** - * All Atomic getAndUpdate methods throw npe on null function argument + * All Atomic getAndUpdate methods throw NullPointerException on + * null function argument */ public void testGetAndUpdateNPE() { - try { new AtomicLong().getAndUpdate(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().getAndUpdate(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().getAndUpdate(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).getAndUpdate(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).getAndUpdate(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).getAndUpdate(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - getAndUpdate(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - getAndUpdate(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - getAndUpdate(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - } - - /** - * All Atomic updateAndGet methods throw npe on null function argument - */ - public void testUpdateGetAndNPE() { - try { new AtomicLong().updateAndGet(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().updateAndGet(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().updateAndGet(null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).updateAndGet(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).updateAndGet(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).updateAndGet(0, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - updateAndGet(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - updateAndGet(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - updateAndGet(this, null); - shouldThrow(); - } catch(NullPointerException ok) {} + Runnable[] throwingActions = { + () -> new AtomicLong().getAndUpdate(null), + () -> new AtomicInteger().getAndUpdate(null), + () -> new AtomicReference().getAndUpdate(null), + () -> new AtomicLongArray(1).getAndUpdate(0, null), + () -> new AtomicIntegerArray(1).getAndUpdate(0, null), + () -> new AtomicReferenceArray(1).getAndUpdate(0, null), + () -> aLongFieldUpdater().getAndUpdate(this, null), + () -> anIntFieldUpdater().getAndUpdate(this, null), + () -> anIntegerFieldUpdater().getAndUpdate(this, null), + ////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17), + ////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17), + ////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17), + }; + assertThrows(NullPointerException.class, throwingActions); } /** - * All Atomic getAndAccumulate methods throw npe on null function argument + * All Atomic updateAndGet methods throw NullPointerException on null function argument + */ + public void testUpdateAndGetNPE() { + Runnable[] throwingActions = { + () -> new AtomicLong().updateAndGet(null), + () -> new AtomicInteger().updateAndGet(null), + () -> new AtomicReference().updateAndGet(null), + () -> new AtomicLongArray(1).updateAndGet(0, null), + () -> new AtomicIntegerArray(1).updateAndGet(0, null), + () -> new AtomicReferenceArray(1).updateAndGet(0, null), + () -> aLongFieldUpdater().updateAndGet(this, null), + () -> anIntFieldUpdater().updateAndGet(this, null), + () -> anIntegerFieldUpdater().updateAndGet(this, null), + }; + assertThrows(NullPointerException.class, throwingActions); + } + + /** + * All Atomic getAndAccumulate methods throw NullPointerException + * on null function argument */ public void testGetAndAccumulateNPE() { - try { new AtomicLong().getAndAccumulate(1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().getAndAccumulate(1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().getAndAccumulate(one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).getAndAccumulate(0, 1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).getAndAccumulate(0, 1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).getAndAccumulate(0, one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - getAndAccumulate(this, 1L, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - getAndAccumulate(this, 1, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - getAndAccumulate(this, one, null); - shouldThrow(); - } catch(NullPointerException ok) {} + Runnable[] throwingActions = { + () -> new AtomicLong().getAndAccumulate(1L, null), + () -> new AtomicInteger().getAndAccumulate(1, null), + () -> new AtomicReference().getAndAccumulate(one, null), + () -> new AtomicLongArray(1).getAndAccumulate(0, 1L, null), + () -> new AtomicIntegerArray(1).getAndAccumulate(0, 1, null), + () -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null), + () -> aLongFieldUpdater().getAndAccumulate(this, 1L, null), + () -> anIntFieldUpdater().getAndAccumulate(this, 1, null), + () -> anIntegerFieldUpdater().getAndAccumulate(this, one, null), + }; + assertThrows(NullPointerException.class, throwingActions); } /** - * All Atomic accumulateAndGet methods throw npe on null function argument + * All Atomic accumulateAndGet methods throw NullPointerException + * on null function argument */ public void testAccumulateAndGetNPE() { - try { new AtomicLong().accumulateAndGet(1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicInteger().accumulateAndGet(1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReference().accumulateAndGet(one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicLongArray(1).accumulateAndGet(0, 1L, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicIntegerArray(1).accumulateAndGet(0, 1, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { new AtomicReferenceArray(1).accumulateAndGet(0, one, null); shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"). - accumulateAndGet(this, 1L, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicIntegerFieldUpdater. - newUpdater(Atomic8Test.class, "anIntField"). - accumulateAndGet(this, 1, null); - shouldThrow(); - } catch(NullPointerException ok) {} - try { AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"). - accumulateAndGet(this, one, null); - shouldThrow(); - } catch(NullPointerException ok) {} + Runnable[] throwingActions = { + () -> new AtomicLong().accumulateAndGet(1L, null), + () -> new AtomicInteger().accumulateAndGet(1, null), + () -> new AtomicReference().accumulateAndGet(one, null), + () -> new AtomicLongArray(1).accumulateAndGet(0, 1L, null), + () -> new AtomicIntegerArray(1).accumulateAndGet(0, 1, null), + () -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null), + () -> aLongFieldUpdater().accumulateAndGet(this, 1L, null), + () -> anIntFieldUpdater().accumulateAndGet(this, 1, null), + () -> anIntegerFieldUpdater().accumulateAndGet(this, one, null), + }; + assertThrows(NullPointerException.class, throwingActions); } } -