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 /*
  25  * @test
  26  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccess$Type$
  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccess$Type$
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  30  */
  31 
  32 import org.testng.annotations.BeforeClass;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class VarHandleTestAccess$Type$ extends VarHandleBaseTest {






  45     static final $type$ static_final_v = $value1$;
  46 
  47     static $type$ static_v;
  48 
  49     final $type$ final_v = $value1$;
  50 
  51     $type$ v;
  52 
  53     static final $type$ static_final_v2 = $value1$;
  54 
  55     static $type$ static_v2;
  56 
  57     final $type$ final_v2 = $value1$;
  58 
  59     $type$ v2;
  60 
  61     VarHandle vhFinalField;
  62 
  63     VarHandle vhField;
  64 
  65     VarHandle vhStaticField;
  66 
  67     VarHandle vhStaticFinalField;
  68 
  69     VarHandle vhArray;
  70 
  71 #if[String]
  72     VarHandle vhArrayObject;
  73 #end[String]



  74 
  75     VarHandle[] allocate(boolean same) {
  76         List<VarHandle> vhs = new ArrayList<>();
  77 
  78         String postfix = same ? "" : "2";
  79         VarHandle vh;
  80         try {
  81             vh = MethodHandles.lookup().findVarHandle(
  82                     VarHandleTestAccess$Type$.class, "final_v" + postfix, $type$.class);
  83             vhs.add(vh);
  84 
  85             vh = MethodHandles.lookup().findVarHandle(
  86                     VarHandleTestAccess$Type$.class, "v" + postfix, $type$.class);
  87             vhs.add(vh);
  88 
  89             vh = MethodHandles.lookup().findStaticVarHandle(
  90                 VarHandleTestAccess$Type$.class, "static_final_v" + postfix, $type$.class);
  91             vhs.add(vh);
  92 
  93             vh = MethodHandles.lookup().findStaticVarHandle(
  94                 VarHandleTestAccess$Type$.class, "static_v" + postfix, $type$.class);
  95             vhs.add(vh);
  96 
  97             if (same) {
  98                 vh = MethodHandles.arrayElementVarHandle($type$[].class);
  99             }
 100             else {
 101 #if[String]
 102                 vh = MethodHandles.arrayElementVarHandle(int[].class);
 103 #else[String]
 104                 vh = MethodHandles.arrayElementVarHandle(String[].class);
 105 #end[String]
 106             }
 107             vhs.add(vh);
 108         } catch (Exception e) {
 109             throw new InternalError(e);
 110         }
 111         return vhs.toArray(new VarHandle[0]);
 112     }
 113 
 114     @BeforeClass
 115     public void setup() throws Exception {
 116         vhFinalField = MethodHandles.lookup().findVarHandle(
 117                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 118 
 119         vhField = MethodHandles.lookup().findVarHandle(
 120                 VarHandleTestAccess$Type$.class, "v", $type$.class);
 121 
 122         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 123             VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 124 
 125         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 126             VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 127 
 128         vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
 129 #if[String]
 130         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 131 #end[String]





 132     }
 133 
 134 
 135     @DataProvider
 136     public Object[][] varHandlesProvider() throws Exception {
 137         List<VarHandle> vhs = new ArrayList<>();
 138         vhs.add(vhField);
 139         vhs.add(vhStaticField);
 140         vhs.add(vhArray);
 141 
 142         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 143     }
 144 
 145     @Test
 146     public void testEquals() {
 147         VarHandle[] vhs1 = allocate(true);
 148         VarHandle[] vhs2 = allocate(true);
 149 
 150         for (int i = 0; i < vhs1.length; i++) {
 151             for (int j = 0; j < vhs1.length; j++) {
 152                 if (i != j) {
 153                     assertNotEquals(vhs1[i], vhs1[j]);
 154                     assertNotEquals(vhs1[i], vhs2[j]);
 155                 }
 156             }
 157         }
 158 
 159         VarHandle[] vhs3 = allocate(false);
 160         for (int i = 0; i < vhs1.length; i++) {
 161             assertNotEquals(vhs1[i], vhs3[i]);
 162         }
 163     }
 164 
 165     @Test(dataProvider = "varHandlesProvider")
 166     public void testIsAccessModeSupported(VarHandle vh) {
 167         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 168         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 169         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 170         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 171         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 172         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 173         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 174         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 175 
 176 #if[CAS]
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 178         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 179         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 180         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 181         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 182         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 183         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 184         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 185         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 186         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
 187         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
 188 #else[CAS]
 189         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 190         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 191         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 192         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 193         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 194         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 195         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 196         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 197         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 198         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
 199         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
 200 #end[CAS]
 201 
 202 #if[AtomicAdd]
 203         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 204         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
 205         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
 206 #else[AtomicAdd]
 207         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 208         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
 209         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
 210 #end[AtomicAdd]
 211 
 212 #if[Bitwise]
 213         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
 214         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
 215         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
 216         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
 217         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 218         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 219         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 220         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 221         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 222 #else[Bitwise]
 223         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
 224         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
 225         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
 226         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
 227         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 228         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 229         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 230         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 231         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 232 #end[Bitwise]
 233     }
 234 
 235 
 236     @DataProvider
 237     public Object[][] typesProvider() throws Exception {
 238         List<Object[]> types = new ArrayList<>();
 239         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccess$Type$.class)});
 240         types.add(new Object[] {vhStaticField, Arrays.asList()});
 241         types.add(new Object[] {vhArray, Arrays.asList($type$[].class, int.class)});
 242 
 243         return types.stream().toArray(Object[][]::new);
 244     }
 245 
 246     @Test(dataProvider = "typesProvider")
 247     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 248         assertEquals(vh.varType(), $type$.class);
 249 
 250         assertEquals(vh.coordinateTypes(), pts);
 251 
 252         testTypes(vh);
 253     }
 254 
 255 
 256     @Test
 257     public void testLookupInstanceToStatic() {
 258         checkIAE("Lookup of static final field to instance final field", () -> {
 259             MethodHandles.lookup().findStaticVarHandle(
 260                     VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 261         });
 262 
 263         checkIAE("Lookup of static field to instance field", () -> {
 264             MethodHandles.lookup().findStaticVarHandle(
 265                     VarHandleTestAccess$Type$.class, "v", $type$.class);
 266         });
 267     }
 268 
 269     @Test
 270     public void testLookupStaticToInstance() {
 271         checkIAE("Lookup of instance final field to static final field", () -> {
 272             MethodHandles.lookup().findVarHandle(
 273                 VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 274         });
 275 
 276         checkIAE("Lookup of instance field to static field", () -> {
 277             vhStaticField = MethodHandles.lookup().findVarHandle(
 278                 VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 279         });
 280     }
 281 
 282 
 283     @DataProvider
 284     public Object[][] accessTestCaseProvider() throws Exception {
 285         List<AccessTestCase<?>> cases = new ArrayList<>();
 286 
 287         cases.add(new VarHandleAccessTestCase("Instance final field",
 288                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 289         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 290                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 291                                               false));
 292 
 293         cases.add(new VarHandleAccessTestCase("Static final field",
 294                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalField));
 295         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 296                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalFieldUnsupported,
 297                                               false));
 298 
 299         cases.add(new VarHandleAccessTestCase("Instance field",
 300                                               vhField, vh -> testInstanceField(this, vh)));
 301         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 302                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 303                                               false));
 304 
 305         cases.add(new VarHandleAccessTestCase("Static field",
 306                                               vhStaticField, VarHandleTestAccess$Type$::testStaticField));
 307         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 308                                               vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported,
 309                                               false));
 310 
 311         cases.add(new VarHandleAccessTestCase("Array",
 312                                               vhArray, VarHandleTestAccess$Type$::testArray));
 313 #if[String]
 314         cases.add(new VarHandleAccessTestCase("Array Object[]",
 315                                               vhArrayObject, VarHandleTestAccess$Type$::testArray));
 316 #end[String]
 317         cases.add(new VarHandleAccessTestCase("Array unsupported",
 318                                               vhArray, VarHandleTestAccess$Type$::testArrayUnsupported,
 319                                               false));
 320         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 321                                               vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds,
 322                                               false));
 323 #if[String]
 324         cases.add(new VarHandleAccessTestCase("Array store exception",
 325                                               vhArrayObject, VarHandleTestAccess$Type$::testArrayStoreException,
 326                                               false));
 327 #end[String]







 328         // Work around issue with jtreg summary reporting which truncates
 329         // the String result of Object.toString to 30 characters, hence
 330         // the first dummy argument
 331         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 332     }
 333 
 334     @Test(dataProvider = "accessTestCaseProvider")
 335     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 336         T t = atc.get();
 337         int iters = atc.requiresLoop() ? ITERS : 1;
 338         for (int c = 0; c < iters; c++) {
 339             atc.testAccess(t);
 340         }
 341     }
 342 
 343 
 344 
 345 
 346     static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 347         // Plain
 348         {
 349             $type$ x = ($type$) vh.get(recv);
 350             assertEquals(x, $value1$, "get $type$ value");
 351         }
 352 
 353 
 354         // Volatile
 355         {
 356             $type$ x = ($type$) vh.getVolatile(recv);
 357             assertEquals(x, $value1$, "getVolatile $type$ value");
 358         }
 359 
 360         // Lazy
 361         {
 362             $type$ x = ($type$) vh.getAcquire(recv);
 363             assertEquals(x, $value1$, "getRelease $type$ value");
 364         }
 365 
 366         // Opaque
 367         {
 368             $type$ x = ($type$) vh.getOpaque(recv);
 369             assertEquals(x, $value1$, "getOpaque $type$ value");
 370         }
 371     }
 372 
 373     static void testInstanceFinalFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 374         checkUOE(() -> {
 375             vh.set(recv, $value2$);
 376         });
 377 
 378         checkUOE(() -> {
 379             vh.setVolatile(recv, $value2$);
 380         });
 381 
 382         checkUOE(() -> {
 383             vh.setRelease(recv, $value2$);
 384         });
 385 
 386         checkUOE(() -> {
 387             vh.setOpaque(recv, $value2$);
 388         });
 389 
 390 #if[!CAS]
 391         checkUOE(() -> {
 392             boolean r = vh.compareAndSet(recv, $value1$, $value2$);
 393         });
 394 
 395         checkUOE(() -> {
 396             $type$ r = ($type$) vh.compareAndExchange(recv, $value1$, $value2$);
 397         });
 398 
 399         checkUOE(() -> {
 400             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$);
 401         });
 402 
 403         checkUOE(() -> {
 404             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$);
 405         });
 406 
 407         checkUOE(() -> {
 408             boolean r = vh.weakCompareAndSetPlain(recv, $value1$, $value2$);
 409         });
 410 
 411         checkUOE(() -> {
 412             boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$);
 413         });
 414 
 415         checkUOE(() -> {
 416             boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$);
 417         });
 418 
 419         checkUOE(() -> {
 420             boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$);
 421         });
 422 
 423         checkUOE(() -> {
 424             $type$ r = ($type$) vh.getAndSet(recv, $value1$);
 425         });
 426 
 427         checkUOE(() -> {
 428             $type$ r = ($type$) vh.getAndSetAcquire(recv, $value1$);
 429         });
 430 
 431         checkUOE(() -> {
 432             $type$ r = ($type$) vh.getAndSetRelease(recv, $value1$);
 433         });
 434 #end[CAS]
 435 
 436 #if[!AtomicAdd]
 437         checkUOE(() -> {
 438             $type$ o = ($type$) vh.getAndAdd(recv, $value1$);
 439         });
 440 
 441         checkUOE(() -> {
 442             $type$ o = ($type$) vh.getAndAddAcquire(recv, $value1$);
 443         });
 444 
 445         checkUOE(() -> {
 446             $type$ o = ($type$) vh.getAndAddRelease(recv, $value1$);
 447         });
 448 #end[AtomicAdd]
 449 
 450 #if[!Bitwise]
 451         checkUOE(() -> {
 452             $type$ o = ($type$) vh.getAndBitwiseOr(recv, $value1$);
 453         });
 454 
 455         checkUOE(() -> {
 456             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(recv, $value1$);
 457         });
 458 
 459         checkUOE(() -> {
 460             $type$ o = ($type$) vh.getAndBitwiseOrRelease(recv, $value1$);
 461         });
 462 
 463         checkUOE(() -> {
 464             $type$ o = ($type$) vh.getAndBitwiseAnd(recv, $value1$);
 465         });
 466 
 467         checkUOE(() -> {
 468             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(recv, $value1$);
 469         });
 470 
 471         checkUOE(() -> {
 472             $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value1$);
 473         });
 474 
 475         checkUOE(() -> {
 476             $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value1$);
 477         });
 478 
 479         checkUOE(() -> {
 480             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value1$);
 481         });
 482 
 483         checkUOE(() -> {
 484             $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value1$);
 485         });
 486 #end[Bitwise]
 487     }
 488 















 489 
 490     static void testStaticFinalField(VarHandle vh) {
 491         // Plain
 492         {
 493             $type$ x = ($type$) vh.get();
 494             assertEquals(x, $value1$, "get $type$ value");
 495         }
 496 
 497 
 498         // Volatile
 499         {
 500             $type$ x = ($type$) vh.getVolatile();
 501             assertEquals(x, $value1$, "getVolatile $type$ value");
 502         }
 503 
 504         // Lazy
 505         {
 506             $type$ x = ($type$) vh.getAcquire();
 507             assertEquals(x, $value1$, "getRelease $type$ value");
 508         }
 509 
 510         // Opaque
 511         {
 512             $type$ x = ($type$) vh.getOpaque();
 513             assertEquals(x, $value1$, "getOpaque $type$ value");
 514         }
 515     }
 516 
 517     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 518         checkUOE(() -> {
 519             vh.set($value2$);
 520         });
 521 
 522         checkUOE(() -> {
 523             vh.setVolatile($value2$);
 524         });
 525 
 526         checkUOE(() -> {
 527             vh.setRelease($value2$);
 528         });
 529 
 530         checkUOE(() -> {
 531             vh.setOpaque($value2$);
 532         });
 533 
 534 #if[!CAS]
 535         checkUOE(() -> {
 536             boolean r = vh.compareAndSet($value1$, $value2$);
 537         });
 538 
 539         checkUOE(() -> {
 540             $type$ r = ($type$) vh.compareAndExchange($value1$, $value2$);
 541         });
 542 
 543         checkUOE(() -> {
 544             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$);
 545         });
 546 
 547         checkUOE(() -> {
 548             $type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$);
 549         });
 550 
 551         checkUOE(() -> {
 552             boolean r = vh.weakCompareAndSetPlain($value1$, $value2$);
 553         });
 554 
 555         checkUOE(() -> {
 556             boolean r = vh.weakCompareAndSet($value1$, $value2$);
 557         });
 558 
 559         checkUOE(() -> {
 560             boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$);
 561         });
 562 
 563         checkUOE(() -> {
 564             boolean r = vh.weakCompareAndSetRelease($value1$, $value2$);
 565         });
 566 
 567         checkUOE(() -> {
 568             $type$ r = ($type$) vh.getAndSet($value1$);
 569         });
 570 
 571         checkUOE(() -> {
 572             $type$ r = ($type$) vh.getAndSetAcquire($value1$);
 573         });
 574 
 575         checkUOE(() -> {
 576             $type$ r = ($type$) vh.getAndSetRelease($value1$);
 577         });
 578 #end[CAS]
 579 
 580 #if[!AtomicAdd]
 581         checkUOE(() -> {
 582             $type$ o = ($type$) vh.getAndAdd($value1$);
 583         });
 584 
 585         checkUOE(() -> {
 586             $type$ o = ($type$) vh.getAndAddAcquire($value1$);
 587         });
 588 
 589         checkUOE(() -> {
 590             $type$ o = ($type$) vh.getAndAddRelease($value1$);
 591         });
 592 #end[AtomicAdd]
 593 
 594 #if[!Bitwise]
 595         checkUOE(() -> {
 596             $type$ o = ($type$) vh.getAndBitwiseOr($value1$);
 597         });
 598 
 599         checkUOE(() -> {
 600             $type$ o = ($type$) vh.getAndBitwiseOrAcquire($value1$);
 601         });
 602 
 603         checkUOE(() -> {
 604             $type$ o = ($type$) vh.getAndBitwiseOrRelease($value1$);
 605         });
 606 
 607         checkUOE(() -> {
 608             $type$ o = ($type$) vh.getAndBitwiseAnd($value1$);
 609         });
 610 
 611         checkUOE(() -> {
 612             $type$ o = ($type$) vh.getAndBitwiseAndAcquire($value1$);
 613         });
 614 
 615         checkUOE(() -> {
 616             $type$ o = ($type$) vh.getAndBitwiseAndRelease($value1$);
 617         });
 618 
 619         checkUOE(() -> {
 620             $type$ o = ($type$) vh.getAndBitwiseXor($value1$);
 621         });
 622 
 623         checkUOE(() -> {
 624             $type$ o = ($type$) vh.getAndBitwiseXorAcquire($value1$);
 625         });
 626 
 627         checkUOE(() -> {
 628             $type$ o = ($type$) vh.getAndBitwiseXorRelease($value1$);
 629         });
 630 #end[Bitwise]
 631     }
 632 
 633 
 634     static void testInstanceField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 635         // Plain
 636         {
 637             vh.set(recv, $value1$);
 638             $type$ x = ($type$) vh.get(recv);
 639             assertEquals(x, $value1$, "set $type$ value");
 640         }
 641 
 642 
 643         // Volatile
 644         {
 645             vh.setVolatile(recv, $value2$);
 646             $type$ x = ($type$) vh.getVolatile(recv);
 647             assertEquals(x, $value2$, "setVolatile $type$ value");
 648         }
 649 
 650         // Lazy
 651         {
 652             vh.setRelease(recv, $value1$);
 653             $type$ x = ($type$) vh.getAcquire(recv);
 654             assertEquals(x, $value1$, "setRelease $type$ value");
 655         }
 656 
 657         // Opaque
 658         {
 659             vh.setOpaque(recv, $value2$);
 660             $type$ x = ($type$) vh.getOpaque(recv);
 661             assertEquals(x, $value2$, "setOpaque $type$ value");
 662         }
 663 
 664 #if[CAS]
 665         vh.set(recv, $value1$);
 666 
 667         // Compare
 668         {
 669             boolean r = vh.compareAndSet(recv, $value1$, $value2$);
 670             assertEquals(r, true, "success compareAndSet $type$");
 671             $type$ x = ($type$) vh.get(recv);
 672             assertEquals(x, $value2$, "success compareAndSet $type$ value");
 673         }
 674 
 675         {
 676             boolean r = vh.compareAndSet(recv, $value1$, $value3$);
 677             assertEquals(r, false, "failing compareAndSet $type$");
 678             $type$ x = ($type$) vh.get(recv);
 679             assertEquals(x, $value2$, "failing compareAndSet $type$ value");
 680         }
 681 
 682         {
 683             $type$ r = ($type$) vh.compareAndExchange(recv, $value2$, $value1$);
 684             assertEquals(r, $value2$, "success compareAndExchange $type$");
 685             $type$ x = ($type$) vh.get(recv);
 686             assertEquals(x, $value1$, "success compareAndExchange $type$ value");
 687         }
 688 
 689         {
 690             $type$ r = ($type$) vh.compareAndExchange(recv, $value2$, $value3$);
 691             assertEquals(r, $value1$, "failing compareAndExchange $type$");
 692             $type$ x = ($type$) vh.get(recv);
 693             assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
 694         }
 695 
 696         {
 697             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$);
 698             assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
 699             $type$ x = ($type$) vh.get(recv);
 700             assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
 701         }
 702 
 703         {
 704             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value3$);
 705             assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
 706             $type$ x = ($type$) vh.get(recv);
 707             assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
 708         }
 709 
 710         {
 711             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value1$);
 712             assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
 713             $type$ x = ($type$) vh.get(recv);
 714             assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
 715         }
 716 
 717         {
 718             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value3$);
 719             assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
 720             $type$ x = ($type$) vh.get(recv);
 721             assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
 722         }
 723 
 724         {
 725             boolean success = false;
 726             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 727                 success = vh.weakCompareAndSetPlain(recv, $value1$, $value2$);
 728             }
 729             assertEquals(success, true, "weakCompareAndSetPlain $type$");
 730             $type$ x = ($type$) vh.get(recv);
 731             assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
 732         }
 733 
 734         {
 735             boolean success = false;
 736             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 737                 success = vh.weakCompareAndSetAcquire(recv, $value2$, $value1$);
 738             }
 739             assertEquals(success, true, "weakCompareAndSetAcquire $type$");
 740             $type$ x = ($type$) vh.get(recv);
 741             assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
 742         }
 743 
 744         {
 745             boolean success = false;
 746             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 747                 success = vh.weakCompareAndSetRelease(recv, $value1$, $value2$);
 748             }
 749             assertEquals(success, true, "weakCompareAndSetRelease $type$");
 750             $type$ x = ($type$) vh.get(recv);
 751             assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
 752         }
 753 
 754         {
 755             boolean success = false;
 756             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 757                 success = vh.weakCompareAndSet(recv, $value2$, $value1$);
 758             }
 759             assertEquals(success, true, "weakCompareAndSet $type$");
 760             $type$ x = ($type$) vh.get(recv);
 761             assertEquals(x, $value1$, "weakCompareAndSet $type$ value");
 762         }
 763 
 764         // Compare set and get
 765         {
 766             vh.set(recv, $value1$);
 767 
 768             $type$ o = ($type$) vh.getAndSet(recv, $value2$);
 769             assertEquals(o, $value1$, "getAndSet $type$");
 770             $type$ x = ($type$) vh.get(recv);
 771             assertEquals(x, $value2$, "getAndSet $type$ value");
 772         }
 773 
 774         {
 775             vh.set(recv, $value1$);
 776 
 777             $type$ o = ($type$) vh.getAndSetAcquire(recv, $value2$);
 778             assertEquals(o, $value1$, "getAndSetAcquire $type$");
 779             $type$ x = ($type$) vh.get(recv);
 780             assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
 781         }
 782 
 783         {
 784             vh.set(recv, $value1$);
 785 
 786             $type$ o = ($type$) vh.getAndSetRelease(recv, $value2$);
 787             assertEquals(o, $value1$, "getAndSetRelease $type$");
 788             $type$ x = ($type$) vh.get(recv);
 789             assertEquals(x, $value2$, "getAndSetRelease $type$ value");
 790         }
 791 #end[CAS]
 792 
 793 #if[AtomicAdd]
 794         // get and add, add and get
 795         {
 796             vh.set(recv, $value1$);
 797 
 798             $type$ o = ($type$) vh.getAndAdd(recv, $value2$);
 799             assertEquals(o, $value1$, "getAndAdd $type$");
 800             $type$ x = ($type$) vh.get(recv);
 801             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
 802         }
 803 
 804         {
 805             vh.set(recv, $value1$);
 806 
 807             $type$ o = ($type$) vh.getAndAddAcquire(recv, $value2$);
 808             assertEquals(o, $value1$, "getAndAddAcquire $type$");
 809             $type$ x = ($type$) vh.get(recv);
 810             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
 811         }
 812 
 813         {
 814             vh.set(recv, $value1$);
 815 
 816             $type$ o = ($type$) vh.getAndAddRelease(recv, $value2$);
 817             assertEquals(o, $value1$, "getAndAddRelease$type$");
 818             $type$ x = ($type$) vh.get(recv);
 819             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
 820         }
 821 #end[AtomicAdd]
 822 
 823 #if[Bitwise]
 824         // get and bitwise or
 825         {
 826             vh.set(recv, $value1$);
 827 
 828             $type$ o = ($type$) vh.getAndBitwiseOr(recv, $value2$);
 829             assertEquals(o, $value1$, "getAndBitwiseOr $type$");
 830             $type$ x = ($type$) vh.get(recv);
 831             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
 832         }
 833 
 834         {
 835             vh.set(recv, $value1$);
 836 
 837             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(recv, $value2$);
 838             assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
 839             $type$ x = ($type$) vh.get(recv);
 840             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
 841         }
 842 
 843         {
 844             vh.set(recv, $value1$);
 845 
 846             $type$ o = ($type$) vh.getAndBitwiseOrRelease(recv, $value2$);
 847             assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
 848             $type$ x = ($type$) vh.get(recv);
 849             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
 850         }
 851 
 852         // get and bitwise and
 853         {
 854             vh.set(recv, $value1$);
 855 
 856             $type$ o = ($type$) vh.getAndBitwiseAnd(recv, $value2$);
 857             assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
 858             $type$ x = ($type$) vh.get(recv);
 859             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
 860         }
 861 
 862         {
 863             vh.set(recv, $value1$);
 864 
 865             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(recv, $value2$);
 866             assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
 867             $type$ x = ($type$) vh.get(recv);
 868             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
 869         }
 870 
 871         {
 872             vh.set(recv, $value1$);
 873 
 874             $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value2$);
 875             assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
 876             $type$ x = ($type$) vh.get(recv);
 877             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
 878         }
 879 
 880         // get and bitwise xor
 881         {
 882             vh.set(recv, $value1$);
 883 
 884             $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value2$);
 885             assertEquals(o, $value1$, "getAndBitwiseXor $type$");
 886             $type$ x = ($type$) vh.get(recv);
 887             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
 888         }
 889 
 890         {
 891             vh.set(recv, $value1$);
 892 
 893             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value2$);
 894             assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
 895             $type$ x = ($type$) vh.get(recv);
 896             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
 897         }
 898 
 899         {
 900             vh.set(recv, $value1$);
 901 
 902             $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value2$);
 903             assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
 904             $type$ x = ($type$) vh.get(recv);
 905             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
 906         }
 907 #end[Bitwise]
 908     }
 909 
 910     static void testInstanceFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 911 #if[!CAS]
 912         checkUOE(() -> {
 913             boolean r = vh.compareAndSet(recv, $value1$, $value2$);
 914         });
 915 
 916         checkUOE(() -> {
 917             $type$ r = ($type$) vh.compareAndExchange(recv, $value1$, $value2$);
 918         });
 919 
 920         checkUOE(() -> {
 921             $type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$);
 922         });
 923 
 924         checkUOE(() -> {
 925             $type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$);
 926         });
 927 
 928         checkUOE(() -> {
 929             boolean r = vh.weakCompareAndSetPlain(recv, $value1$, $value2$);
 930         });
 931 
 932         checkUOE(() -> {
 933             boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$);
 934         });
 935 
 936         checkUOE(() -> {
 937             boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$);
 938         });
 939 
 940         checkUOE(() -> {
 941             boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$);
 942         });
 943 
 944         checkUOE(() -> {
 945             $type$ r = ($type$) vh.getAndSet(recv, $value1$);
 946         });
 947 
 948         checkUOE(() -> {
 949             $type$ r = ($type$) vh.getAndSetAcquire(recv, $value1$);
 950         });
 951 
 952         checkUOE(() -> {
 953             $type$ r = ($type$) vh.getAndSetRelease(recv, $value1$);
 954         });
 955 #end[CAS]
 956 
 957 #if[!AtomicAdd]
 958         checkUOE(() -> {
 959             $type$ o = ($type$) vh.getAndAdd(recv, $value1$);
 960         });
 961 
 962         checkUOE(() -> {
 963             $type$ o = ($type$) vh.getAndAddAcquire(recv, $value1$);
 964         });
 965 
 966         checkUOE(() -> {
 967             $type$ o = ($type$) vh.getAndAddRelease(recv, $value1$);
 968         });
 969 #end[AtomicAdd]
 970 
 971 #if[!Bitwise]
 972         checkUOE(() -> {
 973             $type$ o = ($type$) vh.getAndBitwiseOr(recv, $value1$);
 974         });
 975 
 976         checkUOE(() -> {
 977             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(recv, $value1$);
 978         });
 979 
 980         checkUOE(() -> {
 981             $type$ o = ($type$) vh.getAndBitwiseOrRelease(recv, $value1$);
 982         });
 983 
 984         checkUOE(() -> {
 985             $type$ o = ($type$) vh.getAndBitwiseAnd(recv, $value1$);
 986         });
 987 
 988         checkUOE(() -> {
 989             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(recv, $value1$);
 990         });
 991 
 992         checkUOE(() -> {
 993             $type$ o = ($type$) vh.getAndBitwiseAndRelease(recv, $value1$);
 994         });
 995 
 996         checkUOE(() -> {
 997             $type$ o = ($type$) vh.getAndBitwiseXor(recv, $value1$);
 998         });
 999 
