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