1 /*
   2  * Copyright (c) 2015, 2018, 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 -XX:+EnableValhalla -Diters=2000 VarHandleTestMethodHandleAccessPoint
  29  */
  30 
  31 import org.testng.annotations.BeforeClass;
  32 import org.testng.annotations.DataProvider;
  33 import org.testng.annotations.Test;
  34 
  35 import java.lang.invoke.MethodHandles;
  36 import java.lang.invoke.VarHandle;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 
  41 import static org.testng.Assert.*;
  42 
  43 public class VarHandleTestMethodHandleAccessPoint extends VarHandleBaseTest {
  44     static final Point static_final_v = Point.getInstance(1,1);
  45 
  46     static Point static_v;
  47 
  48     final Point final_v = Point.getInstance(1,1);
  49 
  50     Point v;
  51 
  52     VarHandle vhFinalField;
  53 
  54     VarHandle vhField;
  55 
  56     VarHandle vhStaticField;
  57 
  58     VarHandle vhStaticFinalField;
  59 
  60     VarHandle vhArray;
  61 
  62     VarHandle vhValueTypeField;
  63 
  64     @BeforeClass
  65     public void setup() throws Exception {
  66         vhFinalField = MethodHandles.lookup().findVarHandle(
  67                 VarHandleTestMethodHandleAccessPoint.class, "final_v", Point.class);
  68 
  69         vhField = MethodHandles.lookup().findVarHandle(
  70                 VarHandleTestMethodHandleAccessPoint.class, "v", Point.class);
  71 
  72         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  73             VarHandleTestMethodHandleAccessPoint.class, "static_final_v", Point.class);
  74 
  75         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  76             VarHandleTestMethodHandleAccessPoint.class, "static_v", Point.class);
  77 
  78         vhArray = MethodHandles.arrayElementVarHandle(Point[].class);
  79 
  80         vhValueTypeField = MethodHandles.lookup().findVarHandle(
  81                     Value.class, "Point_v", Point.class);
  82     }
  83 
  84 
  85     @DataProvider
  86     public Object[][] accessTestCaseProvider() throws Exception {
  87         List<AccessTestCase<?>> cases = new ArrayList<>();
  88 
  89         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
  90             cases.add(new MethodHandleAccessTestCase("Instance field",
  91                                                      vhField, f, hs -> testInstanceField(this, hs)));
  92             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
  93                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
  94                                                      false));
  95 
  96             cases.add(new MethodHandleAccessTestCase("Static field",
  97                                                      vhStaticField, f, VarHandleTestMethodHandleAccessPoint::testStaticField));
  98             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
  99                                                      vhStaticField, f, VarHandleTestMethodHandleAccessPoint::testStaticFieldUnsupported,
 100                                                      false));
 101 
 102             cases.add(new MethodHandleAccessTestCase("Array",
 103                                                      vhArray, f, VarHandleTestMethodHandleAccessPoint::testArray));
 104             cases.add(new MethodHandleAccessTestCase("Array unsupported",
 105                                                      vhArray, f, VarHandleTestMethodHandleAccessPoint::testArrayUnsupported,
 106                                                      false));
 107             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
 108                                                      vhArray, f, VarHandleTestMethodHandleAccessPoint::testArrayIndexOutOfBounds,
 109                                                      false));
 110         cases.add(new MethodHandleAccessTestCase("Value type field",
 111                                                  vhValueTypeField, f, hs -> testValueTypeField(Value.getInstance(), hs)));
 112         cases.add(new MethodHandleAccessTestCase("Value type field unsupported",
 113                                                  vhValueTypeField, f, hs -> testValueTypeFieldUnsupported(Value.getInstance(), hs),
 114                                                  false));
 115         }
 116 
 117         // Work around issue with jtreg summary reporting which truncates
 118         // the String result of Object.toString to 30 characters, hence
 119         // the first dummy argument
 120         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 121     }
 122 
 123     @Test(dataProvider = "accessTestCaseProvider")
 124     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 125         T t = atc.get();
 126         int iters = atc.requiresLoop() ? ITERS : 1;
 127         for (int c = 0; c < iters; c++) {
 128             atc.testAccess(t);
 129         }
 130     }
 131 
 132 
 133     static void testInstanceField(VarHandleTestMethodHandleAccessPoint recv, Handles hs) throws Throwable {
 134         // Plain
 135         {
 136             hs.get(TestAccessMode.SET).invokeExact(recv, Point.getInstance(1,1));
 137             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 138             assertEquals(x, Point.getInstance(1,1), "set Point value");
 139         }
 140 
 141 
 142         // Volatile
 143         {
 144             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, Point.getInstance(2,2));
 145             Point x = (Point) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
 146             assertEquals(x, Point.getInstance(2,2), "setVolatile Point value");
 147         }
 148 
 149         // Lazy
 150         {
 151             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, Point.getInstance(1,1));
 152             Point x = (Point) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
 153             assertEquals(x, Point.getInstance(1,1), "setRelease Point value");
 154         }
 155 
 156         // Opaque
 157         {
 158             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, Point.getInstance(2,2));
 159             Point x = (Point) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
 160             assertEquals(x, Point.getInstance(2,2), "setOpaque Point value");
 161         }
 162 
 163         hs.get(TestAccessMode.SET).invokeExact(recv, Point.getInstance(1,1));
 164 
 165         // Compare
 166         {
 167             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 168             assertEquals(r, true, "success compareAndSet Point");
 169             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 170             assertEquals(x, Point.getInstance(2,2), "success compareAndSet Point value");
 171         }
 172 
 173         {
 174             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, Point.getInstance(1,1), Point.getInstance(3,3));
 175             assertEquals(r, false, "failing compareAndSet Point");
 176             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 177             assertEquals(x, Point.getInstance(2,2), "failing compareAndSet Point value");
 178         }
 179 
 180         {
 181             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 182             assertEquals(r, Point.getInstance(2,2), "success compareAndExchange Point");
 183             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 184             assertEquals(x, Point.getInstance(1,1), "success compareAndExchange Point value");
 185         }
 186 
 187         {
 188             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, Point.getInstance(2,2), Point.getInstance(3,3));
 189             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchange Point");
 190             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 191             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchange Point value");
 192         }
 193 
 194         {
 195             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 196             assertEquals(r, Point.getInstance(1,1), "success compareAndExchangeAcquire Point");
 197             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 198             assertEquals(x, Point.getInstance(2,2), "success compareAndExchangeAcquire Point value");
 199         }
 200 
 201         {
 202             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, Point.getInstance(1,1), Point.getInstance(3,3));
 203             assertEquals(r, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point");
 204             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 205             assertEquals(x, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point value");
 206         }
 207 
 208         {
 209             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 210             assertEquals(r, Point.getInstance(2,2), "success compareAndExchangeRelease Point");
 211             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 212             assertEquals(x, Point.getInstance(1,1), "success compareAndExchangeRelease Point value");
 213         }
 214 
 215         {
 216             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, Point.getInstance(2,2), Point.getInstance(3,3));
 217             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchangeRelease Point");
 218             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 219             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchangeRelease Point value");
 220         }
 221 
 222         {
 223             boolean success = false;
 224             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 225                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 226             }
 227             assertEquals(success, true, "weakCompareAndSetPlain Point");
 228             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 229             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetPlain Point value");
 230         }
 231 
 232         {
 233             boolean success = false;
 234             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 235                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 236             }
 237             assertEquals(success, true, "weakCompareAndSetAcquire Point");
 238             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 239             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSetAcquire Point");
 240         }
 241 
 242         {
 243             boolean success = false;
 244             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 245                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, Point.getInstance(1,1), Point.getInstance(2,2));
 246             }
 247             assertEquals(success, true, "weakCompareAndSetRelease Point");
 248             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 249             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetRelease Point");
 250         }
 251 
 252         {
 253             boolean success = false;
 254             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 255                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, Point.getInstance(2,2), Point.getInstance(1,1));
 256             }
 257             assertEquals(success, true, "weakCompareAndSet Point");
 258             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 259             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSet Point");
 260         }
 261 
 262         // Compare set and get
 263         {
 264             Point o = (Point) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, Point.getInstance(2,2));
 265             assertEquals(o, Point.getInstance(1,1), "getAndSet Point");
 266             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 267             assertEquals(x, Point.getInstance(2,2), "getAndSet Point value");
 268         }
 269 
 270 
 271     }
 272 
 273     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessPoint recv, Handles hs) throws Throwable {
 274 
 275         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 276             checkUOE(am, () -> {
 277                 Point r = (Point) hs.get(am).invokeExact(recv, Point.getInstance(1,1));
 278             });
 279         }
 280 
 281         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
 282             checkUOE(am, () -> {
 283                 Point r = (Point) hs.get(am).invokeExact(recv, Point.getInstance(1,1));
 284             });
 285         }
 286     }
 287 
 288     static void testValueTypeField(Value recv, Handles hs) throws Throwable {
 289         // Plain
 290         {
 291             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(recv);
 292             assertEquals(x, Point.getInstance(1,1), "get Point value");
 293         }
 294     }
 295 
 296     static void testValueTypeFieldUnsupported(Value recv, Handles hs) throws Throwable {
 297         // Plain
 298         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 299             checkUOE(am, () -> {
 300                 hs.get(am).invokeExact(recv, Point.getInstance(1,1));
 301             });
 302         }
 303 
 304         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 305             checkUOE(am, () -> {
 306                 Point r = (Point) hs.get(am).invokeExact(recv, Point.getInstance(1,1));
 307             });
 308         }
 309 
 310         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
 311             checkUOE(am, () -> {
 312                 Point r = (Point) hs.get(am).invokeExact(recv, Point.getInstance(1,1));
 313             });
 314         }
 315     }
 316 
 317     static void testStaticField(Handles hs) throws Throwable {
 318         // Plain
 319         {
 320             hs.get(TestAccessMode.SET).invokeExact(Point.getInstance(1,1));
 321             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 322             assertEquals(x, Point.getInstance(1,1), "set Point value");
 323         }
 324 
 325 
 326         // Volatile
 327         {
 328             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(Point.getInstance(2,2));
 329             Point x = (Point) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 330             assertEquals(x, Point.getInstance(2,2), "setVolatile Point value");
 331         }
 332 
 333         // Lazy
 334         {
 335             hs.get(TestAccessMode.SET_RELEASE).invokeExact(Point.getInstance(1,1));
 336             Point x = (Point) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
 337             assertEquals(x, Point.getInstance(1,1), "setRelease Point value");
 338         }
 339 
 340         // Opaque
 341         {
 342             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(Point.getInstance(2,2));
 343             Point x = (Point) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
 344             assertEquals(x, Point.getInstance(2,2), "setOpaque Point value");
 345         }
 346 
 347         hs.get(TestAccessMode.SET).invokeExact(Point.getInstance(1,1));
 348 
 349         // Compare
 350         {
 351             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(Point.getInstance(1,1), Point.getInstance(2,2));
 352             assertEquals(r, true, "success compareAndSet Point");
 353             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 354             assertEquals(x, Point.getInstance(2,2), "success compareAndSet Point value");
 355         }
 356 
 357         {
 358             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(Point.getInstance(1,1), Point.getInstance(3,3));
 359             assertEquals(r, false, "failing compareAndSet Point");
 360             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 361             assertEquals(x, Point.getInstance(2,2), "failing compareAndSet Point value");
 362         }
 363 
 364         {
 365             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(Point.getInstance(2,2), Point.getInstance(1,1));
 366             assertEquals(r, Point.getInstance(2,2), "success compareAndExchange Point");
 367             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 368             assertEquals(x, Point.getInstance(1,1), "success compareAndExchange Point value");
 369         }
 370 
 371         {
 372             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(Point.getInstance(2,2), Point.getInstance(3,3));
 373             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchange Point");
 374             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 375             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchange Point value");
 376         }
 377 
 378         {
 379             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(Point.getInstance(1,1), Point.getInstance(2,2));
 380             assertEquals(r, Point.getInstance(1,1), "success compareAndExchangeAcquire Point");
 381             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 382             assertEquals(x, Point.getInstance(2,2), "success compareAndExchangeAcquire Point value");
 383         }
 384 
 385         {
 386             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(Point.getInstance(1,1), Point.getInstance(3,3));
 387             assertEquals(r, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point");
 388             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 389             assertEquals(x, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point value");
 390         }
 391 
 392         {
 393             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(Point.getInstance(2,2), Point.getInstance(1,1));
 394             assertEquals(r, Point.getInstance(2,2), "success compareAndExchangeRelease Point");
 395             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 396             assertEquals(x, Point.getInstance(1,1), "success compareAndExchangeRelease Point value");
 397         }
 398 
 399         {
 400             Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(Point.getInstance(2,2), Point.getInstance(3,3));
 401             assertEquals(r, Point.getInstance(1,1), "failing compareAndExchangeRelease Point");
 402             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 403             assertEquals(x, Point.getInstance(1,1), "failing compareAndExchangeRelease Point value");
 404         }
 405 
 406         {
 407             boolean success = false;
 408             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 409                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(Point.getInstance(1,1), Point.getInstance(2,2));
 410             }
 411             assertEquals(success, true, "weakCompareAndSetPlain Point");
 412             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 413             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetPlain Point value");
 414         }
 415 
 416         {
 417             boolean success = false;
 418             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 419                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(Point.getInstance(2,2), Point.getInstance(1,1));
 420             }
 421             assertEquals(success, true, "weakCompareAndSetAcquire Point");
 422             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 423             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSetAcquire Point");
 424         }
 425 
 426         {
 427             boolean success = false;
 428             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 429                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(Point.getInstance(1,1), Point.getInstance(2,2));
 430             }
 431             assertEquals(success, true, "weakCompareAndSetRelease Point");
 432             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 433             assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetRelease Point");
 434         }
 435 
 436         {
 437             boolean success = false;
 438             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 439                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(Point.getInstance(2,2), Point.getInstance(1,1));
 440             }
 441             assertEquals(success, true, "weakCompareAndSet Point");
 442             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 443             assertEquals(x, Point.getInstance(1,1), "weakCompareAndSet Point");
 444         }
 445 
 446         // Compare set and get
 447         {
 448             hs.get(TestAccessMode.SET).invokeExact(Point.getInstance(1,1));
 449 
 450             Point o = (Point) hs.get(TestAccessMode.GET_AND_SET).invokeExact(Point.getInstance(2,2));
 451             assertEquals(o, Point.getInstance(1,1), "getAndSet Point");
 452             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 453             assertEquals(x, Point.getInstance(2,2), "getAndSet Point value");
 454         }
 455 
 456         // Compare set and get
 457         {
 458             hs.get(TestAccessMode.SET).invokeExact(Point.getInstance(1,1));
 459 
 460             Point o = (Point) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(Point.getInstance(2,2));
 461             assertEquals(o, Point.getInstance(1,1), "getAndSetAcquire Point");
 462             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 463             assertEquals(x, Point.getInstance(2,2), "getAndSetAcquire Point value");
 464         }
 465 
 466         // Compare set and get
 467         {
 468             hs.get(TestAccessMode.SET).invokeExact(Point.getInstance(1,1));
 469 
 470             Point o = (Point) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(Point.getInstance(2,2));
 471             assertEquals(o, Point.getInstance(1,1), "getAndSetRelease Point");
 472             Point x = (Point) hs.get(TestAccessMode.GET).invokeExact();
 473             assertEquals(x, Point.getInstance(2,2), "getAndSetRelease Point value");
 474         }
 475 
 476 
 477     }
 478 
 479     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
 480 
 481         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 482             checkUOE(am, () -> {
 483                 Point r = (Point) hs.get(am).invokeExact(Point.getInstance(1,1));
 484             });
 485         }
 486 
 487         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
 488             checkUOE(am, () -> {
 489                 Point r = (Point) hs.get(am).invokeExact(Point.getInstance(1,1));
 490             });
 491         }
 492     }
 493 
 494 
 495     static void testArray(Handles hs) throws Throwable {
 496         Point[] array = new Point[10];
 497 
 498         for (int i = 0; i < array.length; i++) {
 499             // Plain
 500             {
 501                 hs.get(TestAccessMode.SET).invokeExact(array, i, Point.getInstance(1,1));
 502                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 503                 assertEquals(x, Point.getInstance(1,1), "get Point value");
 504             }
 505 
 506 
 507             // Volatile
 508             {
 509                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, Point.getInstance(2,2));
 510                 Point x = (Point) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
 511                 assertEquals(x, Point.getInstance(2,2), "setVolatile Point value");
 512             }
 513 
 514             // Lazy
 515             {
 516                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, Point.getInstance(1,1));
 517                 Point x = (Point) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
 518                 assertEquals(x, Point.getInstance(1,1), "setRelease Point value");
 519             }
 520 
 521             // Opaque
 522             {
 523                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, Point.getInstance(2,2));
 524                 Point x = (Point) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
 525                 assertEquals(x, Point.getInstance(2,2), "setOpaque Point value");
 526             }
 527 
 528             hs.get(TestAccessMode.SET).invokeExact(array, i, Point.getInstance(1,1));
 529 
 530             // Compare
 531             {
 532                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
 533                 assertEquals(r, true, "success compareAndSet Point");
 534                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 535                 assertEquals(x, Point.getInstance(2,2), "success compareAndSet Point value");
 536             }
 537 
 538             {
 539                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, Point.getInstance(1,1), Point.getInstance(3,3));
 540                 assertEquals(r, false, "failing compareAndSet Point");
 541                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 542                 assertEquals(x, Point.getInstance(2,2), "failing compareAndSet Point value");
 543             }
 544 
 545             {
 546                 Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
 547                 assertEquals(r, Point.getInstance(2,2), "success compareAndExchange Point");
 548                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 549                 assertEquals(x, Point.getInstance(1,1), "success compareAndExchange Point value");
 550             }
 551 
 552             {
 553                 Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, Point.getInstance(2,2), Point.getInstance(3,3));
 554                 assertEquals(r, Point.getInstance(1,1), "failing compareAndExchange Point");
 555                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 556                 assertEquals(x, Point.getInstance(1,1), "failing compareAndExchange Point value");
 557             }
 558 
 559             {
 560                 Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
 561                 assertEquals(r, Point.getInstance(1,1), "success compareAndExchangeAcquire Point");
 562                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 563                 assertEquals(x, Point.getInstance(2,2), "success compareAndExchangeAcquire Point value");
 564             }
 565 
 566             {
 567                 Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, Point.getInstance(1,1), Point.getInstance(3,3));
 568                 assertEquals(r, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point");
 569                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 570                 assertEquals(x, Point.getInstance(2,2), "failing compareAndExchangeAcquire Point value");
 571             }
 572 
 573             {
 574                 Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
 575                 assertEquals(r, Point.getInstance(2,2), "success compareAndExchangeRelease Point");
 576                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 577                 assertEquals(x, Point.getInstance(1,1), "success compareAndExchangeRelease Point value");
 578             }
 579 
 580             {
 581                 Point r = (Point) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, Point.getInstance(2,2), Point.getInstance(3,3));
 582                 assertEquals(r, Point.getInstance(1,1), "failing compareAndExchangeRelease Point");
 583                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 584                 assertEquals(x, Point.getInstance(1,1), "failing compareAndExchangeRelease Point value");
 585             }
 586 
 587             {
 588                 boolean success = false;
 589                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 590                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
 591                 }
 592                 assertEquals(success, true, "weakCompareAndSetPlain Point");
 593                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 594                 assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetPlain Point value");
 595             }
 596 
 597             {
 598                 boolean success = false;
 599                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 600                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
 601                 }
 602                 assertEquals(success, true, "weakCompareAndSetAcquire Point");
 603                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 604                 assertEquals(x, Point.getInstance(1,1), "weakCompareAndSetAcquire Point");
 605             }
 606 
 607             {
 608                 boolean success = false;
 609                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 610                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, Point.getInstance(1,1), Point.getInstance(2,2));
 611                 }
 612                 assertEquals(success, true, "weakCompareAndSetRelease Point");
 613                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 614                 assertEquals(x, Point.getInstance(2,2), "weakCompareAndSetRelease Point");
 615             }
 616 
 617             {
 618                 boolean success = false;
 619                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 620                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, Point.getInstance(2,2), Point.getInstance(1,1));
 621                 }
 622                 assertEquals(success, true, "weakCompareAndSet Point");
 623                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 624                 assertEquals(x, Point.getInstance(1,1), "weakCompareAndSet Point");
 625             }
 626 
 627             // Compare set and get
 628             {
 629                 hs.get(TestAccessMode.SET).invokeExact(array, i, Point.getInstance(1,1));
 630 
 631                 Point o = (Point) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, Point.getInstance(2,2));
 632                 assertEquals(o, Point.getInstance(1,1), "getAndSet Point");
 633                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 634                 assertEquals(x, Point.getInstance(2,2), "getAndSet Point value");
 635             }
 636 
 637             {
 638                 hs.get(TestAccessMode.SET).invokeExact(array, i, Point.getInstance(1,1));
 639 
 640                 Point o = (Point) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, Point.getInstance(2,2));
 641                 assertEquals(o, Point.getInstance(1,1), "getAndSetAcquire Point");
 642                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 643                 assertEquals(x, Point.getInstance(2,2), "getAndSetAcquire Point value");
 644             }
 645 
 646             {
 647                 hs.get(TestAccessMode.SET).invokeExact(array, i, Point.getInstance(1,1));
 648 
 649                 Point o = (Point) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, Point.getInstance(2,2));
 650                 assertEquals(o, Point.getInstance(1,1), "getAndSetRelease Point");
 651                 Point x = (Point) hs.get(TestAccessMode.GET).invokeExact(array, i);
 652                 assertEquals(x, Point.getInstance(2,2), "getAndSetRelease Point value");
 653             }
 654 
 655 
 656         }
 657     }
 658 
 659     static void testArrayUnsupported(Handles hs) throws Throwable {
 660         Point[] array = new Point[10];
 661 
 662         final int i = 0;
 663 
 664         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 665             checkUOE(am, () -> {
 666                 Point o = (Point) hs.get(am).invokeExact(array, i, Point.getInstance(1,1));
 667             });
 668         }
 669 
 670         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
 671             checkUOE(am, () -> {
 672                 Point o = (Point) hs.get(am).invokeExact(array, i, Point.getInstance(1,1));
 673             });
 674         }
 675     }
 676 
 677     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
 678         Point[] array = new Point[10];
 679 
 680         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 681             final int ci = i;
 682 
 683             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
 684                 checkIOOBE(am, () -> {
 685                     Point x = (Point) hs.get(am).invokeExact(array, ci);
 686                 });
 687             }
 688 
 689             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 690                 checkIOOBE(am, () -> {
 691                     hs.get(am).invokeExact(array, ci, Point.getInstance(1,1));
 692                 });
 693             }
 694 
 695             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 696                 checkIOOBE(am, () -> {
 697                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, Point.getInstance(1,1), Point.getInstance(2,2));
 698                 });
 699             }
 700 
 701             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 702                 checkIOOBE(am, () -> {
 703                     Point r = (Point) hs.get(am).invokeExact(array, ci, Point.getInstance(2,2), Point.getInstance(1,1));
 704                 });
 705             }
 706 
 707             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 708                 checkIOOBE(am, () -> {
 709                     Point o = (Point) hs.get(am).invokeExact(array, ci, Point.getInstance(1,1));
 710                 });
 711             }
 712 
 713 
 714         }
 715     }
 716 }
 717