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