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     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63     @BeforeClass
  64     public void setup() throws Exception {
  65         vhFinalField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestAccessLong.class, "final_v", long.class);
  67 
  68         vhField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestAccessLong.class, "v", long.class);
  70 
  71         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  72             VarHandleTestAccessLong.class, "static_final_v", long.class);
  73 
  74         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  75             VarHandleTestAccessLong.class, "static_v", long.class);
  76 
  77         vhArray = MethodHandles.arrayElementVarHandle(long[].class);
  78     }
  79 
  80 
  81     @DataProvider
  82     public Object[][] varHandlesProvider() throws Exception {
  83         List<VarHandle> vhs = new ArrayList<>();
  84         vhs.add(vhField);
  85         vhs.add(vhStaticField);
  86         vhs.add(vhArray);
  87 
  88         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
  89     }
  90 
  91     @Test(dataProvider = "varHandlesProvider")
  92     public void testIsAccessModeSupported(VarHandle vh) {
  93         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
  94         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
  95         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
  96         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
  97         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
  98         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
  99         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 100         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 101 
 102         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 103         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
 104         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 105         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 106         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 107         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 108         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 109         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 110 
 111         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 112         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
 113     }
 114 
 115 
 116     @DataProvider
 117     public Object[][] typesProvider() throws Exception {
 118         List<Object[]> types = new ArrayList<>();
 119         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessLong.class)});
 120         types.add(new Object[] {vhStaticField, Arrays.asList()});
 121         types.add(new Object[] {vhArray, Arrays.asList(long[].class, int.class)});
 122 
 123         return types.stream().toArray(Object[][]::new);
 124     }
 125 
 126     @Test(dataProvider = "typesProvider")
 127     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 128         assertEquals(vh.varType(), long.class);
 129 
 130         assertEquals(vh.coordinateTypes(), pts);
 131 
 132         testTypes(vh);
 133     }
 134 
 135 
 136     @Test
 137     public void testLookupInstanceToStatic() {
 138         checkIAE("Lookup of static final field to instance final field", () -> {
 139             MethodHandles.lookup().findStaticVarHandle(
 140                     VarHandleTestAccessLong.class, "final_v", long.class);
 141         });
 142 
 143         checkIAE("Lookup of static field to instance field", () -> {
 144             MethodHandles.lookup().findStaticVarHandle(
 145                     VarHandleTestAccessLong.class, "v", long.class);
 146         });
 147     }
 148 
 149     @Test
 150     public void testLookupStaticToInstance() {
 151         checkIAE("Lookup of instance final field to static final field", () -> {
 152             MethodHandles.lookup().findVarHandle(
 153                 VarHandleTestAccessLong.class, "static_final_v", long.class);
 154         });
 155 
 156         checkIAE("Lookup of instance field to static field", () -> {
 157             vhStaticField = MethodHandles.lookup().findVarHandle(
 158                 VarHandleTestAccessLong.class, "static_v", long.class);
 159         });
 160     }
 161 
 162 
 163     @DataProvider
 164     public Object[][] accessTestCaseProvider() throws Exception {
 165         List<AccessTestCase<?>> cases = new ArrayList<>();
 166 
 167         cases.add(new VarHandleAccessTestCase("Instance final field",
 168                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 169         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 170                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 171                                               false));
 172 
 173         cases.add(new VarHandleAccessTestCase("Static final field",
 174                                               vhStaticFinalField, VarHandleTestAccessLong::testStaticFinalField));
 175         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 176                                               vhStaticFinalField, VarHandleTestAccessLong::testStaticFinalFieldUnsupported,
 177                                               false));
 178 
 179         cases.add(new VarHandleAccessTestCase("Instance field",
 180                                               vhField, vh -> testInstanceField(this, vh)));
 181         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 182                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 183                                               false));
 184 
 185         cases.add(new VarHandleAccessTestCase("Static field",
 186                                               vhStaticField, VarHandleTestAccessLong::testStaticField));
 187         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 188                                               vhStaticField, VarHandleTestAccessLong::testStaticFieldUnsupported,
 189                                               false));
 190 
 191         cases.add(new VarHandleAccessTestCase("Array",
 192                                               vhArray, VarHandleTestAccessLong::testArray));
 193         cases.add(new VarHandleAccessTestCase("Array unsupported",
 194                                               vhArray, VarHandleTestAccessLong::testArrayUnsupported,
 195                                               false));
 196         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 197                                               vhArray, VarHandleTestAccessLong::testArrayIndexOutOfBounds,
 198                                               false));
 199 
 200         // Work around issue with jtreg summary reporting which truncates
 201         // the String result of Object.toString to 30 characters, hence
 202         // the first dummy argument
 203         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 204     }
 205 
 206     @Test(dataProvider = "accessTestCaseProvider")
 207     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 208         T t = atc.get();
 209         int iters = atc.requiresLoop() ? ITERS : 1;
 210         for (int c = 0; c < iters; c++) {
 211             atc.testAccess(t);
 212         }
 213     }
 214 
 215 
 216 
 217 
 218     static void testInstanceFinalField(VarHandleTestAccessLong recv, VarHandle vh) {
 219         // Plain
 220         {
 221             long x = (long) vh.get(recv);
 222             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 223         }
 224 
 225 
 226         // Volatile
 227         {
 228             long x = (long) vh.getVolatile(recv);
 229             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 230         }
 231 
 232         // Lazy
 233         {
 234             long x = (long) vh.getAcquire(recv);
 235             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 236         }
 237 
 238         // Opaque
 239         {
 240             long x = (long) vh.getOpaque(recv);
 241             assertEquals(x, 0x0123456789ABCDEFL, "getOpaque long value");
 242         }
 243     }
 244 
 245     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 246         checkUOE(() -> {
 247             vh.set(recv, 0xCAFEBABECAFEBABEL);
 248         });
 249 
 250         checkUOE(() -> {
 251             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 252         });
 253 
 254         checkUOE(() -> {
 255             vh.setRelease(recv, 0xCAFEBABECAFEBABEL);
 256         });
 257 
 258         checkUOE(() -> {
 259             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 260         });
 261 
 262 
 263     }
 264 
 265 
 266     static void testStaticFinalField(VarHandle vh) {
 267         // Plain
 268         {
 269             long x = (long) vh.get();
 270             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 271         }
 272 
 273 
 274         // Volatile
 275         {
 276             long x = (long) vh.getVolatile();
 277             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 278         }
 279 
 280         // Lazy
 281         {
 282             long x = (long) vh.getAcquire();
 283             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 284         }
 285 
 286         // Opaque
 287         {
 288             long x = (long) vh.getOpaque();
 289             assertEquals(x, 0x0123456789ABCDEFL, "getOpaque long value");
 290         }
 291     }
 292 
 293     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 294         checkUOE(() -> {
 295             vh.set(0xCAFEBABECAFEBABEL);
 296         });
 297 
 298         checkUOE(() -> {
 299             vh.setVolatile(0xCAFEBABECAFEBABEL);
 300         });
 301 
 302         checkUOE(() -> {
 303             vh.setRelease(0xCAFEBABECAFEBABEL);
 304         });
 305 
 306         checkUOE(() -> {
 307             vh.setOpaque(0xCAFEBABECAFEBABEL);
 308         });
 309 
 310 
 311     }
 312 
 313 
 314     static void testInstanceField(VarHandleTestAccessLong recv, VarHandle vh) {
 315         // Plain
 316         {
 317             vh.set(recv, 0x0123456789ABCDEFL);
 318             long x = (long) vh.get(recv);
 319             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 320         }
 321 
 322 
 323         // Volatile
 324         {
 325             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 326             long x = (long) vh.getVolatile(recv);
 327             assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 328         }
 329 
 330         // Lazy
 331         {
 332             vh.setRelease(recv, 0x0123456789ABCDEFL);
 333             long x = (long) vh.getAcquire(recv);
 334             assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 335         }
 336 
 337         // Opaque
 338         {
 339             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 340             long x = (long) vh.getOpaque(recv);
 341             assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 342         }
 343 
 344         vh.set(recv, 0x0123456789ABCDEFL);
 345 
 346         // Compare
 347         {
 348             boolean r = vh.compareAndSet(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 349             assertEquals(r, true, "success compareAndSet long");
 350             long x = (long) vh.get(recv);
 351             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 352         }
 353 
 354         {
 355             boolean r = vh.compareAndSet(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 356             assertEquals(r, false, "failing compareAndSet long");
 357             long x = (long) vh.get(recv);
 358             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 359         }
 360 
 361         {
 362             long r = (long) vh.compareAndExchangeVolatile(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 363             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long");
 364             long x = (long) vh.get(recv);
 365             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value");
 366         }
 367 
 368         {
 369             long r = (long) vh.compareAndExchangeVolatile(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 370             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long");
 371             long x = (long) vh.get(recv);
 372             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value");
 373         }
 374 
 375         {
 376             long r = (long) vh.compareAndExchangeAcquire(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 377             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 378             long x = (long) vh.get(recv);
 379             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 380         }
 381 
 382         {
 383             long r = (long) vh.compareAndExchangeAcquire(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 384             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 385             long x = (long) vh.get(recv);
 386             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 387         }
 388 
 389         {
 390             long r = (long) vh.compareAndExchangeRelease(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 391             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 392             long x = (long) vh.get(recv);
 393             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 394         }
 395 
 396         {
 397             long r = (long) vh.compareAndExchangeRelease(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 398             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 399             long x = (long) vh.get(recv);
 400             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 401         }
 402 
 403         {
 404             boolean success = false;
 405             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 406                 success = vh.weakCompareAndSet(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 407             }
 408             assertEquals(success, true, "weakCompareAndSet long");
 409             long x = (long) vh.get(recv);
 410             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSet long value");
 411         }
 412 
 413         {
 414             boolean success = false;
 415             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 416                 success = vh.weakCompareAndSetAcquire(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 417             }
 418             assertEquals(success, true, "weakCompareAndSetAcquire long");
 419             long x = (long) vh.get(recv);
 420             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 421         }
 422 
 423         {
 424             boolean success = false;
 425             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 426                 success = vh.weakCompareAndSetRelease(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 427             }
 428             assertEquals(success, true, "weakCompareAndSetRelease long");
 429             long x = (long) vh.get(recv);
 430             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 431         }
 432 
 433         // Compare set and get
 434         {
 435             long o = (long) vh.getAndSet(recv, 0x0123456789ABCDEFL);
 436             assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 437             long x = (long) vh.get(recv);
 438             assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 439         }
 440 
 441         vh.set(recv, 0x0123456789ABCDEFL);
 442 
 443         // get and add, add and get
 444         {
 445             long o = (long) vh.getAndAdd(recv, 0xDEADBEEFDEADBEEFL);
 446             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 447             long c = (long) vh.addAndGet(recv, 0xDEADBEEFDEADBEEFL);
 448             assertEquals(c, (long)(0x0123456789ABCDEFL + 0xDEADBEEFDEADBEEFL + 0xDEADBEEFDEADBEEFL), "getAndAdd long value");
 449         }
 450     }
 451 
 452     static void testInstanceFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 453 
 454     }
 455 
 456 
 457     static void testStaticField(VarHandle vh) {
 458         // Plain
 459         {
 460             vh.set(0x0123456789ABCDEFL);
 461             long x = (long) vh.get();
 462             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 463         }
 464 
 465 
 466         // Volatile
 467         {
 468             vh.setVolatile(0xCAFEBABECAFEBABEL);
 469             long x = (long) vh.getVolatile();
 470             assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 471         }
 472 
 473         // Lazy
 474         {
 475             vh.setRelease(0x0123456789ABCDEFL);
 476             long x = (long) vh.getAcquire();
 477             assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 478         }
 479 
 480         // Opaque
 481         {
 482             vh.setOpaque(0xCAFEBABECAFEBABEL);
 483             long x = (long) vh.getOpaque();
 484             assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 485         }
 486 
 487         vh.set(0x0123456789ABCDEFL);
 488 
 489         // Compare
 490         {
 491             boolean r = vh.compareAndSet(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 492             assertEquals(r, true, "success compareAndSet long");
 493             long x = (long) vh.get();
 494             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 495         }
 496 
 497         {
 498             boolean r = vh.compareAndSet(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 499             assertEquals(r, false, "failing compareAndSet long");
 500             long x = (long) vh.get();
 501             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 502         }
 503 
 504         {
 505             long r = (long) vh.compareAndExchangeVolatile(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 506             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long");
 507             long x = (long) vh.get();
 508             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value");
 509         }
 510 
 511         {
 512             long r = (long) vh.compareAndExchangeVolatile(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 513             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long");
 514             long x = (long) vh.get();
 515             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value");
 516         }
 517 
 518         {
 519             long r = (long) vh.compareAndExchangeAcquire(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 520             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 521             long x = (long) vh.get();
 522             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 523         }
 524 
 525         {
 526             long r = (long) vh.compareAndExchangeAcquire(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 527             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 528             long x = (long) vh.get();
 529             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 530         }
 531 
 532         {
 533             long r = (long) vh.compareAndExchangeRelease(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 534             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 535             long x = (long) vh.get();
 536             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 537         }
 538 
 539         {
 540             long r = (long) vh.compareAndExchangeRelease(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 541             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 542             long x = (long) vh.get();
 543             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 544         }
 545 
 546         {
 547             boolean success = false;
 548             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 549                 success = vh.weakCompareAndSet(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 550             }
 551             assertEquals(success, true, "weakCompareAndSet long");
 552             long x = (long) vh.get();
 553             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSet long value");
 554         }
 555 
 556         {
 557             boolean success = false;
 558             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 559                 success = vh.weakCompareAndSetAcquire(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 560             }
 561             assertEquals(success, true, "weakCompareAndSetAcquire long");
 562             long x = (long) vh.get();
 563             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 564         }
 565 
 566         {
 567             boolean success = false;
 568             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 569                 success = vh.weakCompareAndSetRelease(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 570             }
 571             assertEquals(success, true, "weakCompareAndSetRelease long");
 572             long x = (long) vh.get();
 573             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 574         }
 575 
 576         // Compare set and get
 577         {
 578             long o = (long) vh.getAndSet( 0x0123456789ABCDEFL);
 579             assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 580             long x = (long) vh.get();
 581             assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 582         }
 583 
 584         vh.set(0x0123456789ABCDEFL);
 585 
 586         // get and add, add and get
 587         {
 588             long o = (long) vh.getAndAdd( 0xDEADBEEFDEADBEEFL);
 589             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 590             long c = (long) vh.addAndGet(0xDEADBEEFDEADBEEFL);
 591             assertEquals(c, (long)(0x0123456789ABCDEFL + 0xDEADBEEFDEADBEEFL + 0xDEADBEEFDEADBEEFL), "getAndAdd long value");
 592         }
 593     }
 594 
 595     static void testStaticFieldUnsupported(VarHandle vh) {
 596 
 597     }
 598 
 599 
 600     static void testArray(VarHandle vh) {
 601         long[] array = new long[10];
 602 
 603         for (int i = 0; i < array.length; i++) {
 604             // Plain
 605             {
 606                 vh.set(array, i, 0x0123456789ABCDEFL);
 607                 long x = (long) vh.get(array, i);
 608                 assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 609             }
 610 
 611 
 612             // Volatile
 613             {
 614                 vh.setVolatile(array, i, 0xCAFEBABECAFEBABEL);
 615                 long x = (long) vh.getVolatile(array, i);
 616                 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 617             }
 618 
 619             // Lazy
 620             {
 621                 vh.setRelease(array, i, 0x0123456789ABCDEFL);
 622                 long x = (long) vh.getAcquire(array, i);
 623                 assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 624             }
 625 
 626             // Opaque
 627             {
 628                 vh.setOpaque(array, i, 0xCAFEBABECAFEBABEL);
 629                 long x = (long) vh.getOpaque(array, i);
 630                 assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 631             }
 632 
 633             vh.set(array, i, 0x0123456789ABCDEFL);
 634 
 635             // Compare
 636             {
 637                 boolean r = vh.compareAndSet(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 638                 assertEquals(r, true, "success compareAndSet long");
 639                 long x = (long) vh.get(array, i);
 640                 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 641             }
 642 
 643             {
 644                 boolean r = vh.compareAndSet(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 645                 assertEquals(r, false, "failing compareAndSet long");
 646                 long x = (long) vh.get(array, i);
 647                 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 648             }
 649 
 650             {
 651                 long r = (long) vh.compareAndExchangeVolatile(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 652                 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long");
 653                 long x = (long) vh.get(array, i);
 654                 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value");
 655             }
 656 
 657             {
 658                 long r = (long) vh.compareAndExchangeVolatile(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 659                 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long");
 660                 long x = (long) vh.get(array, i);
 661                 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value");
 662             }
 663 
 664             {
 665                 long r = (long) vh.compareAndExchangeAcquire(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 666                 assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 667                 long x = (long) vh.get(array, i);
 668                 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 669             }
 670 
 671             {
 672                 long r = (long) vh.compareAndExchangeAcquire(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 673                 assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 674                 long x = (long) vh.get(array, i);
 675                 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 676             }
 677 
 678             {
 679                 long r = (long) vh.compareAndExchangeRelease(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 680                 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 681                 long x = (long) vh.get(array, i);
 682                 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 683             }
 684 
 685             {
 686                 long r = (long) vh.compareAndExchangeRelease(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 687                 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 688                 long x = (long) vh.get(array, i);
 689                 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 690             }
 691 
 692             {
 693                 boolean success = false;
 694                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 695                     success = vh.weakCompareAndSet(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 696                 }
 697                 assertEquals(success, true, "weakCompareAndSet long");
 698                 long x = (long) vh.get(array, i);
 699                 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSet long value");
 700             }
 701 
 702             {
 703                 boolean success = false;
 704                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 705                     success = vh.weakCompareAndSetAcquire(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 706                 }
 707                 assertEquals(success, true, "weakCompareAndSetAcquire long");
 708                 long x = (long) vh.get(array, i);
 709                 assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 710             }
 711 
 712             {
 713                 boolean success = false;
 714                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 715                     success = vh.weakCompareAndSetRelease(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 716                 }
 717                 assertEquals(success, true, "weakCompareAndSetRelease long");
 718                 long x = (long) vh.get(array, i);
 719                 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 720             }
 721 
 722             // Compare set and get
 723             {
 724                 long o = (long) vh.getAndSet(array, i, 0x0123456789ABCDEFL);
 725                 assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 726                 long x = (long) vh.get(array, i);
 727                 assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 728             }
 729 
 730             vh.set(array, i, 0x0123456789ABCDEFL);
 731 
 732             // get and add, add and get
 733             {
 734                 long o = (long) vh.getAndAdd(array, i, 0xDEADBEEFDEADBEEFL);
 735                 assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 736                 long c = (long) vh.addAndGet(array, i, 0xDEADBEEFDEADBEEFL);
 737                 assertEquals(c, (long)(0x0123456789ABCDEFL + 0xDEADBEEFDEADBEEFL + 0xDEADBEEFDEADBEEFL), "getAndAdd long value");
 738             }
 739         }
 740     }
 741 
 742     static void testArrayUnsupported(VarHandle vh) {
 743         long[] array = new long[10];
 744 
 745         int i = 0;
 746 
 747     }
 748 
 749     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 750         long[] array = new long[10];
 751 
 752         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 753             final int ci = i;
 754 
 755             checkIOOBE(() -> {
 756                 long x = (long) vh.get(array, ci);
 757             });
 758 
 759             checkIOOBE(() -> {
 760                 vh.set(array, ci, 0x0123456789ABCDEFL);
 761             });
 762 
 763             checkIOOBE(() -> {
 764                 long x = (long) vh.getVolatile(array, ci);
 765             });
 766 
 767             checkIOOBE(() -> {
 768                 vh.setVolatile(array, ci, 0x0123456789ABCDEFL);
 769             });
 770 
 771             checkIOOBE(() -> {
 772                 long x = (long) vh.getAcquire(array, ci);
 773             });
 774 
 775             checkIOOBE(() -> {
 776                 vh.setRelease(array, ci, 0x0123456789ABCDEFL);
 777             });
 778 
 779             checkIOOBE(() -> {
 780                 long x = (long) vh.getOpaque(array, ci);
 781             });
 782 
 783             checkIOOBE(() -> {
 784                 vh.setOpaque(array, ci, 0x0123456789ABCDEFL);
 785             });
 786 
 787             checkIOOBE(() -> {
 788                 boolean r = vh.compareAndSet(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 789             });
 790 
 791             checkIOOBE(() -> {
 792                 long r = (long) vh.compareAndExchangeVolatile(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 793             });
 794 
 795             checkIOOBE(() -> {
 796                 long r = (long) vh.compareAndExchangeAcquire(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 797             });
 798 
 799             checkIOOBE(() -> {
 800                 long r = (long) vh.compareAndExchangeRelease(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 801             });
 802 
 803             checkIOOBE(() -> {
 804                 boolean r = vh.weakCompareAndSet(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 805             });
 806 
 807             checkIOOBE(() -> {
 808                 boolean r = vh.weakCompareAndSetAcquire(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 809             });
 810 
 811             checkIOOBE(() -> {
 812                 boolean r = vh.weakCompareAndSetRelease(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 813             });
 814 
 815             checkIOOBE(() -> {
 816                 long o = (long) vh.getAndSet(array, ci, 0x0123456789ABCDEFL);
 817             });
 818 
 819             checkIOOBE(() -> {
 820                 long o = (long) vh.getAndAdd(array, ci, 0xDEADBEEFDEADBEEFL);
 821             });
 822 
 823             checkIOOBE(() -> {
 824                 long o = (long) vh.addAndGet(array, ci, 0xDEADBEEFDEADBEEFL);
 825             });
 826         }
 827     }
 828 }
 829