1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccessBoolean
  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessBoolean
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccessBoolean
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessBoolean
  30  */
  31 
  32 import org.testng.annotations.BeforeClass;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class VarHandleTestAccessBoolean extends VarHandleBaseTest {
  45     static final boolean static_final_v = true;
  46 
  47     static boolean static_v;
  48 
  49     final boolean final_v = true;
  50 
  51     boolean v;
  52 
  53     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63     @BeforeClass
  64     public void setup() throws Exception {
  65         vhFinalField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestAccessBoolean.class, "final_v", boolean.class);
  67 
  68         vhField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestAccessBoolean.class, "v", boolean.class);
  70 
  71         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  72             VarHandleTestAccessBoolean.class, "static_final_v", boolean.class);
  73 
  74         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  75             VarHandleTestAccessBoolean.class, "static_v", boolean.class);
  76 
  77         vhArray = MethodHandles.arrayElementVarHandle(boolean[].class);
  78     }
  79 
  80 
  81     @DataProvider
  82     public Object[][] varHandlesProvider() throws Exception {
  83         List<VarHandle> vhs = new ArrayList<>();
  84         vhs.add(vhField);
  85         vhs.add(vhStaticField);
  86         vhs.add(vhArray);
  87 
  88         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
  89     }
  90 
  91     @Test(dataProvider = "varHandlesProvider")
  92     public void testIsAccessModeSupported(VarHandle vh) {
  93         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
  94         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
  95         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
  96         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
  97         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
  98         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
  99         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 100         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 101 
 102         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 103         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
 104         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 105         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 106         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 107         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 108         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 109         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));

 110 
 111         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 112         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
 113     }
 114 
 115 
 116     @DataProvider
 117     public Object[][] typesProvider() throws Exception {
 118         List<Object[]> types = new ArrayList<>();
 119         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessBoolean.class)});
 120         types.add(new Object[] {vhStaticField, Arrays.asList()});
 121         types.add(new Object[] {vhArray, Arrays.asList(boolean[].class, int.class)});
 122 
 123         return types.stream().toArray(Object[][]::new);
 124     }
 125 
 126     @Test(dataProvider = "typesProvider")
 127     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 128         assertEquals(vh.varType(), boolean.class);
 129 
 130         assertEquals(vh.coordinateTypes(), pts);
 131 
 132         testTypes(vh);
 133     }
 134 
 135 
 136     @Test
 137     public void testLookupInstanceToStatic() {
 138         checkIAE("Lookup of static final field to instance final field", () -> {
 139             MethodHandles.lookup().findStaticVarHandle(
 140                     VarHandleTestAccessBoolean.class, "final_v", boolean.class);
 141         });
 142 
 143         checkIAE("Lookup of static field to instance field", () -> {
 144             MethodHandles.lookup().findStaticVarHandle(
 145                     VarHandleTestAccessBoolean.class, "v", boolean.class);
 146         });
 147     }
 148 
 149     @Test
 150     public void testLookupStaticToInstance() {
 151         checkIAE("Lookup of instance final field to static final field", () -> {
 152             MethodHandles.lookup().findVarHandle(
 153                 VarHandleTestAccessBoolean.class, "static_final_v", boolean.class);
 154         });
 155 
 156         checkIAE("Lookup of instance field to static field", () -> {
 157             vhStaticField = MethodHandles.lookup().findVarHandle(
 158                 VarHandleTestAccessBoolean.class, "static_v", boolean.class);
 159         });
 160     }
 161 
 162 
 163     @DataProvider
 164     public Object[][] accessTestCaseProvider() throws Exception {
 165         List<AccessTestCase<?>> cases = new ArrayList<>();
 166 
 167         cases.add(new VarHandleAccessTestCase("Instance final field",
 168                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 169         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 170                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 171                                               false));
 172 
 173         cases.add(new VarHandleAccessTestCase("Static final field",
 174                                               vhStaticFinalField, VarHandleTestAccessBoolean::testStaticFinalField));
 175         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 176                                               vhStaticFinalField, VarHandleTestAccessBoolean::testStaticFinalFieldUnsupported,
 177                                               false));
 178 
 179         cases.add(new VarHandleAccessTestCase("Instance field",
 180                                               vhField, vh -> testInstanceField(this, vh)));
 181         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 182                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 183                                               false));
 184 
 185         cases.add(new VarHandleAccessTestCase("Static field",
 186                                               vhStaticField, VarHandleTestAccessBoolean::testStaticField));
 187         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 188                                               vhStaticField, VarHandleTestAccessBoolean::testStaticFieldUnsupported,
 189                                               false));
 190 
 191         cases.add(new VarHandleAccessTestCase("Array",
 192                                               vhArray, VarHandleTestAccessBoolean::testArray));
 193         cases.add(new VarHandleAccessTestCase("Array unsupported",
 194                                               vhArray, VarHandleTestAccessBoolean::testArrayUnsupported,
 195                                               false));
 196         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 197                                               vhArray, VarHandleTestAccessBoolean::testArrayIndexOutOfBounds,
 198                                               false));
 199 
 200         // Work around issue with jtreg summary reporting which truncates
 201         // the String result of Object.toString to 30 characters, hence
 202         // the first dummy argument
 203         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 204     }
 205 
 206     @Test(dataProvider = "accessTestCaseProvider")
 207     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 208         T t = atc.get();
 209         int iters = atc.requiresLoop() ? ITERS : 1;
 210         for (int c = 0; c < iters; c++) {
 211             atc.testAccess(t);
 212         }
 213     }
 214 
 215 
 216 
 217 
 218     static void testInstanceFinalField(VarHandleTestAccessBoolean recv, VarHandle vh) {
 219         // Plain
 220         {
 221             boolean x = (boolean) vh.get(recv);
 222             assertEquals(x, true, "get boolean value");
 223         }
 224 
 225 
 226         // Volatile
 227         {
 228             boolean x = (boolean) vh.getVolatile(recv);
 229             assertEquals(x, true, "getVolatile boolean value");
 230         }
 231 
 232         // Lazy
 233         {
 234             boolean x = (boolean) vh.getAcquire(recv);
 235             assertEquals(x, true, "getRelease boolean value");
 236         }
 237 
 238         // Opaque
 239         {
 240             boolean x = (boolean) vh.getOpaque(recv);
 241             assertEquals(x, true, "getOpaque boolean value");
 242         }
 243     }
 244 
 245     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) {
 246         checkUOE(() -> {
 247             vh.set(recv, false);
 248         });
 249 
 250         checkUOE(() -> {
 251             vh.setVolatile(recv, false);
 252         });
 253 
 254         checkUOE(() -> {
 255             vh.setRelease(recv, false);
 256         });
 257 
 258         checkUOE(() -> {
 259             vh.setOpaque(recv, false);
 260         });
 261 































 262 
 263         checkUOE(() -> {
 264             boolean o = (boolean) vh.getAndAdd(recv, true);
 265         });
 266 
 267         checkUOE(() -> {
 268             boolean o = (boolean) vh.addAndGet(recv, true);
 269         });
 270     }
 271 
 272 
 273     static void testStaticFinalField(VarHandle vh) {
 274         // Plain
 275         {
 276             boolean x = (boolean) vh.get();
 277             assertEquals(x, true, "get boolean value");
 278         }
 279 
 280 
 281         // Volatile
 282         {
 283             boolean x = (boolean) vh.getVolatile();
 284             assertEquals(x, true, "getVolatile boolean value");
 285         }
 286 
 287         // Lazy
 288         {
 289             boolean x = (boolean) vh.getAcquire();
 290             assertEquals(x, true, "getRelease boolean value");
 291         }
 292 
 293         // Opaque
 294         {
 295             boolean x = (boolean) vh.getOpaque();
 296             assertEquals(x, true, "getOpaque boolean value");
 297         }
 298     }
 299 
 300     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 301         checkUOE(() -> {
 302             vh.set(false);
 303         });
 304 
 305         checkUOE(() -> {
 306             vh.setVolatile(false);
 307         });
 308 
 309         checkUOE(() -> {
 310             vh.setRelease(false);
 311         });
 312 
 313         checkUOE(() -> {
 314             vh.setOpaque(false);
 315         });
 316 































 317 
 318         checkUOE(() -> {
 319             boolean o = (boolean) vh.getAndAdd(true);
 320         });
 321 
 322         checkUOE(() -> {
 323             boolean o = (boolean) vh.addAndGet(true);
 324         });
 325     }
 326 
 327 
 328     static void testInstanceField(VarHandleTestAccessBoolean recv, VarHandle vh) {
 329         // Plain
 330         {
 331             vh.set(recv, true);
 332             boolean x = (boolean) vh.get(recv);
 333             assertEquals(x, true, "set boolean value");
 334         }
 335 
 336 
 337         // Volatile
 338         {
 339             vh.setVolatile(recv, false);
 340             boolean x = (boolean) vh.getVolatile(recv);
 341             assertEquals(x, false, "setVolatile boolean value");
 342         }
 343 
 344         // Lazy
 345         {
 346             vh.setRelease(recv, true);
 347             boolean x = (boolean) vh.getAcquire(recv);
 348             assertEquals(x, true, "setRelease boolean value");
 349         }
 350 
 351         // Opaque
 352         {
 353             vh.setOpaque(recv, false);
 354             boolean x = (boolean) vh.getOpaque(recv);
 355             assertEquals(x, false, "setOpaque boolean value");
 356         }
 357 
 358         vh.set(recv, true);
 359 
 360         // Compare
 361         {
 362             boolean r = vh.compareAndSet(recv, true, false);
 363             assertEquals(r, true, "success compareAndSet boolean");
 364             boolean x = (boolean) vh.get(recv);
 365             assertEquals(x, false, "success compareAndSet boolean value");
 366         }
 367 
 368         {

 369             boolean r = vh.compareAndSet(recv, true, false);
 370             assertEquals(r, false, "failing compareAndSet boolean");
 371             boolean x = (boolean) vh.get(recv);
 372             assertEquals(x, false, "failing compareAndSet boolean value");
 373         }
 374 
 375         {
 376             boolean r = (boolean) vh.compareAndExchangeVolatile(recv, false, true);
 377             assertEquals(r, false, "success compareAndExchangeVolatile boolean");
 378             boolean x = (boolean) vh.get(recv);
 379             assertEquals(x, true, "success compareAndExchangeVolatile boolean value");
 380         }
 381 
 382         {
 383             boolean r = (boolean) vh.compareAndExchangeVolatile(recv, false, false);
 384             assertEquals(r, true, "failing compareAndExchangeVolatile boolean");
 385             boolean x = (boolean) vh.get(recv);
 386             assertEquals(x, true, "failing compareAndExchangeVolatile boolean value");
 387         }
 388 
 389         {
 390             boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false);
 391             assertEquals(r, true, "success compareAndExchangeAcquire boolean");
 392             boolean x = (boolean) vh.get(recv);
 393             assertEquals(x, false, "success compareAndExchangeAcquire boolean value");
 394         }
 395 
 396         {
 397             boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false);
 398             assertEquals(r, false, "failing compareAndExchangeAcquire boolean");
 399             boolean x = (boolean) vh.get(recv);
 400             assertEquals(x, false, "failing compareAndExchangeAcquire boolean value");
 401         }
 402 
 403         {
 404             boolean r = (boolean) vh.compareAndExchangeRelease(recv, false, true);
 405             assertEquals(r, false, "success compareAndExchangeRelease boolean");
 406             boolean x = (boolean) vh.get(recv);
 407             assertEquals(x, true, "success compareAndExchangeRelease boolean value");
 408         }
 409 
 410         {
 411             boolean r = (boolean) vh.compareAndExchangeRelease(recv, false, false);
 412             assertEquals(r, true, "failing compareAndExchangeRelease boolean");
 413             boolean x = (boolean) vh.get(recv);
 414             assertEquals(x, true, "failing compareAndExchangeRelease boolean value");
 415         }
 416 
 417         {
 418             boolean success = false;
 419             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 420                 success = vh.weakCompareAndSet(recv, true, false);
 421             }
 422             assertEquals(success, true, "weakCompareAndSet boolean");
 423             boolean x = (boolean) vh.get(recv);
 424             assertEquals(x, false, "weakCompareAndSet boolean value");
 425         }
 426 
 427         {
 428             boolean success = false;
 429             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 430                 success = vh.weakCompareAndSetAcquire(recv, false, true);
 431             }
 432             assertEquals(success, true, "weakCompareAndSetAcquire boolean");
 433             boolean x = (boolean) vh.get(recv);
 434             assertEquals(x, true, "weakCompareAndSetAcquire boolean");
 435         }
 436 
 437         {
 438             boolean success = false;
 439             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 440                 success = vh.weakCompareAndSetRelease(recv, true, false);
 441             }
 442             assertEquals(success, true, "weakCompareAndSetRelease boolean");
 443             boolean x = (boolean) vh.get(recv);
 444             assertEquals(x, false, "weakCompareAndSetRelease boolean");
 445         }
 446 
 447         // Compare set and get
 448         {
 449             boolean o = (boolean) vh.getAndSet(recv, true);
 450             assertEquals(o, false, "getAndSet boolean");
 451             boolean x = (boolean) vh.get(recv);
 452             assertEquals(x, true, "getAndSet boolean value");
 453         }
 454 
 455     }
 456 
 457     static void testInstanceFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) {
 458 
 459         checkUOE(() -> {
 460             boolean o = (boolean) vh.getAndAdd(recv, true);
 461         });
 462 
 463         checkUOE(() -> {
 464             boolean o = (boolean) vh.addAndGet(recv, true);
 465         });
 466     }
 467 
 468 
 469     static void testStaticField(VarHandle vh) {
 470         // Plain
 471         {
 472             vh.set(true);
 473             boolean x = (boolean) vh.get();
 474             assertEquals(x, true, "set boolean value");
 475         }
 476 
 477 
 478         // Volatile
 479         {
 480             vh.setVolatile(false);
 481             boolean x = (boolean) vh.getVolatile();
 482             assertEquals(x, false, "setVolatile boolean value");
 483         }
 484 
 485         // Lazy
 486         {
 487             vh.setRelease(true);
 488             boolean x = (boolean) vh.getAcquire();
 489             assertEquals(x, true, "setRelease boolean value");
 490         }
 491 
 492         // Opaque
 493         {
 494             vh.setOpaque(false);
 495             boolean x = (boolean) vh.getOpaque();
 496             assertEquals(x, false, "setOpaque boolean value");
 497         }
 498 
 499         vh.set(true);
 500 
 501         // Compare
 502         {
 503             boolean r = vh.compareAndSet(true, false);
 504             assertEquals(r, true, "success compareAndSet boolean");
 505             boolean x = (boolean) vh.get();
 506             assertEquals(x, false, "success compareAndSet boolean value");
 507         }
 508 
 509         {

 510             boolean r = vh.compareAndSet(true, false);
 511             assertEquals(r, false, "failing compareAndSet boolean");
 512             boolean x = (boolean) vh.get();
 513             assertEquals(x, false, "failing compareAndSet boolean value");
 514         }
 515 
 516         {
 517             boolean r = (boolean) vh.compareAndExchangeVolatile(false, true);
 518             assertEquals(r, false, "success compareAndExchangeVolatile boolean");
 519             boolean x = (boolean) vh.get();
 520             assertEquals(x, true, "success compareAndExchangeVolatile boolean value");
 521         }
 522 
 523         {
 524             boolean r = (boolean) vh.compareAndExchangeVolatile(false, false);
 525             assertEquals(r, true, "failing compareAndExchangeVolatile boolean");
 526             boolean x = (boolean) vh.get();
 527             assertEquals(x, true, "failing compareAndExchangeVolatile boolean value");
 528         }
 529 
 530         {
 531             boolean r = (boolean) vh.compareAndExchangeAcquire(true, false);
 532             assertEquals(r, true, "success compareAndExchangeAcquire boolean");
 533             boolean x = (boolean) vh.get();
 534             assertEquals(x, false, "success compareAndExchangeAcquire boolean value");
 535         }
 536 
 537         {
 538             boolean r = (boolean) vh.compareAndExchangeAcquire(true, false);
 539             assertEquals(r, false, "failing compareAndExchangeAcquire boolean");
 540             boolean x = (boolean) vh.get();
 541             assertEquals(x, false, "failing compareAndExchangeAcquire boolean value");
 542         }
 543 
 544         {
 545             boolean r = (boolean) vh.compareAndExchangeRelease(false, true);
 546             assertEquals(r, false, "success compareAndExchangeRelease boolean");
 547             boolean x = (boolean) vh.get();
 548             assertEquals(x, true, "success compareAndExchangeRelease boolean value");
 549         }
 550 
 551         {
 552             boolean r = (boolean) vh.compareAndExchangeRelease(false, false);
 553             assertEquals(r, true, "failing compareAndExchangeRelease boolean");
 554             boolean x = (boolean) vh.get();
 555             assertEquals(x, true, "failing compareAndExchangeRelease boolean value");
 556         }
 557 
 558         {
 559             boolean success = false;
 560             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 561                 success = vh.weakCompareAndSet(true, false);
 562             }
 563             assertEquals(success, true, "weakCompareAndSet boolean");
 564             boolean x = (boolean) vh.get();
 565             assertEquals(x, false, "weakCompareAndSet boolean value");
 566         }
 567 
 568         {
 569             boolean success = false;
 570             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 571                 success = vh.weakCompareAndSetAcquire(false, true);
 572             }
 573             assertEquals(success, true, "weakCompareAndSetAcquire boolean");
 574             boolean x = (boolean) vh.get();
 575             assertEquals(x, true, "weakCompareAndSetAcquire boolean");
 576         }
 577 
 578         {
 579             boolean success = false;
 580             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 581                 success = vh.weakCompareAndSetRelease(true, false);
 582             }
 583             assertEquals(success, true, "weakCompareAndSetRelease boolean");
 584             boolean x = (boolean) vh.get();
 585             assertEquals(x, false, "weakCompareAndSetRelease boolean");
 586         }
 587 
 588         // Compare set and get
 589         {
 590             boolean o = (boolean) vh.getAndSet( true);
 591             assertEquals(o, false, "getAndSet boolean");
 592             boolean x = (boolean) vh.get();
 593             assertEquals(x, true, "getAndSet boolean value");
 594         }
 595 
 596     }
 597 
 598     static void testStaticFieldUnsupported(VarHandle vh) {
 599 
 600         checkUOE(() -> {
 601             boolean o = (boolean) vh.getAndAdd(true);
 602         });
 603 
 604         checkUOE(() -> {
 605             boolean o = (boolean) vh.addAndGet(true);
 606         });
 607     }
 608 
 609 
 610     static void testArray(VarHandle vh) {
 611         boolean[] array = new boolean[10];
 612 
 613         for (int i = 0; i < array.length; i++) {
 614             // Plain
 615             {
 616                 vh.set(array, i, true);
 617                 boolean x = (boolean) vh.get(array, i);
 618                 assertEquals(x, true, "get boolean value");
 619             }
 620 
 621 
 622             // Volatile
 623             {
 624                 vh.setVolatile(array, i, false);
 625                 boolean x = (boolean) vh.getVolatile(array, i);
 626                 assertEquals(x, false, "setVolatile boolean value");
 627             }
 628 
 629             // Lazy
 630             {
 631                 vh.setRelease(array, i, true);
 632                 boolean x = (boolean) vh.getAcquire(array, i);
 633                 assertEquals(x, true, "setRelease boolean value");
 634             }
 635 
 636             // Opaque
 637             {
 638                 vh.setOpaque(array, i, false);
 639                 boolean x = (boolean) vh.getOpaque(array, i);
 640                 assertEquals(x, false, "setOpaque boolean value");
 641             }
 642 
 643             vh.set(array, i, true);
 644 
 645             // Compare
 646             {
 647                 boolean r = vh.compareAndSet(array, i, true, false);
 648                 assertEquals(r, true, "success compareAndSet boolean");
 649                 boolean x = (boolean) vh.get(array, i);
 650                 assertEquals(x, false, "success compareAndSet boolean value");
 651             }
 652 
 653             {
 654                 boolean r = vh.compareAndSet(array, i, true, false);
 655                 assertEquals(r, false, "failing compareAndSet boolean");
 656                 boolean x = (boolean) vh.get(array, i);
 657                 assertEquals(x, false, "failing compareAndSet boolean value");
 658             }
 659 
 660             {
 661                 boolean r = (boolean) vh.compareAndExchangeVolatile(array, i, false, true);
 662                 assertEquals(r, false, "success compareAndExchangeVolatile boolean");
 663                 boolean x = (boolean) vh.get(array, i);
 664                 assertEquals(x, true, "success compareAndExchangeVolatile boolean value");
 665             }
 666 
 667             {
 668                 boolean r = (boolean) vh.compareAndExchangeVolatile(array, i, false, false);
 669                 assertEquals(r, true, "failing compareAndExchangeVolatile boolean");
 670                 boolean x = (boolean) vh.get(array, i);
 671                 assertEquals(x, true, "failing compareAndExchangeVolatile boolean value");
 672             }
 673 
 674             {
 675                 boolean r = (boolean) vh.compareAndExchangeAcquire(array, i, true, false);
 676                 assertEquals(r, true, "success compareAndExchangeAcquire boolean");
 677                 boolean x = (boolean) vh.get(array, i);
 678                 assertEquals(x, false, "success compareAndExchangeAcquire boolean value");
 679             }
 680 
 681             {
 682                 boolean r = (boolean) vh.compareAndExchangeAcquire(array, i, true, false);
 683                 assertEquals(r, false, "failing compareAndExchangeAcquire boolean");
 684                 boolean x = (boolean) vh.get(array, i);
 685                 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value");
 686             }
 687 
 688             {
 689                 boolean r = (boolean) vh.compareAndExchangeRelease(array, i, false, true);
 690                 assertEquals(r, false, "success compareAndExchangeRelease boolean");
 691                 boolean x = (boolean) vh.get(array, i);
 692                 assertEquals(x, true, "success compareAndExchangeRelease boolean value");
 693             }
 694 
 695             {
 696                 boolean r = (boolean) vh.compareAndExchangeRelease(array, i, false, false);
 697                 assertEquals(r, true, "failing compareAndExchangeRelease boolean");
 698                 boolean x = (boolean) vh.get(array, i);
 699                 assertEquals(x, true, "failing compareAndExchangeRelease boolean value");
 700             }
 701 
 702             {
 703                 boolean success = false;
 704                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 705                     success = vh.weakCompareAndSet(array, i, true, false);
 706                 }
 707                 assertEquals(success, true, "weakCompareAndSet boolean");
 708                 boolean x = (boolean) vh.get(array, i);
 709                 assertEquals(x, false, "weakCompareAndSet boolean value");
 710             }
 711 
 712             {
 713                 boolean success = false;
 714                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 715                     success = vh.weakCompareAndSetAcquire(array, i, false, true);
 716                 }
 717                 assertEquals(success, true, "weakCompareAndSetAcquire boolean");
 718                 boolean x = (boolean) vh.get(array, i);
 719                 assertEquals(x, true, "weakCompareAndSetAcquire boolean");
 720             }
 721 
 722             {
 723                 boolean success = false;
 724                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 725                     success = vh.weakCompareAndSetRelease(array, i, true, false);
 726                 }
 727                 assertEquals(success, true, "weakCompareAndSetRelease boolean");
 728                 boolean x = (boolean) vh.get(array, i);
 729                 assertEquals(x, false, "weakCompareAndSetRelease boolean");
 730             }
 731 
 732             // Compare set and get
 733             {
 734                 boolean o = (boolean) vh.getAndSet(array, i, true);
 735                 assertEquals(o, false, "getAndSet boolean");
 736                 boolean x = (boolean) vh.get(array, i);
 737                 assertEquals(x, true, "getAndSet boolean value");
 738             }
 739 
 740         }
 741     }
 742 
 743     static void testArrayUnsupported(VarHandle vh) {
 744         boolean[] array = new boolean[10];
 745 
 746         int i = 0;
 747 
 748         checkUOE(() -> {
 749             boolean o = (boolean) vh.getAndAdd(array, i, true);
 750         });
 751 
 752         checkUOE(() -> {
 753             boolean o = (boolean) vh.addAndGet(array, i, true);
 754         });
 755     }
 756 
 757     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 758         boolean[] array = new boolean[10];
 759 
 760         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 761             final int ci = i;
 762 
 763             checkIOOBE(() -> {
 764                 boolean x = (boolean) vh.get(array, ci);
 765             });
 766 
 767             checkIOOBE(() -> {
 768                 vh.set(array, ci, true);
 769             });
 770 
 771             checkIOOBE(() -> {
 772                 boolean x = (boolean) vh.getVolatile(array, ci);
 773             });
 774 
 775             checkIOOBE(() -> {
 776                 vh.setVolatile(array, ci, true);
 777             });
 778 
 779             checkIOOBE(() -> {
 780                 boolean x = (boolean) vh.getAcquire(array, ci);
 781             });
 782 
 783             checkIOOBE(() -> {
 784                 vh.setRelease(array, ci, true);
 785             });
 786 
 787             checkIOOBE(() -> {
 788                 boolean x = (boolean) vh.getOpaque(array, ci);
 789             });
 790 
 791             checkIOOBE(() -> {
 792                 vh.setOpaque(array, ci, true);
 793             });
 794 
 795             checkIOOBE(() -> {
 796                 boolean r = vh.compareAndSet(array, ci, true, false);
 797             });
 798 
 799             checkIOOBE(() -> {
 800                 boolean r = (boolean) vh.compareAndExchangeVolatile(array, ci, false, true);
 801             });
 802 
 803             checkIOOBE(() -> {
 804                 boolean r = (boolean) vh.compareAndExchangeAcquire(array, ci, false, true);
 805             });
 806 
 807             checkIOOBE(() -> {
 808                 boolean r = (boolean) vh.compareAndExchangeRelease(array, ci, false, true);
 809             });
 810 
 811             checkIOOBE(() -> {
 812                 boolean r = vh.weakCompareAndSet(array, ci, true, false);
 813             });
 814 
 815             checkIOOBE(() -> {
 816                 boolean r = vh.weakCompareAndSetAcquire(array, ci, true, false);
 817             });
 818 
 819             checkIOOBE(() -> {
 820                 boolean r = vh.weakCompareAndSetRelease(array, ci, true, false);
 821             });
 822 
 823             checkIOOBE(() -> {
 824                 boolean o = (boolean) vh.getAndSet(array, ci, true);
 825             });
 826 
 827         }
 828     }
 829 }
 830 
--- EOF ---