1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccessLong
  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessLong
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccessLong
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessLong
  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 VarHandleTestAccessLong extends VarHandleBaseTest {
  45     static final long static_final_v = 0x0123456789ABCDEFL;
  46 
  47     static long static_v;
  48 
  49     final long final_v = 0x0123456789ABCDEFL;
  50 
  51     long v;
  52 
  53     static final long static_final_v2 = 0x0123456789ABCDEFL;
  54 
  55     static long static_v2;
  56 
  57     final long final_v2 = 0x0123456789ABCDEFL;
  58 
  59     long v2;
  60 
  61     VarHandle vhFinalField;
  62 
  63     VarHandle vhField;
  64 
  65     VarHandle vhStaticField;
  66 
  67     VarHandle vhStaticFinalField;
  68 
  69     VarHandle vhArray;
  70 
  71 
  72     VarHandle[] allocate(boolean same) {
  73         List<VarHandle> vhs = new ArrayList<>();
  74 
  75         String postfix = same ? "" : "2";
  76         VarHandle vh;
  77         try {
  78             vh = MethodHandles.lookup().findVarHandle(
  79                     VarHandleTestAccessLong.class, "final_v" + postfix, long.class);
  80             vhs.add(vh);
  81 
  82             vh = MethodHandles.lookup().findVarHandle(
  83                     VarHandleTestAccessLong.class, "v" + postfix, long.class);
  84             vhs.add(vh);
  85 
  86             vh = MethodHandles.lookup().findStaticVarHandle(
  87                 VarHandleTestAccessLong.class, "static_final_v" + postfix, long.class);
  88             vhs.add(vh);
  89 
  90             vh = MethodHandles.lookup().findStaticVarHandle(
  91                 VarHandleTestAccessLong.class, "static_v" + postfix, long.class);
  92             vhs.add(vh);
  93 
  94             if (same) {
  95                 vh = MethodHandles.arrayElementVarHandle(long[].class);
  96             }
  97             else {
  98                 vh = MethodHandles.arrayElementVarHandle(String[].class);
  99             }
 100             vhs.add(vh);
 101         } catch (Exception e) {
 102             throw new InternalError(e);
 103         }
 104         return vhs.toArray(new VarHandle[0]);
 105     }
 106 
 107     @BeforeClass
 108     public void setup() throws Exception {
 109         vhFinalField = MethodHandles.lookup().findVarHandle(
 110                 VarHandleTestAccessLong.class, "final_v", long.class);
 111 
 112         vhField = MethodHandles.lookup().findVarHandle(
 113                 VarHandleTestAccessLong.class, "v", long.class);
 114 
 115         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 116             VarHandleTestAccessLong.class, "static_final_v", long.class);
 117 
 118         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 119             VarHandleTestAccessLong.class, "static_v", long.class);
 120 
 121         vhArray = MethodHandles.arrayElementVarHandle(long[].class);
 122     }
 123 
 124 
 125     @DataProvider
 126     public Object[][] varHandlesProvider() throws Exception {
 127         List<VarHandle> vhs = new ArrayList<>();
 128         vhs.add(vhField);
 129         vhs.add(vhStaticField);
 130         vhs.add(vhArray);
 131 
 132         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 133     }
 134 
 135     @Test
 136     public void testEqualsAndHashCode() {
 137         VarHandle[] vhs1 = allocate(true);
 138         VarHandle[] vhs2 = allocate(true);
 139 
 140         for (int i = 0; i < vhs1.length; i++) {
 141             for (int j = 0; j < vhs1.length; j++) {
 142                 if (i == j) {
 143                     assertEquals(vhs1[i], vhs1[i]);
 144                     assertEquals(vhs1[i], vhs2[i]);
 145                     assertEquals(vhs1[i].hashCode(), vhs2[i].hashCode());
 146                 }
 147                 else {
 148                     assertNotEquals(vhs1[i], vhs1[j]);
 149                     assertNotEquals(vhs1[i], vhs2[j]);
 150                 }
 151             }
 152         }
 153 
 154         VarHandle[] vhs3 = allocate(false);
 155         for (int i = 0; i < vhs1.length; i++) {
 156             assertNotEquals(vhs1[i], vhs3[i]);
 157         }
 158     }
 159 
 160     @Test(dataProvider = "varHandlesProvider")
 161     public void testIsAccessModeSupported(VarHandle vh) {
 162         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 163         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 164         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 165         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 166         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 167         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 168         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 169         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 170 
 171         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 172         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 173         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 174         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 175         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 176         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 178         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 179         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 180         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
 181         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
 182 
 183         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 184         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
 185         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
 186 
 187         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
 188         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
 189         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
 190         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
 191         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 192         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 193         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 194         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 195         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 196     }
 197 
 198 
 199     @DataProvider
 200     public Object[][] typesProvider() throws Exception {
 201         List<Object[]> types = new ArrayList<>();
 202         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessLong.class)});
 203         types.add(new Object[] {vhStaticField, Arrays.asList()});
 204         types.add(new Object[] {vhArray, Arrays.asList(long[].class, int.class)});
 205 
 206         return types.stream().toArray(Object[][]::new);
 207     }
 208 
 209     @Test(dataProvider = "typesProvider")
 210     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 211         assertEquals(vh.varType(), long.class);
 212 
 213         assertEquals(vh.coordinateTypes(), pts);
 214 
 215         testTypes(vh);
 216     }
 217 
 218 
 219     @Test
 220     public void testLookupInstanceToStatic() {
 221         checkIAE("Lookup of static final field to instance final field", () -> {
 222             MethodHandles.lookup().findStaticVarHandle(
 223                     VarHandleTestAccessLong.class, "final_v", long.class);
 224         });
 225 
 226         checkIAE("Lookup of static field to instance field", () -> {
 227             MethodHandles.lookup().findStaticVarHandle(
 228                     VarHandleTestAccessLong.class, "v", long.class);
 229         });
 230     }
 231 
 232     @Test
 233     public void testLookupStaticToInstance() {
 234         checkIAE("Lookup of instance final field to static final field", () -> {
 235             MethodHandles.lookup().findVarHandle(
 236                 VarHandleTestAccessLong.class, "static_final_v", long.class);
 237         });
 238 
 239         checkIAE("Lookup of instance field to static field", () -> {
 240             vhStaticField = MethodHandles.lookup().findVarHandle(
 241                 VarHandleTestAccessLong.class, "static_v", long.class);
 242         });
 243     }
 244 
 245 
 246     @DataProvider
 247     public Object[][] accessTestCaseProvider() throws Exception {
 248         List<AccessTestCase<?>> cases = new ArrayList<>();
 249 
 250         cases.add(new VarHandleAccessTestCase("Instance final field",
 251                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 252         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 253                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 254                                               false));
 255 
 256         cases.add(new VarHandleAccessTestCase("Static final field",
 257                                               vhStaticFinalField, VarHandleTestAccessLong::testStaticFinalField));
 258         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 259                                               vhStaticFinalField, VarHandleTestAccessLong::testStaticFinalFieldUnsupported,
 260                                               false));
 261 
 262         cases.add(new VarHandleAccessTestCase("Instance field",
 263                                               vhField, vh -> testInstanceField(this, vh)));
 264         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 265                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 266                                               false));
 267 
 268         cases.add(new VarHandleAccessTestCase("Static field",
 269                                               vhStaticField, VarHandleTestAccessLong::testStaticField));
 270         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 271                                               vhStaticField, VarHandleTestAccessLong::testStaticFieldUnsupported,
 272                                               false));
 273 
 274         cases.add(new VarHandleAccessTestCase("Array",
 275                                               vhArray, VarHandleTestAccessLong::testArray));
 276         cases.add(new VarHandleAccessTestCase("Array unsupported",
 277                                               vhArray, VarHandleTestAccessLong::testArrayUnsupported,
 278                                               false));
 279         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 280                                               vhArray, VarHandleTestAccessLong::testArrayIndexOutOfBounds,
 281                                               false));
 282         // Work around issue with jtreg summary reporting which truncates
 283         // the String result of Object.toString to 30 characters, hence
 284         // the first dummy argument
 285         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 286     }
 287 
 288     @Test(dataProvider = "accessTestCaseProvider")
 289     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 290         T t = atc.get();
 291         int iters = atc.requiresLoop() ? ITERS : 1;
 292         for (int c = 0; c < iters; c++) {
 293             atc.testAccess(t);
 294         }
 295     }
 296 
 297 
 298 
 299 
 300     static void testInstanceFinalField(VarHandleTestAccessLong recv, VarHandle vh) {
 301         // Plain
 302         {
 303             long x = (long) vh.get(recv);
 304             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 305         }
 306 
 307 
 308         // Volatile
 309         {
 310             long x = (long) vh.getVolatile(recv);
 311             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 312         }
 313 
 314         // Lazy
 315         {
 316             long x = (long) vh.getAcquire(recv);
 317             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 318         }
 319 
 320         // Opaque
 321         {
 322             long x = (long) vh.getOpaque(recv);
 323             assertEquals(x, 0x0123456789ABCDEFL, "getOpaque long value");
 324         }
 325     }
 326 
 327     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 328         checkUOE(() -> {
 329             vh.set(recv, 0xCAFEBABECAFEBABEL);
 330         });
 331 
 332         checkUOE(() -> {
 333             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 334         });
 335 
 336         checkUOE(() -> {
 337             vh.setRelease(recv, 0xCAFEBABECAFEBABEL);
 338         });
 339 
 340         checkUOE(() -> {
 341             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 342         });
 343 
 344 
 345 
 346     }
 347 
 348 
 349     static void testStaticFinalField(VarHandle vh) {
 350         // Plain
 351         {
 352             long x = (long) vh.get();
 353             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 354         }
 355 
 356 
 357         // Volatile
 358         {
 359             long x = (long) vh.getVolatile();
 360             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 361         }
 362 
 363         // Lazy
 364         {
 365             long x = (long) vh.getAcquire();
 366             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 367         }
 368 
 369         // Opaque
 370         {
 371             long x = (long) vh.getOpaque();
 372             assertEquals(x, 0x0123456789ABCDEFL, "getOpaque long value");
 373         }
 374     }
 375 
 376     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 377         checkUOE(() -> {
 378             vh.set(0xCAFEBABECAFEBABEL);
 379         });
 380 
 381         checkUOE(() -> {
 382             vh.setVolatile(0xCAFEBABECAFEBABEL);
 383         });
 384 
 385         checkUOE(() -> {
 386             vh.setRelease(0xCAFEBABECAFEBABEL);
 387         });
 388 
 389         checkUOE(() -> {
 390             vh.setOpaque(0xCAFEBABECAFEBABEL);
 391         });
 392 
 393 
 394 
 395     }
 396 
 397 
 398     static void testInstanceField(VarHandleTestAccessLong recv, VarHandle vh) {
 399         // Plain
 400         {
 401             vh.set(recv, 0x0123456789ABCDEFL);
 402             long x = (long) vh.get(recv);
 403             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 404         }
 405 
 406 
 407         // Volatile
 408         {
 409             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 410             long x = (long) vh.getVolatile(recv);
 411             assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 412         }
 413 
 414         // Lazy
 415         {
 416             vh.setRelease(recv, 0x0123456789ABCDEFL);
 417             long x = (long) vh.getAcquire(recv);
 418             assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 419         }
 420 
 421         // Opaque
 422         {
 423             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 424             long x = (long) vh.getOpaque(recv);
 425             assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 426         }
 427 
 428         vh.set(recv, 0x0123456789ABCDEFL);
 429 
 430         // Compare
 431         {
 432             boolean r = vh.compareAndSet(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 433             assertEquals(r, true, "success compareAndSet long");
 434             long x = (long) vh.get(recv);
 435             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 436         }
 437 
 438         {
 439             boolean r = vh.compareAndSet(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 440             assertEquals(r, false, "failing compareAndSet long");
 441             long x = (long) vh.get(recv);
 442             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 443         }
 444 
 445         {
 446             long r = (long) vh.compareAndExchange(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 447             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchange long");
 448             long x = (long) vh.get(recv);
 449             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchange long value");
 450         }
 451 
 452         {
 453             long r = (long) vh.compareAndExchange(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 454             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchange long");
 455             long x = (long) vh.get(recv);
 456             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchange long value");
 457         }
 458 
 459         {
 460             long r = (long) vh.compareAndExchangeAcquire(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 461             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 462             long x = (long) vh.get(recv);
 463             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 464         }
 465 
 466         {
 467             long r = (long) vh.compareAndExchangeAcquire(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 468             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 469             long x = (long) vh.get(recv);
 470             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 471         }
 472 
 473         {
 474             long r = (long) vh.compareAndExchangeRelease(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 475             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 476             long x = (long) vh.get(recv);
 477             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 478         }
 479 
 480         {
 481             long r = (long) vh.compareAndExchangeRelease(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 482             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 483             long x = (long) vh.get(recv);
 484             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 485         }
 486 
 487         {
 488             boolean success = false;
 489             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 490                 success = vh.weakCompareAndSetPlain(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 491             }
 492             assertEquals(success, true, "weakCompareAndSetPlain long");
 493             long x = (long) vh.get(recv);
 494             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetPlain long value");
 495         }
 496 
 497         {
 498             boolean success = false;
 499             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 500                 success = vh.weakCompareAndSetAcquire(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 501             }
 502             assertEquals(success, true, "weakCompareAndSetAcquire long");
 503             long x = (long) vh.get(recv);
 504             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 505         }
 506 
 507         {
 508             boolean success = false;
 509             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 510                 success = vh.weakCompareAndSetRelease(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 511             }
 512             assertEquals(success, true, "weakCompareAndSetRelease long");
 513             long x = (long) vh.get(recv);
 514             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 515         }
 516 
 517         {
 518             boolean success = false;
 519             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 520                 success = vh.weakCompareAndSet(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 521             }
 522             assertEquals(success, true, "weakCompareAndSet long");
 523             long x = (long) vh.get(recv);
 524             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSet long value");
 525         }
 526 
 527         // Compare set and get
 528         {
 529             vh.set(recv, 0x0123456789ABCDEFL);
 530 
 531             long o = (long) vh.getAndSet(recv, 0xCAFEBABECAFEBABEL);
 532             assertEquals(o, 0x0123456789ABCDEFL, "getAndSet long");
 533             long x = (long) vh.get(recv);
 534             assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSet long value");
 535         }
 536 
 537         {
 538             vh.set(recv, 0x0123456789ABCDEFL);
 539 
 540             long o = (long) vh.getAndSetAcquire(recv, 0xCAFEBABECAFEBABEL);
 541             assertEquals(o, 0x0123456789ABCDEFL, "getAndSetAcquire long");
 542             long x = (long) vh.get(recv);
 543             assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetAcquire long value");
 544         }
 545 
 546         {
 547             vh.set(recv, 0x0123456789ABCDEFL);
 548 
 549             long o = (long) vh.getAndSetRelease(recv, 0xCAFEBABECAFEBABEL);
 550             assertEquals(o, 0x0123456789ABCDEFL, "getAndSetRelease long");
 551             long x = (long) vh.get(recv);
 552             assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetRelease long value");
 553         }
 554 
 555         // get and add, add and get
 556         {
 557             vh.set(recv, 0x0123456789ABCDEFL);
 558 
 559             long o = (long) vh.getAndAdd(recv, 0xCAFEBABECAFEBABEL);
 560             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 561             long x = (long) vh.get(recv);
 562             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long value");
 563         }
 564 
 565         {
 566             vh.set(recv, 0x0123456789ABCDEFL);
 567 
 568             long o = (long) vh.getAndAddAcquire(recv, 0xCAFEBABECAFEBABEL);
 569             assertEquals(o, 0x0123456789ABCDEFL, "getAndAddAcquire long");
 570             long x = (long) vh.get(recv);
 571             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddAcquire long value");
 572         }
 573 
 574         {
 575             vh.set(recv, 0x0123456789ABCDEFL);
 576 
 577             long o = (long) vh.getAndAddRelease(recv, 0xCAFEBABECAFEBABEL);
 578             assertEquals(o, 0x0123456789ABCDEFL, "getAndAddReleaselong");
 579             long x = (long) vh.get(recv);
 580             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddRelease long value");
 581         }
 582 
 583         // get and bitwise or
 584         {
 585             vh.set(recv, 0x0123456789ABCDEFL);
 586 
 587             long o = (long) vh.getAndBitwiseOr(recv, 0xCAFEBABECAFEBABEL);
 588             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOr long");
 589             long x = (long) vh.get(recv);
 590             assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOr long value");
 591         }
 592 
 593         {
 594             vh.set(recv, 0x0123456789ABCDEFL);
 595 
 596             long o = (long) vh.getAndBitwiseOrAcquire(recv, 0xCAFEBABECAFEBABEL);
 597             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrAcquire long");
 598             long x = (long) vh.get(recv);
 599             assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrAcquire long value");
 600         }
 601 
 602         {
 603             vh.set(recv, 0x0123456789ABCDEFL);
 604 
 605             long o = (long) vh.getAndBitwiseOrRelease(recv, 0xCAFEBABECAFEBABEL);
 606             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrRelease long");
 607             long x = (long) vh.get(recv);
 608             assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrRelease long value");
 609         }
 610 
 611         // get and bitwise and
 612         {
 613             vh.set(recv, 0x0123456789ABCDEFL);
 614 
 615             long o = (long) vh.getAndBitwiseAnd(recv, 0xCAFEBABECAFEBABEL);
 616             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAnd long");
 617             long x = (long) vh.get(recv);
 618             assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAnd long value");
 619         }
 620 
 621         {
 622             vh.set(recv, 0x0123456789ABCDEFL);
 623 
 624             long o = (long) vh.getAndBitwiseAndAcquire(recv, 0xCAFEBABECAFEBABEL);
 625             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndAcquire long");
 626             long x = (long) vh.get(recv);
 627             assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndAcquire long value");
 628         }
 629 
 630         {
 631             vh.set(recv, 0x0123456789ABCDEFL);
 632 
 633             long o = (long) vh.getAndBitwiseAndRelease(recv, 0xCAFEBABECAFEBABEL);
 634             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndRelease long");
 635             long x = (long) vh.get(recv);
 636             assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndRelease long value");
 637         }
 638 
 639         // get and bitwise xor
 640         {
 641             vh.set(recv, 0x0123456789ABCDEFL);
 642 
 643             long o = (long) vh.getAndBitwiseXor(recv, 0xCAFEBABECAFEBABEL);
 644             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXor long");
 645             long x = (long) vh.get(recv);
 646             assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXor long value");
 647         }
 648 
 649         {
 650             vh.set(recv, 0x0123456789ABCDEFL);
 651 
 652             long o = (long) vh.getAndBitwiseXorAcquire(recv, 0xCAFEBABECAFEBABEL);
 653             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long");
 654             long x = (long) vh.get(recv);
 655             assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value");
 656         }
 657 
 658         {
 659             vh.set(recv, 0x0123456789ABCDEFL);
 660 
 661             long o = (long) vh.getAndBitwiseXorRelease(recv, 0xCAFEBABECAFEBABEL);
 662             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long");
 663             long x = (long) vh.get(recv);
 664             assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value");
 665         }
 666     }
 667 
 668     static void testInstanceFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 669 
 670 
 671     }
 672 
 673 
 674     static void testStaticField(VarHandle vh) {
 675         // Plain
 676         {
 677             vh.set(0x0123456789ABCDEFL);
 678             long x = (long) vh.get();
 679             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 680         }
 681 
 682 
 683         // Volatile
 684         {
 685             vh.setVolatile(0xCAFEBABECAFEBABEL);
 686             long x = (long) vh.getVolatile();
 687             assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 688         }
 689 
 690         // Lazy
 691         {
 692             vh.setRelease(0x0123456789ABCDEFL);
 693             long x = (long) vh.getAcquire();
 694             assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 695         }
 696 
 697         // Opaque
 698         {
 699             vh.setOpaque(0xCAFEBABECAFEBABEL);
 700             long x = (long) vh.getOpaque();
 701             assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 702         }
 703 
 704         vh.set(0x0123456789ABCDEFL);
 705 
 706         // Compare
 707         {
 708             boolean r = vh.compareAndSet(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 709             assertEquals(r, true, "success compareAndSet long");
 710             long x = (long) vh.get();
 711             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 712         }
 713 
 714         {
 715             boolean r = vh.compareAndSet(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 716             assertEquals(r, false, "failing compareAndSet long");
 717             long x = (long) vh.get();
 718             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 719         }
 720 
 721         {
 722             long r = (long) vh.compareAndExchange(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 723             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchange long");
 724             long x = (long) vh.get();
 725             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchange long value");
 726         }
 727 
 728         {
 729             long r = (long) vh.compareAndExchange(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 730             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchange long");
 731             long x = (long) vh.get();
 732             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchange long value");
 733         }
 734 
 735         {
 736             long r = (long) vh.compareAndExchangeAcquire(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 737             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 738             long x = (long) vh.get();
 739             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 740         }
 741 
 742         {
 743             long r = (long) vh.compareAndExchangeAcquire(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 744             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 745             long x = (long) vh.get();
 746             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 747         }
 748 
 749         {
 750             long r = (long) vh.compareAndExchangeRelease(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 751             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 752             long x = (long) vh.get();
 753             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 754         }
 755 
 756         {
 757             long r = (long) vh.compareAndExchangeRelease(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 758             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 759             long x = (long) vh.get();
 760             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 761         }
 762 
 763         {
 764             boolean success = false;
 765             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 766                 success = vh.weakCompareAndSetPlain(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 767             }
 768             assertEquals(success, true, "weakCompareAndSetPlain long");
 769             long x = (long) vh.get();
 770             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetPlain long value");
 771         }
 772 
 773         {
 774             boolean success = false;
 775             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 776                 success = vh.weakCompareAndSetAcquire(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 777             }
 778             assertEquals(success, true, "weakCompareAndSetAcquire long");
 779             long x = (long) vh.get();
 780             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 781         }
 782 
 783         {
 784             boolean success = false;
 785             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 786                 success = vh.weakCompareAndSetRelease(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 787             }
 788             assertEquals(success, true, "weakCompareAndSetRelease long");
 789             long x = (long) vh.get();
 790             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 791         }
 792 
 793         {
 794             boolean success = false;
 795             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 796                 success = vh.weakCompareAndSet(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 797             }
 798             assertEquals(success, true, "weakCompareAndSet long");
 799             long x = (long) vh.get();
 800             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSet long");
 801         }
 802 
 803         // Compare set and get
 804         {
 805             vh.set(0x0123456789ABCDEFL);
 806 
 807             long o = (long) vh.getAndSet(0xCAFEBABECAFEBABEL);
 808             assertEquals(o, 0x0123456789ABCDEFL, "getAndSet long");
 809             long x = (long) vh.get();
 810             assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSet long value");
 811         }
 812 
 813         {
 814             vh.set(0x0123456789ABCDEFL);
 815 
 816             long o = (long) vh.getAndSetAcquire(0xCAFEBABECAFEBABEL);
 817             assertEquals(o, 0x0123456789ABCDEFL, "getAndSetAcquire long");
 818             long x = (long) vh.get();
 819             assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetAcquire long value");
 820         }
 821 
 822         {
 823             vh.set(0x0123456789ABCDEFL);
 824 
 825             long o = (long) vh.getAndSetRelease(0xCAFEBABECAFEBABEL);
 826             assertEquals(o, 0x0123456789ABCDEFL, "getAndSetRelease long");
 827             long x = (long) vh.get();
 828             assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetRelease long value");
 829         }
 830 
 831         // get and add, add and get
 832         {
 833             vh.set(0x0123456789ABCDEFL);
 834 
 835             long o = (long) vh.getAndAdd(0xCAFEBABECAFEBABEL);
 836             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 837             long x = (long) vh.get();
 838             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long value");
 839         }
 840 
 841         {
 842             vh.set(0x0123456789ABCDEFL);
 843 
 844             long o = (long) vh.getAndAddAcquire(0xCAFEBABECAFEBABEL);
 845             assertEquals(o, 0x0123456789ABCDEFL, "getAndAddAcquire long");
 846             long x = (long) vh.get();
 847             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddAcquire long value");
 848         }
 849 
 850         {
 851             vh.set(0x0123456789ABCDEFL);
 852 
 853             long o = (long) vh.getAndAddRelease(0xCAFEBABECAFEBABEL);
 854             assertEquals(o, 0x0123456789ABCDEFL, "getAndAddReleaselong");
 855             long x = (long) vh.get();
 856             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddRelease long value");
 857         }
 858 
 859         // get and bitwise or
 860         {
 861             vh.set(0x0123456789ABCDEFL);
 862 
 863             long o = (long) vh.getAndBitwiseOr(0xCAFEBABECAFEBABEL);
 864             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOr long");
 865             long x = (long) vh.get();
 866             assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOr long value");
 867         }
 868 
 869         {
 870             vh.set(0x0123456789ABCDEFL);
 871 
 872             long o = (long) vh.getAndBitwiseOrAcquire(0xCAFEBABECAFEBABEL);
 873             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrAcquire long");
 874             long x = (long) vh.get();
 875             assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrAcquire long value");
 876         }
 877 
 878         {
 879             vh.set(0x0123456789ABCDEFL);
 880 
 881             long o = (long) vh.getAndBitwiseOrRelease(0xCAFEBABECAFEBABEL);
 882             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrRelease long");
 883             long x = (long) vh.get();
 884             assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrRelease long value");
 885         }
 886 
 887         // get and bitwise and
 888         {
 889             vh.set(0x0123456789ABCDEFL);
 890 
 891             long o = (long) vh.getAndBitwiseAnd(0xCAFEBABECAFEBABEL);
 892             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAnd long");
 893             long x = (long) vh.get();
 894             assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAnd long value");
 895         }
 896 
 897         {
 898             vh.set(0x0123456789ABCDEFL);
 899 
 900             long o = (long) vh.getAndBitwiseAndAcquire(0xCAFEBABECAFEBABEL);
 901             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndAcquire long");
 902             long x = (long) vh.get();
 903             assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndAcquire long value");
 904         }
 905 
 906         {
 907             vh.set(0x0123456789ABCDEFL);
 908 
 909             long o = (long) vh.getAndBitwiseAndRelease(0xCAFEBABECAFEBABEL);
 910             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndRelease long");
 911             long x = (long) vh.get();
 912             assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndRelease long value");
 913         }
 914 
 915         // get and bitwise xor
 916         {
 917             vh.set(0x0123456789ABCDEFL);
 918 
 919             long o = (long) vh.getAndBitwiseXor(0xCAFEBABECAFEBABEL);
 920             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXor long");
 921             long x = (long) vh.get();
 922             assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXor long value");
 923         }
 924 
 925         {
 926             vh.set(0x0123456789ABCDEFL);
 927 
 928             long o = (long) vh.getAndBitwiseXorAcquire(0xCAFEBABECAFEBABEL);
 929             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long");
 930             long x = (long) vh.get();
 931             assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value");
 932         }
 933 
 934         {
 935             vh.set(0x0123456789ABCDEFL);
 936 
 937             long o = (long) vh.getAndBitwiseXorRelease(0xCAFEBABECAFEBABEL);
 938             assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long");
 939             long x = (long) vh.get();
 940             assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value");
 941         }
 942     }
 943 
 944     static void testStaticFieldUnsupported(VarHandle vh) {
 945 
 946 
 947     }
 948 
 949 
 950     static void testArray(VarHandle vh) {
 951         long[] array = new long[10];
 952 
 953         for (int i = 0; i < array.length; i++) {
 954             // Plain
 955             {
 956                 vh.set(array, i, 0x0123456789ABCDEFL);
 957                 long x = (long) vh.get(array, i);
 958                 assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 959             }
 960 
 961 
 962             // Volatile
 963             {
 964                 vh.setVolatile(array, i, 0xCAFEBABECAFEBABEL);
 965                 long x = (long) vh.getVolatile(array, i);
 966                 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 967             }
 968 
 969             // Lazy
 970             {
 971                 vh.setRelease(array, i, 0x0123456789ABCDEFL);
 972                 long x = (long) vh.getAcquire(array, i);
 973                 assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 974             }
 975 
 976             // Opaque
 977             {
 978                 vh.setOpaque(array, i, 0xCAFEBABECAFEBABEL);
 979                 long x = (long) vh.getOpaque(array, i);
 980                 assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 981             }
 982 
 983             vh.set(array, i, 0x0123456789ABCDEFL);
 984 
 985             // Compare
 986             {
 987                 boolean r = vh.compareAndSet(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 988                 assertEquals(r, true, "success compareAndSet long");
 989                 long x = (long) vh.get(array, i);
 990                 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 991             }
 992 
 993             {
 994                 boolean r = vh.compareAndSet(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 995                 assertEquals(r, false, "failing compareAndSet long");
 996                 long x = (long) vh.get(array, i);
 997                 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 998             }
 999 
1000             {
1001                 long r = (long) vh.compareAndExchange(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
1002                 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchange long");
1003                 long x = (long) vh.get(array, i);
1004                 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchange long value");
1005             }
1006 
1007             {
1008                 long r = (long) vh.compareAndExchange(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
1009                 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchange long");
1010                 long x = (long) vh.get(array, i);
1011                 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchange long value");
1012             }
1013 
1014             {
1015                 long r = (long) vh.compareAndExchangeAcquire(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1016                 assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
1017                 long x = (long) vh.get(array, i);
1018                 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
1019             }
1020 
1021             {
1022                 long r = (long) vh.compareAndExchangeAcquire(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
1023                 assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
1024                 long x = (long) vh.get(array, i);
1025                 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
1026             }
1027 
1028             {
1029                 long r = (long) vh.compareAndExchangeRelease(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
1030                 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
1031                 long x = (long) vh.get(array, i);
1032                 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
1033             }
1034 
1035             {
1036                 long r = (long) vh.compareAndExchangeRelease(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
1037                 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
1038                 long x = (long) vh.get(array, i);
1039                 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
1040             }
1041 
1042             {
1043                 boolean success = false;
1044                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1045                     success = vh.weakCompareAndSetPlain(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1046                 }
1047                 assertEquals(success, true, "weakCompareAndSetPlain long");
1048                 long x = (long) vh.get(array, i);
1049                 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetPlain long value");
1050             }
1051 
1052             {
1053                 boolean success = false;
1054                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1055                     success = vh.weakCompareAndSetAcquire(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
1056                 }
1057                 assertEquals(success, true, "weakCompareAndSetAcquire long");
1058                 long x = (long) vh.get(array, i);
1059                 assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
1060             }
1061 
1062             {
1063                 boolean success = false;
1064                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1065                     success = vh.weakCompareAndSetRelease(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1066                 }
1067                 assertEquals(success, true, "weakCompareAndSetRelease long");
1068                 long x = (long) vh.get(array, i);
1069                 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
1070             }
1071 
1072             {
1073                 boolean success = false;
1074                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1075                     success = vh.weakCompareAndSet(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
1076                 }
1077                 assertEquals(success, true, "weakCompareAndSet long");
1078                 long x = (long) vh.get(array, i);
1079                 assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSet long");
1080             }
1081 
1082             // Compare set and get
1083             {
1084                 vh.set(array, i, 0x0123456789ABCDEFL);
1085 
1086                 long o = (long) vh.getAndSet(array, i, 0xCAFEBABECAFEBABEL);
1087                 assertEquals(o, 0x0123456789ABCDEFL, "getAndSet long");
1088                 long x = (long) vh.get(array, i);
1089                 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSet long value");
1090             }
1091 
1092             {
1093                 vh.set(array, i, 0x0123456789ABCDEFL);
1094 
1095                 long o = (long) vh.getAndSetAcquire(array, i, 0xCAFEBABECAFEBABEL);
1096                 assertEquals(o, 0x0123456789ABCDEFL, "getAndSetAcquire long");
1097                 long x = (long) vh.get(array, i);
1098                 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetAcquire long value");
1099             }
1100 
1101             {
1102                 vh.set(array, i, 0x0123456789ABCDEFL);
1103 
1104                 long o = (long) vh.getAndSetRelease(array, i, 0xCAFEBABECAFEBABEL);
1105                 assertEquals(o, 0x0123456789ABCDEFL, "getAndSetRelease long");
1106                 long x = (long) vh.get(array, i);
1107                 assertEquals(x, 0xCAFEBABECAFEBABEL, "getAndSetRelease long value");
1108             }
1109 
1110             // get and add, add and get
1111             {
1112                 vh.set(array, i, 0x0123456789ABCDEFL);
1113 
1114                 long o = (long) vh.getAndAdd(array, i, 0xCAFEBABECAFEBABEL);
1115                 assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
1116                 long x = (long) vh.get(array, i);
1117                 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long value");
1118             }
1119 
1120             {
1121                 vh.set(array, i, 0x0123456789ABCDEFL);
1122 
1123                 long o = (long) vh.getAndAddAcquire(array, i, 0xCAFEBABECAFEBABEL);
1124                 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddAcquire long");
1125                 long x = (long) vh.get(array, i);
1126                 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddAcquire long value");
1127             }
1128 
1129             {
1130                 vh.set(array, i, 0x0123456789ABCDEFL);
1131 
1132                 long o = (long) vh.getAndAddRelease(array, i, 0xCAFEBABECAFEBABEL);
1133                 assertEquals(o, 0x0123456789ABCDEFL, "getAndAddReleaselong");
1134                 long x = (long) vh.get(array, i);
1135                 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAddRelease long value");
1136             }
1137 
1138             // get and bitwise or
1139             {
1140                 vh.set(array, i, 0x0123456789ABCDEFL);
1141 
1142                 long o = (long) vh.getAndBitwiseOr(array, i, 0xCAFEBABECAFEBABEL);
1143                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOr long");
1144                 long x = (long) vh.get(array, i);
1145                 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOr long value");
1146             }
1147 
1148             {
1149                 vh.set(array, i, 0x0123456789ABCDEFL);
1150 
1151                 long o = (long) vh.getAndBitwiseOrAcquire(array, i, 0xCAFEBABECAFEBABEL);
1152                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrAcquire long");
1153                 long x = (long) vh.get(array, i);
1154                 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrAcquire long value");
1155             }
1156 
1157             {
1158                 vh.set(array, i, 0x0123456789ABCDEFL);
1159 
1160                 long o = (long) vh.getAndBitwiseOrRelease(array, i, 0xCAFEBABECAFEBABEL);
1161                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseOrRelease long");
1162                 long x = (long) vh.get(array, i);
1163                 assertEquals(x, (long)(0x0123456789ABCDEFL | 0xCAFEBABECAFEBABEL), "getAndBitwiseOrRelease long value");
1164             }
1165 
1166             // get and bitwise and
1167             {
1168                 vh.set(array, i, 0x0123456789ABCDEFL);
1169 
1170                 long o = (long) vh.getAndBitwiseAnd(array, i, 0xCAFEBABECAFEBABEL);
1171                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAnd long");
1172                 long x = (long) vh.get(array, i);
1173                 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAnd long value");
1174             }
1175 
1176             {
1177                 vh.set(array, i, 0x0123456789ABCDEFL);
1178 
1179                 long o = (long) vh.getAndBitwiseAndAcquire(array, i, 0xCAFEBABECAFEBABEL);
1180                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndAcquire long");
1181                 long x = (long) vh.get(array, i);
1182                 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndAcquire long value");
1183             }
1184 
1185             {
1186                 vh.set(array, i, 0x0123456789ABCDEFL);
1187 
1188                 long o = (long) vh.getAndBitwiseAndRelease(array, i, 0xCAFEBABECAFEBABEL);
1189                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseAndRelease long");
1190                 long x = (long) vh.get(array, i);
1191                 assertEquals(x, (long)(0x0123456789ABCDEFL & 0xCAFEBABECAFEBABEL), "getAndBitwiseAndRelease long value");
1192             }
1193 
1194             // get and bitwise xor
1195             {
1196                 vh.set(array, i, 0x0123456789ABCDEFL);
1197 
1198                 long o = (long) vh.getAndBitwiseXor(array, i, 0xCAFEBABECAFEBABEL);
1199                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXor long");
1200                 long x = (long) vh.get(array, i);
1201                 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXor long value");
1202             }
1203 
1204             {
1205                 vh.set(array, i, 0x0123456789ABCDEFL);
1206 
1207                 long o = (long) vh.getAndBitwiseXorAcquire(array, i, 0xCAFEBABECAFEBABEL);
1208                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long");
1209                 long x = (long) vh.get(array, i);
1210                 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value");
1211             }
1212 
1213             {
1214                 vh.set(array, i, 0x0123456789ABCDEFL);
1215 
1216                 long o = (long) vh.getAndBitwiseXorRelease(array, i, 0xCAFEBABECAFEBABEL);
1217                 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long");
1218                 long x = (long) vh.get(array, i);
1219                 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value");
1220             }
1221         }
1222     }
1223 
1224     static void testArrayUnsupported(VarHandle vh) {
1225         long[] array = new long[10];
1226 
1227         int i = 0;
1228 
1229 
1230     }
1231 
1232     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1233         long[] array = new long[10];
1234 
1235         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1236             final int ci = i;
1237 
1238             checkIOOBE(() -> {
1239                 long x = (long) vh.get(array, ci);
1240             });
1241 
1242             checkIOOBE(() -> {
1243                 vh.set(array, ci, 0x0123456789ABCDEFL);
1244             });
1245 
1246             checkIOOBE(() -> {
1247                 long x = (long) vh.getVolatile(array, ci);
1248             });
1249 
1250             checkIOOBE(() -> {
1251                 vh.setVolatile(array, ci, 0x0123456789ABCDEFL);
1252             });
1253 
1254             checkIOOBE(() -> {
1255                 long x = (long) vh.getAcquire(array, ci);
1256             });
1257 
1258             checkIOOBE(() -> {
1259                 vh.setRelease(array, ci, 0x0123456789ABCDEFL);
1260             });
1261 
1262             checkIOOBE(() -> {
1263                 long x = (long) vh.getOpaque(array, ci);
1264             });
1265 
1266             checkIOOBE(() -> {
1267                 vh.setOpaque(array, ci, 0x0123456789ABCDEFL);
1268             });
1269 
1270             checkIOOBE(() -> {
1271                 boolean r = vh.compareAndSet(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1272             });
1273 
1274             checkIOOBE(() -> {
1275                 long r = (long) vh.compareAndExchange(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
1276             });
1277 
1278             checkIOOBE(() -> {
1279                 long r = (long) vh.compareAndExchangeAcquire(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
1280             });
1281 
1282             checkIOOBE(() -> {
1283                 long r = (long) vh.compareAndExchangeRelease(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
1284             });
1285 
1286             checkIOOBE(() -> {
1287                 boolean r = vh.weakCompareAndSetPlain(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1288             });
1289 
1290             checkIOOBE(() -> {
1291                 boolean r = vh.weakCompareAndSet(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1292             });
1293 
1294             checkIOOBE(() -> {
1295                 boolean r = vh.weakCompareAndSetAcquire(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1296             });
1297 
1298             checkIOOBE(() -> {
1299                 boolean r = vh.weakCompareAndSetRelease(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
1300             });
1301 
1302             checkIOOBE(() -> {
1303                 long o = (long) vh.getAndSet(array, ci, 0x0123456789ABCDEFL);
1304             });
1305 
1306             checkIOOBE(() -> {
1307                 long o = (long) vh.getAndSetAcquire(array, ci, 0x0123456789ABCDEFL);
1308             });
1309 
1310             checkIOOBE(() -> {
1311                 long o = (long) vh.getAndSetRelease(array, ci, 0x0123456789ABCDEFL);
1312             });
1313 
1314             checkIOOBE(() -> {
1315                 long o = (long) vh.getAndAdd(array, ci, 0x0123456789ABCDEFL);
1316             });
1317 
1318             checkIOOBE(() -> {
1319                 long o = (long) vh.getAndAddAcquire(array, ci, 0x0123456789ABCDEFL);
1320             });
1321 
1322             checkIOOBE(() -> {
1323                 long o = (long) vh.getAndAddRelease(array, ci, 0x0123456789ABCDEFL);
1324             });
1325 
1326             checkIOOBE(() -> {
1327                 long o = (long) vh.getAndBitwiseOr(array, ci, 0x0123456789ABCDEFL);
1328             });
1329 
1330             checkIOOBE(() -> {
1331                 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, 0x0123456789ABCDEFL);
1332             });
1333 
1334             checkIOOBE(() -> {
1335                 long o = (long) vh.getAndBitwiseOrRelease(array, ci, 0x0123456789ABCDEFL);
1336             });
1337 
1338             checkIOOBE(() -> {
1339                 long o = (long) vh.getAndBitwiseAnd(array, ci, 0x0123456789ABCDEFL);
1340             });
1341 
1342             checkIOOBE(() -> {
1343                 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, 0x0123456789ABCDEFL);
1344             });
1345 
1346             checkIOOBE(() -> {
1347                 long o = (long) vh.getAndBitwiseAndRelease(array, ci, 0x0123456789ABCDEFL);
1348             });
1349 
1350             checkIOOBE(() -> {
1351                 long o = (long) vh.getAndBitwiseXor(array, ci, 0x0123456789ABCDEFL);
1352             });
1353 
1354             checkIOOBE(() -> {
1355                 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, 0x0123456789ABCDEFL);
1356             });
1357 
1358             checkIOOBE(() -> {
1359                 long o = (long) vh.getAndBitwiseXorRelease(array, ci, 0x0123456789ABCDEFL);
1360             });
1361         }
1362     }
1363 
1364 }
1365