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