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