1 /*
   2  * Copyright (c) 2015, 2023, 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 /*
  25  * @test
  26  * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
  27  *          to hit compilation thresholds
  28  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 VarHandleTestMethodHandleAccessInt
  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.MethodHandle;
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class VarHandleTestMethodHandleAccessInt extends VarHandleBaseTest {
  45     static final int static_final_v = 0x01234567;
  46 
  47     static int static_v;
  48 
  49     final int final_v = 0x01234567;
  50 
  51     int v;
  52 
  53     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63     @BeforeClass
  64     public void setup() throws Exception {
  65         vhFinalField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestMethodHandleAccessInt.class, "final_v", int.class);
  67 
  68         vhField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestMethodHandleAccessInt.class, "v", int.class);
  70 
  71         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  72             VarHandleTestMethodHandleAccessInt.class, "static_final_v", int.class);
  73 
  74         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  75             VarHandleTestMethodHandleAccessInt.class, "static_v", int.class);
  76 
  77         vhArray = MethodHandles.arrayElementVarHandle(int[].class);
  78     }
  79 
  80 
  81     @DataProvider
  82     public Object[][] accessTestCaseProvider() throws Exception {
  83         List<AccessTestCase<?>> cases = new ArrayList<>();
  84 
  85         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
  86             cases.add(new MethodHandleAccessTestCase("Instance field",
  87                                                      vhField, f, hs -> testInstanceField(this, hs)));
  88             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
  89                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
  90                                                      false));
  91 
  92             cases.add(new MethodHandleAccessTestCase("Static field",
  93                                                      vhStaticField, f, VarHandleTestMethodHandleAccessInt::testStaticField));
  94             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
  95                                                      vhStaticField, f, VarHandleTestMethodHandleAccessInt::testStaticFieldUnsupported,
  96                                                      false));
  97 
  98             cases.add(new MethodHandleAccessTestCase("Array",
  99                                                      vhArray, f, VarHandleTestMethodHandleAccessInt::testArray));
 100             cases.add(new MethodHandleAccessTestCase("Array unsupported",
 101                                                      vhArray, f, VarHandleTestMethodHandleAccessInt::testArrayUnsupported,
 102                                                      false));
 103             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
 104                                                      vhArray, f, VarHandleTestMethodHandleAccessInt::testArrayIndexOutOfBounds,
 105                                                      false));
 106         }
 107 
 108         // Work around issue with jtreg summary reporting which truncates
 109         // the String result of Object.toString to 30 characters, hence
 110         // the first dummy argument
 111         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 112     }
 113 
 114     @Test(dataProvider = "accessTestCaseProvider")
 115     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 116         T t = atc.get();
 117         int iters = atc.requiresLoop() ? ITERS : 1;
 118         for (int c = 0; c < iters; c++) {
 119             atc.testAccess(t);
 120         }
 121     }
 122 
 123 
 124     static void testInstanceField(VarHandleTestMethodHandleAccessInt recv, Handles hs) throws Throwable {
 125         // Plain
 126         {
 127             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 128             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 129             assertEquals(x, 0x01234567, "set int value");
 130         }
 131 
 132 
 133         // Volatile
 134         {
 135             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 0x89ABCDEF);
 136             int x = (int) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
 137             assertEquals(x, 0x89ABCDEF, "setVolatile int value");
 138         }
 139 
 140         // Lazy
 141         {
 142             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 0x01234567);
 143             int x = (int) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
 144             assertEquals(x, 0x01234567, "setRelease int value");
 145         }
 146 
 147         // Opaque
 148         {
 149             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 0x89ABCDEF);
 150             int x = (int) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
 151             assertEquals(x, 0x89ABCDEF, "setOpaque int value");
 152         }
 153 
 154         hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 155 
 156         // Compare
 157         {
 158             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 0x01234567, 0x89ABCDEF);
 159             assertEquals(r, true, "success compareAndSet int");
 160             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 161             assertEquals(x, 0x89ABCDEF, "success compareAndSet int value");
 162         }
 163 
 164         {
 165             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 0x01234567, 0xCAFEBABE);
 166             assertEquals(r, false, "failing compareAndSet int");
 167             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 168             assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value");
 169         }
 170 
 171         {
 172             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 0x89ABCDEF, 0x01234567);
 173             assertEquals(r, 0x89ABCDEF, "success compareAndExchange int");
 174             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 175             assertEquals(x, 0x01234567, "success compareAndExchange int value");
 176         }
 177 
 178         {
 179             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 0x89ABCDEF, 0xCAFEBABE);
 180             assertEquals(r, 0x01234567, "failing compareAndExchange int");
 181             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 182             assertEquals(x, 0x01234567, "failing compareAndExchange int value");
 183         }
 184 
 185         {
 186             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 0x01234567, 0x89ABCDEF);
 187             assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int");
 188             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 189             assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value");
 190         }
 191 
 192         {
 193             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 0x01234567, 0xCAFEBABE);
 194             assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int");
 195             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 196             assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value");
 197         }
 198 
 199         {
 200             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 0x89ABCDEF, 0x01234567);
 201             assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int");
 202             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 203             assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value");
 204         }
 205 
 206         {
 207             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 0x89ABCDEF, 0xCAFEBABE);
 208             assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int");
 209             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 210             assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value");
 211         }
 212 
 213         {
 214             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
 215             boolean success = false;
 216             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 217                 success = (boolean) mh.invokeExact(recv, 0x01234567, 0x89ABCDEF);
 218                 if (!success) weakDelay();
 219             }
 220             assertEquals(success, true, "success weakCompareAndSetPlain int");
 221             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 222             assertEquals(x, 0x89ABCDEF, "success weakCompareAndSetPlain int value");
 223         }
 224 
 225         {
 226             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 0x01234567, 0xCAFEBABE);
 227             assertEquals(success, false, "failing weakCompareAndSetPlain int");
 228             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 229             assertEquals(x, 0x89ABCDEF, "failing weakCompareAndSetPlain int value");
 230         }
 231 
 232         {
 233             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 234             boolean success = false;
 235             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 236                 success = (boolean) mh.invokeExact(recv, 0x89ABCDEF, 0x01234567);
 237                 if (!success) weakDelay();
 238             }
 239             assertEquals(success, true, "success weakCompareAndSetAcquire int");
 240             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 241             assertEquals(x, 0x01234567, "success weakCompareAndSetAcquire int");
 242         }
 243 
 244         {
 245             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 0x89ABCDEF, 0xCAFEBABE);
 246             assertEquals(success, false, "failing weakCompareAndSetAcquire int");
 247             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 248             assertEquals(x, 0x01234567, "failing weakCompareAndSetAcquire int value");
 249         }
 250 
 251         {
 252             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
 253             boolean success = false;
 254             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 255                 success = (boolean) mh.invokeExact(recv, 0x01234567, 0x89ABCDEF);
 256                 if (!success) weakDelay();
 257             }
 258             assertEquals(success, true, "success weakCompareAndSetRelease int");
 259             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 260             assertEquals(x, 0x89ABCDEF, "success weakCompareAndSetRelease int");
 261         }
 262 
 263         {
 264             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 0x01234567, 0xCAFEBABE);
 265             assertEquals(success, false, "failing weakCompareAndSetRelease int");
 266             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 267             assertEquals(x, 0x89ABCDEF, "failing weakCompareAndSetRelease int value");
 268         }
 269 
 270         {
 271             boolean success = false;
 272             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
 273             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 274                 success = (boolean) mh.invokeExact(recv, 0x89ABCDEF, 0x01234567);
 275                 if (!success) weakDelay();
 276             }
 277             assertEquals(success, true, "success weakCompareAndSet int");
 278             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 279             assertEquals(x, 0x01234567, "success weakCompareAndSet int");
 280         }
 281 
 282         {
 283             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 0x89ABCDEF, 0xCAFEBABE);
 284             assertEquals(success, false, "failing weakCompareAndSet int");
 285             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 286             assertEquals(x, 0x01234567, "failing weakCompareAndSet int value");
 287         }
 288 
 289         // Compare set and get
 290         {
 291             int o = (int) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 0x89ABCDEF);
 292             assertEquals(o, 0x01234567, "getAndSet int");
 293             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 294             assertEquals(x, 0x89ABCDEF, "getAndSet int value");
 295         }
 296 
 297         // get and add, add and get
 298         {
 299             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 300 
 301             int o = (int) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 0x89ABCDEF);
 302             assertEquals(o, 0x01234567, "getAndAdd int");
 303             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 304             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAdd int value");
 305         }
 306 
 307         {
 308             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 309 
 310             int o = (int) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 0x89ABCDEF);
 311             assertEquals(o, 0x01234567, "getAndAddAcquire int");
 312             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 313             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAddAcquire int value");
 314         }
 315 
 316         {
 317             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 318 
 319             int o = (int) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 0x89ABCDEF);
 320             assertEquals(o, 0x01234567, "getAndAddRelease int");
 321             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 322             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAddRelease int value");
 323         }
 324 
 325         // get and bitwise or
 326         {
 327             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 328 
 329             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(recv, 0x89ABCDEF);
 330             assertEquals(o, 0x01234567, "getAndBitwiseOr int");
 331             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 332             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOr int value");
 333         }
 334 
 335         {
 336             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 337 
 338             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(recv, 0x89ABCDEF);
 339             assertEquals(o, 0x01234567, "getAndBitwiseOrAcquire int");
 340             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 341             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOrAcquire int value");
 342         }
 343 
 344         {
 345             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 346 
 347             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(recv, 0x89ABCDEF);
 348             assertEquals(o, 0x01234567, "getAndBitwiseOrRelease int");
 349             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 350             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOrRelease int value");
 351         }
 352 
 353         // get and bitwise and
 354         {
 355             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 356 
 357             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(recv, 0x89ABCDEF);
 358             assertEquals(o, 0x01234567, "getAndBitwiseAnd int");
 359             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 360             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAnd int value");
 361         }
 362 
 363         {
 364             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 365 
 366             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(recv, 0x89ABCDEF);
 367             assertEquals(o, 0x01234567, "getAndBitwiseAndAcquire int");
 368             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 369             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAndAcquire int value");
 370         }
 371 
 372         {
 373             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 374 
 375             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(recv, 0x89ABCDEF);
 376             assertEquals(o, 0x01234567, "getAndBitwiseAndRelease int");
 377             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 378             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAndRelease int value");
 379         }
 380 
 381         // get and bitwise xor
 382         {
 383             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 384 
 385             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(recv, 0x89ABCDEF);
 386             assertEquals(o, 0x01234567, "getAndBitwiseXor int");
 387             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 388             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXor int value");
 389         }
 390 
 391         {
 392             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 393 
 394             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(recv, 0x89ABCDEF);
 395             assertEquals(o, 0x01234567, "getAndBitwiseXorAcquire int");
 396             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 397             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorAcquire int value");
 398         }
 399 
 400         {
 401             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 402 
 403             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, 0x89ABCDEF);
 404             assertEquals(o, 0x01234567, "getAndBitwiseXorRelease int");
 405             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 406             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorRelease int value");
 407         }
 408     }
 409 
 410     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessInt recv, Handles hs) throws Throwable {
 411 
 412 
 413     }
 414 
 415 
 416     static void testStaticField(Handles hs) throws Throwable {
 417         // Plain
 418         {
 419             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 420             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 421             assertEquals(x, 0x01234567, "set int value");
 422         }
 423 
 424 
 425         // Volatile
 426         {
 427             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(0x89ABCDEF);
 428             int x = (int) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 429             assertEquals(x, 0x89ABCDEF, "setVolatile int value");
 430         }
 431 
 432         // Lazy
 433         {
 434             hs.get(TestAccessMode.SET_RELEASE).invokeExact(0x01234567);
 435             int x = (int) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
 436             assertEquals(x, 0x01234567, "setRelease int value");
 437         }
 438 
 439         // Opaque
 440         {
 441             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(0x89ABCDEF);
 442             int x = (int) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
 443             assertEquals(x, 0x89ABCDEF, "setOpaque int value");
 444         }
 445 
 446         hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 447 
 448         // Compare
 449         {
 450             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(0x01234567, 0x89ABCDEF);
 451             assertEquals(r, true, "success compareAndSet int");
 452             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 453             assertEquals(x, 0x89ABCDEF, "success compareAndSet int value");
 454         }
 455 
 456         {
 457             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(0x01234567, 0xCAFEBABE);
 458             assertEquals(r, false, "failing compareAndSet int");
 459             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 460             assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value");
 461         }
 462 
 463         {
 464             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(0x89ABCDEF, 0x01234567);
 465             assertEquals(r, 0x89ABCDEF, "success compareAndExchange int");
 466             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 467             assertEquals(x, 0x01234567, "success compareAndExchange int value");
 468         }
 469 
 470         {
 471             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(0x89ABCDEF, 0xCAFEBABE);
 472             assertEquals(r, 0x01234567, "failing compareAndExchange int");
 473             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 474             assertEquals(x, 0x01234567, "failing compareAndExchange int value");
 475         }
 476 
 477         {
 478             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(0x01234567, 0x89ABCDEF);
 479             assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int");
 480             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 481             assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value");
 482         }
 483 
 484         {
 485             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(0x01234567, 0xCAFEBABE);
 486             assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int");
 487             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 488             assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value");
 489         }
 490 
 491         {
 492             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(0x89ABCDEF, 0x01234567);
 493             assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int");
 494             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 495             assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value");
 496         }
 497 
 498         {
 499             int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(0x89ABCDEF, 0xCAFEBABE);
 500             assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int");
 501             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 502             assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value");
 503         }
 504 
 505         {
 506             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
 507             boolean success = false;
 508             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 509                 success = (boolean) mh.invokeExact(0x01234567, 0x89ABCDEF);
 510                 if (!success) weakDelay();
 511             }
 512             assertEquals(success, true, "success weakCompareAndSetPlain int");
 513             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 514             assertEquals(x, 0x89ABCDEF, "success weakCompareAndSetPlain int value");
 515         }
 516 
 517         {
 518             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(0x01234567, 0xCAFEBABE);
 519             assertEquals(success, false, "failing weakCompareAndSetPlain int");
 520             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 521             assertEquals(x, 0x89ABCDEF, "failing weakCompareAndSetPlain int value");
 522         }
 523 
 524         {
 525             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 526             boolean success = false;
 527             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 528                 success = (boolean) mh.invokeExact(0x89ABCDEF, 0x01234567);
 529                 if (!success) weakDelay();
 530             }
 531             assertEquals(success, true, "success weakCompareAndSetAcquire int");
 532             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 533             assertEquals(x, 0x01234567, "success weakCompareAndSetAcquire int");
 534         }
 535 
 536         {
 537             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 538             boolean success = (boolean) mh.invokeExact(0x89ABCDEF, 0xCAFEBABE);
 539             assertEquals(success, false, "failing weakCompareAndSetAcquire int");
 540             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 541             assertEquals(x, 0x01234567, "failing weakCompareAndSetAcquire int value");
 542         }
 543 
 544         {
 545             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
 546             boolean success = false;
 547             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 548                 success = (boolean) mh.invokeExact(0x01234567, 0x89ABCDEF);
 549                 if (!success) weakDelay();
 550             }
 551             assertEquals(success, true, "success weakCompareAndSetRelease int");
 552             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 553             assertEquals(x, 0x89ABCDEF, "success weakCompareAndSetRelease int");
 554         }
 555 
 556         {
 557             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(0x01234567, 0xCAFEBABE);
 558             assertEquals(success, false, "failing weakCompareAndSetRelease int");
 559             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 560             assertEquals(x, 0x89ABCDEF, "failing weakCompareAndSetRelease int value");
 561         }
 562 
 563         {
 564             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
 565             boolean success = false;
 566             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 567                 success = (boolean) mh.invokeExact(0x89ABCDEF, 0x01234567);
 568                 if (!success) weakDelay();
 569             }
 570             assertEquals(success, true, "success weakCompareAndSet int");
 571             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 572             assertEquals(x, 0x01234567, "success weakCompareAndSet int");
 573         }
 574 
 575         {
 576             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(0x89ABCDEF, 0xCAFEBABE);
 577             assertEquals(success, false, "failing weakCompareAndSet int");
 578             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 579             assertEquals(x, 0x01234567, "failing weakCompareAndSetRe int value");
 580         }
 581 
 582         // Compare set and get
 583         {
 584             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 585 
 586             int o = (int) hs.get(TestAccessMode.GET_AND_SET).invokeExact(0x89ABCDEF);
 587             assertEquals(o, 0x01234567, "getAndSet int");
 588             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 589             assertEquals(x, 0x89ABCDEF, "getAndSet int value");
 590         }
 591 
 592         // Compare set and get
 593         {
 594             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 595 
 596             int o = (int) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(0x89ABCDEF);
 597             assertEquals(o, 0x01234567, "getAndSetAcquire int");
 598             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 599             assertEquals(x, 0x89ABCDEF, "getAndSetAcquire int value");
 600         }
 601 
 602         // Compare set and get
 603         {
 604             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 605 
 606             int o = (int) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(0x89ABCDEF);
 607             assertEquals(o, 0x01234567, "getAndSetRelease int");
 608             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 609             assertEquals(x, 0x89ABCDEF, "getAndSetRelease int value");
 610         }
 611 
 612         // get and add, add and get
 613         {
 614             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 615 
 616             int o = (int) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(0x89ABCDEF);
 617             assertEquals(o, 0x01234567, "getAndAdd int");
 618             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 619             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAdd int value");
 620         }
 621 
 622         {
 623             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 624 
 625             int o = (int) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(0x89ABCDEF);
 626             assertEquals(o, 0x01234567, "getAndAddAcquire int");
 627             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 628             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAddAcquire int value");
 629         }
 630 
 631         {
 632             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 633 
 634             int o = (int) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(0x89ABCDEF);
 635             assertEquals(o, 0x01234567, "getAndAddRelease int");
 636             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 637             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAddRelease int value");
 638         }
 639 
 640         // get and bitwise or
 641         {
 642             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 643 
 644             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(0x89ABCDEF);
 645             assertEquals(o, 0x01234567, "getAndBitwiseOr int");
 646             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 647             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOr int value");
 648         }
 649 
 650         {
 651             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 652 
 653             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(0x89ABCDEF);
 654             assertEquals(o, 0x01234567, "getAndBitwiseOrAcquire int");
 655             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 656             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOrAcquire int value");
 657         }
 658 
 659         {
 660             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 661 
 662             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(0x89ABCDEF);
 663             assertEquals(o, 0x01234567, "getAndBitwiseOrRelease int");
 664             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 665             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOrRelease int value");
 666         }
 667 
 668         // get and bitwise and
 669         {
 670             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 671 
 672             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(0x89ABCDEF);
 673             assertEquals(o, 0x01234567, "getAndBitwiseAnd int");
 674             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 675             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAnd int value");
 676         }
 677 
 678         {
 679             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 680 
 681             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(0x89ABCDEF);
 682             assertEquals(o, 0x01234567, "getAndBitwiseAndAcquire int");
 683             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 684             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAndAcquire int value");
 685         }
 686 
 687         {
 688             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 689 
 690             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(0x89ABCDEF);
 691             assertEquals(o, 0x01234567, "getAndBitwiseAndRelease int");
 692             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 693             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAndRelease int value");
 694         }
 695 
 696         // get and bitwise xor
 697         {
 698             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 699 
 700             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(0x89ABCDEF);
 701             assertEquals(o, 0x01234567, "getAndBitwiseXor int");
 702             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 703             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXor int value");
 704         }
 705 
 706         {
 707             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 708 
 709             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(0x89ABCDEF);
 710             assertEquals(o, 0x01234567, "getAndBitwiseXorAcquire int");
 711             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 712             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorAcquire int value");
 713         }
 714 
 715         {
 716             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 717 
 718             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(0x89ABCDEF);
 719             assertEquals(o, 0x01234567, "getAndBitwiseXorRelease int");
 720             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 721             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorRelease int value");
 722         }
 723     }
 724 
 725     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
 726 
 727 
 728     }
 729 
 730 
 731     static void testArray(Handles hs) throws Throwable {
 732         int[] array = new int[10];
 733 
 734         for (int i = 0; i < array.length; i++) {
 735             // Plain
 736             {
 737                 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 738                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 739                 assertEquals(x, 0x01234567, "get int value");
 740             }
 741 
 742 
 743             // Volatile
 744             {
 745                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 0x89ABCDEF);
 746                 int x = (int) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
 747                 assertEquals(x, 0x89ABCDEF, "setVolatile int value");
 748             }
 749 
 750             // Lazy
 751             {
 752                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 0x01234567);
 753                 int x = (int) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
 754                 assertEquals(x, 0x01234567, "setRelease int value");
 755             }
 756 
 757             // Opaque
 758             {
 759                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 0x89ABCDEF);
 760                 int x = (int) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
 761                 assertEquals(x, 0x89ABCDEF, "setOpaque int value");
 762             }
 763 
 764             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 765 
 766             // Compare
 767             {
 768                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 0x01234567, 0x89ABCDEF);
 769                 assertEquals(r, true, "success compareAndSet int");
 770                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 771                 assertEquals(x, 0x89ABCDEF, "success compareAndSet int value");
 772             }
 773 
 774             {
 775                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 0x01234567, 0xCAFEBABE);
 776                 assertEquals(r, false, "failing compareAndSet int");
 777                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 778                 assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value");
 779             }
 780 
 781             {
 782                 int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 0x89ABCDEF, 0x01234567);
 783                 assertEquals(r, 0x89ABCDEF, "success compareAndExchange int");
 784                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 785                 assertEquals(x, 0x01234567, "success compareAndExchange int value");
 786             }
 787 
 788             {
 789                 int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 0x89ABCDEF, 0xCAFEBABE);
 790                 assertEquals(r, 0x01234567, "failing compareAndExchange int");
 791                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 792                 assertEquals(x, 0x01234567, "failing compareAndExchange int value");
 793             }
 794 
 795             {
 796                 int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 0x01234567, 0x89ABCDEF);
 797                 assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int");
 798                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 799                 assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value");
 800             }
 801 
 802             {
 803                 int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 0x01234567, 0xCAFEBABE);
 804                 assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int");
 805                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 806                 assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value");
 807             }
 808 
 809             {
 810                 int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 0x89ABCDEF, 0x01234567);
 811                 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int");
 812                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 813                 assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value");
 814             }
 815 
 816             {
 817                 int r = (int) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 0x89ABCDEF, 0xCAFEBABE);
 818                 assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int");
 819                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 820                 assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value");
 821             }
 822 
 823             {
 824                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
 825                 boolean success = false;
 826                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 827                     success = (boolean) mh.invokeExact(array, i, 0x01234567, 0x89ABCDEF);
 828                     if (!success) weakDelay();
 829                 }
 830                 assertEquals(success, true, "success weakCompareAndSetPlain int");
 831                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 832                 assertEquals(x, 0x89ABCDEF, "success weakCompareAndSetPlain int value");
 833             }
 834 
 835             {
 836                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 0x01234567, 0xCAFEBABE);
 837                 assertEquals(success, false, "failing weakCompareAndSetPlain int");
 838                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 839                 assertEquals(x, 0x89ABCDEF, "failing weakCompareAndSetPlain int value");
 840             }
 841 
 842             {
 843                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 844                 boolean success = false;
 845                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 846                     success = (boolean) mh.invokeExact(array, i, 0x89ABCDEF, 0x01234567);
 847                     if (!success) weakDelay();
 848                 }
 849                 assertEquals(success, true, "success weakCompareAndSetAcquire int");
 850                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 851                 assertEquals(x, 0x01234567, "success weakCompareAndSetAcquire int");
 852             }
 853 
 854             {
 855                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 0x89ABCDEF, 0xCAFEBABE);
 856                 assertEquals(success, false, "failing weakCompareAndSetAcquire int");
 857                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 858                 assertEquals(x, 0x01234567, "failing weakCompareAndSetAcquire int value");
 859             }
 860 
 861             {
 862                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
 863                 boolean success = false;
 864                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 865                     success = (boolean) mh.invokeExact(array, i, 0x01234567, 0x89ABCDEF);
 866                     if (!success) weakDelay();
 867                 }
 868                 assertEquals(success, true, "success weakCompareAndSetRelease int");
 869                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 870                 assertEquals(x, 0x89ABCDEF, "success weakCompareAndSetRelease int");
 871             }
 872 
 873             {
 874                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 0x01234567, 0xCAFEBABE);
 875                 assertEquals(success, false, "failing weakCompareAndSetAcquire int");
 876                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 877                 assertEquals(x, 0x89ABCDEF, "failing weakCompareAndSetAcquire int value");
 878             }
 879 
 880             {
 881                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
 882                 boolean success = false;
 883                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 884                     success = (boolean) mh.invokeExact(array, i, 0x89ABCDEF, 0x01234567);
 885                     if (!success) weakDelay();
 886                 }
 887                 assertEquals(success, true, "success weakCompareAndSet int");
 888                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 889                 assertEquals(x, 0x01234567, "success weakCompareAndSet int");
 890             }
 891 
 892             {
 893                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 0x89ABCDEF, 0xCAFEBABE);
 894                 assertEquals(success, false, "failing weakCompareAndSet int");
 895                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 896                 assertEquals(x, 0x01234567, "failing weakCompareAndSet int value");
 897             }
 898 
 899             // Compare set and get
 900             {
 901                 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 902 
 903                 int o = (int) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 0x89ABCDEF);
 904                 assertEquals(o, 0x01234567, "getAndSet int");
 905                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 906                 assertEquals(x, 0x89ABCDEF, "getAndSet int value");
 907             }
 908 
 909             {
 910                 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 911 
 912                 int o = (int) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 0x89ABCDEF);
 913                 assertEquals(o, 0x01234567, "getAndSetAcquire int");
 914                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 915                 assertEquals(x, 0x89ABCDEF, "getAndSetAcquire int value");
 916             }
 917 
 918             {
 919                 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 920 
 921                 int o = (int) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 0x89ABCDEF);
 922                 assertEquals(o, 0x01234567, "getAndSetRelease int");
 923                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 924                 assertEquals(x, 0x89ABCDEF, "getAndSetRelease int value");
 925             }
 926 
 927             // get and add, add and get
 928             {
 929                 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 930 
 931                 int o = (int) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 0x89ABCDEF);
 932                 assertEquals(o, 0x01234567, "getAndAdd int");
 933                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 934                 assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAdd int value");
 935             }
 936 
 937             {
 938                 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 939 
 940                 int o = (int) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 0x89ABCDEF);
 941                 assertEquals(o, 0x01234567, "getAndAddAcquire int");
 942                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 943                 assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAddAcquire int value");
 944             }
 945 
 946             {
 947                 hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 948 
 949                 int o = (int) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 0x89ABCDEF);
 950                 assertEquals(o, 0x01234567, "getAndAddRelease int");
 951                 int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 952                 assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAddRelease int value");
 953             }
 954 
 955         // get and bitwise or
 956         {
 957             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 958 
 959             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(array, i, 0x89ABCDEF);
 960             assertEquals(o, 0x01234567, "getAndBitwiseOr int");
 961             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 962             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOr int value");
 963         }
 964 
 965         {
 966             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 967 
 968             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(array, i, 0x89ABCDEF);
 969             assertEquals(o, 0x01234567, "getAndBitwiseOrAcquire int");
 970             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 971             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOrAcquire int value");
 972         }
 973 
 974         {
 975             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 976 
 977             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(array, i, 0x89ABCDEF);
 978             assertEquals(o, 0x01234567, "getAndBitwiseOrRelease int");
 979             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 980             assertEquals(x, (int)(0x01234567 | 0x89ABCDEF), "getAndBitwiseOrRelease int value");
 981         }
 982 
 983         // get and bitwise and
 984         {
 985             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 986 
 987             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(array, i, 0x89ABCDEF);
 988             assertEquals(o, 0x01234567, "getAndBitwiseAnd int");
 989             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 990             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAnd int value");
 991         }
 992 
 993         {
 994             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
 995 
 996             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(array, i, 0x89ABCDEF);
 997             assertEquals(o, 0x01234567, "getAndBitwiseAndAcquire int");
 998             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
 999             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAndAcquire int value");
1000         }
1001 
1002         {
1003             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
1004 
1005             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(array, i, 0x89ABCDEF);
1006             assertEquals(o, 0x01234567, "getAndBitwiseAndRelease int");
1007             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
1008             assertEquals(x, (int)(0x01234567 & 0x89ABCDEF), "getAndBitwiseAndRelease int value");
1009         }
1010 
1011         // get and bitwise xor
1012         {
1013             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
1014 
1015             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(array, i, 0x89ABCDEF);
1016             assertEquals(o, 0x01234567, "getAndBitwiseXor int");
1017             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
1018             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXor int value");
1019         }
1020 
1021         {
1022             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
1023 
1024             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(array, i, 0x89ABCDEF);
1025             assertEquals(o, 0x01234567, "getAndBitwiseXorAcquire int");
1026             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
1027             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorAcquire int value");
1028         }
1029 
1030         {
1031             hs.get(TestAccessMode.SET).invokeExact(array, i, 0x01234567);
1032 
1033             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(array, i, 0x89ABCDEF);
1034             assertEquals(o, 0x01234567, "getAndBitwiseXorRelease int");
1035             int x = (int) hs.get(TestAccessMode.GET).invokeExact(array, i);
1036             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorRelease int value");
1037         }
1038         }
1039     }
1040 
1041     static void testArrayUnsupported(Handles hs) throws Throwable {
1042         int[] array = new int[10];
1043 
1044         final int i = 0;
1045 
1046 
1047     }
1048 
1049     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
1050         int[] array = new int[10];
1051 
1052         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1053             final int ci = i;
1054 
1055             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
1056                 checkAIOOBE(am, () -> {
1057                     int x = (int) hs.get(am).invokeExact(array, ci);
1058                 });
1059             }
1060 
1061             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
1062                 checkAIOOBE(am, () -> {
1063                     hs.get(am).invokeExact(array, ci, 0x01234567);
1064                 });
1065             }
1066 
1067             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1068                 checkAIOOBE(am, () -> {
1069                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, 0x01234567, 0x89ABCDEF);
1070                 });
1071             }
1072 
1073             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1074                 checkAIOOBE(am, () -> {
1075                     int r = (int) hs.get(am).invokeExact(array, ci, 0x89ABCDEF, 0x01234567);
1076                 });
1077             }
1078 
1079             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
1080                 checkAIOOBE(am, () -> {
1081                     int o = (int) hs.get(am).invokeExact(array, ci, 0x01234567);
1082                 });
1083             }
1084 
1085             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
1086                 checkAIOOBE(am, () -> {
1087                     int o = (int) hs.get(am).invokeExact(array, ci, 0xCAFEBABE);
1088                 });
1089             }
1090 
1091             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
1092                 checkAIOOBE(am, () -> {
1093                     int o = (int) hs.get(am).invokeExact(array, ci, 0xCAFEBABE);
1094                 });
1095             }
1096         }
1097     }
1098 }
1099