--- jsr166/src/test/tck/Atomic8Test.java 2013/09/08 23:00:36 1.1 +++ jsr166/src/test/tck/Atomic8Test.java 2021/01/26 13:33:05 1.11 @@ -5,37 +5,58 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicIntegerArray; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicLongArray; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.atomic.AtomicReferenceArray; +import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Tests of atomic class methods accepting lambdas introduced in JDK8. + */ public class Atomic8Test extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(Atomic8Test.class); } - /* - * Tests of atomic class methods accepting lambdas - * introduced in JDK8. - */ - static long addLong17(long x) { return x + 17; } static int addInt17(int x) { return x + 17; } - static Integer addInteger17(Integer x) { - return new Integer(x.intValue() + 17); + static Item addItem17(Item x) { + return new Item(x.intValue() + 17); } - static Integer sumInteger(Integer x, Integer y) { - return new Integer(x.intValue() + y.intValue()); + static Item sumItem(Item x, Item y) { + return new Item(x.intValue() + y.intValue()); } volatile long aLongField; volatile int anIntField; - volatile Integer anIntegerField; + volatile Item anItemField; + + AtomicLongFieldUpdater aLongFieldUpdater() { + return AtomicLongFieldUpdater.newUpdater + (Atomic8Test.class, "aLongField"); + } + + AtomicIntegerFieldUpdater anIntFieldUpdater() { + return AtomicIntegerFieldUpdater.newUpdater + (Atomic8Test.class, "anIntField"); + } + + AtomicReferenceFieldUpdater anItemFieldUpdater() { + return AtomicReferenceFieldUpdater.newUpdater + (Atomic8Test.class, Item.class, "anItemField"); + } /** * AtomicLong getAndUpdate returns previous value and updates @@ -43,9 +64,9 @@ public class Atomic8Test extends JSR166T */ public void testLongGetAndUpdate() { AtomicLong a = new AtomicLong(1L); - assertEquals(1L, a.getAndUpdate(Atomic8Test::addLong17)); - assertEquals(18L, a.getAndUpdate(Atomic8Test::addLong17)); - assertEquals(35L, a.get()); + mustEqual(1L, a.getAndUpdate(Atomic8Test::addLong17)); + mustEqual(18L, a.getAndUpdate(Atomic8Test::addLong17)); + mustEqual(35L, a.get()); } /** @@ -54,8 +75,8 @@ public class Atomic8Test extends JSR166T */ public void testLongUpdateAndGet() { AtomicLong a = new AtomicLong(1L); - assertEquals(18L, a.updateAndGet(Atomic8Test::addLong17)); - assertEquals(35L, a.updateAndGet(Atomic8Test::addLong17)); + mustEqual(18L, a.updateAndGet(Atomic8Test::addLong17)); + mustEqual(35L, a.updateAndGet(Atomic8Test::addLong17)); } /** @@ -64,9 +85,9 @@ public class Atomic8Test extends JSR166T */ public void testLongGetAndAccumulate() { AtomicLong a = new AtomicLong(1L); - assertEquals(1L, a.getAndAccumulate(2L, Long::sum)); - assertEquals(3L, a.getAndAccumulate(3L, Long::sum)); - assertEquals(6L, a.get()); + mustEqual(1L, a.getAndAccumulate(2L, Long::sum)); + mustEqual(3L, a.getAndAccumulate(3L, Long::sum)); + mustEqual(6L, a.get()); } /** @@ -75,8 +96,9 @@ public class Atomic8Test extends JSR166T */ public void testLongAccumulateAndGet() { AtomicLong a = new AtomicLong(1L); - assertEquals(7L, a.accumulateAndGet(6L, Long::sum)); - assertEquals(10L, a.accumulateAndGet(3L, Long::sum)); + mustEqual(7L, a.accumulateAndGet(6L, Long::sum)); + mustEqual(10L, a.accumulateAndGet(3L, Long::sum)); + mustEqual(10L, a.get()); } /** @@ -85,9 +107,9 @@ public class Atomic8Test extends JSR166T */ public void testIntGetAndUpdate() { AtomicInteger a = new AtomicInteger(1); - assertEquals(1, a.getAndUpdate(Atomic8Test::addInt17)); - assertEquals(18, a.getAndUpdate(Atomic8Test::addInt17)); - assertEquals(35, a.get()); + mustEqual(1, a.getAndUpdate(Atomic8Test::addInt17)); + mustEqual(18, a.getAndUpdate(Atomic8Test::addInt17)); + mustEqual(35, a.get()); } /** @@ -96,8 +118,9 @@ public class Atomic8Test extends JSR166T */ public void testIntUpdateAndGet() { AtomicInteger a = new AtomicInteger(1); - assertEquals(18, a.updateAndGet(Atomic8Test::addInt17)); - assertEquals(35, a.updateAndGet(Atomic8Test::addInt17)); + mustEqual(18, a.updateAndGet(Atomic8Test::addInt17)); + mustEqual(35, a.updateAndGet(Atomic8Test::addInt17)); + mustEqual(35, a.get()); } /** @@ -106,9 +129,9 @@ public class Atomic8Test extends JSR166T */ public void testIntGetAndAccumulate() { AtomicInteger a = new AtomicInteger(1); - assertEquals(1, a.getAndAccumulate(2, Integer::sum)); - assertEquals(3, a.getAndAccumulate(3, Integer::sum)); - assertEquals(6, a.get()); + mustEqual(1, a.getAndAccumulate(2, Integer::sum)); + mustEqual(3, a.getAndAccumulate(3, Integer::sum)); + mustEqual(6, a.get()); } /** @@ -117,8 +140,9 @@ public class Atomic8Test extends JSR166T */ public void testIntAccumulateAndGet() { AtomicInteger a = new AtomicInteger(1); - assertEquals(7, a.accumulateAndGet(6, Integer::sum)); - assertEquals(10, a.accumulateAndGet(3, Integer::sum)); + mustEqual(7, a.accumulateAndGet(6, Integer::sum)); + mustEqual(10, a.accumulateAndGet(3, Integer::sum)); + mustEqual(10, a.get()); } /** @@ -126,10 +150,10 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testReferenceGetAndUpdate() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(1), a.getAndUpdate(Atomic8Test::addInteger17)); - assertEquals(new Integer(18), a.getAndUpdate(Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.get()); + AtomicReference a = new AtomicReference<>(one); + mustEqual(1, a.getAndUpdate(Atomic8Test::addItem17)); + mustEqual(18, a.getAndUpdate(Atomic8Test::addItem17)); + mustEqual(35, a.get()); } /** @@ -137,9 +161,10 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceUpdateAndGet() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(18), a.updateAndGet(Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.updateAndGet(Atomic8Test::addInteger17)); + AtomicReference a = new AtomicReference<>(one); + mustEqual(18, a.updateAndGet(Atomic8Test::addItem17)); + mustEqual(35, a.updateAndGet(Atomic8Test::addItem17)); + mustEqual(35, a.get()); } /** @@ -147,10 +172,10 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceGetAndAccumulate() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(1), a.getAndAccumulate(2, Atomic8Test::sumInteger)); - assertEquals(new Integer(3), a.getAndAccumulate(3, Atomic8Test::sumInteger)); - assertEquals(new Integer(6), a.get()); + AtomicReference a = new AtomicReference<>(one); + mustEqual( 1, a.getAndAccumulate(two, Atomic8Test::sumItem)); + mustEqual( 3, a.getAndAccumulate(three, Atomic8Test::sumItem)); + mustEqual( 6, a.get()); } /** @@ -158,12 +183,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceAccumulateAndGet() { - AtomicReference a = new AtomicReference(one); - assertEquals(new Integer(7), a.accumulateAndGet(6, Atomic8Test::sumInteger)); - assertEquals(new Integer(10), a.accumulateAndGet(3, Atomic8Test::sumInteger)); + AtomicReference a = new AtomicReference<>(one); + mustEqual( 7, a.accumulateAndGet(six, Atomic8Test::sumItem)); + mustEqual( 10, a.accumulateAndGet(three, Atomic8Test::sumItem)); + mustEqual( 10, a.get()); } - /** * AtomicLongArray getAndUpdate returns previous value and updates * result of supplied function @@ -171,9 +196,9 @@ public class Atomic8Test extends JSR166T public void testLongArrayGetAndUpdate() { AtomicLongArray a = new AtomicLongArray(1); a.set(0, 1); - assertEquals(1L, a.getAndUpdate(0, Atomic8Test::addLong17)); - assertEquals(18L, a.getAndUpdate(0, Atomic8Test::addLong17)); - assertEquals(35L, a.get(0)); + mustEqual(1L, a.getAndUpdate(0, Atomic8Test::addLong17)); + mustEqual(18L, a.getAndUpdate(0, Atomic8Test::addLong17)); + mustEqual(35L, a.get(0)); } /** @@ -183,8 +208,9 @@ public class Atomic8Test extends JSR166T public void testLongArrayUpdateAndGet() { AtomicLongArray a = new AtomicLongArray(1); a.set(0, 1); - assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17)); - assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17)); + mustEqual(18L, a.updateAndGet(0, Atomic8Test::addLong17)); + mustEqual(35L, a.updateAndGet(0, Atomic8Test::addLong17)); + mustEqual(35L, a.get(0)); } /** @@ -194,9 +220,9 @@ public class Atomic8Test extends JSR166T public void testLongArrayGetAndAccumulate() { AtomicLongArray a = new AtomicLongArray(1); a.set(0, 1); - assertEquals(1L, a.getAndAccumulate(0, 2L, Long::sum)); - assertEquals(3L, a.getAndAccumulate(0, 3L, Long::sum)); - assertEquals(6L, a.get(0)); + mustEqual(1L, a.getAndAccumulate(0, 2L, Long::sum)); + mustEqual(3L, a.getAndAccumulate(0, 3L, Long::sum)); + mustEqual(6L, a.get(0)); } /** @@ -206,8 +232,9 @@ public class Atomic8Test extends JSR166T public void testLongArrayAccumulateAndGet() { AtomicLongArray a = new AtomicLongArray(1); a.set(0, 1); - assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum)); - assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum)); + mustEqual(7L, a.accumulateAndGet(0, 6L, Long::sum)); + mustEqual(10L, a.accumulateAndGet(0, 3L, Long::sum)); + mustEqual(10L, a.get(0)); } /** @@ -217,9 +244,9 @@ public class Atomic8Test extends JSR166T public void testIntArrayGetAndUpdate() { AtomicIntegerArray a = new AtomicIntegerArray(1); a.set(0, 1); - assertEquals(1, a.getAndUpdate(0, Atomic8Test::addInt17)); - assertEquals(18, a.getAndUpdate(0, Atomic8Test::addInt17)); - assertEquals(35, a.get(0)); + mustEqual(1, a.getAndUpdate(0, Atomic8Test::addInt17)); + mustEqual(18, a.getAndUpdate(0, Atomic8Test::addInt17)); + mustEqual(35, a.get(0)); } /** @@ -229,8 +256,9 @@ public class Atomic8Test extends JSR166T public void testIntArrayUpdateAndGet() { AtomicIntegerArray a = new AtomicIntegerArray(1); a.set(0, 1); - assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17)); - assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17)); + mustEqual(18, a.updateAndGet(0, Atomic8Test::addInt17)); + mustEqual(35, a.updateAndGet(0, Atomic8Test::addInt17)); + mustEqual(35, a.get(0)); } /** @@ -240,9 +268,9 @@ public class Atomic8Test extends JSR166T public void testIntArrayGetAndAccumulate() { AtomicIntegerArray a = new AtomicIntegerArray(1); a.set(0, 1); - assertEquals(1, a.getAndAccumulate(0, 2, Integer::sum)); - assertEquals(3, a.getAndAccumulate(0, 3, Integer::sum)); - assertEquals(6, a.get(0)); + mustEqual(1, a.getAndAccumulate(0, 2, Integer::sum)); + mustEqual(3, a.getAndAccumulate(0, 3, Integer::sum)); + mustEqual(6, a.get(0)); } /** @@ -252,8 +280,8 @@ public class Atomic8Test extends JSR166T public void testIntArrayAccumulateAndGet() { AtomicIntegerArray a = new AtomicIntegerArray(1); a.set(0, 1); - assertEquals(7, a.accumulateAndGet(0, 6, Integer::sum)); - assertEquals(10, a.accumulateAndGet(0, 3, Integer::sum)); + mustEqual(7, a.accumulateAndGet(0, 6, Integer::sum)); + mustEqual(10, a.accumulateAndGet(0, 3, Integer::sum)); } /** @@ -261,11 +289,11 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testReferenceArrayGetAndUpdate() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(1), a.getAndUpdate(0, Atomic8Test::addInteger17)); - assertEquals(new Integer(18), a.getAndUpdate(0, Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.get(0)); + mustEqual( 1, a.getAndUpdate(0, Atomic8Test::addItem17)); + mustEqual( 18, a.getAndUpdate(0, Atomic8Test::addItem17)); + mustEqual( 35, a.get(0)); } /** @@ -273,10 +301,10 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceArrayUpdateAndGet() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(18), a.updateAndGet(0, Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.updateAndGet(0, Atomic8Test::addInteger17)); + mustEqual( 18, a.updateAndGet(0, Atomic8Test::addItem17)); + mustEqual( 35, a.updateAndGet(0, Atomic8Test::addItem17)); } /** @@ -284,11 +312,11 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceArrayGetAndAccumulate() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(1), a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); - assertEquals(new Integer(3), a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); - assertEquals(new Integer(6), a.get(0)); + mustEqual( 1, a.getAndAccumulate(0, two, Atomic8Test::sumItem)); + mustEqual( 3, a.getAndAccumulate(0, three, Atomic8Test::sumItem)); + mustEqual( 6, a.get(0)); } /** @@ -296,10 +324,10 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testReferenceArrayAccumulateAndGet() { - AtomicReferenceArray a = new AtomicReferenceArray(1); + AtomicReferenceArray a = new AtomicReferenceArray<>(1); a.set(0, one); - assertEquals(new Integer(7), a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); - assertEquals(new Integer(10), a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); + mustEqual( 7, a.accumulateAndGet(0, six, Atomic8Test::sumItem)); + mustEqual( 10, a.accumulateAndGet(0, three, Atomic8Test::sumItem)); } /** @@ -307,12 +335,12 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testLongFieldUpdaterGetAndUpdate() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); - a.set(this, 1); - assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); - assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); - assertEquals(35L, a.get(this)); + AtomicLongFieldUpdater a = aLongFieldUpdater(); + a.set(this, 1L); + mustEqual(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); + mustEqual(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); + mustEqual(35L, a.get(this)); + mustEqual(35L, aLongField); } /** @@ -320,11 +348,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testLongFieldUpdaterUpdateAndGet() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); - a.set(this, 1); - assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17)); - assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17)); + AtomicLongFieldUpdater a = aLongFieldUpdater(); + a.set(this, 1L); + mustEqual(18L, a.updateAndGet(this, Atomic8Test::addLong17)); + mustEqual(35L, a.updateAndGet(this, Atomic8Test::addLong17)); + mustEqual(35L, a.get(this)); + mustEqual(35L, aLongField); } /** @@ -332,12 +361,12 @@ public class Atomic8Test extends JSR166T * and updates with supplied function. */ public void testLongFieldUpdaterGetAndAccumulate() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); - 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)); + AtomicLongFieldUpdater a = aLongFieldUpdater(); + a.set(this, 1L); + mustEqual(1L, a.getAndAccumulate(this, 2L, Long::sum)); + mustEqual(3L, a.getAndAccumulate(this, 3L, Long::sum)); + mustEqual(6L, a.get(this)); + mustEqual(6L, aLongField); } /** @@ -345,11 +374,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testLongFieldUpdaterAccumulateAndGet() { - AtomicLongFieldUpdater a = AtomicLongFieldUpdater. - newUpdater(Atomic8Test.class, "aLongField"); - a.set(this, 1); - assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum)); - assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum)); + AtomicLongFieldUpdater a = aLongFieldUpdater(); + a.set(this, 1L); + mustEqual(7L, a.accumulateAndGet(this, 6L, Long::sum)); + mustEqual(10L, a.accumulateAndGet(this, 3L, Long::sum)); + mustEqual(10L, a.get(this)); + mustEqual(10L, aLongField); } /** @@ -357,12 +387,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)); + mustEqual(1, a.getAndUpdate(this, Atomic8Test::addInt17)); + mustEqual(18, a.getAndUpdate(this, Atomic8Test::addInt17)); + mustEqual(35, a.get(this)); + mustEqual(35, anIntField); } /** @@ -370,11 +400,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)); + mustEqual(18, a.updateAndGet(this, Atomic8Test::addInt17)); + mustEqual(35, a.updateAndGet(this, Atomic8Test::addInt17)); + mustEqual(35, a.get(this)); + mustEqual(35, anIntField); } /** @@ -382,12 +413,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)); + mustEqual(1, a.getAndAccumulate(this, 2, Integer::sum)); + mustEqual(3, a.getAndAccumulate(this, 3, Integer::sum)); + mustEqual(6, a.get(this)); + mustEqual(6, anIntField); } /** @@ -395,25 +426,25 @@ 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)); + mustEqual(7, a.accumulateAndGet(this, 6, Integer::sum)); + mustEqual(10, a.accumulateAndGet(this, 3, Integer::sum)); + mustEqual(10, a.get(this)); + mustEqual(10, anIntField); } - /** * AtomicReferenceFieldUpdater getAndUpdate returns previous value * and updates result of supplied function */ public void testReferenceFieldUpdaterGetAndUpdate() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anItemFieldUpdater(); 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)); + mustEqual( 1, a.getAndUpdate(this, Atomic8Test::addItem17)); + mustEqual( 18, a.getAndUpdate(this, Atomic8Test::addItem17)); + mustEqual( 35, a.get(this)); + mustEqual( 35, anItemField); } /** @@ -421,11 +452,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testReferenceFieldUpdaterUpdateAndGet() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anItemFieldUpdater(); a.set(this, one); - assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17)); - assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17)); + mustEqual( 18, a.updateAndGet(this, Atomic8Test::addItem17)); + mustEqual( 35, a.updateAndGet(this, Atomic8Test::addItem17)); + mustEqual( 35, a.get(this)); + mustEqual( 35, anItemField); } /** @@ -433,12 +465,12 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceFieldUpdaterGetAndAccumulate() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anItemFieldUpdater(); 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)); + mustEqual( 1, a.getAndAccumulate(this, two, Atomic8Test::sumItem)); + mustEqual( 3, a.getAndAccumulate(this, three, Atomic8Test::sumItem)); + mustEqual( 6, a.get(this)); + mustEqual( 6, anItemField); } /** @@ -446,144 +478,122 @@ public class Atomic8Test extends JSR166T * supplied function and returns result. */ public void testReferenceFieldUpdaterAccumulateAndGet() { - AtomicReferenceFieldUpdater a = AtomicReferenceFieldUpdater. - newUpdater(Atomic8Test.class, Integer.class, "anIntegerField"); + AtomicReferenceFieldUpdater a = anItemFieldUpdater(); a.set(this, one); - assertEquals(new Integer(7), a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); - assertEquals(new Integer(10), a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); + mustEqual( 7, a.accumulateAndGet(this, six, Atomic8Test::sumItem)); + mustEqual( 10, a.accumulateAndGet(this, three, Atomic8Test::sumItem)); + mustEqual( 10, a.get(this)); + mustEqual( 10, anItemField); } /** - * All Atomic getAndUpdate methods throw npe on null function argument + * All Atomic getAndUpdate methods throw NullPointerException on + * null function argument */ + @SuppressWarnings("unchecked") 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) {} + assertThrows( + NullPointerException.class, + () -> 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), + () -> anItemFieldUpdater().getAndUpdate(this, null)); } /** - * All Atomic getAndAccumulate methods throw npe on null function argument + * All Atomic updateAndGet methods throw NullPointerException on null function argument + */ + @SuppressWarnings("unchecked") + public void testUpdateAndGetNPE() { + assertThrows( + NullPointerException.class, + () -> 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), + () -> anItemFieldUpdater().updateAndGet(this, null)); + } + + /** + * All Atomic getAndAccumulate methods throw NullPointerException + * on null function argument */ + @SuppressWarnings("unchecked") 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) {} + assertThrows( + NullPointerException.class, + () -> 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), + () -> anItemFieldUpdater().getAndAccumulate(this, one, null)); } /** - * All Atomic accumulateAndGet methods throw npe on null function argument + * All Atomic accumulateAndGet methods throw NullPointerException + * on null function argument */ + @SuppressWarnings("unchecked") 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) {} + assertThrows( + NullPointerException.class, + () -> 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), + () -> anItemFieldUpdater().accumulateAndGet(this, one, null)); } -} + /** + * Object arguments for parameters of type T that are not + * instances of the class passed to the newUpdater call will + * result in a ClassCastException being thrown. + */ + @SuppressWarnings("unchecked") + public void testFieldUpdaters_ClassCastException() { + // Use raw types to allow passing wrong object type, provoking CCE + final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); + final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); + final AtomicReferenceFieldUpdater refUpdater = anItemFieldUpdater(); + for (Object x : new Object[]{ new Object(), null }) { + assertThrows( + ClassCastException.class, + () -> longUpdater.get(x), + () -> intUpdater.get(x), + () -> refUpdater.get(x), + + () -> longUpdater.set(x, 17L), + () -> intUpdater.set(x, 17), + () -> refUpdater.set(x, new Item(17)), + + () -> longUpdater.addAndGet(x, 17L), + () -> intUpdater.addAndGet(x, 17), + + () -> longUpdater.getAndUpdate(x, y -> y), + () -> intUpdater.getAndUpdate(x, y -> y), + () -> refUpdater.getAndUpdate(x, y -> y), + + () -> longUpdater.compareAndSet(x, 17L, 42L), + () -> intUpdater.compareAndSet(x, 17, 42), + () -> refUpdater.compareAndSet(x, 17, fortytwo)); + } + } +}