--- jsr166/src/test/tck/Atomic8Test.java 2013/09/09 06:23:16 1.2 +++ jsr166/src/test/tck/Atomic8Test.java 2021/01/26 13:33:05 1.11 @@ -5,51 +5,57 @@ * 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() { + AtomicLongFieldUpdater aLongFieldUpdater() { return AtomicLongFieldUpdater.newUpdater (Atomic8Test.class, "aLongField"); } - AtomicIntegerFieldUpdater anIntFieldUpdater() { + AtomicIntegerFieldUpdater anIntFieldUpdater() { return AtomicIntegerFieldUpdater.newUpdater (Atomic8Test.class, "anIntField"); } - AtomicReferenceFieldUpdater anIntegerFieldUpdater() { + AtomicReferenceFieldUpdater anItemFieldUpdater() { return AtomicReferenceFieldUpdater.newUpdater - (Atomic8Test.class, Integer.class, "anIntegerField"); + (Atomic8Test.class, Item.class, "anItemField"); } /** @@ -58,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()); } /** @@ -69,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)); } /** @@ -79,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()); } /** @@ -90,9 +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)); - assertEquals(10L, a.get()); + mustEqual(7L, a.accumulateAndGet(6L, Long::sum)); + mustEqual(10L, a.accumulateAndGet(3L, Long::sum)); + mustEqual(10L, a.get()); } /** @@ -101,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()); } /** @@ -112,9 +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)); - assertEquals(35, a.get()); + mustEqual(18, a.updateAndGet(Atomic8Test::addInt17)); + mustEqual(35, a.updateAndGet(Atomic8Test::addInt17)); + mustEqual(35, a.get()); } /** @@ -123,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()); } /** @@ -134,9 +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)); - assertEquals(10, a.get()); + mustEqual(7, a.accumulateAndGet(6, Integer::sum)); + mustEqual(10, a.accumulateAndGet(3, Integer::sum)); + mustEqual(10, a.get()); } /** @@ -144,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()); } /** @@ -155,10 +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)); - assertEquals(new Integer(35), a.get()); + AtomicReference a = new AtomicReference<>(one); + mustEqual(18, a.updateAndGet(Atomic8Test::addItem17)); + mustEqual(35, a.updateAndGet(Atomic8Test::addItem17)); + mustEqual(35, a.get()); } /** @@ -166,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()); } /** @@ -177,13 +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)); - assertEquals(new Integer(10), a.get()); + 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 @@ -191,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)); } /** @@ -203,9 +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)); - assertEquals(35L, a.get(0)); + mustEqual(18L, a.updateAndGet(0, Atomic8Test::addLong17)); + mustEqual(35L, a.updateAndGet(0, Atomic8Test::addLong17)); + mustEqual(35L, a.get(0)); } /** @@ -215,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)); } /** @@ -227,9 +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)); - assertEquals(10L, a.get(0)); + mustEqual(7L, a.accumulateAndGet(0, 6L, Long::sum)); + mustEqual(10L, a.accumulateAndGet(0, 3L, Long::sum)); + mustEqual(10L, a.get(0)); } /** @@ -239,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)); } /** @@ -251,9 +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)); - assertEquals(35, a.get(0)); + mustEqual(18, a.updateAndGet(0, Atomic8Test::addInt17)); + mustEqual(35, a.updateAndGet(0, Atomic8Test::addInt17)); + mustEqual(35, a.get(0)); } /** @@ -263,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)); } /** @@ -275,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)); } /** @@ -284,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)); } /** @@ -296,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)); } /** @@ -307,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)); } /** @@ -319,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)); } /** @@ -330,12 +335,12 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testLongFieldUpdaterGetAndUpdate() { - 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); + 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); } /** @@ -343,12 +348,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testLongFieldUpdaterUpdateAndGet() { - 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); + 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); } /** @@ -356,12 +361,12 @@ public class Atomic8Test extends JSR166T * and updates with supplied function. */ public void testLongFieldUpdaterGetAndAccumulate() { - 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); + 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); } /** @@ -369,12 +374,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testLongFieldUpdaterAccumulateAndGet() { - 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); + 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); } /** @@ -382,12 +387,12 @@ public class Atomic8Test extends JSR166T * result of supplied function */ public void testIntegerFieldUpdaterGetAndUpdate() { - AtomicIntegerFieldUpdater a = anIntFieldUpdater(); + 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); + mustEqual(1, a.getAndUpdate(this, Atomic8Test::addInt17)); + mustEqual(18, a.getAndUpdate(this, Atomic8Test::addInt17)); + mustEqual(35, a.get(this)); + mustEqual(35, anIntField); } /** @@ -395,12 +400,12 @@ public class Atomic8Test extends JSR166T * returns result. */ public void testIntegerFieldUpdaterUpdateAndGet() { - AtomicIntegerFieldUpdater a = anIntFieldUpdater(); + 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); + mustEqual(18, a.updateAndGet(this, Atomic8Test::addInt17)); + mustEqual(35, a.updateAndGet(this, Atomic8Test::addInt17)); + mustEqual(35, a.get(this)); + mustEqual(35, anIntField); } /** @@ -408,12 +413,12 @@ public class Atomic8Test extends JSR166T * and updates with supplied function. */ public void testIntegerFieldUpdaterGetAndAccumulate() { - AtomicIntegerFieldUpdater a = anIntFieldUpdater(); + 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); + mustEqual(1, a.getAndAccumulate(this, 2, Integer::sum)); + mustEqual(3, a.getAndAccumulate(this, 3, Integer::sum)); + mustEqual(6, a.get(this)); + mustEqual(6, anIntField); } /** @@ -421,26 +426,25 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testIntegerFieldUpdaterAccumulateAndGet() { - AtomicIntegerFieldUpdater a = anIntFieldUpdater(); + 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); + 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 = anIntegerFieldUpdater(); + 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)); - assertEquals(new Integer(35), anIntegerField); + mustEqual( 1, a.getAndUpdate(this, Atomic8Test::addItem17)); + mustEqual( 18, a.getAndUpdate(this, Atomic8Test::addItem17)); + mustEqual( 35, a.get(this)); + mustEqual( 35, anItemField); } /** @@ -448,12 +452,12 @@ public class Atomic8Test extends JSR166T * function and returns result. */ public void testReferenceFieldUpdaterUpdateAndGet() { - AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); + 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)); - assertEquals(new Integer(35), a.get(this)); - assertEquals(new Integer(35), anIntegerField); + mustEqual( 18, a.updateAndGet(this, Atomic8Test::addItem17)); + mustEqual( 35, a.updateAndGet(this, Atomic8Test::addItem17)); + mustEqual( 35, a.get(this)); + mustEqual( 35, anItemField); } /** @@ -461,12 +465,12 @@ public class Atomic8Test extends JSR166T * with supplied function. */ public void testReferenceFieldUpdaterGetAndAccumulate() { - AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); + 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)); - assertEquals(new Integer(6), anIntegerField); + mustEqual( 1, a.getAndAccumulate(this, two, Atomic8Test::sumItem)); + mustEqual( 3, a.getAndAccumulate(this, three, Atomic8Test::sumItem)); + mustEqual( 6, a.get(this)); + mustEqual( 6, anItemField); } /** @@ -474,20 +478,22 @@ public class Atomic8Test extends JSR166T * supplied function and returns result. */ public void testReferenceFieldUpdaterAccumulateAndGet() { - AtomicReferenceFieldUpdater a = anIntegerFieldUpdater(); + 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)); - assertEquals(new Integer(10), a.get(this)); - assertEquals(new Integer(10), anIntegerField); + 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 NullPointerException on * null function argument */ + @SuppressWarnings("unchecked") public void testGetAndUpdateNPE() { - Runnable[] throwingActions = { + assertThrows( + NullPointerException.class, () -> new AtomicLong().getAndUpdate(null), () -> new AtomicInteger().getAndUpdate(null), () -> new AtomicReference().getAndUpdate(null), @@ -496,19 +502,16 @@ public class Atomic8Test extends JSR166T () -> 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); + () -> anItemFieldUpdater().getAndUpdate(this, null)); } /** * All Atomic updateAndGet methods throw NullPointerException on null function argument */ + @SuppressWarnings("unchecked") public void testUpdateAndGetNPE() { - Runnable[] throwingActions = { + assertThrows( + NullPointerException.class, () -> new AtomicLong().updateAndGet(null), () -> new AtomicInteger().updateAndGet(null), () -> new AtomicReference().updateAndGet(null), @@ -517,17 +520,17 @@ public class Atomic8Test extends JSR166T () -> new AtomicReferenceArray(1).updateAndGet(0, null), () -> aLongFieldUpdater().updateAndGet(this, null), () -> anIntFieldUpdater().updateAndGet(this, null), - () -> anIntegerFieldUpdater().updateAndGet(this, null), - }; - assertThrows(NullPointerException.class, throwingActions); + () -> anItemFieldUpdater().updateAndGet(this, null)); } /** * All Atomic getAndAccumulate methods throw NullPointerException * on null function argument */ + @SuppressWarnings("unchecked") public void testGetAndAccumulateNPE() { - Runnable[] throwingActions = { + assertThrows( + NullPointerException.class, () -> new AtomicLong().getAndAccumulate(1L, null), () -> new AtomicInteger().getAndAccumulate(1, null), () -> new AtomicReference().getAndAccumulate(one, null), @@ -536,17 +539,17 @@ public class Atomic8Test extends JSR166T () -> 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); + () -> anItemFieldUpdater().getAndAccumulate(this, one, null)); } /** * All Atomic accumulateAndGet methods throw NullPointerException * on null function argument */ + @SuppressWarnings("unchecked") public void testAccumulateAndGetNPE() { - Runnable[] throwingActions = { + assertThrows( + NullPointerException.class, () -> new AtomicLong().accumulateAndGet(1L, null), () -> new AtomicInteger().accumulateAndGet(1, null), () -> new AtomicReference().accumulateAndGet(one, null), @@ -555,9 +558,42 @@ public class Atomic8Test extends JSR166T () -> 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); + () -> 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)); + } } }