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