1000         checkUOE(() -> {
1001             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(recv, $value1$);
1002         });
1003 
1004         checkUOE(() -> {
1005             $type$ o = ($type$) vh.getAndBitwiseXorRelease(recv, $value1$);
1006         });
1007 #end[Bitwise]
1008     }
1009 
1010 
1011     static void testStaticField(VarHandle vh) {
1012         // Plain
1013         {
1014             vh.set($value1$);
1015             $type$ x = ($type$) vh.get();
1016             assertEquals(x, $value1$, "set $type$ value");
1017         }
1018 
1019 
1020         // Volatile
1021         {
1022             vh.setVolatile($value2$);
1023             $type$ x = ($type$) vh.getVolatile();
1024             assertEquals(x, $value2$, "setVolatile $type$ value");
1025         }
1026 
1027         // Lazy
1028         {
1029             vh.setRelease($value1$);
1030             $type$ x = ($type$) vh.getAcquire();
1031             assertEquals(x, $value1$, "setRelease $type$ value");
1032         }
1033 
1034         // Opaque
1035         {
1036             vh.setOpaque($value2$);
1037             $type$ x = ($type$) vh.getOpaque();
1038             assertEquals(x, $value2$, "setOpaque $type$ value");
1039         }
1040 
1041 #if[CAS]
1042         vh.set($value1$);
1043 
1044         // Compare
1045         {
1046             boolean r = vh.compareAndSet($value1$, $value2$);
1047             assertEquals(r, true, "success compareAndSet $type$");
1048             $type$ x = ($type$) vh.get();
1049             assertEquals(x, $value2$, "success compareAndSet $type$ value");
1050         }
1051 
1052         {
1053             boolean r = vh.compareAndSet($value1$, $value3$);
1054             assertEquals(r, false, "failing compareAndSet $type$");
1055             $type$ x = ($type$) vh.get();
1056             assertEquals(x, $value2$, "failing compareAndSet $type$ value");
1057         }
1058 
1059         {
1060             $type$ r = ($type$) vh.compareAndExchange($value2$, $value1$);
1061             assertEquals(r, $value2$, "success compareAndExchange $type$");
1062             $type$ x = ($type$) vh.get();
1063             assertEquals(x, $value1$, "success compareAndExchange $type$ value");
1064         }
1065 
1066         {
1067             $type$ r = ($type$) vh.compareAndExchange($value2$, $value3$);
1068             assertEquals(r, $value1$, "failing compareAndExchange $type$");
1069             $type$ x = ($type$) vh.get();
1070             assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
1071         }
1072 
1073         {
1074             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$);
1075             assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
1076             $type$ x = ($type$) vh.get();
1077             assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
1078         }
1079 
1080         {
1081             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value3$);
1082             assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
1083             $type$ x = ($type$) vh.get();
1084             assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
1085         }
1086 
1087         {
1088             $type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value1$);
1089             assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
1090             $type$ x = ($type$) vh.get();
1091             assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
1092         }
1093 
1094         {
1095             $type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value3$);
1096             assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
1097             $type$ x = ($type$) vh.get();
1098             assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
1099         }
1100 
1101         {
1102             boolean success = false;
1103             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1104                 success = vh.weakCompareAndSetPlain($value1$, $value2$);
1105             }
1106             assertEquals(success, true, "weakCompareAndSetPlain $type$");
1107             $type$ x = ($type$) vh.get();
1108             assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
1109         }
1110 
1111         {
1112             boolean success = false;
1113             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1114                 success = vh.weakCompareAndSetAcquire($value2$, $value1$);
1115             }
1116             assertEquals(success, true, "weakCompareAndSetAcquire $type$");
1117             $type$ x = ($type$) vh.get();
1118             assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
1119         }
1120 
1121         {
1122             boolean success = false;
1123             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1124                 success = vh.weakCompareAndSetRelease($value1$, $value2$);
1125             }
1126             assertEquals(success, true, "weakCompareAndSetRelease $type$");
1127             $type$ x = ($type$) vh.get();
1128             assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
1129         }
1130 
1131         {
1132             boolean success = false;
1133             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1134                 success = vh.weakCompareAndSet($value2$, $value1$);
1135             }
1136             assertEquals(success, true, "weakCompareAndSet $type$");
1137             $type$ x = ($type$) vh.get();
1138             assertEquals(x, $value1$, "weakCompareAndSet $type$");
1139         }
1140 
1141         // Compare set and get
1142         {
1143             vh.set($value1$);
1144 
1145             $type$ o = ($type$) vh.getAndSet($value2$);
1146             assertEquals(o, $value1$, "getAndSet $type$");
1147             $type$ x = ($type$) vh.get();
1148             assertEquals(x, $value2$, "getAndSet $type$ value");
1149         }
1150 
1151         {
1152             vh.set($value1$);
1153 
1154             $type$ o = ($type$) vh.getAndSetAcquire($value2$);
1155             assertEquals(o, $value1$, "getAndSetAcquire $type$");
1156             $type$ x = ($type$) vh.get();
1157             assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
1158         }
1159 
1160         {
1161             vh.set($value1$);
1162 
1163             $type$ o = ($type$) vh.getAndSetRelease($value2$);
1164             assertEquals(o, $value1$, "getAndSetRelease $type$");
1165             $type$ x = ($type$) vh.get();
1166             assertEquals(x, $value2$, "getAndSetRelease $type$ value");
1167         }
1168 #end[CAS]
1169 
1170 #if[AtomicAdd]
1171         // get and add, add and get
1172         {
1173             vh.set($value1$);
1174 
1175             $type$ o = ($type$) vh.getAndAdd($value2$);
1176             assertEquals(o, $value1$, "getAndAdd $type$");
1177             $type$ x = ($type$) vh.get();
1178             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
1179         }
1180 
1181         {
1182             vh.set($value1$);
1183 
1184             $type$ o = ($type$) vh.getAndAddAcquire($value2$);
1185             assertEquals(o, $value1$, "getAndAddAcquire $type$");
1186             $type$ x = ($type$) vh.get();
1187             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
1188         }
1189 
1190         {
1191             vh.set($value1$);
1192 
1193             $type$ o = ($type$) vh.getAndAddRelease($value2$);
1194             assertEquals(o, $value1$, "getAndAddRelease$type$");
1195             $type$ x = ($type$) vh.get();
1196             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
1197         }
1198 #end[AtomicAdd]
1199 
1200 #if[Bitwise]
1201         // get and bitwise or
1202         {
1203             vh.set($value1$);
1204 
1205             $type$ o = ($type$) vh.getAndBitwiseOr($value2$);
1206             assertEquals(o, $value1$, "getAndBitwiseOr $type$");
1207             $type$ x = ($type$) vh.get();
1208             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
1209         }
1210 
1211         {
1212             vh.set($value1$);
1213 
1214             $type$ o = ($type$) vh.getAndBitwiseOrAcquire($value2$);
1215             assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
1216             $type$ x = ($type$) vh.get();
1217             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
1218         }
1219 
1220         {
1221             vh.set($value1$);
1222 
1223             $type$ o = ($type$) vh.getAndBitwiseOrRelease($value2$);
1224             assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
1225             $type$ x = ($type$) vh.get();
1226             assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
1227         }
1228 
1229         // get and bitwise and
1230         {
1231             vh.set($value1$);
1232 
1233             $type$ o = ($type$) vh.getAndBitwiseAnd($value2$);
1234             assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
1235             $type$ x = ($type$) vh.get();
1236             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
1237         }
1238 
1239         {
1240             vh.set($value1$);
1241 
1242             $type$ o = ($type$) vh.getAndBitwiseAndAcquire($value2$);
1243             assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
1244             $type$ x = ($type$) vh.get();
1245             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
1246         }
1247 
1248         {
1249             vh.set($value1$);
1250 
1251             $type$ o = ($type$) vh.getAndBitwiseAndRelease($value2$);
1252             assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
1253             $type$ x = ($type$) vh.get();
1254             assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
1255         }
1256 
1257         // get and bitwise xor
1258         {
1259             vh.set($value1$);
1260 
1261             $type$ o = ($type$) vh.getAndBitwiseXor($value2$);
1262             assertEquals(o, $value1$, "getAndBitwiseXor $type$");
1263             $type$ x = ($type$) vh.get();
1264             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
1265         }
1266 
1267         {
1268             vh.set($value1$);
1269 
1270             $type$ o = ($type$) vh.getAndBitwiseXorAcquire($value2$);
1271             assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
1272             $type$ x = ($type$) vh.get();
1273             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
1274         }
1275 
1276         {
1277             vh.set($value1$);
1278 
1279             $type$ o = ($type$) vh.getAndBitwiseXorRelease($value2$);
1280             assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
1281             $type$ x = ($type$) vh.get();
1282             assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
1283         }
1284 #end[Bitwise]
1285     }
1286 
1287     static void testStaticFieldUnsupported(VarHandle vh) {
1288 #if[!CAS]
1289         checkUOE(() -> {
1290             boolean r = vh.compareAndSet($value1$, $value2$);
1291         });
1292 
1293         checkUOE(() -> {
1294             $type$ r = ($type$) vh.compareAndExchange($value1$, $value2$);
1295         });
1296 
1297         checkUOE(() -> {
1298             $type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$);
1299         });
1300 
1301         checkUOE(() -> {
1302             $type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$);
1303         });
1304 
1305         checkUOE(() -> {
1306             boolean r = vh.weakCompareAndSetPlain($value1$, $value2$);
1307         });
1308 
1309         checkUOE(() -> {
1310             boolean r = vh.weakCompareAndSet($value1$, $value2$);
1311         });
1312 
1313         checkUOE(() -> {
1314             boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$);
1315         });
1316 
1317         checkUOE(() -> {
1318             boolean r = vh.weakCompareAndSetRelease($value1$, $value2$);
1319         });
1320 
1321         checkUOE(() -> {
1322             $type$ r = ($type$) vh.getAndSet($value1$);
1323         });
1324 
1325         checkUOE(() -> {
1326             $type$ r = ($type$) vh.getAndSetAcquire($value1$);
1327         });
1328 
1329         checkUOE(() -> {
1330             $type$ r = ($type$) vh.getAndSetRelease($value1$);
1331         });
1332 #end[CAS]
1333 
1334 #if[!AtomicAdd]
1335         checkUOE(() -> {
1336             $type$ o = ($type$) vh.getAndAdd($value1$);
1337         });
1338 
1339         checkUOE(() -> {
1340             $type$ o = ($type$) vh.getAndAddAcquire($value1$);
1341         });
1342 
1343         checkUOE(() -> {
1344             $type$ o = ($type$) vh.getAndAddRelease($value1$);
1345         });
1346 #end[AtomicAdd]
1347 
1348 #if[!Bitwise]
1349         checkUOE(() -> {
1350             $type$ o = ($type$) vh.getAndBitwiseOr($value1$);
1351         });
1352 
1353         checkUOE(() -> {
1354             $type$ o = ($type$) vh.getAndBitwiseOrAcquire($value1$);
1355         });
1356 
1357         checkUOE(() -> {
1358             $type$ o = ($type$) vh.getAndBitwiseOrRelease($value1$);
1359         });
1360 
1361         checkUOE(() -> {
1362             $type$ o = ($type$) vh.getAndBitwiseAnd($value1$);
1363         });
1364 
1365         checkUOE(() -> {
1366             $type$ o = ($type$) vh.getAndBitwiseAndAcquire($value1$);
1367         });
1368 
1369         checkUOE(() -> {
1370             $type$ o = ($type$) vh.getAndBitwiseAndRelease($value1$);
1371         });
1372 
1373         checkUOE(() -> {
1374             $type$ o = ($type$) vh.getAndBitwiseXor($value1$);
1375         });
1376 
1377         checkUOE(() -> {
1378             $type$ o = ($type$) vh.getAndBitwiseXorAcquire($value1$);
1379         });
1380 
1381         checkUOE(() -> {
1382             $type$ o = ($type$) vh.getAndBitwiseXorRelease($value1$);
1383         });
1384 #end[Bitwise]
1385     }
1386 
1387 
1388     static void testArray(VarHandle vh) {
1389         $type$[] array = new $type$[10];
1390 
1391         for (int i = 0; i < array.length; i++) {
1392             // Plain
1393             {
1394                 vh.set(array, i, $value1$);
1395                 $type$ x = ($type$) vh.get(array, i);
1396                 assertEquals(x, $value1$, "get $type$ value");
1397             }
1398 
1399 
1400             // Volatile
1401             {
1402                 vh.setVolatile(array, i, $value2$);
1403                 $type$ x = ($type$) vh.getVolatile(array, i);
1404                 assertEquals(x, $value2$, "setVolatile $type$ value");
1405             }
1406 
1407             // Lazy
1408             {
1409                 vh.setRelease(array, i, $value1$);
1410                 $type$ x = ($type$) vh.getAcquire(array, i);
1411                 assertEquals(x, $value1$, "setRelease $type$ value");
1412             }
1413 
1414             // Opaque
1415             {
1416                 vh.setOpaque(array, i, $value2$);
1417                 $type$ x = ($type$) vh.getOpaque(array, i);
1418                 assertEquals(x, $value2$, "setOpaque $type$ value");
1419             }
1420 
1421 #if[CAS]
1422             vh.set(array, i, $value1$);
1423 
1424             // Compare
1425             {
1426                 boolean r = vh.compareAndSet(array, i, $value1$, $value2$);
1427                 assertEquals(r, true, "success compareAndSet $type$");
1428                 $type$ x = ($type$) vh.get(array, i);
1429                 assertEquals(x, $value2$, "success compareAndSet $type$ value");
1430             }
1431 
1432             {
1433                 boolean r = vh.compareAndSet(array, i, $value1$, $value3$);
1434                 assertEquals(r, false, "failing compareAndSet $type$");
1435                 $type$ x = ($type$) vh.get(array, i);
1436                 assertEquals(x, $value2$, "failing compareAndSet $type$ value");
1437             }
1438 
1439             {
1440                 $type$ r = ($type$) vh.compareAndExchange(array, i, $value2$, $value1$);
1441                 assertEquals(r, $value2$, "success compareAndExchange $type$");
1442                 $type$ x = ($type$) vh.get(array, i);
1443                 assertEquals(x, $value1$, "success compareAndExchange $type$ value");
1444             }
1445 
1446             {
1447                 $type$ r = ($type$) vh.compareAndExchange(array, i, $value2$, $value3$);
1448                 assertEquals(r, $value1$, "failing compareAndExchange $type$");
1449                 $type$ x = ($type$) vh.get(array, i);
1450                 assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
1451             }
1452 
1453             {
1454                 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$);
1455                 assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
1456                 $type$ x = ($type$) vh.get(array, i);
1457                 assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
1458             }
1459 
1460             {
1461                 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value3$);
1462                 assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
1463                 $type$ x = ($type$) vh.get(array, i);
1464                 assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
1465             }
1466 
1467             {
1468                 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value1$);
1469                 assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
1470                 $type$ x = ($type$) vh.get(array, i);
1471                 assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
1472             }
1473 
1474             {
1475                 $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value3$);
1476                 assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
1477                 $type$ x = ($type$) vh.get(array, i);
1478                 assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
1479             }
1480 
1481             {
1482                 boolean success = false;
1483                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1484                     success = vh.weakCompareAndSetPlain(array, i, $value1$, $value2$);
1485                 }
1486                 assertEquals(success, true, "weakCompareAndSetPlain $type$");
1487                 $type$ x = ($type$) vh.get(array, i);
1488                 assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
1489             }
1490 
1491             {
1492                 boolean success = false;
1493                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1494                     success = vh.weakCompareAndSetAcquire(array, i, $value2$, $value1$);
1495                 }
1496                 assertEquals(success, true, "weakCompareAndSetAcquire $type$");
1497                 $type$ x = ($type$) vh.get(array, i);
1498                 assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
1499             }
1500 
1501             {
1502                 boolean success = false;
1503                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1504                     success = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$);
1505                 }
1506                 assertEquals(success, true, "weakCompareAndSetRelease $type$");
1507                 $type$ x = ($type$) vh.get(array, i);
1508                 assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
1509             }
1510 
1511             {
1512                 boolean success = false;
1513                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1514                     success = vh.weakCompareAndSet(array, i, $value2$, $value1$);
1515                 }
1516                 assertEquals(success, true, "weakCompareAndSet $type$");
1517                 $type$ x = ($type$) vh.get(array, i);
1518                 assertEquals(x, $value1$, "weakCompareAndSet $type$");
1519             }
1520 
1521             // Compare set and get
1522             {
1523                 vh.set(array, i, $value1$);
1524 
1525                 $type$ o = ($type$) vh.getAndSet(array, i, $value2$);
1526                 assertEquals(o, $value1$, "getAndSet $type$");
1527                 $type$ x = ($type$) vh.get(array, i);
1528                 assertEquals(x, $value2$, "getAndSet $type$ value");
1529             }
1530 
1531             {
1532                 vh.set(array, i, $value1$);
1533 
1534                 $type$ o = ($type$) vh.getAndSetAcquire(array, i, $value2$);
1535                 assertEquals(o, $value1$, "getAndSetAcquire $type$");
1536                 $type$ x = ($type$) vh.get(array, i);
1537                 assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
1538             }
1539 
1540             {
1541                 vh.set(array, i, $value1$);
1542 
1543                 $type$ o = ($type$) vh.getAndSetRelease(array, i, $value2$);
1544                 assertEquals(o, $value1$, "getAndSetRelease $type$");
1545                 $type$ x = ($type$) vh.get(array, i);
1546                 assertEquals(x, $value2$, "getAndSetRelease $type$ value");
1547             }
1548 #end[CAS]
1549 
1550 #if[AtomicAdd]
1551             // get and add, add and get
1552             {
1553                 vh.set(array, i, $value1$);
1554 
1555                 $type$ o = ($type$) vh.getAndAdd(array, i, $value2$);
1556                 assertEquals(o, $value1$, "getAndAdd $type$");
1557                 $type$ x = ($type$) vh.get(array, i);
1558                 assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
1559             }
1560 
1561             {
1562                 vh.set(array, i, $value1$);
1563 
1564                 $type$ o = ($type$) vh.getAndAddAcquire(array, i, $value2$);
1565                 assertEquals(o, $value1$, "getAndAddAcquire $type$");
1566                 $type$ x = ($type$) vh.get(array, i);
1567                 assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
1568             }
1569 
1570             {
1571                 vh.set(array, i, $value1$);
1572 
1573                 $type$ o = ($type$) vh.getAndAddRelease(array, i, $value2$);
1574                 assertEquals(o, $value1$, "getAndAddRelease$type$");
1575                 $type$ x = ($type$) vh.get(array, i);
1576                 assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
1577             }
1578 #end[AtomicAdd]
1579 
1580 #if[Bitwise]
1581             // get and bitwise or
1582             {
1583                 vh.set(array, i, $value1$);
1584 
1585                 $type$ o = ($type$) vh.getAndBitwiseOr(array, i, $value2$);
1586                 assertEquals(o, $value1$, "getAndBitwiseOr $type$");
1587                 $type$ x = ($type$) vh.get(array, i);
1588                 assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
1589             }
1590 
1591             {
1592                 vh.set(array, i, $value1$);
1593 
1594                 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, $value2$);
1595                 assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
1596                 $type$ x = ($type$) vh.get(array, i);
1597                 assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
1598             }
1599 
1600             {
1601                 vh.set(array, i, $value1$);
1602 
1603                 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, $value2$);
1604                 assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
1605                 $type$ x = ($type$) vh.get(array, i);
1606                 assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
1607             }
1608 
1609             // get and bitwise and
1610             {
1611                 vh.set(array, i, $value1$);
1612 
1613                 $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, $value2$);
1614                 assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
1615                 $type$ x = ($type$) vh.get(array, i);
1616                 assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
1617             }
1618 
1619             {
1620                 vh.set(array, i, $value1$);
1621 
1622                 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, $value2$);
1623                 assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
1624                 $type$ x = ($type$) vh.get(array, i);
1625                 assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
1626             }
1627 
1628             {
1629                 vh.set(array, i, $value1$);
1630 
1631                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, $value2$);
1632                 assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
1633                 $type$ x = ($type$) vh.get(array, i);
1634                 assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
1635             }
1636 
1637             // get and bitwise xor
1638             {
1639                 vh.set(array, i, $value1$);
1640 
1641                 $type$ o = ($type$) vh.getAndBitwiseXor(array, i, $value2$);
1642                 assertEquals(o, $value1$, "getAndBitwiseXor $type$");
1643                 $type$ x = ($type$) vh.get(array, i);
1644                 assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
1645             }
1646 
1647             {
1648                 vh.set(array, i, $value1$);
1649 
1650                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, $value2$);
1651                 assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
1652                 $type$ x = ($type$) vh.get(array, i);
1653                 assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
1654             }
1655 
1656             {
1657                 vh.set(array, i, $value1$);
1658 
1659                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, $value2$);
1660                 assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
1661                 $type$ x = ($type$) vh.get(array, i);
1662                 assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
1663             }
1664 #end[Bitwise]
1665         }
1666     }
1667 
1668     static void testArrayUnsupported(VarHandle vh) {
1669         $type$[] array = new $type$[10];
1670 
1671         int i = 0;
1672 #if[!CAS]
1673         checkUOE(() -> {
1674             boolean r = vh.compareAndSet(array, i, $value1$, $value2$);
1675         });
1676 
1677         checkUOE(() -> {
1678             $type$ r = ($type$) vh.compareAndExchange(array, i, $value1$, $value2$);
1679         });
1680 
1681         checkUOE(() -> {
1682             $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$);
1683         });
1684 
1685         checkUOE(() -> {
1686             $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value1$, $value2$);
1687         });
1688 
1689         checkUOE(() -> {
1690             boolean r = vh.weakCompareAndSetPlain(array, i, $value1$, $value2$);
1691         });
1692 
1693         checkUOE(() -> {
1694             boolean r = vh.weakCompareAndSet(array, i, $value1$, $value2$);
1695         });
1696 
1697         checkUOE(() -> {
1698             boolean r = vh.weakCompareAndSetAcquire(array, i, $value1$, $value2$);
1699         });
1700 
1701         checkUOE(() -> {
1702             boolean r = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$);
1703         });
1704 
1705         checkUOE(() -> {
1706             $type$ r = ($type$) vh.getAndSet(array, i, $value1$);
1707         });
1708 
1709         checkUOE(() -> {
1710             $type$ r = ($type$) vh.getAndSetAcquire(array, i, $value1$);
1711         });
1712 
1713         checkUOE(() -> {
1714             $type$ r = ($type$) vh.getAndSetRelease(array, i, $value1$);
1715         });
1716 #end[CAS]
1717 
1718 #if[!AtomicAdd]
1719         checkUOE(() -> {
1720             $type$ o = ($type$) vh.getAndAdd(array, i, $value1$);
1721         });
1722 
1723         checkUOE(() -> {
1724             $type$ o = ($type$) vh.getAndAddAcquire(array, i, $value1$);
1725         });
1726 
1727         checkUOE(() -> {
1728             $type$ o = ($type$) vh.getAndAddRelease(array, i, $value1$);
1729         });
1730 #end[AtomicAdd]
1731 
1732 #if[!Bitwise]
1733         checkUOE(() -> {
1734             $type$ o = ($type$) vh.getAndBitwiseOr(array, i, $value1$);
1735         });
1736 
1737         checkUOE(() -> {
1738             $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, $value1$);
1739         });
1740 
1741         checkUOE(() -> {
1742             $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, $value1$);
1743         });
1744 
1745         checkUOE(() -> {
1746             $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, $value1$);
1747         });
1748 
1749         checkUOE(() -> {
1750             $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, $value1$);
1751         });
1752 
1753         checkUOE(() -> {
1754             $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, $value1$);
1755         });
1756 
1757         checkUOE(() -> {
1758             $type$ o = ($type$) vh.getAndBitwiseXor(array, i, $value1$);
1759         });
1760 
1761         checkUOE(() -> {
1762             $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, $value1$);
1763         });
1764 
1765         checkUOE(() -> {
1766             $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, $value1$);
1767         });
1768 #end[Bitwise]
1769     }
1770 
1771     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1772         $type$[] array = new $type$[10];
1773 
1774         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1775             final int ci = i;
1776 
1777             checkAIOOBE(() -> {
1778                 $type$ x = ($type$) vh.get(array, ci);
1779             });
1780 
1781             checkAIOOBE(() -> {
1782                 vh.set(array, ci, $value1$);
1783             });
1784 
1785             checkAIOOBE(() -> {
1786                 $type$ x = ($type$) vh.getVolatile(array, ci);
1787             });
1788 
1789             checkAIOOBE(() -> {
1790                 vh.setVolatile(array, ci, $value1$);
1791             });
1792 
1793             checkAIOOBE(() -> {
1794                 $type$ x = ($type$) vh.getAcquire(array, ci);
1795             });
1796 
1797             checkAIOOBE(() -> {
1798                 vh.setRelease(array, ci, $value1$);
1799             });
1800 
1801             checkAIOOBE(() -> {
1802                 $type$ x = ($type$) vh.getOpaque(array, ci);
1803             });
1804 
1805             checkAIOOBE(() -> {
1806                 vh.setOpaque(array, ci, $value1$);
1807             });
1808 
1809 #if[CAS]
1810             checkAIOOBE(() -> {
1811                 boolean r = vh.compareAndSet(array, ci, $value1$, $value2$);
1812             });
1813 
1814             checkAIOOBE(() -> {
1815                 $type$ r = ($type$) vh.compareAndExchange(array, ci, $value2$, $value1$);
1816             });
1817 
1818             checkAIOOBE(() -> {
1819                 $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, $value2$, $value1$);
1820             });
1821 
1822             checkAIOOBE(() -> {
1823                 $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, $value2$, $value1$);
1824             });
1825 
1826             checkAIOOBE(() -> {
1827                 boolean r = vh.weakCompareAndSetPlain(array, ci, $value1$, $value2$);
1828             });
1829 
1830             checkAIOOBE(() -> {
1831                 boolean r = vh.weakCompareAndSet(array, ci, $value1$, $value2$);
1832             });
1833 
1834             checkAIOOBE(() -> {
1835                 boolean r = vh.weakCompareAndSetAcquire(array, ci, $value1$, $value2$);
1836             });
1837 
1838             checkAIOOBE(() -> {
1839                 boolean r = vh.weakCompareAndSetRelease(array, ci, $value1$, $value2$);
1840             });
1841 
1842             checkAIOOBE(() -> {
1843                 $type$ o = ($type$) vh.getAndSet(array, ci, $value1$);
1844             });
1845 
1846             checkAIOOBE(() -> {
1847                 $type$ o = ($type$) vh.getAndSetAcquire(array, ci, $value1$);
1848             });
1849 
1850             checkAIOOBE(() -> {
1851                 $type$ o = ($type$) vh.getAndSetRelease(array, ci, $value1$);
1852             });
1853 #end[CAS]
1854 
1855 #if[AtomicAdd]
1856             checkAIOOBE(() -> {
1857                 $type$ o = ($type$) vh.getAndAdd(array, ci, $value1$);
1858             });
1859 
1860             checkAIOOBE(() -> {
1861                 $type$ o = ($type$) vh.getAndAddAcquire(array, ci, $value1$);
1862             });
1863 
1864             checkAIOOBE(() -> {
1865                 $type$ o = ($type$) vh.getAndAddRelease(array, ci, $value1$);
1866             });
1867 #end[AtomicAdd]
1868 
1869 #if[Bitwise]
1870             checkAIOOBE(() -> {
1871                 $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, $value1$);
1872             });
1873 
1874             checkAIOOBE(() -> {
1875                 $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, $value1$);
1876             });
1877 
1878             checkAIOOBE(() -> {
1879                 $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, $value1$);
1880             });
1881 
1882             checkAIOOBE(() -> {
1883                 $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, $value1$);
1884             });
1885 
1886             checkAIOOBE(() -> {
1887                 $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, $value1$);
1888             });
1889 
1890             checkAIOOBE(() -> {
1891                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, $value1$);
1892             });
1893 
1894             checkAIOOBE(() -> {
1895                 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, $value1$);
1896             });
1897 
1898             checkAIOOBE(() -> {
1899                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, $value1$);
1900             });
1901 
1902             checkAIOOBE(() -> {
1903                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, $value1$);
1904             });
1905 #end[Bitwise]
1906         }
1907     }
1908 
1909 #if[String]
1910     static void testArrayStoreException(VarHandle vh) throws Throwable {
1911         Object[] array = new $type$[10];
1912         Arrays.fill(array, $value1$);
1913         Object value = new Object();
1914 
1915         // Set
1916         checkASE(() -> {
1917             vh.set(array, 0, value);
1918         });
1919 
1920         // SetVolatile
1921         checkASE(() -> {
1922             vh.setVolatile(array, 0, value);
1923         });
1924 
1925         // SetOpaque
1926         checkASE(() -> {
1927             vh.setOpaque(array, 0, value);
1928         });
1929 
1930         // SetRelease
1931         checkASE(() -> {
1932             vh.setRelease(array, 0, value);
1933         });
1934 
1935         // CompareAndSet
1936         checkASE(() -> { // receiver reference class
1937             boolean r = vh.compareAndSet(array, 0, $value1$, value);
1938         });
1939 
1940         // WeakCompareAndSet
1941         checkASE(() -> { // receiver reference class
1942             boolean r = vh.weakCompareAndSetPlain(array, 0, $value1$, value);
1943         });
1944 
1945         // WeakCompareAndSetVolatile
1946         checkASE(() -> { // receiver reference class
1947             boolean r = vh.weakCompareAndSet(array, 0, $value1$, value);
1948         });
1949 
1950         // WeakCompareAndSetAcquire
1951         checkASE(() -> { // receiver reference class
1952             boolean r = vh.weakCompareAndSetAcquire(array, 0, $value1$, value);
1953         });
1954 
1955         // WeakCompareAndSetRelease
1956         checkASE(() -> { // receiver reference class
1957             boolean r = vh.weakCompareAndSetRelease(array, 0, $value1$, value);
1958         });
1959 
1960         // CompareAndExchange
1961         checkASE(() -> { // receiver reference class
1962             $type$ x = ($type$) vh.compareAndExchange(array, 0, $value1$, value);
1963         });
1964 
1965         // CompareAndExchangeAcquire
1966         checkASE(() -> { // receiver reference class
1967             $type$ x = ($type$) vh.compareAndExchangeAcquire(array, 0, $value1$, value);
1968         });
1969 
1970         // CompareAndExchangeRelease
1971         checkASE(() -> { // receiver reference class
1972             $type$ x = ($type$) vh.compareAndExchangeRelease(array, 0, $value1$, value);
1973         });
1974 
1975         // GetAndSet
1976         checkASE(() -> { // receiver reference class
1977             $type$ x = ($type$) vh.getAndSet(array, 0, value);
1978         });
1979 
1980         // GetAndSetAcquire
1981         checkASE(() -> { // receiver reference class
1982             $type$ x = ($type$) vh.getAndSetAcquire(array, 0, value);
1983         });
1984 
1985         // GetAndSetRelease
1986         checkASE(() -> { // receiver reference class
1987             $type$ x = ($type$) vh.getAndSetRelease(array, 0, value);
1988         });
1989     }
1990 #end[String]
1991 }
1992 
--- EOF ---