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