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