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