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