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 #warn
  25 
  26 /*
  27  * @test
  28 #if[Value]
  29  * @enablePreview
  30  * @modules java.base/jdk.internal.vm.annotation
  31 #end[Value]
  32  * @run testng/othervm -Diters=10   -Xint                                                   VarHandleTestAccess$Type$
  33  *
  34  * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
  35  *          to hit compilation thresholds
  36  *
  37  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  38  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1                         VarHandleTestAccess$Type$
  39  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  40  */
  41 
  42 import org.testng.annotations.BeforeClass;
  43 import org.testng.annotations.DataProvider;
  44 import org.testng.annotations.Test;
  45 
  46 import java.lang.invoke.MethodHandles;
  47 import java.lang.invoke.VarHandle;
  48 import java.util.ArrayList;
  49 import java.util.Arrays;
  50 import java.util.List;
  51 
  52 import static org.testng.Assert.*;
  53 
  54 public class VarHandleTestAccess$Type$ extends VarHandleBaseTest {
  55     static final $type$ static_final_v = $value1$;
  56 
  57     static $type$ static_v;
  58 
  59     final $type$ final_v = $value1$;
  60 
  61     $type$ v;
  62 
  63     static final $type$ static_final_v2 = $value1$;
  64 
  65     static $type$ static_v2;
  66 
  67     final $type$ final_v2 = $value1$;
  68 
  69     $type$ v2;
  70 
  71     VarHandle vhFinalField;
  72 
  73     VarHandle vhField;
  74 
  75     VarHandle vhStaticField;
  76 
  77     VarHandle vhStaticFinalField;
  78 
  79     VarHandle vhArray;
  80 
  81 #if[Object]
  82     VarHandle vhArrayObject;
  83 #end[Object]
  84 
  85     VarHandle[] allocate(boolean same) {
  86         List<VarHandle> vhs = new ArrayList<>();
  87 
  88         String postfix = same ? "" : "2";
  89         VarHandle vh;
  90         try {
  91             vh = MethodHandles.lookup().findVarHandle(
  92                     VarHandleTestAccess$Type$.class, "final_v" + postfix, $type$.class);
  93             vhs.add(vh);
  94 
  95             vh = MethodHandles.lookup().findVarHandle(
  96                     VarHandleTestAccess$Type$.class, "v" + postfix, $type$.class);
  97             vhs.add(vh);
  98 
  99             vh = MethodHandles.lookup().findStaticVarHandle(
 100                 VarHandleTestAccess$Type$.class, "static_final_v" + postfix, $type$.class);
 101             vhs.add(vh);
 102 
 103             vh = MethodHandles.lookup().findStaticVarHandle(
 104                 VarHandleTestAccess$Type$.class, "static_v" + postfix, $type$.class);
 105             vhs.add(vh);
 106 
 107             if (same) {
 108                 vh = MethodHandles.arrayElementVarHandle($type$[].class);
 109             }
 110             else {
 111 #if[String]
 112                 vh = MethodHandles.arrayElementVarHandle(int[].class);
 113 #else[String]
 114                 vh = MethodHandles.arrayElementVarHandle(String[].class);
 115 #end[String]
 116             }
 117             vhs.add(vh);
 118         } catch (Exception e) {
 119             throw new InternalError(e);
 120         }
 121         return vhs.toArray(new VarHandle[0]);
 122     }
 123 
 124     @BeforeClass
 125     public void setup() throws Exception {
 126         vhFinalField = MethodHandles.lookup().findVarHandle(
 127                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 128 
 129         vhField = MethodHandles.lookup().findVarHandle(
 130                 VarHandleTestAccess$Type$.class, "v", $type$.class);
 131 
 132         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 133             VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 134 
 135         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 136             VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 137 
 138         vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
 139 #if[Object]
 140         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 141 #end[Object]
 142     }
 143 
 144 
 145     @DataProvider
 146     public Object[][] varHandlesProvider() throws Exception {
 147         List<VarHandle> vhs = new ArrayList<>();
 148         vhs.add(vhField);
 149         vhs.add(vhStaticField);
 150         vhs.add(vhArray);
 151 
 152         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 153     }
 154 
 155     @Test
 156     public void testEquals() {
 157         VarHandle[] vhs1 = allocate(true);
 158         VarHandle[] vhs2 = allocate(true);
 159 
 160         for (int i = 0; i < vhs1.length; i++) {
 161             for (int j = 0; j < vhs1.length; j++) {
 162                 if (i != j) {
 163                     assertNotEquals(vhs1[i], vhs1[j]);
 164                     assertNotEquals(vhs1[i], vhs2[j]);
 165                 }
 166             }
 167         }
 168 
 169         VarHandle[] vhs3 = allocate(false);
 170         for (int i = 0; i < vhs1.length; i++) {
 171             assertNotEquals(vhs1[i], vhs3[i]);
 172         }
 173     }
 174 
 175     @Test(dataProvider = "varHandlesProvider")
 176     public void testIsAccessModeSupported(VarHandle vh) {
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 178         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 179         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 180         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 181         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 182         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 183         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 184         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 185 
 186 #if[CAS]
 187         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 188         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 189         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 190         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 191         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 192         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 193         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 194         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 195         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 196         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
 197         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
 198 #else[CAS]
 199         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 200         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 201         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 202         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 203         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 204         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 205         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 206         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 207         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 208         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
 209         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
 210 #end[CAS]
 211 
 212 #if[AtomicAdd]
 213         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 214         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
 215         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
 216 #else[AtomicAdd]
 217         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 218         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
 219         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
 220 #end[AtomicAdd]
 221 
 222 #if[Bitwise]
 223         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
 224         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
 225         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
 226         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
 227         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 228         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 229         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 230         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 231         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 232 #else[Bitwise]
 233         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
 234         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
 235         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
 236         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
 237         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 238         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 239         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 240         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 241         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 242 #end[Bitwise]
 243     }
 244 
 245 
 246     @DataProvider
 247     public Object[][] typesProvider() throws Exception {
 248         List<Object[]> types = new ArrayList<>();
 249         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccess$Type$.class)});
 250         types.add(new Object[] {vhStaticField, Arrays.asList()});
 251         types.add(new Object[] {vhArray, Arrays.asList($type$[].class, int.class)});
 252 
 253         return types.stream().toArray(Object[][]::new);
 254     }
 255 
 256     @Test(dataProvider = "typesProvider")
 257     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 258         assertEquals(vh.varType(), $type$.class);
 259 
 260         assertEquals(vh.coordinateTypes(), pts);
 261 
 262         testTypes(vh);
 263     }
 264 
 265 
 266     @Test
 267     public void testLookupInstanceToStatic() {
 268         checkIAE("Lookup of static final field to instance final field", () -> {
 269             MethodHandles.lookup().findStaticVarHandle(
 270                     VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 271         });
 272 
 273         checkIAE("Lookup of static field to instance field", () -> {
 274             MethodHandles.lookup().findStaticVarHandle(
 275                     VarHandleTestAccess$Type$.class, "v", $type$.class);
 276         });
 277     }
 278 
 279     @Test
 280     public void testLookupStaticToInstance() {
 281         checkIAE("Lookup of instance final field to static final field", () -> {
 282             MethodHandles.lookup().findVarHandle(
 283                 VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 284         });
 285 
 286         checkIAE("Lookup of instance field to static field", () -> {
 287             vhStaticField = MethodHandles.lookup().findVarHandle(
 288                 VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 289         });
 290     }
 291 
 292 
 293     @DataProvider
 294     public Object[][] accessTestCaseProvider() throws Exception {
 295         List<AccessTestCase<?>> cases = new ArrayList<>();
 296 
 297         cases.add(new VarHandleAccessTestCase("Instance final field",
 298                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 299         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 300                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 301                                               false));
 302 
 303         cases.add(new VarHandleAccessTestCase("Static final field",
 304                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalField));
 305         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 306                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalFieldUnsupported,
 307                                               false));
 308 
 309         cases.add(new VarHandleAccessTestCase("Instance field",
 310                                               vhField, vh -> testInstanceField(this, vh)));
 311         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 312                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 313                                               false));
 314 
 315         cases.add(new VarHandleAccessTestCase("Static field",
 316                                               vhStaticField, VarHandleTestAccess$Type$::testStaticField));
 317         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 318                                               vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported,
 319                                               false));
 320 
 321         cases.add(new VarHandleAccessTestCase("Array",
 322                                               vhArray, VarHandleTestAccess$Type$::testArray));
 323 #if[Object]
 324         cases.add(new VarHandleAccessTestCase("Array Object[]",
 325                                               vhArrayObject, VarHandleTestAccess$Type$::testArray));
 326 #end[Object]
 327         cases.add(new VarHandleAccessTestCase("Array unsupported",
 328                                               vhArray, VarHandleTestAccess$Type$::testArrayUnsupported,
 329                                               false));
 330         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 331                                               vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds,
 332                                               false));
 333 #if[Object]
 334         cases.add(new VarHandleAccessTestCase("Array store exception",
 335                                               vhArrayObject, VarHandleTestAccess$Type$::testArrayStoreException,
 336                                               false));
 337 #end[Object]
 338         // Work around issue with jtreg summary reporting which truncates
 339         // the String result of Object.toString to 30 characters, hence
 340         // the first dummy argument
 341         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 342     }
 343 
 344     @Test(dataProvider = "accessTestCaseProvider")
 345     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 346         T t = atc.get();
 347         int iters = atc.requiresLoop() ? ITERS : 1;
 348         for (int c = 0; c < iters; c++) {
 349             atc.testAccess(t);
 350         }
 351     }
 352 
 353     static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 354         // Plain
 355         {
 356             $type$ x = ($type$) vh.get(recv);
 357             assertEquals(x, $value1$, "get $type$ value");
 358         }
 359 
 360 
 361         // Volatile
 362         {
 363             $type$ x = ($type$) vh.getVolatile(recv);
 364             assertEquals(x, $value1$, "getVolatile $type$ value");
 365         }
 366 
 367         // Lazy
 368         {
 369             $type$ x = ($type$) vh.getAcquire(recv);
 370             assertEquals(x, $value1$, "getRelease $type$ value");
 371         }
 372 
 373         // Opaque
 374         {
 375             $type$ x = ($type$) vh.getOpaque(recv);
 376             assertEquals(x, $value1$, "getOpaque $type$ value");
 377         }
 378     }
 379 
 380     static void testInstanceFinalFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 381         checkUOE(() -> {
 382             vh.set(recv, $value2$);
 383         });
 384 
 385         checkUOE(() -> {
 386             vh.setVolatile(recv, $value2$);
 387         });
 388 
 389         checkUOE(() -> {
 390             vh.setRelease(recv, $value2$);
 391         });
 392 
 393         checkUOE(() -> {
 394             vh.setOpaque(recv, $value2$);
 395         });
 396 
 397 #if[!CAS]
 398         checkUOE(() -> {
 399             boolean r = vh.compareAndSet(recv, $value1$, $value2$);
 400         });
 401 
 402         checkUOE(() -> {
 403             $type$ r = ($type$) vh.compareAndExchange(recv, $value1$, $value2$);
 404         });
 405 
 406         checkUOE(() -> {
 407             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$);
 408         });
 409 
 410         checkUOE(() -> {
 411             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$);
 412         });
 413 
 414         checkUOE(() -> {
 415             boolean r = vh.weakCompareAndSetPlain(recv, $value1$, $value2$);
 416         });
 417 
 418         checkUOE(() -> {
 419             boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$);
 420         });
 421 
 422         checkUOE(() -> {
 423             boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$);
 424         });
 425 
 426         checkUOE(() -> {
 427             boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$);
 428         });
 429 
 430         checkUOE(() -> {
 431             $type$ r = ($type$) vh.getAndSet(recv, $value1$);
 432         });
 433 
 434         checkUOE(() -> {
 435             $type$ r = ($type$) vh.getAndSetAcquire(recv, $value1$);
 436         });
 437 
 438         checkUOE(() -> {
 439             $type$ r = ($type$) vh.getAndSetRelease(recv, $value1$);
 440         });
 441 #end[CAS]
 442 
 443 #if[!AtomicAdd]
 444         checkUOE(() -> {
 445             $type$ o = ($type$) vh.getAndAdd(recv, $value1$);
 446         });
 447 
 448         checkUOE(() -> {
 449             $type$ o = ($type$) vh.getAndAddAcquire(recv, $value1$);
 450         });
 451 
 452         checkUOE(() -> {
 453             $type$ o = ($type$) vh.getAndAddRelease(recv, $value1$);
 454         });
 455 #end[AtomicAdd]
 456 
 457 #if[!Bitwise]
 458         checkUOE(() -> {
 459             $type$ o = ($type$) vh.getAndBitwiseOr(recv, $value1$);
 460         });
 461 
 462         checkUOE(() -> {
 463             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(recv, $value1$);
 464         });
 465 
 466         checkUOE(() -> {
 467             $type$ o = ($type$) vh.getAndBitwiseOrRelease(recv, $value1$);
 468         });
 469 
 470         checkUOE(() -> {
 471             $type$ o = ($type$) vh.getAndBitwiseAnd(recv, $value1$);
 472         });
 473 
 474         checkUOE(() -> {
 475             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(recv, $value1$);
 476         });
 477 
 478         checkUOE(() -> {
 479             $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value1$);
 480         });
 481 
 482         checkUOE(() -> {
 483             $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value1$);
 484         });
 485 
 486         checkUOE(() -> {
 487             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value1$);
 488         });
 489 
 490         checkUOE(() -> {
 491             $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value1$);
 492         });
 493 #end[Bitwise]
 494     }
 495 
 496 
 497     static void testStaticFinalField(VarHandle vh) {
 498         // Plain
 499         {
 500             $type$ x = ($type$) vh.get();
 501             assertEquals(x, $value1$, "get $type$ value");
 502         }
 503 
 504 
 505         // Volatile
 506         {
 507             $type$ x = ($type$) vh.getVolatile();
 508             assertEquals(x, $value1$, "getVolatile $type$ value");
 509         }
 510 
 511         // Lazy
 512         {
 513             $type$ x = ($type$) vh.getAcquire();
 514             assertEquals(x, $value1$, "getRelease $type$ value");
 515         }
 516 
 517         // Opaque
 518         {
 519             $type$ x = ($type$) vh.getOpaque();
 520             assertEquals(x, $value1$, "getOpaque $type$ value");
 521         }
 522     }
 523 
 524     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 525         checkUOE(() -> {
 526             vh.set($value2$);
 527         });
 528 
 529         checkUOE(() -> {
 530             vh.setVolatile($value2$);
 531         });
 532 
 533         checkUOE(() -> {
 534             vh.setRelease($value2$);
 535         });
 536 
 537         checkUOE(() -> {
 538             vh.setOpaque($value2$);
 539         });
 540 
 541 #if[!CAS]
 542         checkUOE(() -> {
 543             boolean r = vh.compareAndSet($value1$, $value2$);
 544         });
 545 
 546         checkUOE(() -> {
 547             $type$ r = ($type$) vh.compareAndExchange($value1$, $value2$);
 548         });
 549 
 550         checkUOE(() -> {
 551             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$);
 552         });
 553 
 554         checkUOE(() -> {
 555             $type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$);
 556         });
 557 
 558         checkUOE(() -> {
 559             boolean r = vh.weakCompareAndSetPlain($value1$, $value2$);
 560         });
 561 
 562         checkUOE(() -> {
 563             boolean r = vh.weakCompareAndSet($value1$, $value2$);
 564         });
 565 
 566         checkUOE(() -> {
 567             boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$);
 568         });
 569 
 570         checkUOE(() -> {
 571             boolean r = vh.weakCompareAndSetRelease($value1$, $value2$);
 572         });
 573 
 574         checkUOE(() -> {
 575             $type$ r = ($type$) vh.getAndSet($value1$);
 576         });
 577 
 578         checkUOE(() -> {
 579             $type$ r = ($type$) vh.getAndSetAcquire($value1$);
 580         });
 581 
 582         checkUOE(() -> {
 583             $type$ r = ($type$) vh.getAndSetRelease($value1$);
 584         });
 585 #end[CAS]
 586 
 587 #if[!AtomicAdd]
 588         checkUOE(() -> {
 589             $type$ o = ($type$) vh.getAndAdd($value1$);
 590         });
 591 
 592         checkUOE(() -> {
 593             $type$ o = ($type$) vh.getAndAddAcquire($value1$);
 594         });
 595 
 596         checkUOE(() -> {
 597             $type$ o = ($type$) vh.getAndAddRelease($value1$);
 598         });
 599 #end[AtomicAdd]
 600 
 601 #if[!Bitwise]
 602         checkUOE(() -> {
 603             $type$ o = ($type$) vh.getAndBitwiseOr($value1$);
 604         });
 605 
 606         checkUOE(() -> {
 607             $type$ o = ($type$) vh.getAndBitwiseOrAcquire($value1$);
 608         });
 609 
 610         checkUOE(() -> {
 611             $type$ o = ($type$) vh.getAndBitwiseOrRelease($value1$);
 612         });
 613 
 614         checkUOE(() -> {
 615             $type$ o = ($type$) vh.getAndBitwiseAnd($value1$);
 616         });
 617 
 618         checkUOE(() -> {
 619             $type$ o = ($type$) vh.getAndBitwiseAndAcquire($value1$);
 620         });
 621 
 622         checkUOE(() -> {
 623             $type$ o = ($type$) vh.getAndBitwiseAndRelease($value1$);
 624         });
 625 
 626         checkUOE(() -> {
 627             $type$ o = ($type$) vh.getAndBitwiseXor($value1$);
 628         });
 629 
 630         checkUOE(() -> {
 631             $type$ o = ($type$) vh.getAndBitwiseXorAcquire($value1$);
 632         });
 633 
 634         checkUOE(() -> {
 635             $type$ o = ($type$) vh.getAndBitwiseXorRelease($value1$);
 636         });
 637 #end[Bitwise]
 638     }
 639 
 640 
 641     static void testInstanceField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 642         // Plain
 643         {
 644             vh.set(recv, $value1$);
 645             $type$ x = ($type$) vh.get(recv);
 646             assertEquals(x, $value1$, "set $type$ value");
 647         }
 648 
 649 
 650         // Volatile
 651         {
 652             vh.setVolatile(recv, $value2$);
 653             $type$ x = ($type$) vh.getVolatile(recv);
 654             assertEquals(x, $value2$, "setVolatile $type$ value");
 655         }
 656 
 657         // Lazy
 658         {
 659             vh.setRelease(recv, $value1$);
 660             $type$ x = ($type$) vh.getAcquire(recv);
 661             assertEquals(x, $value1$, "setRelease $type$ value");
 662         }
 663 
 664         // Opaque
 665         {
 666             vh.setOpaque(recv, $value2$);
 667             $type$ x = ($type$) vh.getOpaque(recv);
 668             assertEquals(x, $value2$, "setOpaque $type$ value");
 669         }
 670 
 671 #if[CAS]
 672         vh.set(recv, $value1$);
 673 
 674         // Compare
 675         {
 676             boolean r = vh.compareAndSet(recv, $value1$, $value2$);
 677             assertEquals(r, true, "success compareAndSet $type$");
 678             $type$ x = ($type$) vh.get(recv);
 679             assertEquals(x, $value2$, "success compareAndSet $type$ value");
 680         }
 681 
 682         {
 683             boolean r = vh.compareAndSet(recv, $value1$, $value3$);
 684             assertEquals(r, false, "failing compareAndSet $type$");
 685             $type$ x = ($type$) vh.get(recv);
 686             assertEquals(x, $value2$, "failing compareAndSet $type$ value");
 687         }
 688 
 689         {
 690             $type$ r = ($type$) vh.compareAndExchange(recv, $value2$, $value1$);
 691             assertEquals(r, $value2$, "success compareAndExchange $type$");
 692             $type$ x = ($type$) vh.get(recv);
 693             assertEquals(x, $value1$, "success compareAndExchange $type$ value");
 694         }
 695 
 696         {
 697             $type$ r = ($type$) vh.compareAndExchange(recv, $value2$, $value3$);
 698             assertEquals(r, $value1$, "failing compareAndExchange $type$");
 699             $type$ x = ($type$) vh.get(recv);
 700             assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
 701         }
 702 
 703         {
 704             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$);
 705             assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
 706             $type$ x = ($type$) vh.get(recv);
 707             assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
 708         }
 709 
 710         {
 711             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value3$);
 712             assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
 713             $type$ x = ($type$) vh.get(recv);
 714             assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
 715         }
 716 
 717         {
 718             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value1$);
 719             assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
 720             $type$ x = ($type$) vh.get(recv);
 721             assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
 722         }
 723 
 724         {
 725             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value3$);
 726             assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
 727             $type$ x = ($type$) vh.get(recv);
 728             assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
 729         }
 730 
 731         {
 732             boolean success = false;
 733             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 734                 success = vh.weakCompareAndSetPlain(recv, $value1$, $value2$);
 735                 if (!success) weakDelay();
 736             }
 737             assertEquals(success, true, "success weakCompareAndSetPlain $type$");
 738             $type$ x = ($type$) vh.get(recv);
 739             assertEquals(x, $value2$, "success weakCompareAndSetPlain $type$ value");
 740         }
 741 
 742         {
 743             boolean success = vh.weakCompareAndSetPlain(recv, $value1$, $value3$);
 744             assertEquals(success, false, "failing weakCompareAndSetPlain $type$");
 745             $type$ x = ($type$) vh.get(recv);
 746             assertEquals(x, $value2$, "failing weakCompareAndSetPlain $type$ value");
 747         }
 748 
 749         {
 750             boolean success = false;
 751             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 752                 success = vh.weakCompareAndSetAcquire(recv, $value2$, $value1$);
 753                 if (!success) weakDelay();
 754             }
 755             assertEquals(success, true, "success weakCompareAndSetAcquire $type$");
 756             $type$ x = ($type$) vh.get(recv);
 757             assertEquals(x, $value1$, "success weakCompareAndSetAcquire $type$");
 758         }
 759 
 760         {
 761             boolean success = vh.weakCompareAndSetAcquire(recv, $value2$, $value3$);
 762             assertEquals(success, false, "failing weakCompareAndSetAcquire $type$");
 763             $type$ x = ($type$) vh.get(recv);
 764             assertEquals(x, $value1$, "failing weakCompareAndSetAcquire $type$ value");
 765         }
 766 
 767         {
 768             boolean success = false;
 769             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 770                 success = vh.weakCompareAndSetRelease(recv, $value1$, $value2$);
 771                 if (!success) weakDelay();
 772             }
 773             assertEquals(success, true, "success weakCompareAndSetRelease $type$");
 774             $type$ x = ($type$) vh.get(recv);
 775             assertEquals(x, $value2$, "success weakCompareAndSetRelease $type$");
 776         }
 777 
 778         {
 779             boolean success = vh.weakCompareAndSetRelease(recv, $value1$, $value3$);
 780             assertEquals(success, false, "failing weakCompareAndSetRelease $type$");
 781             $type$ x = ($type$) vh.get(recv);
 782             assertEquals(x, $value2$, "failing weakCompareAndSetRelease $type$ value");
 783         }
 784 
 785         {
 786             boolean success = false;
 787             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 788                 success = vh.weakCompareAndSet(recv, $value2$, $value1$);
 789                 if (!success) weakDelay();
 790             }
 791             assertEquals(success, true, "success weakCompareAndSet $type$");
 792             $type$ x = ($type$) vh.get(recv);
 793             assertEquals(x, $value1$, "success weakCompareAndSet $type$ value");
 794         }
 795 
 796         {
 797             boolean success = vh.weakCompareAndSet(recv, $value2$, $value3$);
 798             assertEquals(success, false, "failing weakCompareAndSet $type$");
 799             $type$ x = ($type$) vh.get(recv);
 800             assertEquals(x, $value1$, "failing weakCompareAndSet $type$ value");
 801         }
 802 
 803         // Compare set and get
 804         {
 805             vh.set(recv, $value1$);
 806 
 807             $type$ o = ($type$) vh.getAndSet(recv, $value2$);
 808             assertEquals(o, $value1$, "getAndSet $type$");
 809             $type$ x = ($type$) vh.get(recv);
 810             assertEquals(x, $value2$, "getAndSet $type$ value");
 811         }
 812 
 813         {
 814             vh.set(recv, $value1$);
 815 
 816             $type$ o = ($type$) vh.getAndSetAcquire(recv, $value2$);
 817             assertEquals(o, $value1$, "getAndSetAcquire $type$");
 818             $type$ x = ($type$) vh.get(recv);
 819             assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
 820         }
 821 
 822         {
 823             vh.set(recv, $value1$);
 824 
 825             $type$ o = ($type$) vh.getAndSetRelease(recv, $value2$);
 826             assertEquals(o, $value1$, "getAndSetRelease $type$");
 827             $type$ x = ($type$) vh.get(recv);
 828             assertEquals(x, $value2$, "getAndSetRelease $type$ value");
 829         }
 830 #end[CAS]
 831 
 832 #if[AtomicAdd]
 833         // get and add, add and get
 834         {
 835             vh.set(recv, $value1$);
 836 
 837             $type$ o = ($type$) vh.getAndAdd(recv, $value2$);
 838             assertEquals(o, $value1$, "getAndAdd $type$");
 839             $type$ x = ($type$) vh.get(recv);
 840             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
 841         }
 842 
 843         {
 844             vh.set(recv, $value1$);
 845 
 846             $type$ o = ($type$) vh.getAndAddAcquire(recv, $value2$);
 847             assertEquals(o, $value1$, "getAndAddAcquire $type$");
 848             $type$ x = ($type$) vh.get(recv);
 849             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
 850         }
 851 
 852         {
 853             vh.set(recv, $value1$);
 854 
 855             $type$ o = ($type$) vh.getAndAddRelease(recv, $value2$);
 856             assertEquals(o, $value1$, "getAndAddRelease$type$");
 857             $type$ x = ($type$) vh.get(recv);
 858             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
 859         }
 860 #end[AtomicAdd]
 861 
 862 #if[Bitwise]
 863         // get and bitwise or
 864         {
 865             vh.set(recv, $value1$);
 866 
 867             $type$ o = ($type$) vh.getAndBitwiseOr(recv, $value2$);
 868             assertEquals(o, $value1$, "getAndBitwiseOr $type$");
 869             $type$ x = ($type$) vh.get(recv);
 870             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
 871         }
 872 
 873         {
 874             vh.set(recv, $value1$);
 875 
 876             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(recv, $value2$);
 877             assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
 878             $type$ x = ($type$) vh.get(recv);
 879             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
 880         }
 881 
 882         {
 883             vh.set(recv, $value1$);
 884 
 885             $type$ o = ($type$) vh.getAndBitwiseOrRelease(recv, $value2$);
 886             assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
 887             $type$ x = ($type$) vh.get(recv);
 888             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
 889         }
 890 
 891         // get and bitwise and
 892         {
 893             vh.set(recv, $value1$);
 894 
 895             $type$ o = ($type$) vh.getAndBitwiseAnd(recv, $value2$);
 896             assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
 897             $type$ x = ($type$) vh.get(recv);
 898             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
 899         }
 900 
 901         {
 902             vh.set(recv, $value1$);
 903 
 904             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(recv, $value2$);
 905             assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
 906             $type$ x = ($type$) vh.get(recv);
 907             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
 908         }
 909 
 910         {
 911             vh.set(recv, $value1$);
 912 
 913             $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value2$);
 914             assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
 915             $type$ x = ($type$) vh.get(recv);
 916             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
 917         }
 918 
 919         // get and bitwise xor
 920         {
 921             vh.set(recv, $value1$);
 922 
 923             $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value2$);
 924             assertEquals(o, $value1$, "getAndBitwiseXor $type$");
 925             $type$ x = ($type$) vh.get(recv);
 926             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
 927         }
 928 
 929         {
 930             vh.set(recv, $value1$);
 931 
 932             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value2$);
 933             assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
 934             $type$ x = ($type$) vh.get(recv);
 935             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
 936         }
 937 
 938         {
 939             vh.set(recv, $value1$);
 940 
 941             $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value2$);
 942             assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
 943             $type$ x = ($type$) vh.get(recv);
 944             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
 945         }
 946 #end[Bitwise]
 947     }
 948 
 949     static void testInstanceFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 950 #if[!CAS]
 951         checkUOE(() -> {
 952             boolean r = vh.compareAndSet(recv, $value1$, $value2$);
 953         });
 954 
 955         checkUOE(() -> {
 956             $type$ r = ($type$) vh.compareAndExchange(recv, $value1$, $value2$);
 957         });
 958 
 959         checkUOE(() -> {
 960             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$);
 961         });
 962 
 963         checkUOE(() -> {
 964             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$);
 965         });
 966 
 967         checkUOE(() -> {
 968             boolean r = vh.weakCompareAndSetPlain(recv, $value1$, $value2$);
 969         });
 970 
 971         checkUOE(() -> {
 972             boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$);
 973         });
 974 
 975         checkUOE(() -> {
 976             boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$);
 977         });
 978 
 979         checkUOE(() -> {
 980             boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$);
 981         });
 982 
 983         checkUOE(() -> {
 984             $type$ r = ($type$) vh.getAndSet(recv, $value1$);
 985         });
 986 
 987         checkUOE(() -> {
 988             $type$ r = ($type$) vh.getAndSetAcquire(recv, $value1$);
 989         });
 990 
 991         checkUOE(() -> {
 992             $type$ r = ($type$) vh.getAndSetRelease(recv, $value1$);
 993         });
 994 #end[CAS]
 995 
 996 #if[!AtomicAdd]
 997         checkUOE(() -> {
 998             $type$ o = ($type$) vh.getAndAdd(recv, $value1$);
 999         });
