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