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