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