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