1 /*
   2  * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 // -- This file was mechanically generated: Do not edit! -- //
  25 
  26 /*
  27  * @test
  28  * @run testng/othervm -Diters=10    -XX:+EnableValhalla -Xint                   VarHandleTestAccessPoint
  29  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla -XX:TieredStopAtLevel=1 VarHandleTestAccessPoint
  30  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla                         VarHandleTestAccessPoint
  31  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla -XX:-TieredCompilation  VarHandleTestAccessPoint
  32  */
  33 
  34 import org.testng.annotations.BeforeClass;
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 
  38 import java.lang.invoke.MethodHandles;
  39 import java.lang.invoke.VarHandle;
  40 import java.util.ArrayList;
  41 import java.util.Arrays;
  42 import java.util.List;
  43 
  44 import static org.testng.Assert.*;
  45 
  46 public class VarHandleTestAccessPoint extends VarHandleBaseTest {
  47     static final Point static_final_v = Point.getInstance(1,1);
  48 
  49     static Point static_v;
  50 
  51     final Point final_v = Point.getInstance(1,1);
  52 
  53     Point v;
  54 
  55     static final Point static_final_v2 = Point.getInstance(1,1);
  56 
  57     static Point static_v2;
  58 
  59     final Point final_v2 = Point.getInstance(1,1);
  60 
  61     Point v2;
  62 
  63     VarHandle vhFinalField;
  64 
  65     VarHandle vhField;
  66 
  67     VarHandle vhStaticField;
  68 
  69     VarHandle vhStaticFinalField;
  70 
  71     VarHandle vhArray;
  72 
  73     VarHandle vhArrayObject;
  74     VarHandle vhValueTypeField;
  75 
  76     VarHandle[] allocate(boolean same) {
  77         List<VarHandle> vhs = new ArrayList<>();
  78 
  79         String postfix = same ? "" : "2";
  80         VarHandle vh;
  81         try {
  82             vh = MethodHandles.lookup().findVarHandle(
  83                     VarHandleTestAccessPoint.class, "final_v" + postfix, Point.class);
  84             vhs.add(vh);
  85 
  86             vh = MethodHandles.lookup().findVarHandle(
  87                     VarHandleTestAccessPoint.class, "v" + postfix, Point.class);
  88             vhs.add(vh);
  89 
  90             vh = MethodHandles.lookup().findStaticVarHandle(
  91                 VarHandleTestAccessPoint.class, "static_final_v" + postfix, Point.class);
  92             vhs.add(vh);
  93 
  94             vh = MethodHandles.lookup().findStaticVarHandle(
  95                 VarHandleTestAccessPoint.class, "static_v" + postfix, Point.class);
  96             vhs.add(vh);
  97 
  98             if (same) {
  99                 vh = MethodHandles.arrayElementVarHandle(Point[].class);
 100             }
 101             else {
 102                 vh = MethodHandles.arrayElementVarHandle(String[].class);
 103             }
 104             vhs.add(vh);
 105         } catch (Exception e) {
 106             throw new InternalError(e);
 107         }
 108         return vhs.toArray(new VarHandle[0]);
 109     }
 110 
 111     @BeforeClass
 112     public void setup() throws Exception {
 113         vhFinalField = MethodHandles.lookup().findVarHandle(
 114                 VarHandleTestAccessPoint.class, "final_v", Point.class);
 115 
 116         vhField = MethodHandles.lookup().findVarHandle(
 117                 VarHandleTestAccessPoint.class, "v", Point.class);
 118 
 119         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 120             VarHandleTestAccessPoint.class, "static_final_v", Point.class);
 121 
 122         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 123             VarHandleTestAccessPoint.class, "static_v", Point.class);
 124 
 125         vhArray = MethodHandles.arrayElementVarHandle(Point[].class);
 126         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 127 
 128         vhValueTypeField = MethodHandles.lookup().findVarHandle(
 129                     Value.class, "Point_v", Point.class);
 130     }
 131 
 132 
 133     @DataProvider
 134     public Object[][] varHandlesProvider() throws Exception {
 135         List<VarHandle> vhs = new ArrayList<>();
 136         vhs.add(vhField);
 137         vhs.add(vhStaticField);
 138         vhs.add(vhArray);
 139 
 140         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 141     }
 142 
 143     @Test
 144     public void testEquals() {
 145         VarHandle[] vhs1 = allocate(true);
 146         VarHandle[] vhs2 = allocate(true);
 147 
 148         for (int i = 0; i < vhs1.length; i++) {
 149             for (int j = 0; j < vhs1.length; j++) {
 150                 if (i != j) {
 151                     assertNotEquals(vhs1[i], vhs1[j]);
 152                     assertNotEquals(vhs1[i], vhs2[j]);
 153                 }
 154             }
 155         }
 156 
 157         VarHandle[] vhs3 = allocate(false);
 158         for (int i = 0; i < vhs1.length; i++) {
 159             assertNotEquals(vhs1[i], vhs3[i]);
 160         }
 161     }
 162 
 163     @Test(dataProvider = "varHandlesProvider")
 164     public void testIsAccessModeSupported(VarHandle vh) {
 165         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 166         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 167         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 168         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 169         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 170         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 171         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 172         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 173 
 174         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 175         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 176         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 178         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 179         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 180         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 181         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 182         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 183         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
 184         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
 185 
 186         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 187         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
 188         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
 189 
 190         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
 191         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
 192         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
 193         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
 194         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 195         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 196         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 197         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 198         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 199     }
 200 
 201 
 202     @DataProvider
 203     public Object[][] typesProvider() throws Exception {
 204         List<Object[]> types = new ArrayList<>();
 205         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessPoint.class)});
 206         types.add(new Object[] {vhStaticField, Arrays.asList()});
 207         types.add(new Object[] {vhArray, Arrays.asList(Point[].class, int.class)});
 208 
 209         return types.stream().toArray(Object[][]::new);
 210     }
 211 
 212     @Test(dataProvider = "typesProvider")
 213     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 214         assertEquals(vh.varType(), Point.class);
 215 
 216         assertEquals(vh.coordinateTypes(), pts);
 217 
 218         testTypes(vh);
 219     }
 220 
 221 
 222     @Test
 223     public void testLookupInstanceToStatic() {
 224         checkIAE("Lookup of static final field to instance final field", () -> {
 225             MethodHandles.lookup().findStaticVarHandle(
 226                     VarHandleTestAccessPoint.class, "final_v", Point.class);
 227         });
 228 
 229         checkIAE("Lookup of static field to instance field", () -> {
 230             MethodHandles.lookup().findStaticVarHandle(
 231                     VarHandleTestAccessPoint.class, "v", Point.class);
 232         });
 233     }
 234 
 235     @Test
 236     public void testLookupStaticToInstance() {
 237         checkIAE("Lookup of instance final field to static final field", () -> {
 238             MethodHandles.lookup().findVarHandle(
 239                 VarHandleTestAccessPoint.class, "static_final_v", Point.class);
 240         });
 241 
 242         checkIAE("Lookup of instance field to static field", () -> {
 243             vhStaticField = MethodHandles.lookup().findVarHandle(
 244                 VarHandleTestAccessPoint.class, "static_v", Point.class);
 245         });
 246     }
 247 
 248 
 249     @DataProvider
 250     public Object[][] accessTestCaseProvider() throws Exception {
 251         List<AccessTestCase<?>> cases = new ArrayList<>();
 252 
 253         cases.add(new VarHandleAccessTestCase("Instance final field",
 254                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 255         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 256                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 257                                               false));
 258 
 259         cases.add(new VarHandleAccessTestCase("Static final field",
 260                                               vhStaticFinalField, VarHandleTestAccessPoint::testStaticFinalField));
 261         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 262                                               vhStaticFinalField, VarHandleTestAccessPoint::testStaticFinalFieldUnsupported,
 263                                               false));
 264 
 265         cases.add(new VarHandleAccessTestCase("Instance field",
 266                                               vhField, vh -> testInstanceField(this, vh)));
 267         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 268                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 269                                               false));
 270 
 271         cases.add(new VarHandleAccessTestCase("Static field",
 272                                               vhStaticField, VarHandleTestAccessPoint::testStaticField));
 273         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 274                                               vhStaticField, VarHandleTestAccessPoint::testStaticFieldUnsupported,
 275                                               false));
 276 
 277         cases.add(new VarHandleAccessTestCase("Array",
 278                                               vhArray, VarHandleTestAccessPoint::testArray));
 279         cases.add(new VarHandleAccessTestCase("Array Object[]",
 280                                               vhArrayObject, VarHandleTestAccessPoint::testArray));
 281         cases.add(new VarHandleAccessTestCase("Array unsupported",
 282                                               vhArray, VarHandleTestAccessPoint::testArrayUnsupported,
 283                                               false));
 284         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 285                                               vhArray, VarHandleTestAccessPoint::testArrayIndexOutOfBounds,
 286                                               false));
 287         cases.add(new VarHandleAccessTestCase("Array store exception",
 288                                               vhArrayObject, VarHandleTestAccessPoint::testArrayStoreException,
 289                                               false));
 290         cases.add(new VarHandleAccessTestCase("Value type field",
 291                                               vhValueTypeField, vh -> testValueTypeField(Value.getInstance(), vh)));
 292         cases.add(new VarHandleAccessTestCase("Value type field unsupported",
 293                                               vhValueTypeField, vh -> testValueTypeFieldUnsupported(Value.getInstance(), vh),
 294                                               false));
 295         // Work around issue with jtreg summary reporting which truncates
 296         // the String result of Object.toString to 30 characters, hence
 297         // the first dummy argument
 298         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 299     }
 300 
 301     @Test(dataProvider = "accessTestCaseProvider")
 302     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 303         T t = atc.get();
 304         int iters = atc.requiresLoop() ? ITERS : 1;
 305         for (int c = 0; c < iters; c++) {
 306             atc.testAccess(t);
 307         }
 308     }
 309 
 310 
 311 
 312 
 313     static void testInstanceFinalField(VarHandleTestAccessPoint recv, VarHandle vh) {
 314         // Plain
 315         {
 316             Point x = (Point) vh.get(recv);
 317             assertEquals(x, Point.getInstance(1,1), "get Point value");
 318         }
 319 
 320 
 321         // Volatile
 322         {
 323             Point x = (Point) vh.getVolatile(recv);
 324             assertEquals(x, Point.getInstance(1,1), "getVolatile Point value");
 325         }
 326 
 327         // Lazy
 328         {
 329             Point x = (Point) vh.getAcquire(recv);
 330             assertEquals(x, Point.getInstance(1,1), "getRelease Point value");
 331         }
 332 
 333         // Opaque
 334         {
 335             Point x = (Point) vh.getOpaque(recv);
 336             assertEquals(x, Point.getInstance(1,1), "getOpaque Point value");
 337         }
 338     }
 339 
 340     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessPoint recv, VarHandle vh) {
 341         checkUOE(() -> {
 342             vh.set(recv, Point.getInstance(2,2));
 343         });
 344 
 345         checkUOE(() -> {
 346             vh.setVolatile(recv, Point.getInstance(2,2));
 347         });
 348 
 349         checkUOE(() -> {
 350             vh.setRelease(recv, Point.getInstance(2,2));
 351         });
 352 
 353         checkUOE(() -> {
 354             vh.setOpaque(recv, Point.getInstance(2,2));
 355         });
 356 
 357 
 358         checkUOE(() -> {
 359             Point o = (Point) vh.getAndAdd(recv, Point.getInstance(1,1));
 360         });
 361 
 362         checkUOE(() -> {
 363             Point o = (Point) vh.getAndAddAcquire(recv, Point.getInstance(1,1));
 364         });
 365 
 366         checkUOE(() -> {
 367             Point o = (Point) vh.getAndAddRelease(recv, Point.getInstance(1,1));
 368         });
 369 
 370         checkUOE(() -> {
 371             Point o = (Point) vh.getAndBitwiseOr(recv, Point.getInstance(1,1));
 372         });
 373 
 374         checkUOE(() -> {
 375             Point o = (Point) vh.getAndBitwiseOrAcquire(recv, Point.getInstance(1,1));
 376         });
 377 
 378         checkUOE(() -> {
 379             Point o = (Point) vh.getAndBitwiseOrRelease(recv, Point.getInstance(1,1));
 380         });
 381 
 382         checkUOE(() -> {
 383             Point o = (Point) vh.getAndBitwiseAnd(recv, Point.getInstance(1,1));
 384         });
 385 
 386         checkUOE(() -> {
 387             Point o = (Point) vh.getAndBitwiseAndAcquire(recv, Point.getInstance(1,1));
 388         });
 389 
 390         checkUOE(() -> {
 391             Point o = (Point) vh.getAndBitwiseAndRelease(recv, Point.getInstance(1,1));
 392         });
 393 
 394         checkUOE(() -> {
 395             Point o = (Point) vh.getAndBitwiseXor(recv, Point.getInstance(1,1));
 396         });
 397 
 398         checkUOE(() -> {
 399             Point o = (Point) vh.getAndBitwiseXorAcquire(recv, Point.getInstance(1,1));
 400         });
 401 
 402         checkUOE(() -> {
 403             Point o = (Point) vh.getAndBitwiseXorRelease(recv, Point.getInstance(1,1));
 404         });
 405     }
 406 
 407     static void testValueTypeField(Value recv, VarHandle vh) {
 408         // Plain
 409         {
 410             Point x = (Point) vh.get(recv);
 411             assertEquals(x, Point.getInstance(1,1), "get Point value");
 412         }
 413     }
 414 
 415     static void testValueTypeFieldUnsupported(Value recv, VarHandle vh) {
 416         checkUOE(() -> {
 417             vh.set(recv, Point.getInstance(2,2));
 418         });
 419     }
 420 
 421     static void testStaticFinalField(VarHandle vh) {
 422         // Plain
 423         {
 424             Point x = (Point) vh.get();
 425             assertEquals(x, Point.getInstance(1,1), "get Point value");
 426         }
 427 
 428 
 429         // Volatile
 430         {
 431             Point x = (Point) vh.getVolatile();
 432             assertEquals(x, Point.getInstance(1,1), "getVolatile Point value");
 433         }
 434 
 435         // Lazy
 436         {
 437             Point x = (Point) vh.getAcquire();
 438             assertEquals(x, Point.getInstance(1,1), "getRelease Point value");
 439         }
 440 
 441         // Opaque
 442         {
 443             Point x = (Point) vh.getOpaque();
 444             assertEquals(x, Point.getInstance(1,1), "getOpaque Point value");
 445         }
 446     }
 447 
 448     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 449         checkUOE(() -> {
 450             vh.set(Point.getInstance(2,2));
 451         });
 452 
 453         checkUOE(() -> {
 454             vh.setVolatile(Point.getInstance(2,2));
 455         });
 456 
 457         checkUOE(() -> {
 458             vh.setRelease(Point.getInstance(2,2));
 459         });
 460 
 461         checkUOE(() -> {
 462             vh.setOpaque(Point.getInstance(2,2));
 463         });
 464 
 465 
 466         checkUOE(() -> {
 467             Point o = (Point) vh.getAndAdd(Point.getInstance(1,1));
 468         });
 469 
 470         checkUOE(() -> {
 471             Point o = (Point) vh.getAndAddAcquire(Point.getInstance(1,1));
 472         });
 473 
 474         checkUOE(() -> {
 475             Point o = (Point) vh.getAndAddRelease(Point.getInstance(1,1));
 476         });
 477 
 478         checkUOE(() -> {
 479             Point o = (Point) vh.getAndBitwiseOr(Point.getInstance(1,1));
 480         });
 481 
 482         checkUOE(() -> {
 483             Point o = (Point) vh.getAndBitwiseOrAcquire(Point.getInstance(1,1));
 484         });
 485 
 486         checkUOE(() -> {
 487             Point o = (Point) vh.getAndBitwiseOrRelease(Point.getInstance(1,1));
 488         });
 489 
 490         checkUOE(() -> {
 491             Point o = (Point) vh.getAndBitwiseAnd(Point.getInstance(1,1));
 492         });
 493 
 494         checkUOE(() -> {
 495             Point o = (Point) vh.getAndBitwiseAndAcquire(Point.getInstance(1,1));
 496         });
 497 
 498         checkUOE(() -> {
 499             Point o = (Point) vh.getAndBitwiseAndRelease(Point.getInstance(1,1));
 500         });
 501 
 502         checkUOE(() -> {
 503             Point o = (Point) vh.getAndBitwiseXor(Point.getInstance(1,1));
 504         });
 505 
 506         checkUOE(() -> {
 507             Point o = (Point) vh.getAndBitwiseXorAcquire(Point.getInstance(1,1));
 508         });
 509 
 510         checkUOE(() -> {
 511             Point o = (Point) vh.getAndBitwiseXorRelease(Point.getInstance(1,1));
 512         });
 513     }
 514 
 515 
 516     static void testInstanceField(VarHandleTestAccessPoint recv, VarHandle vh) {
 517         // Plain
 518         {
 519             vh.set(recv, Point.getInstance(1,1));
 520             Point x = (Point) vh.get(recv);
 521             assertEquals(x, Point.getInstance(1,1), "set Point value");
 522         }
 523 
 524 
 525         // Volatile
 526         {
 527             vh.setVolatile(recv, Point.getInstance(2,2));
 528             Point x = (Point) vh.getVolatile(recv);
 529             assertEquals(x, Point.getInstance(2,2), "setVolatile Point value");
 530         }
 531 
 532         // Lazy
 533         {
 534             vh.setRelease(recv, Point.getInstance(1,1));
 535             Point x = (Point) vh.getAcquire(recv);
 536             assertEquals(x, Point.getInstance(1,1), "setRelease Point value");
 537         }
 538 
 539         // Opaque
 540         {
 541             vh.setOpaque(recv, Point.getInstance(2,2));
 542             Point x = (Point) vh.getOpaque(recv);
 543             assertEquals(x, Point.getInstance(2,2), "setOpaque Point value");
 544         }
 545 
 546         vh.set(recv, Point.getInstance(1,1));
 547 
 548         // Compare
 549         {
 550             boolean r = vh.compareAndSet(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 551             assertEquals(r, true, "success compareAndSet Point");
 552             Point x = (Point) vh.get(recv);
 553             assertEquals(x, Point.getInstance(2,2), "success compareAndSet Point value");
 554         }
 555 
 556         {
 557             boolean r = vh.compareAndSet(recv, Point.getInstance(1,1), Point.getInstance(3,3));
 558             assertEquals(r, false, "failing compareAndSet Point");
 559             Point x = (Point) vh.get(recv);
 560             assertEquals(x, Point.getInstance(2,2), "failing compareAndSet Point value");
 561         }
 562 
 563         {
 564             Point r = (Point) vh.compareAndExchange(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 565             assertEquals(r, Point.getInstance(2,2), "success compareAndExchange Point");
 566             Point x = (Point) vh.get(recv);
 567             assertEquals(x, Point.getInstance(1,1), "success compareAndExchange Point value");
 568         }
 569 
 570         {
 571             Point r = (Point) vh.compareAndExchange(recv, Point.getInstance(2,2), Point.getInstance(3,3));
 572             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchange Point");
 573             Point x = (Point) vh.get(recv);
 574             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchange Point value");
 575         }
 576 
 577         {
 578             Point r = (Point) vh.compareAndExchangeAcquire(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 579             assertEquals(r, Point.getInstance(1,1), "success compareAndExchangeAcquire Point");
 580             Point x = (Point) vh.get(recv);
 581             assertEquals(x, Point.getInstance(2,2), "success compareAndExchangeAcquire Point value");
 582         }
 583 
 584         {
 585             Point r = (Point) vh.compareAndExchangeAcquire(recv, Point.getInstance(1,1), Point.getInstance(3,3));
 586             assertEquals(r, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point");
 587             Point x = (Point) vh.get(recv);
 588             assertEquals(x, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point value");
 589         }
 590 
 591         {
 592             Point r = (Point) vh.compareAndExchangeRelease(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 593             assertEquals(r, Point.getInstance(2,2), "success compareAndExchangeRelease Point");
 594             Point x = (Point) vh.get(recv);
 595             assertEquals(x, Point.getInstance(1,1), "success compareAndExchangeRelease Point value");
 596         }
 597 
 598         {
 599             Point r = (Point) vh.compareAndExchangeRelease(recv, Point.getInstance(2,2), Point.getInstance(3,3));
 600             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchangeRelease Point");
 601             Point x = (Point) vh.get(recv);
 602             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchangeRelease Point value");
 603         }
 604 
 605         {
 606             boolean success = false;
 607             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 608                 success = vh.weakCompareAndSetPlain(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 609             }
 610             assertEquals(success, true, "weakCompareAndSetPlain Point");
 611             Point x = (Point) vh.get(recv);
 612             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetPlain Point value");
 613         }
 614 
 615         {
 616             boolean success = false;
 617             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 618                 success = vh.weakCompareAndSetAcquire(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 619             }
 620             assertEquals(success, true, "weakCompareAndSetAcquire Point");
 621             Point x = (Point) vh.get(recv);
 622             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSetAcquire Point");
 623         }
 624 
 625         {
 626             boolean success = false;
 627             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 628                 success = vh.weakCompareAndSetRelease(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 629             }
 630             assertEquals(success, true, "weakCompareAndSetRelease Point");
 631             Point x = (Point) vh.get(recv);
 632             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetRelease Point");
 633         }
 634 
 635         {
 636             boolean success = false;
 637             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 638                 success = vh.weakCompareAndSet(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 639             }
 640             assertEquals(success, true, "weakCompareAndSet Point");
 641             Point x = (Point) vh.get(recv);
 642             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSet Point value");
 643         }
 644 
 645         // Compare set and get
 646         {
 647             vh.set(recv, Point.getInstance(1,1));
 648 
 649             Point o = (Point) vh.getAndSet(recv, Point.getInstance(2,2));
 650             assertEquals(o, Point.getInstance(1,1), "getAndSet Point");
 651             Point x = (Point) vh.get(recv);
 652             assertEquals(x, Point.getInstance(2,2), "getAndSet Point value");
 653         }
 654 
 655         {
 656             vh.set(recv, Point.getInstance(1,1));
 657 
 658             Point o = (Point) vh.getAndSetAcquire(recv, Point.getInstance(2,2));
 659             assertEquals(o, Point.getInstance(1,1), "getAndSetAcquire Point");
 660             Point x = (Point) vh.get(recv);
 661             assertEquals(x, Point.getInstance(2,2), "getAndSetAcquire Point value");
 662         }
 663 
 664         {
 665             vh.set(recv, Point.getInstance(1,1));
 666 
 667             Point o = (Point) vh.getAndSetRelease(recv, Point.getInstance(2,2));
 668             assertEquals(o, Point.getInstance(1,1), "getAndSetRelease Point");
 669             Point x = (Point) vh.get(recv);
 670             assertEquals(x, Point.getInstance(2,2), "getAndSetRelease Point value");
 671         }
 672 
 673 
 674     }
 675 
 676     static void testInstanceFieldUnsupported(VarHandleTestAccessPoint recv, VarHandle vh) {
 677 
 678         checkUOE(() -> {
 679             Point o = (Point) vh.getAndAdd(recv, Point.getInstance(1,1));
 680         });
 681 
 682         checkUOE(() -> {
 683             Point o = (Point) vh.getAndAddAcquire(recv, Point.getInstance(1,1));
 684         });
 685 
 686         checkUOE(() -> {
 687             Point o = (Point) vh.getAndAddRelease(recv, Point.getInstance(1,1));
 688         });
 689 
 690         checkUOE(() -> {
 691             Point o = (Point) vh.getAndBitwiseOr(recv, Point.getInstance(1,1));
 692         });
 693 
 694         checkUOE(() -> {
 695             Point o = (Point) vh.getAndBitwiseOrAcquire(recv, Point.getInstance(1,1));
 696         });
 697 
 698         checkUOE(() -> {
 699             Point o = (Point) vh.getAndBitwiseOrRelease(recv, Point.getInstance(1,1));
 700         });
 701 
 702         checkUOE(() -> {
 703             Point o = (Point) vh.getAndBitwiseAnd(recv, Point.getInstance(1,1));
 704         });
 705 
 706         checkUOE(() -> {
 707             Point o = (Point) vh.getAndBitwiseAndAcquire(recv, Point.getInstance(1,1));
 708         });
 709 
 710         checkUOE(() -> {
 711             Point o = (Point) vh.getAndBitwiseAndRelease(recv, Point.getInstance(1,1));
 712         });
 713 
 714         checkUOE(() -> {
 715             Point o = (Point) vh.getAndBitwiseXor(recv, Point.getInstance(1,1));
 716         });
 717 
 718         checkUOE(() -> {
 719             Point o = (Point) vh.getAndBitwiseXorAcquire(recv, Point.getInstance(1,1));
 720         });
 721 
 722         checkUOE(() -> {
 723             Point o = (Point) vh.getAndBitwiseXorRelease(recv, Point.getInstance(1,1));
 724         });
 725     }
 726 
 727 
 728     static void testStaticField(VarHandle vh) {
 729         // Plain
 730         {
 731             vh.set(Point.getInstance(1,1));
 732             Point x = (Point) vh.get();
 733             assertEquals(x, Point.getInstance(1,1), "set Point value");
 734         }
 735 
 736 
 737         // Volatile
 738         {
 739             vh.setVolatile(Point.getInstance(2,2));
 740             Point x = (Point) vh.getVolatile();
 741             assertEquals(x, Point.getInstance(2,2), "setVolatile Point value");
 742         }
 743 
 744         // Lazy
 745         {
 746             vh.setRelease(Point.getInstance(1,1));
 747             Point x = (Point) vh.getAcquire();
 748             assertEquals(x, Point.getInstance(1,1), "setRelease Point value");
 749         }
 750 
 751         // Opaque
 752         {
 753             vh.setOpaque(Point.getInstance(2,2));
 754             Point x = (Point) vh.getOpaque();
 755             assertEquals(x, Point.getInstance(2,2), "setOpaque Point value");
 756         }
 757 
 758         vh.set(Point.getInstance(1,1));
 759 
 760         // Compare
 761         {
 762             boolean r = vh.compareAndSet(Point.getInstance(1,1), Point.getInstance(2,2));
 763             assertEquals(r, true, "success compareAndSet Point");
 764             Point x = (Point) vh.get();
 765             assertEquals(x, Point.getInstance(2,2), "success compareAndSet Point value");
 766         }
 767 
 768         {
 769             boolean r = vh.compareAndSet(Point.getInstance(1,1), Point.getInstance(3,3));
 770             assertEquals(r, false, "failing compareAndSet Point");
 771             Point x = (Point) vh.get();
 772             assertEquals(x, Point.getInstance(2,2), "failing compareAndSet Point value");
 773         }
 774 
 775         {
 776             Point r = (Point) vh.compareAndExchange(Point.getInstance(2,2), Point.getInstance(1,1));
 777             assertEquals(r, Point.getInstance(2,2), "success compareAndExchange Point");
 778             Point x = (Point) vh.get();
 779             assertEquals(x, Point.getInstance(1,1), "success compareAndExchange Point value");
 780         }
 781 
 782         {
 783             Point r = (Point) vh.compareAndExchange(Point.getInstance(2,2), Point.getInstance(3,3));
 784             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchange Point");
 785             Point x = (Point) vh.get();
 786             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchange Point value");
 787         }
 788 
 789         {
 790             Point r = (Point) vh.compareAndExchangeAcquire(Point.getInstance(1,1), Point.getInstance(2,2));
 791             assertEquals(r, Point.getInstance(1,1), "success compareAndExchangeAcquire Point");
 792             Point x = (Point) vh.get();
 793             assertEquals(x, Point.getInstance(2,2), "success compareAndExchangeAcquire Point value");
 794         }
 795 
 796         {
 797             Point r = (Point) vh.compareAndExchangeAcquire(Point.getInstance(1,1), Point.getInstance(3,3));
 798             assertEquals(r, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point");
 799             Point x = (Point) vh.get();
 800             assertEquals(x, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point value");
 801         }
 802 
 803         {
 804             Point r = (Point) vh.compareAndExchangeRelease(Point.getInstance(2,2), Point.getInstance(1,1));
 805             assertEquals(r, Point.getInstance(2,2), "success compareAndExchangeRelease Point");
 806             Point x = (Point) vh.get();
 807             assertEquals(x, Point.getInstance(1,1), "success compareAndExchangeRelease Point value");
 808         }
 809 
 810         {
 811             Point r = (Point) vh.compareAndExchangeRelease(Point.getInstance(2,2), Point.getInstance(3,3));
 812             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchangeRelease Point");
 813             Point x = (Point) vh.get();
 814             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchangeRelease Point value");
 815         }
 816 
 817         {
 818             boolean success = false;
 819             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 820                 success = vh.weakCompareAndSetPlain(Point.getInstance(1,1), Point.getInstance(2,2));
 821             }
 822             assertEquals(success, true, "weakCompareAndSetPlain Point");
 823             Point x = (Point) vh.get();
 824             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetPlain Point value");
 825         }
 826 
 827         {
 828             boolean success = false;
 829             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 830                 success = vh.weakCompareAndSetAcquire(Point.getInstance(2,2), Point.getInstance(1,1));
 831             }
 832             assertEquals(success, true, "weakCompareAndSetAcquire Point");
 833             Point x = (Point) vh.get();
 834             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSetAcquire Point");
 835         }
 836 
 837         {
 838             boolean success = false;
 839             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 840                 success = vh.weakCompareAndSetRelease(Point.getInstance(1,1), Point.getInstance(2,2));
 841             }
 842             assertEquals(success, true, "weakCompareAndSetRelease Point");
 843             Point x = (Point) vh.get();
 844             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetRelease Point");
 845         }
 846 
 847         {
 848             boolean success = false;
 849             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 850                 success = vh.weakCompareAndSet(Point.getInstance(2,2), Point.getInstance(1,1));
 851             }
 852             assertEquals(success, true, "weakCompareAndSet Point");
 853             Point x = (Point) vh.get();
 854             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSet Point");
 855         }
 856 
 857         // Compare set and get
 858         {
 859             vh.set(Point.getInstance(1,1));
 860 
 861             Point o = (Point) vh.getAndSet(Point.getInstance(2,2));
 862             assertEquals(o, Point.getInstance(1,1), "getAndSet Point");
 863             Point x = (Point) vh.get();
 864             assertEquals(x, Point.getInstance(2,2), "getAndSet Point value");
 865         }
 866 
 867         {
 868             vh.set(Point.getInstance(1,1));
 869 
 870             Point o = (Point) vh.getAndSetAcquire(Point.getInstance(2,2));
 871             assertEquals(o, Point.getInstance(1,1), "getAndSetAcquire Point");
 872             Point x = (Point) vh.get();
 873             assertEquals(x, Point.getInstance(2,2), "getAndSetAcquire Point value");
 874         }
 875 
 876         {
 877             vh.set(Point.getInstance(1,1));
 878 
 879             Point o = (Point) vh.getAndSetRelease(Point.getInstance(2,2));
 880             assertEquals(o, Point.getInstance(1,1), "getAndSetRelease Point");
 881             Point x = (Point) vh.get();
 882             assertEquals(x, Point.getInstance(2,2), "getAndSetRelease Point value");
 883         }
 884 
 885 
 886     }
 887 
 888     static void testStaticFieldUnsupported(VarHandle vh) {
 889 
 890         checkUOE(() -> {
 891             Point o = (Point) vh.getAndAdd(Point.getInstance(1,1));
 892         });
 893 
 894         checkUOE(() -> {
 895             Point o = (Point) vh.getAndAddAcquire(Point.getInstance(1,1));
 896         });
 897 
 898         checkUOE(() -> {
 899             Point o = (Point) vh.getAndAddRelease(Point.getInstance(1,1));
 900         });
 901 
 902         checkUOE(() -> {
 903             Point o = (Point) vh.getAndBitwiseOr(Point.getInstance(1,1));
 904         });
 905 
 906         checkUOE(() -> {
 907             Point o = (Point) vh.getAndBitwiseOrAcquire(Point.getInstance(1,1));
 908         });
 909 
 910         checkUOE(() -> {
 911             Point o = (Point) vh.getAndBitwiseOrRelease(Point.getInstance(1,1));
 912         });
 913 
 914         checkUOE(() -> {
 915             Point o = (Point) vh.getAndBitwiseAnd(Point.getInstance(1,1));
 916         });
 917 
 918         checkUOE(() -> {
 919             Point o = (Point) vh.getAndBitwiseAndAcquire(Point.getInstance(1,1));
 920         });
 921 
 922         checkUOE(() -> {
 923             Point o = (Point) vh.getAndBitwiseAndRelease(Point.getInstance(1,1));
 924         });
 925 
 926         checkUOE(() -> {
 927             Point o = (Point) vh.getAndBitwiseXor(Point.getInstance(1,1));
 928         });
 929 
 930         checkUOE(() -> {
 931             Point o = (Point) vh.getAndBitwiseXorAcquire(Point.getInstance(1,1));
 932         });
 933 
 934         checkUOE(() -> {
 935             Point o = (Point) vh.getAndBitwiseXorRelease(Point.getInstance(1,1));
 936         });
 937     }
 938 
 939 
 940     static void testArray(VarHandle vh) {
 941         Point[] array = new Point[10];
 942 
 943         for (int i = 0; i < array.length; i++) {
 944             // Plain
 945             {
 946                 vh.set(array, i, Point.getInstance(1,1));
 947                 Point x = (Point) vh.get(array, i);
 948                 assertEquals(x, Point.getInstance(1,1), "get Point value");
 949             }
 950 
 951 
 952             // Volatile
 953             {
 954                 vh.setVolatile(array, i, Point.getInstance(2,2));
 955                 Point x = (Point) vh.getVolatile(array, i);
 956                 assertEquals(x, Point.getInstance(2,2), "setVolatile Point value");
 957             }
 958 
 959             // Lazy
 960             {
 961                 vh.setRelease(array, i, Point.getInstance(1,1));
 962                 Point x = (Point) vh.getAcquire(array, i);
 963                 assertEquals(x, Point.getInstance(1,1), "setRelease Point value");
 964             }
 965 
 966             // Opaque
 967             {
 968                 vh.setOpaque(array, i, Point.getInstance(2,2));
 969                 Point x = (Point) vh.getOpaque(array, i);
 970                 assertEquals(x, Point.getInstance(2,2), "setOpaque Point value");
 971             }
 972 
 973             vh.set(array, i, Point.getInstance(1,1));
 974 
 975             // Compare
 976             {
 977                 boolean r = vh.compareAndSet(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
 978                 assertEquals(r, true, "success compareAndSet Point");
 979                 Point x = (Point) vh.get(array, i);
 980                 assertEquals(x, Point.getInstance(2,2), "success compareAndSet Point value");
 981             }
 982 
 983             {
 984                 boolean r = vh.compareAndSet(array, i, Point.getInstance(1,1), Point.getInstance(3,3));
 985                 assertEquals(r, false, "failing compareAndSet Point");
 986                 Point x = (Point) vh.get(array, i);
 987                 assertEquals(x, Point.getInstance(2,2), "failing compareAndSet Point value");
 988             }
 989 
 990             {
 991                 Point r = (Point) vh.compareAndExchange(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
 992                 assertEquals(r, Point.getInstance(2,2), "success compareAndExchange Point");
 993                 Point x = (Point) vh.get(array, i);
 994                 assertEquals(x, Point.getInstance(1,1), "success compareAndExchange Point value");
 995             }
 996 
 997             {
 998                 Point r = (Point) vh.compareAndExchange(array, i, Point.getInstance(2,2), Point.getInstance(3,3));
 999                 assertEquals(r, Point.getInstance(1,1), "failing compareAndExchange Point");
1000                 Point x = (Point) vh.get(array, i);
1001                 assertEquals(x, Point.getInstance(1,1), "failing compareAndExchange Point value");
1002             }
1003 
1004             {
1005                 Point r = (Point) vh.compareAndExchangeAcquire(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
1006                 assertEquals(r, Point.getInstance(1,1), "success compareAndExchangeAcquire Point");
1007                 Point x = (Point) vh.get(array, i);
1008                 assertEquals(x, Point.getInstance(2,2), "success compareAndExchangeAcquire Point value");
1009             }
1010 
1011             {
1012                 Point r = (Point) vh.compareAndExchangeAcquire(array, i, Point.getInstance(1,1), Point.getInstance(3,3));
1013                 assertEquals(r, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point");
1014                 Point x = (Point) vh.get(array, i);
1015                 assertEquals(x, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point value");
1016             }
1017 
1018             {
1019                 Point r = (Point) vh.compareAndExchangeRelease(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
1020                 assertEquals(r, Point.getInstance(2,2), "success compareAndExchangeRelease Point");
1021                 Point x = (Point) vh.get(array, i);
1022                 assertEquals(x, Point.getInstance(1,1), "success compareAndExchangeRelease Point value");
1023             }
1024 
1025             {
1026                 Point r = (Point) vh.compareAndExchangeRelease(array, i, Point.getInstance(2,2), Point.getInstance(3,3));
1027                 assertEquals(r, Point.getInstance(1,1), "failing compareAndExchangeRelease Point");
1028                 Point x = (Point) vh.get(array, i);
1029                 assertEquals(x, Point.getInstance(1,1), "failing compareAndExchangeRelease Point value");
1030             }
1031 
1032             {
1033                 boolean success = false;
1034                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1035                     success = vh.weakCompareAndSetPlain(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
1036                 }
1037                 assertEquals(success, true, "weakCompareAndSetPlain Point");
1038                 Point x = (Point) vh.get(array, i);
1039                 assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetPlain Point value");
1040             }
1041 
1042             {
1043                 boolean success = false;
1044                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1045                     success = vh.weakCompareAndSetAcquire(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
1046                 }
1047                 assertEquals(success, true, "weakCompareAndSetAcquire Point");
1048                 Point x = (Point) vh.get(array, i);
1049                 assertEquals(x, Point.getInstance(1,1), "weakCompareAndSetAcquire Point");
1050             }
1051 
1052             {
1053                 boolean success = false;
1054                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1055                     success = vh.weakCompareAndSetRelease(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
1056                 }
1057                 assertEquals(success, true, "weakCompareAndSetRelease Point");
1058                 Point x = (Point) vh.get(array, i);
1059                 assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetRelease Point");
1060             }
1061 
1062             {
1063                 boolean success = false;
1064                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1065                     success = vh.weakCompareAndSet(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
1066                 }
1067                 assertEquals(success, true, "weakCompareAndSet Point");
1068                 Point x = (Point) vh.get(array, i);
1069                 assertEquals(x, Point.getInstance(1,1), "weakCompareAndSet Point");
1070             }
1071 
1072             // Compare set and get
1073             {
1074                 vh.set(array, i, Point.getInstance(1,1));
1075 
1076                 Point o = (Point) vh.getAndSet(array, i, Point.getInstance(2,2));
1077                 assertEquals(o, Point.getInstance(1,1), "getAndSet Point");
1078                 Point x = (Point) vh.get(array, i);
1079                 assertEquals(x, Point.getInstance(2,2), "getAndSet Point value");
1080             }
1081 
1082             {
1083                 vh.set(array, i, Point.getInstance(1,1));
1084 
1085                 Point o = (Point) vh.getAndSetAcquire(array, i, Point.getInstance(2,2));
1086                 assertEquals(o, Point.getInstance(1,1), "getAndSetAcquire Point");
1087                 Point x = (Point) vh.get(array, i);
1088                 assertEquals(x, Point.getInstance(2,2), "getAndSetAcquire Point value");
1089             }
1090 
1091             {
1092                 vh.set(array, i, Point.getInstance(1,1));
1093 
1094                 Point o = (Point) vh.getAndSetRelease(array, i, Point.getInstance(2,2));
1095                 assertEquals(o, Point.getInstance(1,1), "getAndSetRelease Point");
1096                 Point x = (Point) vh.get(array, i);
1097                 assertEquals(x, Point.getInstance(2,2), "getAndSetRelease Point value");
1098             }
1099 
1100 
1101         }
1102     }
1103 
1104     static void testArrayUnsupported(VarHandle vh) {
1105         Point[] array = new Point[10];
1106 
1107         int i = 0;
1108 
1109         checkUOE(() -> {
1110             Point o = (Point) vh.getAndAdd(array, i, Point.getInstance(1,1));
1111         });
1112 
1113         checkUOE(() -> {
1114             Point o = (Point) vh.getAndAddAcquire(array, i, Point.getInstance(1,1));
1115         });
1116 
1117         checkUOE(() -> {
1118             Point o = (Point) vh.getAndAddRelease(array, i, Point.getInstance(1,1));
1119         });
1120 
1121         checkUOE(() -> {
1122             Point o = (Point) vh.getAndBitwiseOr(array, i, Point.getInstance(1,1));
1123         });
1124 
1125         checkUOE(() -> {
1126             Point o = (Point) vh.getAndBitwiseOrAcquire(array, i, Point.getInstance(1,1));
1127         });
1128 
1129         checkUOE(() -> {
1130             Point o = (Point) vh.getAndBitwiseOrRelease(array, i, Point.getInstance(1,1));
1131         });
1132 
1133         checkUOE(() -> {
1134             Point o = (Point) vh.getAndBitwiseAnd(array, i, Point.getInstance(1,1));
1135         });
1136 
1137         checkUOE(() -> {
1138             Point o = (Point) vh.getAndBitwiseAndAcquire(array, i, Point.getInstance(1,1));
1139         });
1140 
1141         checkUOE(() -> {
1142             Point o = (Point) vh.getAndBitwiseAndRelease(array, i, Point.getInstance(1,1));
1143         });
1144 
1145         checkUOE(() -> {
1146             Point o = (Point) vh.getAndBitwiseXor(array, i, Point.getInstance(1,1));
1147         });
1148 
1149         checkUOE(() -> {
1150             Point o = (Point) vh.getAndBitwiseXorAcquire(array, i, Point.getInstance(1,1));
1151         });
1152 
1153         checkUOE(() -> {
1154             Point o = (Point) vh.getAndBitwiseXorRelease(array, i, Point.getInstance(1,1));
1155         });
1156     }
1157 
1158     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1159         Point[] array = new Point[10];
1160 
1161         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1162             final int ci = i;
1163 
1164             checkIOOBE(() -> {
1165                 Point x = (Point) vh.get(array, ci);
1166             });
1167 
1168             checkIOOBE(() -> {
1169                 vh.set(array, ci, Point.getInstance(1,1));
1170             });
1171 
1172             checkIOOBE(() -> {
1173                 Point x = (Point) vh.getVolatile(array, ci);
1174             });
1175 
1176             checkIOOBE(() -> {
1177                 vh.setVolatile(array, ci, Point.getInstance(1,1));
1178             });
1179 
1180             checkIOOBE(() -> {
1181                 Point x = (Point) vh.getAcquire(array, ci);
1182             });
1183 
1184             checkIOOBE(() -> {
1185                 vh.setRelease(array, ci, Point.getInstance(1,1));
1186             });
1187 
1188             checkIOOBE(() -> {
1189                 Point x = (Point) vh.getOpaque(array, ci);
1190             });
1191 
1192             checkIOOBE(() -> {
1193                 vh.setOpaque(array, ci, Point.getInstance(1,1));
1194             });
1195 
1196             checkIOOBE(() -> {
1197                 boolean r = vh.compareAndSet(array, ci, Point.getInstance(1,1), Point.getInstance(2,2));
1198             });
1199 
1200             checkIOOBE(() -> {
1201                 Point r = (Point) vh.compareAndExchange(array, ci, Point.getInstance(2,2), Point.getInstance(1,1));
1202             });
1203 
1204             checkIOOBE(() -> {
1205                 Point r = (Point) vh.compareAndExchangeAcquire(array, ci, Point.getInstance(2,2), Point.getInstance(1,1));
1206             });
1207 
1208             checkIOOBE(() -> {
1209                 Point r = (Point) vh.compareAndExchangeRelease(array, ci, Point.getInstance(2,2), Point.getInstance(1,1));
1210             });
1211 
1212             checkIOOBE(() -> {
1213                 boolean r = vh.weakCompareAndSetPlain(array, ci, Point.getInstance(1,1), Point.getInstance(2,2));
1214             });
1215 
1216             checkIOOBE(() -> {
1217                 boolean r = vh.weakCompareAndSet(array, ci, Point.getInstance(1,1), Point.getInstance(2,2));
1218             });
1219 
1220             checkIOOBE(() -> {
1221                 boolean r = vh.weakCompareAndSetAcquire(array, ci, Point.getInstance(1,1), Point.getInstance(2,2));
1222             });
1223 
1224             checkIOOBE(() -> {
1225                 boolean r = vh.weakCompareAndSetRelease(array, ci, Point.getInstance(1,1), Point.getInstance(2,2));
1226             });
1227 
1228             checkIOOBE(() -> {
1229                 Point o = (Point) vh.getAndSet(array, ci, Point.getInstance(1,1));
1230             });
1231 
1232             checkIOOBE(() -> {
1233                 Point o = (Point) vh.getAndSetAcquire(array, ci, Point.getInstance(1,1));
1234             });
1235 
1236             checkIOOBE(() -> {
1237                 Point o = (Point) vh.getAndSetRelease(array, ci, Point.getInstance(1,1));
1238             });
1239 
1240 
1241         }
1242     }
1243 
1244     static void testArrayStoreException(VarHandle vh) throws Throwable {
1245         Object[] array = new Point[10];
1246         Arrays.fill(array, Point.getInstance(1,1));
1247         Object value = new Object();
1248 
1249         // Set
1250         checkASE(() -> {
1251             vh.set(array, 0, value);
1252         });
1253 
1254         // SetVolatile
1255         checkASE(() -> {
1256             vh.setVolatile(array, 0, value);
1257         });
1258 
1259         // SetOpaque
1260         checkASE(() -> {
1261             vh.setOpaque(array, 0, value);
1262         });
1263 
1264         // SetRelease
1265         checkASE(() -> {
1266             vh.setRelease(array, 0, value);
1267         });
1268 
1269         // CompareAndSet
1270         checkASE(() -> { // receiver reference class
1271             boolean r = vh.compareAndSet(array, 0, Point.getInstance(1,1), value);
1272         });
1273 
1274         // WeakCompareAndSet
1275         checkASE(() -> { // receiver reference class
1276             boolean r = vh.weakCompareAndSetPlain(array, 0, Point.getInstance(1,1), value);
1277         });
1278 
1279         // WeakCompareAndSetVolatile
1280         checkASE(() -> { // receiver reference class
1281             boolean r = vh.weakCompareAndSet(array, 0, Point.getInstance(1,1), value);
1282         });
1283 
1284         // WeakCompareAndSetAcquire
1285         checkASE(() -> { // receiver reference class
1286             boolean r = vh.weakCompareAndSetAcquire(array, 0, Point.getInstance(1,1), value);
1287         });
1288 
1289         // WeakCompareAndSetRelease
1290         checkASE(() -> { // receiver reference class
1291             boolean r = vh.weakCompareAndSetRelease(array, 0, Point.getInstance(1,1), value);
1292         });
1293 
1294         // CompareAndExchange
1295         checkASE(() -> { // receiver reference class
1296             Point x = (Point) vh.compareAndExchange(array, 0, Point.getInstance(1,1), value);
1297         });
1298 
1299         // CompareAndExchangeAcquire
1300         checkASE(() -> { // receiver reference class
1301             Point x = (Point) vh.compareAndExchangeAcquire(array, 0, Point.getInstance(1,1), value);
1302         });
1303 
1304         // CompareAndExchangeRelease
1305         checkASE(() -> { // receiver reference class
1306             Point x = (Point) vh.compareAndExchangeRelease(array, 0, Point.getInstance(1,1), value);
1307         });
1308 
1309         // GetAndSet
1310         checkASE(() -> { // receiver reference class
1311             Point x = (Point) vh.getAndSet(array, 0, value);
1312         });
1313 
1314         // GetAndSetAcquire
1315         checkASE(() -> { // receiver reference class
1316             Point x = (Point) vh.getAndSetAcquire(array, 0, value);
1317         });
1318 
1319         // GetAndSetRelease
1320         checkASE(() -> { // receiver reference class
1321             Point x = (Point) vh.getAndSetRelease(array, 0, value);
1322         });
1323     }
1324 }
1325