1000 
1001         checkUOE(() -> {
1002             $type$ o = ($type$) vh.getAndAddAcquire(recv, $value1$);
1003         });
1004 
1005         checkUOE(() -> {
1006             $type$ o = ($type$) vh.getAndAddRelease(recv, $value1$);
1007         });
1008 #end[AtomicAdd]
1009 
1010 #if[!Bitwise]
1011         checkUOE(() -> {
1012             $type$ o = ($type$) vh.getAndBitwiseOr(recv, $value1$);
1013         });
1014 
1015         checkUOE(() -> {
1016             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(recv, $value1$);
1017         });
1018 
1019         checkUOE(() -> {
1020             $type$ o = ($type$) vh.getAndBitwiseOrRelease(recv, $value1$);
1021         });
1022 
1023         checkUOE(() -> {
1024             $type$ o = ($type$) vh.getAndBitwiseAnd(recv, $value1$);
1025         });
1026 
1027         checkUOE(() -> {
1028             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(recv, $value1$);
1029         });
1030 
1031         checkUOE(() -> {
1032             $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value1$);
1033         });
1034 
1035         checkUOE(() -> {
1036             $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value1$);
1037         });
1038 
1039         checkUOE(() -> {
1040             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value1$);
1041         });
1042 
1043         checkUOE(() -> {
1044             $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value1$);
1045         });
1046 #end[Bitwise]
1047     }
1048 
1049 
1050     static void testStaticField(VarHandle vh) {
1051         // Plain
1052         {
1053             vh.set($value1$);
1054             $type$ x = ($type$) vh.get();
1055             assertEquals(x, $value1$, "set $type$ value");
1056         }
1057 
1058 
1059         // Volatile
1060         {
1061             vh.setVolatile($value2$);
1062             $type$ x = ($type$) vh.getVolatile();
1063             assertEquals(x, $value2$, "setVolatile $type$ value");
1064         }
1065 
1066         // Lazy
1067         {
1068             vh.setRelease($value1$);
1069             $type$ x = ($type$) vh.getAcquire();
1070             assertEquals(x, $value1$, "setRelease $type$ value");
1071         }
1072 
1073         // Opaque
1074         {
1075             vh.setOpaque($value2$);
1076             $type$ x = ($type$) vh.getOpaque();
1077             assertEquals(x, $value2$, "setOpaque $type$ value");
1078         }
1079 
1080 #if[CAS]
1081         vh.set($value1$);
1082 
1083         // Compare
1084         {
1085             boolean r = vh.compareAndSet($value1$, $value2$);
1086             assertEquals(r, true, "success compareAndSet $type$");
1087             $type$ x = ($type$) vh.get();
1088             assertEquals(x, $value2$, "success compareAndSet $type$ value");
1089         }
1090 
1091         {
1092             boolean r = vh.compareAndSet($value1$, $value3$);
1093             assertEquals(r, false, "failing compareAndSet $type$");
1094             $type$ x = ($type$) vh.get();
1095             assertEquals(x, $value2$, "failing compareAndSet $type$ value");
1096         }
1097 
1098         {
1099             $type$ r = ($type$) vh.compareAndExchange($value2$, $value1$);
1100             assertEquals(r, $value2$, "success compareAndExchange $type$");
1101             $type$ x = ($type$) vh.get();
1102             assertEquals(x, $value1$, "success compareAndExchange $type$ value");
1103         }
1104 
1105         {
1106             $type$ r = ($type$) vh.compareAndExchange($value2$, $value3$);
1107             assertEquals(r, $value1$, "failing compareAndExchange $type$");
1108             $type$ x = ($type$) vh.get();
1109             assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
1110         }
1111 
1112         {
1113             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$);
1114             assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
1115             $type$ x = ($type$) vh.get();
1116             assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
1117         }
1118 
1119         {
1120             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value3$);
1121             assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
1122             $type$ x = ($type$) vh.get();
1123             assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
1124         }
1125 
1126         {
1127             $type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value1$);
1128             assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
1129             $type$ x = ($type$) vh.get();
1130             assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
1131         }
1132 
1133         {
1134             $type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value3$);
1135             assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
1136             $type$ x = ($type$) vh.get();
1137             assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
1138         }
1139 
1140         {
1141             boolean success = false;
1142             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1143                 success = vh.weakCompareAndSetPlain($value1$, $value2$);
1144                 if (!success) weakDelay();
1145             }
1146             assertEquals(success, true, "success weakCompareAndSetPlain $type$");
1147             $type$ x = ($type$) vh.get();
1148             assertEquals(x, $value2$, "success weakCompareAndSetPlain $type$ value");
1149         }
1150 
1151         {
1152             boolean success = vh.weakCompareAndSetPlain($value1$, $value3$);
1153             assertEquals(success, false, "failing weakCompareAndSetPlain $type$");
1154             $type$ x = ($type$) vh.get();
1155             assertEquals(x, $value2$, "failing weakCompareAndSetPlain $type$ value");
1156         }
1157 
1158         {
1159             boolean success = false;
1160             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1161                 success = vh.weakCompareAndSetAcquire($value2$, $value1$);
1162                 if (!success) weakDelay();
1163             }
1164             assertEquals(success, true, "success weakCompareAndSetAcquire $type$");
1165             $type$ x = ($type$) vh.get();
1166             assertEquals(x, $value1$, "success weakCompareAndSetAcquire $type$");
1167         }
1168 
1169         {
1170             boolean success = vh.weakCompareAndSetAcquire($value2$, $value3$);
1171             assertEquals(success, false, "failing weakCompareAndSetAcquire $type$");
1172             $type$ x = ($type$) vh.get();
1173             assertEquals(x, $value1$, "failing weakCompareAndSetAcquire $type$ value");
1174         }
1175 
1176         {
1177             boolean success = false;
1178             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1179                 success = vh.weakCompareAndSetRelease($value1$, $value2$);
1180                 if (!success) weakDelay();
1181             }
1182             assertEquals(success, true, "success weakCompareAndSetRelease $type$");
1183             $type$ x = ($type$) vh.get();
1184             assertEquals(x, $value2$, "success weakCompareAndSetRelease $type$");
1185         }
1186 
1187         {
1188             boolean success = vh.weakCompareAndSetRelease($value1$, $value3$);
1189             assertEquals(success, false, "failing weakCompareAndSetRelease $type$");
1190             $type$ x = ($type$) vh.get();
1191             assertEquals(x, $value2$, "failing weakCompareAndSetRelease $type$ value");
1192         }
1193 
1194         {
1195             boolean success = false;
1196             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1197                 success = vh.weakCompareAndSet($value2$, $value1$);
1198                 if (!success) weakDelay();
1199             }
1200             assertEquals(success, true, "success weakCompareAndSet $type$");
1201             $type$ x = ($type$) vh.get();
1202             assertEquals(x, $value1$, "success weakCompareAndSet $type$");
1203         }
1204 
1205         {
1206             boolean success = vh.weakCompareAndSet($value2$, $value3$);
1207             assertEquals(success, false, "failing weakCompareAndSet $type$");
1208             $type$ x = ($type$) vh.get();
1209             assertEquals(x, $value1$, "failing weakCompareAndSet $type$ value");
1210         }
1211 
1212         // Compare set and get
1213         {
1214             vh.set($value1$);
1215 
1216             $type$ o = ($type$) vh.getAndSet($value2$);
1217             assertEquals(o, $value1$, "getAndSet $type$");
1218             $type$ x = ($type$) vh.get();
1219             assertEquals(x, $value2$, "getAndSet $type$ value");
1220         }
1221 
1222         {
1223             vh.set($value1$);
1224 
1225             $type$ o = ($type$) vh.getAndSetAcquire($value2$);
1226             assertEquals(o, $value1$, "getAndSetAcquire $type$");
1227             $type$ x = ($type$) vh.get();
1228             assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
1229         }
1230 
1231         {
1232             vh.set($value1$);
1233 
1234             $type$ o = ($type$) vh.getAndSetRelease($value2$);
1235             assertEquals(o, $value1$, "getAndSetRelease $type$");
1236             $type$ x = ($type$) vh.get();
1237             assertEquals(x, $value2$, "getAndSetRelease $type$ value");
1238         }
1239 #end[CAS]
1240 
1241 #if[AtomicAdd]
1242         // get and add, add and get
1243         {
1244             vh.set($value1$);
1245 
1246             $type$ o = ($type$) vh.getAndAdd($value2$);
1247             assertEquals(o, $value1$, "getAndAdd $type$");
1248             $type$ x = ($type$) vh.get();
1249             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
1250         }
1251 
1252         {
1253             vh.set($value1$);
1254 
1255             $type$ o = ($type$) vh.getAndAddAcquire($value2$);
1256             assertEquals(o, $value1$, "getAndAddAcquire $type$");
1257             $type$ x = ($type$) vh.get();
1258             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
1259         }
1260 
1261         {
1262             vh.set($value1$);
1263 
1264             $type$ o = ($type$) vh.getAndAddRelease($value2$);
1265             assertEquals(o, $value1$, "getAndAddRelease$type$");
1266             $type$ x = ($type$) vh.get();
1267             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
1268         }
1269 #end[AtomicAdd]
1270 
1271 #if[Bitwise]
1272         // get and bitwise or
1273         {
1274             vh.set($value1$);
1275 
1276             $type$ o = ($type$) vh.getAndBitwiseOr($value2$);
1277             assertEquals(o, $value1$, "getAndBitwiseOr $type$");
1278             $type$ x = ($type$) vh.get();
1279             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
1280         }
1281 
1282         {
1283             vh.set($value1$);
1284 
1285             $type$ o = ($type$) vh.getAndBitwiseOrAcquire($value2$);
1286             assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
1287             $type$ x = ($type$) vh.get();
1288             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
1289         }
1290 
1291         {
1292             vh.set($value1$);
1293 
1294             $type$ o = ($type$) vh.getAndBitwiseOrRelease($value2$);
1295             assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
1296             $type$ x = ($type$) vh.get();
1297             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
1298         }
1299 
1300         // get and bitwise and
1301         {
1302             vh.set($value1$);
1303 
1304             $type$ o = ($type$) vh.getAndBitwiseAnd($value2$);
1305             assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
1306             $type$ x = ($type$) vh.get();
1307             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
1308         }
1309 
1310         {
1311             vh.set($value1$);
1312 
1313             $type$ o = ($type$) vh.getAndBitwiseAndAcquire($value2$);
1314             assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
1315             $type$ x = ($type$) vh.get();
1316             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
1317         }
1318 
1319         {
1320             vh.set($value1$);
1321 
1322             $type$ o = ($type$) vh.getAndBitwiseAndRelease($value2$);
1323             assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
1324             $type$ x = ($type$) vh.get();
1325             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
1326         }
1327 
1328         // get and bitwise xor
1329         {
1330             vh.set($value1$);
1331 
1332             $type$ o = ($type$) vh.getAndBitwiseXor($value2$);
1333             assertEquals(o, $value1$, "getAndBitwiseXor $type$");
1334             $type$ x = ($type$) vh.get();
1335             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
1336         }
1337 
1338         {
1339             vh.set($value1$);
1340 
1341             $type$ o = ($type$) vh.getAndBitwiseXorAcquire($value2$);
1342             assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
1343             $type$ x = ($type$) vh.get();
1344             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
1345         }
1346 
1347         {
1348             vh.set($value1$);
1349 
1350             $type$ o = ($type$) vh.getAndBitwiseXorRelease($value2$);
1351             assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
1352             $type$ x = ($type$) vh.get();
1353             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
1354         }
1355 #end[Bitwise]
1356     }
1357 
1358     static void testStaticFieldUnsupported(VarHandle vh) {
1359 #if[!CAS]
1360         checkUOE(() -> {
1361             boolean r = vh.compareAndSet($value1$, $value2$);
1362         });
1363 
1364         checkUOE(() -> {
1365             $type$ r = ($type$) vh.compareAndExchange($value1$, $value2$);
1366         });
1367 
1368         checkUOE(() -> {
1369             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$);
1370         });
1371 
1372         checkUOE(() -> {
1373             $type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$);
1374         });
1375 
1376         checkUOE(() -> {
1377             boolean r = vh.weakCompareAndSetPlain($value1$, $value2$);
1378         });
1379 
1380         checkUOE(() -> {
1381             boolean r = vh.weakCompareAndSet($value1$, $value2$);
1382         });
1383 
1384         checkUOE(() -> {
1385             boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$);
1386         });
1387 
1388         checkUOE(() -> {
1389             boolean r = vh.weakCompareAndSetRelease($value1$, $value2$);
1390         });
1391 
1392         checkUOE(() -> {
1393             $type$ r = ($type$) vh.getAndSet($value1$);
1394         });
1395 
1396         checkUOE(() -> {
1397             $type$ r = ($type$) vh.getAndSetAcquire($value1$);
1398         });
1399 
1400         checkUOE(() -> {
1401             $type$ r = ($type$) vh.getAndSetRelease($value1$);
1402         });
1403 #end[CAS]
1404 
1405 #if[!AtomicAdd]
1406         checkUOE(() -> {
1407             $type$ o = ($type$) vh.getAndAdd($value1$);
1408         });
1409 
1410         checkUOE(() -> {
1411             $type$ o = ($type$) vh.getAndAddAcquire($value1$);
1412         });
1413 
1414         checkUOE(() -> {
1415             $type$ o = ($type$) vh.getAndAddRelease($value1$);
1416         });
1417 #end[AtomicAdd]
1418 
1419 #if[!Bitwise]
1420         checkUOE(() -> {
1421             $type$ o = ($type$) vh.getAndBitwiseOr($value1$);
1422         });
1423 
1424         checkUOE(() -> {
1425             $type$ o = ($type$) vh.getAndBitwiseOrAcquire($value1$);
1426         });
1427 
1428         checkUOE(() -> {
1429             $type$ o = ($type$) vh.getAndBitwiseOrRelease($value1$);
1430         });
1431 
1432         checkUOE(() -> {
1433             $type$ o = ($type$) vh.getAndBitwiseAnd($value1$);
1434         });
1435 
1436         checkUOE(() -> {
1437             $type$ o = ($type$) vh.getAndBitwiseAndAcquire($value1$);
1438         });
1439 
1440         checkUOE(() -> {
1441             $type$ o = ($type$) vh.getAndBitwiseAndRelease($value1$);
1442         });
1443 
1444         checkUOE(() -> {
1445             $type$ o = ($type$) vh.getAndBitwiseXor($value1$);
1446         });
1447 
1448         checkUOE(() -> {
1449             $type$ o = ($type$) vh.getAndBitwiseXorAcquire($value1$);
1450         });
1451 
1452         checkUOE(() -> {
1453             $type$ o = ($type$) vh.getAndBitwiseXorRelease($value1$);
1454         });
1455 #end[Bitwise]
1456     }
1457 
1458 
1459     static void testArray(VarHandle vh) {
1460         $type$[] array = new $type$[10];
1461 
1462         for (int i = 0; i < array.length; i++) {
1463             // Plain
1464             {
1465                 vh.set(array, i, $value1$);
1466                 $type$ x = ($type$) vh.get(array, i);
1467                 assertEquals(x, $value1$, "get $type$ value");
1468             }
1469 
1470 
1471             // Volatile
1472             {
1473                 vh.setVolatile(array, i, $value2$);
1474                 $type$ x = ($type$) vh.getVolatile(array, i);
1475                 assertEquals(x, $value2$, "setVolatile $type$ value");
1476             }
1477 
1478             // Lazy
1479             {
1480                 vh.setRelease(array, i, $value1$);
1481                 $type$ x = ($type$) vh.getAcquire(array, i);
1482                 assertEquals(x, $value1$, "setRelease $type$ value");
1483             }
1484 
1485             // Opaque
1486             {
1487                 vh.setOpaque(array, i, $value2$);
1488                 $type$ x = ($type$) vh.getOpaque(array, i);
1489                 assertEquals(x, $value2$, "setOpaque $type$ value");
1490             }
1491 
1492 #if[CAS]
1493             vh.set(array, i, $value1$);
1494 
1495             // Compare
1496             {
1497                 boolean r = vh.compareAndSet(array, i, $value1$, $value2$);
1498                 assertEquals(r, true, "success compareAndSet $type$");
1499                 $type$ x = ($type$) vh.get(array, i);
1500                 assertEquals(x, $value2$, "success compareAndSet $type$ value");
1501             }
1502 
1503             {
1504                 boolean r = vh.compareAndSet(array, i, $value1$, $value3$);
1505                 assertEquals(r, false, "failing compareAndSet $type$");
1506                 $type$ x = ($type$) vh.get(array, i);
1507                 assertEquals(x, $value2$, "failing compareAndSet $type$ value");
1508             }
1509 
1510             {
1511                 $type$ r = ($type$) vh.compareAndExchange(array, i, $value2$, $value1$);
1512                 assertEquals(r, $value2$, "success compareAndExchange $type$");
1513                 $type$ x = ($type$) vh.get(array, i);
1514                 assertEquals(x, $value1$, "success compareAndExchange $type$ value");
1515             }
1516 
1517             {
1518                 $type$ r = ($type$) vh.compareAndExchange(array, i, $value2$, $value3$);
1519                 assertEquals(r, $value1$, "failing compareAndExchange $type$");
1520                 $type$ x = ($type$) vh.get(array, i);
1521                 assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
1522             }
1523 
1524             {
1525                 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$);
1526                 assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
1527                 $type$ x = ($type$) vh.get(array, i);
1528                 assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
1529             }
1530 
1531             {
1532                 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value3$);
1533                 assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
1534                 $type$ x = ($type$) vh.get(array, i);
1535                 assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
1536             }
1537 
1538             {
1539                 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value1$);
1540                 assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
1541                 $type$ x = ($type$) vh.get(array, i);
1542                 assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
1543             }
1544 
1545             {
1546                 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value3$);
1547                 assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
1548                 $type$ x = ($type$) vh.get(array, i);
1549                 assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
1550             }
1551 
1552             {
1553                 boolean success = false;
1554                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1555                     success = vh.weakCompareAndSetPlain(array, i, $value1$, $value2$);
1556                     if (!success) weakDelay();
1557                 }
1558                 assertEquals(success, true, "success weakCompareAndSetPlain $type$");
1559                 $type$ x = ($type$) vh.get(array, i);
1560                 assertEquals(x, $value2$, "success weakCompareAndSetPlain $type$ value");
1561             }
1562 
1563             {
1564                 boolean success = vh.weakCompareAndSetPlain(array, i, $value1$, $value3$);
1565                 assertEquals(success, false, "failing weakCompareAndSetPlain $type$");
1566                 $type$ x = ($type$) vh.get(array, i);
1567                 assertEquals(x, $value2$, "failing weakCompareAndSetPlain $type$ value");
1568             }
1569 
1570             {
1571                 boolean success = false;
1572                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1573                     success = vh.weakCompareAndSetAcquire(array, i, $value2$, $value1$);
1574                     if (!success) weakDelay();
1575                 }
1576                 assertEquals(success, true, "success weakCompareAndSetAcquire $type$");
1577                 $type$ x = ($type$) vh.get(array, i);
1578                 assertEquals(x, $value1$, "success weakCompareAndSetAcquire $type$");
1579             }
1580 
1581             {
1582                 boolean success = vh.weakCompareAndSetAcquire(array, i, $value2$, $value3$);
1583                 assertEquals(success, false, "failing weakCompareAndSetAcquire $type$");
1584                 $type$ x = ($type$) vh.get(array, i);
1585                 assertEquals(x, $value1$, "failing weakCompareAndSetAcquire $type$ value");
1586             }
1587 
1588             {
1589                 boolean success = false;
1590                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1591                     success = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$);
1592                     if (!success) weakDelay();
1593                 }
1594                 assertEquals(success, true, "success weakCompareAndSetRelease $type$");
1595                 $type$ x = ($type$) vh.get(array, i);
1596                 assertEquals(x, $value2$, "success weakCompareAndSetRelease $type$");
1597             }
1598 
1599             {
1600                 boolean success = vh.weakCompareAndSetRelease(array, i, $value1$, $value3$);
1601                 assertEquals(success, false, "failing weakCompareAndSetRelease $type$");
1602                 $type$ x = ($type$) vh.get(array, i);
1603                 assertEquals(x, $value2$, "failing weakCompareAndSetRelease $type$ value");
1604             }
1605 
1606             {
1607                 boolean success = false;
1608                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1609                     success = vh.weakCompareAndSet(array, i, $value2$, $value1$);
1610                     if (!success) weakDelay();
1611                 }
1612                 assertEquals(success, true, "success weakCompareAndSet $type$");
1613                 $type$ x = ($type$) vh.get(array, i);
1614                 assertEquals(x, $value1$, "success weakCompareAndSet $type$");
1615             }
1616 
1617             {
1618                 boolean success = vh.weakCompareAndSet(array, i, $value2$, $value3$);
1619                 assertEquals(success, false, "failing weakCompareAndSet $type$");
1620                 $type$ x = ($type$) vh.get(array, i);
1621                 assertEquals(x, $value1$, "failing weakCompareAndSet $type$ value");
1622             }
1623 
1624             // Compare set and get
1625             {
1626                 vh.set(array, i, $value1$);
1627 
1628                 $type$ o = ($type$) vh.getAndSet(array, i, $value2$);
1629                 assertEquals(o, $value1$, "getAndSet $type$");
1630                 $type$ x = ($type$) vh.get(array, i);
1631                 assertEquals(x, $value2$, "getAndSet $type$ value");
1632             }
1633 
1634             {
1635                 vh.set(array, i, $value1$);
1636 
1637                 $type$ o = ($type$) vh.getAndSetAcquire(array, i, $value2$);
1638                 assertEquals(o, $value1$, "getAndSetAcquire $type$");
1639                 $type$ x = ($type$) vh.get(array, i);
1640                 assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
1641             }
1642 
1643             {
1644                 vh.set(array, i, $value1$);
1645 
1646                 $type$ o = ($type$) vh.getAndSetRelease(array, i, $value2$);
1647                 assertEquals(o, $value1$, "getAndSetRelease $type$");
1648                 $type$ x = ($type$) vh.get(array, i);
1649                 assertEquals(x, $value2$, "getAndSetRelease $type$ value");
1650             }
1651 #end[CAS]
1652 
1653 #if[AtomicAdd]
1654             // get and add, add and get
1655             {
1656                 vh.set(array, i, $value1$);
1657 
1658                 $type$ o = ($type$) vh.getAndAdd(array, i, $value2$);
1659                 assertEquals(o, $value1$, "getAndAdd $type$");
1660                 $type$ x = ($type$) vh.get(array, i);
1661                 assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
1662             }
1663 
1664             {
1665                 vh.set(array, i, $value1$);
1666 
1667                 $type$ o = ($type$) vh.getAndAddAcquire(array, i, $value2$);
1668                 assertEquals(o, $value1$, "getAndAddAcquire $type$");
1669                 $type$ x = ($type$) vh.get(array, i);
1670                 assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
1671             }
1672 
1673             {
1674                 vh.set(array, i, $value1$);
1675 
1676                 $type$ o = ($type$) vh.getAndAddRelease(array, i, $value2$);
1677                 assertEquals(o, $value1$, "getAndAddRelease$type$");
1678                 $type$ x = ($type$) vh.get(array, i);
1679                 assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
1680             }
1681 #end[AtomicAdd]
1682 
1683 #if[Bitwise]
1684             // get and bitwise or
1685             {
1686                 vh.set(array, i, $value1$);
1687 
1688                 $type$ o = ($type$) vh.getAndBitwiseOr(array, i, $value2$);
1689                 assertEquals(o, $value1$, "getAndBitwiseOr $type$");
1690                 $type$ x = ($type$) vh.get(array, i);
1691                 assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
1692             }
1693 
1694             {
1695                 vh.set(array, i, $value1$);
1696 
1697                 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, $value2$);
1698                 assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
1699                 $type$ x = ($type$) vh.get(array, i);
1700                 assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
1701             }
1702 
1703             {
1704                 vh.set(array, i, $value1$);
1705 
1706                 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, $value2$);
1707                 assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
1708                 $type$ x = ($type$) vh.get(array, i);
1709                 assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
1710             }
1711 
1712             // get and bitwise and
1713             {
1714                 vh.set(array, i, $value1$);
1715 
1716                 $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, $value2$);
1717                 assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
1718                 $type$ x = ($type$) vh.get(array, i);
1719                 assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
1720             }
1721 
1722             {
1723                 vh.set(array, i, $value1$);
1724 
1725                 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, $value2$);
1726                 assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
1727                 $type$ x = ($type$) vh.get(array, i);
1728                 assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
1729             }
1730 
1731             {
1732                 vh.set(array, i, $value1$);
1733 
1734                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, $value2$);
1735                 assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
1736                 $type$ x = ($type$) vh.get(array, i);
1737                 assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
1738             }
1739 
1740             // get and bitwise xor
1741             {
1742                 vh.set(array, i, $value1$);
1743 
1744                 $type$ o = ($type$) vh.getAndBitwiseXor(array, i, $value2$);
1745                 assertEquals(o, $value1$, "getAndBitwiseXor $type$");
1746                 $type$ x = ($type$) vh.get(array, i);
1747                 assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
1748             }
1749 
1750             {
1751                 vh.set(array, i, $value1$);
1752 
1753                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, $value2$);
1754                 assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
1755                 $type$ x = ($type$) vh.get(array, i);
1756                 assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
1757             }
1758 
1759             {
1760                 vh.set(array, i, $value1$);
1761 
1762                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, $value2$);
1763                 assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
1764                 $type$ x = ($type$) vh.get(array, i);
1765                 assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
1766             }
1767 #end[Bitwise]
1768         }
1769     }
1770 
1771     static void testArrayUnsupported(VarHandle vh) {
1772         $type$[] array = new $type$[10];
1773 
1774         int i = 0;
1775 #if[!CAS]
1776         checkUOE(() -> {
1777             boolean r = vh.compareAndSet(array, i, $value1$, $value2$);
1778         });
1779 
1780         checkUOE(() -> {
1781             $type$ r = ($type$) vh.compareAndExchange(array, i, $value1$, $value2$);
1782         });
1783 
1784         checkUOE(() -> {
1785             $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$);
1786         });
1787 
1788         checkUOE(() -> {
1789             $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value1$, $value2$);
1790         });
1791 
1792         checkUOE(() -> {
1793             boolean r = vh.weakCompareAndSetPlain(array, i, $value1$, $value2$);
1794         });
1795 
1796         checkUOE(() -> {
1797             boolean r = vh.weakCompareAndSet(array, i, $value1$, $value2$);
1798         });
1799 
1800         checkUOE(() -> {
1801             boolean r = vh.weakCompareAndSetAcquire(array, i, $value1$, $value2$);
1802         });
1803 
1804         checkUOE(() -> {
1805             boolean r = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$);
1806         });
1807 
1808         checkUOE(() -> {
1809             $type$ r = ($type$) vh.getAndSet(array, i, $value1$);
1810         });
1811 
1812         checkUOE(() -> {
1813             $type$ r = ($type$) vh.getAndSetAcquire(array, i, $value1$);
1814         });
1815 
1816         checkUOE(() -> {
1817             $type$ r = ($type$) vh.getAndSetRelease(array, i, $value1$);
1818         });
1819 #end[CAS]
1820 
1821 #if[!AtomicAdd]
1822         checkUOE(() -> {
1823             $type$ o = ($type$) vh.getAndAdd(array, i, $value1$);
1824         });
1825 
1826         checkUOE(() -> {
1827             $type$ o = ($type$) vh.getAndAddAcquire(array, i, $value1$);
1828         });
1829 
1830         checkUOE(() -> {
1831             $type$ o = ($type$) vh.getAndAddRelease(array, i, $value1$);
1832         });
1833 #end[AtomicAdd]
1834 
1835 #if[!Bitwise]
1836         checkUOE(() -> {
1837             $type$ o = ($type$) vh.getAndBitwiseOr(array, i, $value1$);
1838         });
1839 
1840         checkUOE(() -> {
1841             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, $value1$);
1842         });
1843 
1844         checkUOE(() -> {
1845             $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, $value1$);
1846         });
1847 
1848         checkUOE(() -> {
1849             $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, $value1$);
1850         });
1851 
1852         checkUOE(() -> {
1853             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, $value1$);
1854         });
1855 
1856         checkUOE(() -> {
1857             $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, $value1$);
1858         });
1859 
1860         checkUOE(() -> {
1861             $type$ o = ($type$) vh.getAndBitwiseXor(array, i, $value1$);
1862         });
1863 
1864         checkUOE(() -> {
1865             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, $value1$);
1866         });
1867 
1868         checkUOE(() -> {
1869             $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, $value1$);
1870         });
1871 #end[Bitwise]
1872     }
1873 
1874     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1875         $type$[] array = new $type$[10];
1876 
1877         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1878             final int ci = i;
1879 
1880             checkAIOOBE(() -> {
1881                 $type$ x = ($type$) vh.get(array, ci);
1882             });
1883 
1884             checkAIOOBE(() -> {
1885                 vh.set(array, ci, $value1$);
1886             });
1887 
1888             checkAIOOBE(() -> {
1889                 $type$ x = ($type$) vh.getVolatile(array, ci);
1890             });
1891 
1892             checkAIOOBE(() -> {
1893                 vh.setVolatile(array, ci, $value1$);
1894             });
1895 
1896             checkAIOOBE(() -> {
1897                 $type$ x = ($type$) vh.getAcquire(array, ci);
1898             });
1899 
1900             checkAIOOBE(() -> {
1901                 vh.setRelease(array, ci, $value1$);
1902             });
1903 
1904             checkAIOOBE(() -> {
1905                 $type$ x = ($type$) vh.getOpaque(array, ci);
1906             });
1907 
1908             checkAIOOBE(() -> {
1909                 vh.setOpaque(array, ci, $value1$);
1910             });
1911 
1912 #if[CAS]
1913             checkAIOOBE(() -> {
1914                 boolean r = vh.compareAndSet(array, ci, $value1$, $value2$);
1915             });
1916 
1917             checkAIOOBE(() -> {
1918                 $type$ r = ($type$) vh.compareAndExchange(array, ci, $value2$, $value1$);
1919             });
1920 
1921             checkAIOOBE(() -> {
1922                 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, $value2$, $value1$);
1923             });
1924 
1925             checkAIOOBE(() -> {
1926                 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, $value2$, $value1$);
1927             });
1928 
1929             checkAIOOBE(() -> {
1930                 boolean r = vh.weakCompareAndSetPlain(array, ci, $value1$, $value2$);
1931             });
1932 
1933             checkAIOOBE(() -> {
1934                 boolean r = vh.weakCompareAndSet(array, ci, $value1$, $value2$);
1935             });
1936 
1937             checkAIOOBE(() -> {
1938                 boolean r = vh.weakCompareAndSetAcquire(array, ci, $value1$, $value2$);
1939             });
1940 
1941             checkAIOOBE(() -> {
1942                 boolean r = vh.weakCompareAndSetRelease(array, ci, $value1$, $value2$);
1943             });
1944 
1945             checkAIOOBE(() -> {
1946                 $type$ o = ($type$) vh.getAndSet(array, ci, $value1$);
1947             });
1948 
1949             checkAIOOBE(() -> {
1950                 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, $value1$);
1951             });
1952 
1953             checkAIOOBE(() -> {
1954                 $type$ o = ($type$) vh.getAndSetRelease(array, ci, $value1$);
1955             });
1956 #end[CAS]
1957 
1958 #if[AtomicAdd]
1959             checkAIOOBE(() -> {
1960                 $type$ o = ($type$) vh.getAndAdd(array, ci, $value1$);
1961             });
1962 
1963             checkAIOOBE(() -> {
1964                 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, $value1$);
1965             });
1966 
1967             checkAIOOBE(() -> {
1968                 $type$ o = ($type$) vh.getAndAddRelease(array, ci, $value1$);
1969             });
1970 #end[AtomicAdd]
1971 
1972 #if[Bitwise]
1973             checkAIOOBE(() -> {
1974                 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, $value1$);
1975             });
1976 
1977             checkAIOOBE(() -> {
1978                 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, $value1$);
1979             });
1980 
1981             checkAIOOBE(() -> {
1982                 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, $value1$);
1983             });
1984 
1985             checkAIOOBE(() -> {
1986                 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, $value1$);
1987             });
1988 
1989             checkAIOOBE(() -> {
1990                 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, $value1$);
1991             });
1992 
1993             checkAIOOBE(() -> {
1994                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, $value1$);
1995             });
1996 
1997             checkAIOOBE(() -> {
1998                 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, $value1$);
1999             });
2000 
2001             checkAIOOBE(() -> {
2002                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, $value1$);
2003             });
2004 
2005             checkAIOOBE(() -> {
2006                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, $value1$);
2007             });
2008 #end[Bitwise]
2009         }
2010     }
2011 
2012 #if[Object]
2013     static void testArrayStoreException(VarHandle vh) throws Throwable {
2014         Object[] array = new $type$[10];
2015         Arrays.fill(array, $value1$);
2016         Object value = new Object();
2017 
2018         // Set
2019         checkASE(() -> {
2020             vh.set(array, 0, value);
2021         });
2022 
2023         // SetVolatile
2024         checkASE(() -> {
2025             vh.setVolatile(array, 0, value);
2026         });
2027 
2028         // SetOpaque
2029         checkASE(() -> {
2030             vh.setOpaque(array, 0, value);
2031         });
2032 
2033         // SetRelease
2034         checkASE(() -> {
2035             vh.setRelease(array, 0, value);
2036         });
2037 
2038         // CompareAndSet
2039         checkASE(() -> { // receiver reference class
2040             boolean r = vh.compareAndSet(array, 0, $value1$, value);
2041         });
2042 
2043         // WeakCompareAndSet
2044         checkASE(() -> { // receiver reference class
2045             boolean r = vh.weakCompareAndSetPlain(array, 0, $value1$, value);
2046         });
2047 
2048         // WeakCompareAndSetVolatile
2049         checkASE(() -> { // receiver reference class
2050             boolean r = vh.weakCompareAndSet(array, 0, $value1$, value);
2051         });
2052 
2053         // WeakCompareAndSetAcquire
2054         checkASE(() -> { // receiver reference class
2055             boolean r = vh.weakCompareAndSetAcquire(array, 0, $value1$, value);
2056         });
2057 
2058         // WeakCompareAndSetRelease
2059         checkASE(() -> { // receiver reference class
2060             boolean r = vh.weakCompareAndSetRelease(array, 0, $value1$, value);
2061         });
2062 
2063         // CompareAndExchange
2064         checkASE(() -> { // receiver reference class
2065             $type$ x = ($type$) vh.compareAndExchange(array, 0, $value1$, value);
2066         });
2067 
2068         // CompareAndExchangeAcquire
2069         checkASE(() -> { // receiver reference class
2070             $type$ x = ($type$) vh.compareAndExchangeAcquire(array, 0, $value1$, value);
2071         });
2072 
2073         // CompareAndExchangeRelease
2074         checkASE(() -> { // receiver reference class
2075             $type$ x = ($type$) vh.compareAndExchangeRelease(array, 0, $value1$, value);
2076         });
2077 
2078         // GetAndSet
2079         checkASE(() -> { // receiver reference class
2080             $type$ x = ($type$) vh.getAndSet(array, 0, value);
2081         });
2082 
2083         // GetAndSetAcquire
2084         checkASE(() -> { // receiver reference class
2085             $type$ x = ($type$) vh.getAndSetAcquire(array, 0, value);
2086         });
2087 
2088         // GetAndSetRelease
2089         checkASE(() -> { // receiver reference class
2090             $type$ x = ($type$) vh.getAndSetRelease(array, 0, value);
2091         });
2092     }
2093 #end[Object]
2094 }
2095