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