< prev index next >

test/java/lang/invoke/VarHandles/VarHandleTestAccessBoolean.java

Print this page




  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         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 103         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
 104         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 105         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 106         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 107         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE));
 108         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 109         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 110         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 111 
 112         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 113         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
 114     }
 115 
 116 
 117     @DataProvider
 118     public Object[][] typesProvider() throws Exception {
 119         List<Object[]> types = new ArrayList<>();
 120         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessBoolean.class)});
 121         types.add(new Object[] {vhStaticField, Arrays.asList()});
 122         types.add(new Object[] {vhArray, Arrays.asList(boolean[].class, int.class)});
 123 
 124         return types.stream().toArray(Object[][]::new);
 125     }
 126 
 127     @Test(dataProvider = "typesProvider")
 128     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 129         assertEquals(vh.varType(), boolean.class);
 130 


 243         }
 244     }
 245 
 246     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) {
 247         checkUOE(() -> {
 248             vh.set(recv, false);
 249         });
 250 
 251         checkUOE(() -> {
 252             vh.setVolatile(recv, false);
 253         });
 254 
 255         checkUOE(() -> {
 256             vh.setRelease(recv, false);
 257         });
 258 
 259         checkUOE(() -> {
 260             vh.setOpaque(recv, false);
 261         });
 262 
 263         checkUOE(() -> {
 264             boolean r = vh.compareAndSet(recv, true, false);
 265         });
 266 
 267         checkUOE(() -> {
 268             boolean r = (boolean) vh.compareAndExchangeVolatile(recv, true, false);
 269         });
 270 
 271         checkUOE(() -> {
 272             boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false);
 273         });
 274 
 275         checkUOE(() -> {
 276             boolean r = (boolean) vh.compareAndExchangeRelease(recv, true, false);
 277         });
 278 
 279         checkUOE(() -> {
 280             boolean r = vh.weakCompareAndSet(recv, true, false);
 281         });
 282 
 283         checkUOE(() -> {
 284             boolean r = vh.weakCompareAndSetVolatile(recv, true, false);
 285         });
 286 
 287         checkUOE(() -> {
 288             boolean r = vh.weakCompareAndSetAcquire(recv, true, false);
 289         });
 290 
 291         checkUOE(() -> {
 292             boolean r = vh.weakCompareAndSetRelease(recv, true, false);
 293         });
 294 
 295         checkUOE(() -> {
 296             boolean o = (boolean) vh.getAndAdd(recv, true);
 297         });
 298 
 299         checkUOE(() -> {
 300             boolean o = (boolean) vh.addAndGet(recv, true);
 301         });
 302     }
 303 
 304 
 305     static void testStaticFinalField(VarHandle vh) {
 306         // Plain
 307         {
 308             boolean x = (boolean) vh.get();
 309             assertEquals(x, true, "get boolean value");
 310         }
 311 
 312 
 313         // Volatile


 329         }
 330     }
 331 
 332     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 333         checkUOE(() -> {
 334             vh.set(false);
 335         });
 336 
 337         checkUOE(() -> {
 338             vh.setVolatile(false);
 339         });
 340 
 341         checkUOE(() -> {
 342             vh.setRelease(false);
 343         });
 344 
 345         checkUOE(() -> {
 346             vh.setOpaque(false);
 347         });
 348 
 349         checkUOE(() -> {
 350             boolean r = vh.compareAndSet(true, false);
 351         });
 352 
 353         checkUOE(() -> {
 354             boolean r = (boolean) vh.compareAndExchangeVolatile(true, false);
 355         });
 356 
 357         checkUOE(() -> {
 358             boolean r = (boolean) vh.compareAndExchangeAcquire(true, false);
 359         });
 360 
 361         checkUOE(() -> {
 362             boolean r = (boolean) vh.compareAndExchangeRelease(true, false);
 363         });
 364 
 365         checkUOE(() -> {
 366             boolean r = vh.weakCompareAndSet(true, false);
 367         });
 368 
 369         checkUOE(() -> {
 370             boolean r = vh.weakCompareAndSetVolatile(true, false);
 371         });
 372 
 373         checkUOE(() -> {
 374             boolean r = vh.weakCompareAndSetAcquire(true, false);
 375         });
 376 
 377         checkUOE(() -> {
 378             boolean r = vh.weakCompareAndSetRelease(true, false);
 379         });
 380 
 381         checkUOE(() -> {
 382             boolean o = (boolean) vh.getAndAdd(true);
 383         });
 384 
 385         checkUOE(() -> {
 386             boolean o = (boolean) vh.addAndGet(true);
 387         });
 388     }
 389 
 390 
 391     static void testInstanceField(VarHandleTestAccessBoolean recv, VarHandle vh) {
 392         // Plain
 393         {
 394             vh.set(recv, true);
 395             boolean x = (boolean) vh.get(recv);
 396             assertEquals(x, true, "set boolean value");
 397         }
 398 
 399 


 401         {
 402             vh.setVolatile(recv, false);
 403             boolean x = (boolean) vh.getVolatile(recv);
 404             assertEquals(x, false, "setVolatile boolean value");
 405         }
 406 
 407         // Lazy
 408         {
 409             vh.setRelease(recv, true);
 410             boolean x = (boolean) vh.getAcquire(recv);
 411             assertEquals(x, true, "setRelease boolean value");
 412         }
 413 
 414         // Opaque
 415         {
 416             vh.setOpaque(recv, false);
 417             boolean x = (boolean) vh.getOpaque(recv);
 418             assertEquals(x, false, "setOpaque boolean value");
 419         }
 420 

 421 






 422     }
 423 
 424     static void testInstanceFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) {
 425         checkUOE(() -> {
 426             boolean r = vh.compareAndSet(recv, true, false);
 427         });



 428 
 429         checkUOE(() -> {
 430             boolean r = (boolean) vh.compareAndExchangeVolatile(recv, true, false);
 431         });



 432 
 433         checkUOE(() -> {







 434             boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false);
 435         });



 436 
 437         checkUOE(() -> {
 438             boolean r = (boolean) vh.compareAndExchangeRelease(recv, true, false);
 439         });



 440 
 441         checkUOE(() -> {
 442             boolean r = vh.weakCompareAndSet(recv, true, false);
 443         });



 444 
 445         checkUOE(() -> {
 446             boolean r = vh.weakCompareAndSetVolatile(recv, true, false);
 447         });



 448 
 449         checkUOE(() -> {
 450             boolean r = vh.weakCompareAndSetAcquire(recv, true, false);
 451         });






 452 
 453         checkUOE(() -> {
 454             boolean r = vh.weakCompareAndSetRelease(recv, true, false);
 455         });




























 456 
 457         checkUOE(() -> {
 458             boolean o = (boolean) vh.getAndAdd(recv, true);
 459         });
 460 
 461         checkUOE(() -> {
 462             boolean o = (boolean) vh.addAndGet(recv, true);
 463         });
 464     }
 465 
 466 
 467     static void testStaticField(VarHandle vh) {
 468         // Plain
 469         {
 470             vh.set(true);
 471             boolean x = (boolean) vh.get();
 472             assertEquals(x, true, "set boolean value");
 473         }
 474 
 475 


 477         {
 478             vh.setVolatile(false);
 479             boolean x = (boolean) vh.getVolatile();
 480             assertEquals(x, false, "setVolatile boolean value");
 481         }
 482 
 483         // Lazy
 484         {
 485             vh.setRelease(true);
 486             boolean x = (boolean) vh.getAcquire();
 487             assertEquals(x, true, "setRelease boolean value");
 488         }
 489 
 490         // Opaque
 491         {
 492             vh.setOpaque(false);
 493             boolean x = (boolean) vh.getOpaque();
 494             assertEquals(x, false, "setOpaque boolean value");
 495         }
 496 

 497 






 498     }
 499 
 500     static void testStaticFieldUnsupported(VarHandle vh) {
 501         checkUOE(() -> {
 502             boolean r = vh.compareAndSet(true, false);
 503         });



 504 
 505         checkUOE(() -> {
 506             boolean r = (boolean) vh.compareAndExchangeVolatile(true, false);
 507         });



 508 
 509         checkUOE(() -> {







 510             boolean r = (boolean) vh.compareAndExchangeAcquire(true, false);
 511         });



 512 
 513         checkUOE(() -> {
 514             boolean r = (boolean) vh.compareAndExchangeRelease(true, false);
 515         });



 516 
 517         checkUOE(() -> {
 518             boolean r = vh.weakCompareAndSet(true, false);
 519         });



 520 
 521         checkUOE(() -> {
 522             boolean r = vh.weakCompareAndSetVolatile(true, false);
 523         });



 524 
 525         checkUOE(() -> {
 526             boolean r = vh.weakCompareAndSetAcquire(true, false);
 527         });






 528 
 529         checkUOE(() -> {
 530             boolean r = vh.weakCompareAndSetRelease(true, false);
 531         });




























 532 
 533         checkUOE(() -> {
 534             boolean o = (boolean) vh.getAndAdd(true);
 535         });
 536 
 537         checkUOE(() -> {
 538             boolean o = (boolean) vh.addAndGet(true);
 539         });
 540     }
 541 
 542 
 543     static void testArray(VarHandle vh) {
 544         boolean[] array = new boolean[10];
 545 
 546         for (int i = 0; i < array.length; i++) {
 547             // Plain
 548             {
 549                 vh.set(array, i, true);
 550                 boolean x = (boolean) vh.get(array, i);
 551                 assertEquals(x, true, "get boolean value");


 556             {
 557                 vh.setVolatile(array, i, false);
 558                 boolean x = (boolean) vh.getVolatile(array, i);
 559                 assertEquals(x, false, "setVolatile boolean value");
 560             }
 561 
 562             // Lazy
 563             {
 564                 vh.setRelease(array, i, true);
 565                 boolean x = (boolean) vh.getAcquire(array, i);
 566                 assertEquals(x, true, "setRelease boolean value");
 567             }
 568 
 569             // Opaque
 570             {
 571                 vh.setOpaque(array, i, false);
 572                 boolean x = (boolean) vh.getOpaque(array, i);
 573                 assertEquals(x, false, "setOpaque boolean value");
 574             }
 575 

 576 






 577         }






 578     }
 579 
 580     static void testArrayUnsupported(VarHandle vh) {
 581         boolean[] array = new boolean[10];




 582 
 583         int i = 0;
 584         checkUOE(() -> {
 585             boolean r = vh.compareAndSet(array, i, true, false);
 586         });


 587 
 588         checkUOE(() -> {
 589             boolean r = (boolean) vh.compareAndExchangeVolatile(array, i, true, false);
 590         });



 591 
 592         checkUOE(() -> {
 593             boolean r = (boolean) vh.compareAndExchangeAcquire(array, i, true, false);
 594         });



 595 
 596         checkUOE(() -> {
 597             boolean r = (boolean) vh.compareAndExchangeRelease(array, i, true, false);
 598         });



 599 
 600         checkUOE(() -> {
 601             boolean r = vh.weakCompareAndSet(array, i, true, false);
 602         });



 603 
 604         checkUOE(() -> {
 605             boolean r = vh.weakCompareAndSetVolatile(array, i, true, false);
 606         });






 607 
 608         checkUOE(() -> {
 609             boolean r = vh.weakCompareAndSetAcquire(array, i, true, false);
 610         });






 611 
 612         checkUOE(() -> {
 613             boolean r = vh.weakCompareAndSetRelease(array, i, true, false);
 614         });






















 615 
 616         checkUOE(() -> {
 617             boolean o = (boolean) vh.getAndAdd(array, i, true);
 618         });
 619 
 620         checkUOE(() -> {
 621             boolean o = (boolean) vh.addAndGet(array, i, true);
 622         });
 623     }
 624 
 625     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 626         boolean[] array = new boolean[10];
 627 
 628         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 629             final int ci = i;
 630 
 631             checkIOOBE(() -> {
 632                 boolean x = (boolean) vh.get(array, ci);
 633             });
 634 


 643             checkIOOBE(() -> {
 644                 vh.setVolatile(array, ci, true);
 645             });
 646 
 647             checkIOOBE(() -> {
 648                 boolean x = (boolean) vh.getAcquire(array, ci);
 649             });
 650 
 651             checkIOOBE(() -> {
 652                 vh.setRelease(array, ci, true);
 653             });
 654 
 655             checkIOOBE(() -> {
 656                 boolean x = (boolean) vh.getOpaque(array, ci);
 657             });
 658 
 659             checkIOOBE(() -> {
 660                 vh.setOpaque(array, ci, true);
 661             });
 662 































 663 
 664         }
 665     }
 666 }
 667 


  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 


 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


 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 


 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 


 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");


 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 


 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 
< prev index next >