< prev index next >

test/java/lang/invoke/VarHandles/VarHandleTestAccessChar.java

Print this page




  25  * @test
  26  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccessChar
  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessChar
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccessChar
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessChar
  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 VarHandleTestAccessChar extends VarHandleBaseTest {
  45     static final char static_final_v = 'a';
  46 
  47     static char static_v;
  48 
  49     final char final_v = 'a';
  50 
  51     char 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                 VarHandleTestAccessChar.class, "final_v", char.class);
  67 
  68         vhField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestAccessChar.class, "v", char.class);


  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(VarHandleTestAccessChar.class)});
 121         types.add(new Object[] {vhStaticField, Arrays.asList()});
 122         types.add(new Object[] {vhArray, Arrays.asList(char[].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(), char.class);
 130 
 131         assertEquals(vh.coordinateTypes(), pts);
 132 
 133         testTypes(vh);


 203         // the first dummy argument
 204         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 205     }
 206 
 207     @Test(dataProvider = "accessTestCaseProvider")
 208     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 209         T t = atc.get();
 210         int iters = atc.requiresLoop() ? ITERS : 1;
 211         for (int c = 0; c < iters; c++) {
 212             atc.testAccess(t);
 213         }
 214     }
 215 
 216 
 217 
 218 
 219     static void testInstanceFinalField(VarHandleTestAccessChar recv, VarHandle vh) {
 220         // Plain
 221         {
 222             char x = (char) vh.get(recv);
 223             assertEquals(x, 'a', "get char value");
 224         }
 225 
 226 
 227         // Volatile
 228         {
 229             char x = (char) vh.getVolatile(recv);
 230             assertEquals(x, 'a', "getVolatile char value");
 231         }
 232 
 233         // Lazy
 234         {
 235             char x = (char) vh.getAcquire(recv);
 236             assertEquals(x, 'a', "getRelease char value");
 237         }
 238 
 239         // Opaque
 240         {
 241             char x = (char) vh.getOpaque(recv);
 242             assertEquals(x, 'a', "getOpaque char value");
 243         }
 244     }
 245 
 246     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessChar recv, VarHandle vh) {
 247         checkUOE(() -> {
 248             vh.set(recv, 'b');
 249         });
 250 
 251         checkUOE(() -> {
 252             vh.setVolatile(recv, 'b');
 253         });
 254 
 255         checkUOE(() -> {
 256             vh.setRelease(recv, 'b');
 257         });
 258 
 259         checkUOE(() -> {
 260             vh.setOpaque(recv, 'b');
 261         });
 262 
 263         checkUOE(() -> {
 264             boolean r = vh.compareAndSet(recv, 'a', 'b');
 265         });
 266 
 267         checkUOE(() -> {
 268             char r = (char) vh.compareAndExchangeVolatile(recv, 'a', 'b');
 269         });
 270 
 271         checkUOE(() -> {
 272             char r = (char) vh.compareAndExchangeAcquire(recv, 'a', 'b');
 273         });
 274 
 275         checkUOE(() -> {
 276             char r = (char) vh.compareAndExchangeRelease(recv, 'a', 'b');
 277         });
 278 
 279         checkUOE(() -> {
 280             boolean r = vh.weakCompareAndSet(recv, 'a', 'b');
 281         });
 282 
 283         checkUOE(() -> {
 284             boolean r = vh.weakCompareAndSetVolatile(recv, 'a', 'b');
 285         });
 286 
 287         checkUOE(() -> {
 288             boolean r = vh.weakCompareAndSetAcquire(recv, 'a', 'b');
 289         });
 290 
 291         checkUOE(() -> {
 292             boolean r = vh.weakCompareAndSetRelease(recv, 'a', 'b');
 293         });
 294 
 295         checkUOE(() -> {
 296             char o = (char) vh.getAndAdd(recv, 'a');
 297         });
 298 
 299         checkUOE(() -> {
 300             char o = (char) vh.addAndGet(recv, 'a');
 301         });
 302     }
 303 
 304 
 305     static void testStaticFinalField(VarHandle vh) {
 306         // Plain
 307         {
 308             char x = (char) vh.get();
 309             assertEquals(x, 'a', "get char value");
 310         }
 311 
 312 
 313         // Volatile
 314         {
 315             char x = (char) vh.getVolatile();
 316             assertEquals(x, 'a', "getVolatile char value");
 317         }
 318 
 319         // Lazy
 320         {
 321             char x = (char) vh.getAcquire();
 322             assertEquals(x, 'a', "getRelease char value");
 323         }
 324 
 325         // Opaque
 326         {
 327             char x = (char) vh.getOpaque();
 328             assertEquals(x, 'a', "getOpaque char value");
 329         }
 330     }
 331 
 332     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 333         checkUOE(() -> {
 334             vh.set('b');
 335         });
 336 
 337         checkUOE(() -> {
 338             vh.setVolatile('b');
 339         });
 340 
 341         checkUOE(() -> {
 342             vh.setRelease('b');
 343         });
 344 
 345         checkUOE(() -> {
 346             vh.setOpaque('b');
 347         });
 348 
 349         checkUOE(() -> {
 350             boolean r = vh.compareAndSet('a', 'b');
 351         });
 352 
 353         checkUOE(() -> {
 354             char r = (char) vh.compareAndExchangeVolatile('a', 'b');
 355         });
 356 
 357         checkUOE(() -> {
 358             char r = (char) vh.compareAndExchangeAcquire('a', 'b');
 359         });
 360 
 361         checkUOE(() -> {
 362             char r = (char) vh.compareAndExchangeRelease('a', 'b');
 363         });
 364 
 365         checkUOE(() -> {
 366             boolean r = vh.weakCompareAndSet('a', 'b');
 367         });
 368 
 369         checkUOE(() -> {
 370             boolean r = vh.weakCompareAndSetVolatile('a', 'b');
 371         });
 372 
 373         checkUOE(() -> {
 374             boolean r = vh.weakCompareAndSetAcquire('a', 'b');
 375         });
 376 
 377         checkUOE(() -> {
 378             boolean r = vh.weakCompareAndSetRelease('a', 'b');
 379         });
 380 
 381         checkUOE(() -> {
 382             char o = (char) vh.getAndAdd('a');
 383         });
 384 
 385         checkUOE(() -> {
 386             char o = (char) vh.addAndGet('a');
 387         });
 388     }
 389 
 390 
 391     static void testInstanceField(VarHandleTestAccessChar recv, VarHandle vh) {
 392         // Plain
 393         {
 394             vh.set(recv, 'a');
 395             char x = (char) vh.get(recv);
 396             assertEquals(x, 'a', "set char value");
 397         }
 398 
 399 
 400         // Volatile
 401         {
 402             vh.setVolatile(recv, 'b');
 403             char x = (char) vh.getVolatile(recv);
 404             assertEquals(x, 'b', "setVolatile char value");
 405         }
 406 
 407         // Lazy
 408         {
 409             vh.setRelease(recv, 'a');
 410             char x = (char) vh.getAcquire(recv);
 411             assertEquals(x, 'a', "setRelease char value");
 412         }
 413 
 414         // Opaque
 415         {
 416             vh.setOpaque(recv, 'b');
 417             char x = (char) vh.getOpaque(recv);
 418             assertEquals(x, 'b', "setOpaque char value");
 419         }
 420 

 421 






 422     }
 423 
 424     static void testInstanceFieldUnsupported(VarHandleTestAccessChar recv, VarHandle vh) {
 425         checkUOE(() -> {
 426             boolean r = vh.compareAndSet(recv, 'a', 'b');
 427         });


 428 
 429         checkUOE(() -> {
 430             char r = (char) vh.compareAndExchangeVolatile(recv, 'a', 'b');
 431         });



 432 
 433         checkUOE(() -> {
 434             char r = (char) vh.compareAndExchangeAcquire(recv, 'a', 'b');
 435         });



 436 
 437         checkUOE(() -> {
 438             char r = (char) vh.compareAndExchangeRelease(recv, 'a', 'b');
 439         });



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



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



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



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






 456 
 457         checkUOE(() -> {
 458             char o = (char) vh.getAndAdd(recv, 'a');
 459         });





































 460 
 461         checkUOE(() -> {
 462             char o = (char) vh.addAndGet(recv, 'a');
 463         });
 464     }
 465 
 466 
 467     static void testStaticField(VarHandle vh) {
 468         // Plain
 469         {
 470             vh.set('a');
 471             char x = (char) vh.get();
 472             assertEquals(x, 'a', "set char value");
 473         }
 474 
 475 
 476         // Volatile
 477         {
 478             vh.setVolatile('b');
 479             char x = (char) vh.getVolatile();
 480             assertEquals(x, 'b', "setVolatile char value");
 481         }
 482 
 483         // Lazy
 484         {
 485             vh.setRelease('a');
 486             char x = (char) vh.getAcquire();
 487             assertEquals(x, 'a', "setRelease char value");
 488         }
 489 
 490         // Opaque
 491         {
 492             vh.setOpaque('b');
 493             char x = (char) vh.getOpaque();
 494             assertEquals(x, 'b', "setOpaque char value");
 495         }
 496 

 497 






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


 504 
 505         checkUOE(() -> {
 506             char r = (char) vh.compareAndExchangeVolatile('a', 'b');
 507         });



 508 
 509         checkUOE(() -> {
 510             char r = (char) vh.compareAndExchangeAcquire('a', 'b');
 511         });



 512 
 513         checkUOE(() -> {
 514             char r = (char) vh.compareAndExchangeRelease('a', 'b');
 515         });



 516 
 517         checkUOE(() -> {
 518             boolean r = vh.weakCompareAndSet('a', 'b');
 519         });



 520 
 521         checkUOE(() -> {
 522             boolean r = vh.weakCompareAndSetVolatile('a', 'b');
 523         });



 524 
 525         checkUOE(() -> {
 526             boolean r = vh.weakCompareAndSetAcquire('a', 'b');
 527         });



 528 
 529         checkUOE(() -> {
 530             boolean r = vh.weakCompareAndSetRelease('a', 'b');
 531         });






 532 
 533         checkUOE(() -> {
 534             char o = (char) vh.getAndAdd('a');
 535         });





































 536 
 537         checkUOE(() -> {
 538             char o = (char) vh.addAndGet('a');
 539         });
 540     }
 541 
 542 
 543     static void testArray(VarHandle vh) {
 544         char[] array = new char[10];
 545 
 546         for (int i = 0; i < array.length; i++) {
 547             // Plain
 548             {
 549                 vh.set(array, i, 'a');
 550                 char x = (char) vh.get(array, i);
 551                 assertEquals(x, 'a', "get char value");
 552             }
 553 
 554 
 555             // Volatile
 556             {
 557                 vh.setVolatile(array, i, 'b');
 558                 char x = (char) vh.getVolatile(array, i);
 559                 assertEquals(x, 'b', "setVolatile char value");
 560             }
 561 
 562             // Lazy
 563             {
 564                 vh.setRelease(array, i, 'a');
 565                 char x = (char) vh.getAcquire(array, i);
 566                 assertEquals(x, 'a', "setRelease char value");
 567             }
 568 
 569             // Opaque
 570             {
 571                 vh.setOpaque(array, i, 'b');
 572                 char x = (char) vh.getOpaque(array, i);
 573                 assertEquals(x, 'b', "setOpaque char value");
 574             }
 575 

 576 






 577         }






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




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


 587 
 588         checkUOE(() -> {
 589             char r = (char) vh.compareAndExchangeVolatile(array, i, 'a', 'b');
 590         });



 591 
 592         checkUOE(() -> {
 593             char r = (char) vh.compareAndExchangeAcquire(array, i, 'a', 'b');
 594         });



 595 
 596         checkUOE(() -> {
 597             char r = (char) vh.compareAndExchangeRelease(array, i, 'a', 'b');
 598         });



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



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






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






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






 615 
 616         checkUOE(() -> {
 617             char o = (char) vh.getAndAdd(array, i, 'a');
 618         });





















 619 
 620         checkUOE(() -> {
 621             char o = (char) vh.addAndGet(array, i, 'a');
 622         });
 623     }
 624 
 625     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 626         char[] array = new char[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                 char x = (char) vh.get(array, ci);
 633             });
 634 
 635             checkIOOBE(() -> {
 636                 vh.set(array, ci, 'a');
 637             });
 638 
 639             checkIOOBE(() -> {
 640                 char x = (char) vh.getVolatile(array, ci);
 641             });
 642 
 643             checkIOOBE(() -> {
 644                 vh.setVolatile(array, ci, 'a');
 645             });
 646 
 647             checkIOOBE(() -> {
 648                 char x = (char) vh.getAcquire(array, ci);
 649             });
 650 
 651             checkIOOBE(() -> {
 652                 vh.setRelease(array, ci, 'a');
 653             });
 654 
 655             checkIOOBE(() -> {
 656                 char x = (char) vh.getOpaque(array, ci);
 657             });
 658 
 659             checkIOOBE(() -> {
 660                 vh.setOpaque(array, ci, 'a');




























 661             });
 662 



 663 







 664         }
 665     }
 666 }
 667 


  25  * @test
  26  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccessChar
  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessChar
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccessChar
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessChar
  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 VarHandleTestAccessChar extends VarHandleBaseTest {
  45     static final char static_final_v = '\u0123';
  46 
  47     static char static_v;
  48 
  49     final char final_v = '\u0123';
  50 
  51     char 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                 VarHandleTestAccessChar.class, "final_v", char.class);
  67 
  68         vhField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestAccessChar.class, "v", char.class);


  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         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 112         assertTrue(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(VarHandleTestAccessChar.class)});
 120         types.add(new Object[] {vhStaticField, Arrays.asList()});
 121         types.add(new Object[] {vhArray, Arrays.asList(char[].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(), char.class);
 129 
 130         assertEquals(vh.coordinateTypes(), pts);
 131 
 132         testTypes(vh);


 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(VarHandleTestAccessChar recv, VarHandle vh) {
 219         // Plain
 220         {
 221             char x = (char) vh.get(recv);
 222             assertEquals(x, '\u0123', "get char value");
 223         }
 224 
 225 
 226         // Volatile
 227         {
 228             char x = (char) vh.getVolatile(recv);
 229             assertEquals(x, '\u0123', "getVolatile char value");
 230         }
 231 
 232         // Lazy
 233         {
 234             char x = (char) vh.getAcquire(recv);
 235             assertEquals(x, '\u0123', "getRelease char value");
 236         }
 237 
 238         // Opaque
 239         {
 240             char x = (char) vh.getOpaque(recv);
 241             assertEquals(x, '\u0123', "getOpaque char value");
 242         }
 243     }
 244 
 245     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessChar recv, VarHandle vh) {
 246         checkUOE(() -> {
 247             vh.set(recv, '\u4567');
 248         });
 249 
 250         checkUOE(() -> {
 251             vh.setVolatile(recv, '\u4567');
 252         });
 253 
 254         checkUOE(() -> {
 255             vh.setRelease(recv, '\u4567');
 256         });
 257 
 258         checkUOE(() -> {
 259             vh.setOpaque(recv, '\u4567');
 260         });
 261 







 262 































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







 310 































 311     }
 312 
 313 
 314     static void testInstanceField(VarHandleTestAccessChar recv, VarHandle vh) {
 315         // Plain
 316         {
 317             vh.set(recv, '\u0123');
 318             char x = (char) vh.get(recv);
 319             assertEquals(x, '\u0123', "set char value");
 320         }
 321 
 322 
 323         // Volatile
 324         {
 325             vh.setVolatile(recv, '\u4567');
 326             char x = (char) vh.getVolatile(recv);
 327             assertEquals(x, '\u4567', "setVolatile char value");
 328         }
 329 
 330         // Lazy
 331         {
 332             vh.setRelease(recv, '\u0123');
 333             char x = (char) vh.getAcquire(recv);
 334             assertEquals(x, '\u0123', "setRelease char value");
 335         }
 336 
 337         // Opaque
 338         {
 339             vh.setOpaque(recv, '\u4567');
 340             char x = (char) vh.getOpaque(recv);
 341             assertEquals(x, '\u4567', "setOpaque char value");
 342         }
 343 
 344         vh.set(recv, '\u0123');
 345 
 346         // Compare
 347         {
 348             boolean r = vh.compareAndSet(recv, '\u0123', '\u4567');
 349             assertEquals(r, true, "success compareAndSet char");
 350             char x = (char) vh.get(recv);
 351             assertEquals(x, '\u4567', "success compareAndSet char value");
 352         }
 353 
 354         {
 355             boolean r = vh.compareAndSet(recv, '\u0123', '\u89AB');
 356             assertEquals(r, false, "failing compareAndSet char");
 357             char x = (char) vh.get(recv);
 358             assertEquals(x, '\u4567', "failing compareAndSet char value");
 359         }
 360 
 361         {
 362             char r = (char) vh.compareAndExchangeVolatile(recv, '\u4567', '\u0123');
 363             assertEquals(r, '\u4567', "success compareAndExchangeVolatile char");
 364             char x = (char) vh.get(recv);
 365             assertEquals(x, '\u0123', "success compareAndExchangeVolatile char value");
 366         }
 367 
 368         {
 369             char r = (char) vh.compareAndExchangeVolatile(recv, '\u4567', '\u89AB');
 370             assertEquals(r, '\u0123', "failing compareAndExchangeVolatile char");
 371             char x = (char) vh.get(recv);
 372             assertEquals(x, '\u0123', "failing compareAndExchangeVolatile char value");
 373         }
 374 
 375         {
 376             char r = (char) vh.compareAndExchangeAcquire(recv, '\u0123', '\u4567');
 377             assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 378             char x = (char) vh.get(recv);
 379             assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 380         }
 381 
 382         {
 383             char r = (char) vh.compareAndExchangeAcquire(recv, '\u0123', '\u89AB');
 384             assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 385             char x = (char) vh.get(recv);
 386             assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 387         }
 388 
 389         {
 390             char r = (char) vh.compareAndExchangeRelease(recv, '\u4567', '\u0123');
 391             assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 392             char x = (char) vh.get(recv);
 393             assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 394         }
 395 
 396         {
 397             char r = (char) vh.compareAndExchangeRelease(recv, '\u4567', '\u89AB');
 398             assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 399             char x = (char) vh.get(recv);
 400             assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 401         }
 402 
 403         {
 404             boolean success = false;
 405             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 406                 success = vh.weakCompareAndSet(recv, '\u0123', '\u4567');
 407             }
 408             assertEquals(success, true, "weakCompareAndSet char");
 409             char x = (char) vh.get(recv);
 410             assertEquals(x, '\u4567', "weakCompareAndSet char value");
 411         }
 412 
 413         {
 414             boolean success = false;
 415             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 416                 success = vh.weakCompareAndSetAcquire(recv, '\u4567', '\u0123');
 417             }
 418             assertEquals(success, true, "weakCompareAndSetAcquire char");
 419             char x = (char) vh.get(recv);
 420             assertEquals(x, '\u0123', "weakCompareAndSetAcquire char");
 421         }
 422 
 423         {
 424             boolean success = false;
 425             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 426                 success = vh.weakCompareAndSetRelease(recv, '\u0123', '\u4567');
 427             }
 428             assertEquals(success, true, "weakCompareAndSetRelease char");
 429             char x = (char) vh.get(recv);
 430             assertEquals(x, '\u4567', "weakCompareAndSetRelease char");
 431         }
 432 
 433         // Compare set and get
 434         {
 435             char o = (char) vh.getAndSet(recv, '\u0123');
 436             assertEquals(o, '\u4567', "getAndSet char");
 437             char x = (char) vh.get(recv);
 438             assertEquals(x, '\u0123', "getAndSet char value");
 439         }
 440 
 441         vh.set(recv, '\u0123');
 442 
 443         // get and add, add and get
 444         {
 445             char o = (char) vh.getAndAdd(recv, '\u89AB');
 446             assertEquals(o, '\u0123', "getAndAdd char");
 447             char c = (char) vh.addAndGet(recv, '\u89AB');
 448             assertEquals(c, (char)('\u0123' + '\u89AB' + '\u89AB'), "getAndAdd char value");
 449         }
 450     }
 451 
 452     static void testInstanceFieldUnsupported(VarHandleTestAccessChar recv, VarHandle vh) {
 453 



 454     }
 455 
 456 
 457     static void testStaticField(VarHandle vh) {
 458         // Plain
 459         {
 460             vh.set('\u0123');
 461             char x = (char) vh.get();
 462             assertEquals(x, '\u0123', "set char value");
 463         }
 464 
 465 
 466         // Volatile
 467         {
 468             vh.setVolatile('\u4567');
 469             char x = (char) vh.getVolatile();
 470             assertEquals(x, '\u4567', "setVolatile char value");
 471         }
 472 
 473         // Lazy
 474         {
 475             vh.setRelease('\u0123');
 476             char x = (char) vh.getAcquire();
 477             assertEquals(x, '\u0123', "setRelease char value");
 478         }
 479 
 480         // Opaque
 481         {
 482             vh.setOpaque('\u4567');
 483             char x = (char) vh.getOpaque();
 484             assertEquals(x, '\u4567', "setOpaque char value");
 485         }
 486 
 487         vh.set('\u0123');
 488 
 489         // Compare
 490         {
 491             boolean r = vh.compareAndSet('\u0123', '\u4567');
 492             assertEquals(r, true, "success compareAndSet char");
 493             char x = (char) vh.get();
 494             assertEquals(x, '\u4567', "success compareAndSet char value");
 495         }
 496 
 497         {
 498             boolean r = vh.compareAndSet('\u0123', '\u89AB');
 499             assertEquals(r, false, "failing compareAndSet char");
 500             char x = (char) vh.get();
 501             assertEquals(x, '\u4567', "failing compareAndSet char value");
 502         }
 503 
 504         {
 505             char r = (char) vh.compareAndExchangeVolatile('\u4567', '\u0123');
 506             assertEquals(r, '\u4567', "success compareAndExchangeVolatile char");
 507             char x = (char) vh.get();
 508             assertEquals(x, '\u0123', "success compareAndExchangeVolatile char value");
 509         }
 510 
 511         {
 512             char r = (char) vh.compareAndExchangeVolatile('\u4567', '\u89AB');
 513             assertEquals(r, '\u0123', "failing compareAndExchangeVolatile char");
 514             char x = (char) vh.get();
 515             assertEquals(x, '\u0123', "failing compareAndExchangeVolatile char value");
 516         }
 517 
 518         {
 519             char r = (char) vh.compareAndExchangeAcquire('\u0123', '\u4567');
 520             assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 521             char x = (char) vh.get();
 522             assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 523         }
 524 
 525         {
 526             char r = (char) vh.compareAndExchangeAcquire('\u0123', '\u89AB');
 527             assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 528             char x = (char) vh.get();
 529             assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 530         }
 531 
 532         {
 533             char r = (char) vh.compareAndExchangeRelease('\u4567', '\u0123');
 534             assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 535             char x = (char) vh.get();
 536             assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 537         }
 538 
 539         {
 540             char r = (char) vh.compareAndExchangeRelease('\u4567', '\u89AB');
 541             assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 542             char x = (char) vh.get();
 543             assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 544         }
 545 
 546         {
 547             boolean success = false;
 548             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 549                 success = vh.weakCompareAndSet('\u0123', '\u4567');
 550             }
 551             assertEquals(success, true, "weakCompareAndSet char");
 552             char x = (char) vh.get();
 553             assertEquals(x, '\u4567', "weakCompareAndSet char value");
 554         }
 555 
 556         {
 557             boolean success = false;
 558             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 559                 success = vh.weakCompareAndSetAcquire('\u4567', '\u0123');
 560             }
 561             assertEquals(success, true, "weakCompareAndSetAcquire char");
 562             char x = (char) vh.get();
 563             assertEquals(x, '\u0123', "weakCompareAndSetAcquire char");
 564         }
 565 
 566         {
 567             boolean success = false;
 568             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 569                 success = vh.weakCompareAndSetRelease('\u0123', '\u4567');
 570             }
 571             assertEquals(success, true, "weakCompareAndSetRelease char");
 572             char x = (char) vh.get();
 573             assertEquals(x, '\u4567', "weakCompareAndSetRelease char");
 574         }
 575 
 576         // Compare set and get
 577         {
 578             char o = (char) vh.getAndSet( '\u0123');
 579             assertEquals(o, '\u4567', "getAndSet char");
 580             char x = (char) vh.get();
 581             assertEquals(x, '\u0123', "getAndSet char value");
 582         }
 583 
 584         vh.set('\u0123');
 585 
 586         // get and add, add and get
 587         {
 588             char o = (char) vh.getAndAdd( '\u89AB');
 589             assertEquals(o, '\u0123', "getAndAdd char");
 590             char c = (char) vh.addAndGet('\u89AB');
 591             assertEquals(c, (char)('\u0123' + '\u89AB' + '\u89AB'), "getAndAdd char value");
 592         }
 593     }
 594 
 595     static void testStaticFieldUnsupported(VarHandle vh) {
 596 



 597     }
 598 
 599 
 600     static void testArray(VarHandle vh) {
 601         char[] array = new char[10];
 602 
 603         for (int i = 0; i < array.length; i++) {
 604             // Plain
 605             {
 606                 vh.set(array, i, '\u0123');
 607                 char x = (char) vh.get(array, i);
 608                 assertEquals(x, '\u0123', "get char value");
 609             }
 610 
 611 
 612             // Volatile
 613             {
 614                 vh.setVolatile(array, i, '\u4567');
 615                 char x = (char) vh.getVolatile(array, i);
 616                 assertEquals(x, '\u4567', "setVolatile char value");
 617             }
 618 
 619             // Lazy
 620             {
 621                 vh.setRelease(array, i, '\u0123');
 622                 char x = (char) vh.getAcquire(array, i);
 623                 assertEquals(x, '\u0123', "setRelease char value");
 624             }
 625 
 626             // Opaque
 627             {
 628                 vh.setOpaque(array, i, '\u4567');
 629                 char x = (char) vh.getOpaque(array, i);
 630                 assertEquals(x, '\u4567', "setOpaque char value");
 631             }
 632 
 633             vh.set(array, i, '\u0123');
 634 
 635             // Compare
 636             {
 637                 boolean r = vh.compareAndSet(array, i, '\u0123', '\u4567');
 638                 assertEquals(r, true, "success compareAndSet char");
 639                 char x = (char) vh.get(array, i);
 640                 assertEquals(x, '\u4567', "success compareAndSet char value");
 641             }
 642 
 643             {
 644                 boolean r = vh.compareAndSet(array, i, '\u0123', '\u89AB');
 645                 assertEquals(r, false, "failing compareAndSet char");
 646                 char x = (char) vh.get(array, i);
 647                 assertEquals(x, '\u4567', "failing compareAndSet char value");
 648             }
 649 
 650             {
 651                 char r = (char) vh.compareAndExchangeVolatile(array, i, '\u4567', '\u0123');
 652                 assertEquals(r, '\u4567', "success compareAndExchangeVolatile char");
 653                 char x = (char) vh.get(array, i);
 654                 assertEquals(x, '\u0123', "success compareAndExchangeVolatile char value");
 655             }
 656 
 657             {
 658                 char r = (char) vh.compareAndExchangeVolatile(array, i, '\u4567', '\u89AB');
 659                 assertEquals(r, '\u0123', "failing compareAndExchangeVolatile char");
 660                 char x = (char) vh.get(array, i);
 661                 assertEquals(x, '\u0123', "failing compareAndExchangeVolatile char value");
 662             }
 663 
 664             {
 665                 char r = (char) vh.compareAndExchangeAcquire(array, i, '\u0123', '\u4567');
 666                 assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 667                 char x = (char) vh.get(array, i);
 668                 assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 669             }
 670 
 671             {
 672                 char r = (char) vh.compareAndExchangeAcquire(array, i, '\u0123', '\u89AB');
 673                 assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 674                 char x = (char) vh.get(array, i);
 675                 assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 676             }
 677 
 678             {
 679                 char r = (char) vh.compareAndExchangeRelease(array, i, '\u4567', '\u0123');
 680                 assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 681                 char x = (char) vh.get(array, i);
 682                 assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 683             }
 684 
 685             {
 686                 char r = (char) vh.compareAndExchangeRelease(array, i, '\u4567', '\u89AB');
 687                 assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 688                 char x = (char) vh.get(array, i);
 689                 assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 690             }
 691 
 692             {
 693                 boolean success = false;
 694                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 695                     success = vh.weakCompareAndSet(array, i, '\u0123', '\u4567');
 696                 }
 697                 assertEquals(success, true, "weakCompareAndSet char");
 698                 char x = (char) vh.get(array, i);
 699                 assertEquals(x, '\u4567', "weakCompareAndSet char value");
 700             }
 701 
 702             {
 703                 boolean success = false;
 704                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 705                     success = vh.weakCompareAndSetAcquire(array, i, '\u4567', '\u0123');
 706                 }
 707                 assertEquals(success, true, "weakCompareAndSetAcquire char");
 708                 char x = (char) vh.get(array, i);
 709                 assertEquals(x, '\u0123', "weakCompareAndSetAcquire char");
 710             }
 711 
 712             {
 713                 boolean success = false;
 714                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 715                     success = vh.weakCompareAndSetRelease(array, i, '\u0123', '\u4567');
 716                 }
 717                 assertEquals(success, true, "weakCompareAndSetRelease char");
 718                 char x = (char) vh.get(array, i);
 719                 assertEquals(x, '\u4567', "weakCompareAndSetRelease char");
 720             }
 721 
 722             // Compare set and get
 723             {
 724                 char o = (char) vh.getAndSet(array, i, '\u0123');
 725                 assertEquals(o, '\u4567', "getAndSet char");
 726                 char x = (char) vh.get(array, i);
 727                 assertEquals(x, '\u0123', "getAndSet char value");
 728             }
 729 
 730             vh.set(array, i, '\u0123');
 731 
 732             // get and add, add and get
 733             {
 734                 char o = (char) vh.getAndAdd(array, i, '\u89AB');
 735                 assertEquals(o, '\u0123', "getAndAdd char");
 736                 char c = (char) vh.addAndGet(array, i, '\u89AB');
 737                 assertEquals(c, (char)('\u0123' + '\u89AB' + '\u89AB'), "getAndAdd char value");
 738             }
 739         }
 740     }
 741 
 742     static void testArrayUnsupported(VarHandle vh) {
 743         char[] array = new char[10];
 744 
 745         int i = 0;
 746 



 747     }
 748 
 749     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 750         char[] array = new char[10];
 751 
 752         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 753             final int ci = i;
 754 
 755             checkIOOBE(() -> {
 756                 char x = (char) vh.get(array, ci);
 757             });
 758 
 759             checkIOOBE(() -> {
 760                 vh.set(array, ci, '\u0123');
 761             });
 762 
 763             checkIOOBE(() -> {
 764                 char x = (char) vh.getVolatile(array, ci);
 765             });
 766 
 767             checkIOOBE(() -> {
 768                 vh.setVolatile(array, ci, '\u0123');
 769             });
 770 
 771             checkIOOBE(() -> {
 772                 char x = (char) vh.getAcquire(array, ci);
 773             });
 774 
 775             checkIOOBE(() -> {
 776                 vh.setRelease(array, ci, '\u0123');
 777             });
 778 
 779             checkIOOBE(() -> {
 780                 char x = (char) vh.getOpaque(array, ci);
 781             });
 782 
 783             checkIOOBE(() -> {
 784                 vh.setOpaque(array, ci, '\u0123');
 785             });
 786 
 787             checkIOOBE(() -> {
 788                 boolean r = vh.compareAndSet(array, ci, '\u0123', '\u4567');
 789             });
 790 
 791             checkIOOBE(() -> {
 792                 char r = (char) vh.compareAndExchangeVolatile(array, ci, '\u4567', '\u0123');
 793             });
 794 
 795             checkIOOBE(() -> {
 796                 char r = (char) vh.compareAndExchangeAcquire(array, ci, '\u4567', '\u0123');
 797             });
 798 
 799             checkIOOBE(() -> {
 800                 char r = (char) vh.compareAndExchangeRelease(array, ci, '\u4567', '\u0123');
 801             });
 802 
 803             checkIOOBE(() -> {
 804                 boolean r = vh.weakCompareAndSet(array, ci, '\u0123', '\u4567');
 805             });
 806 
 807             checkIOOBE(() -> {
 808                 boolean r = vh.weakCompareAndSetAcquire(array, ci, '\u0123', '\u4567');
 809             });
 810 
 811             checkIOOBE(() -> {
 812                 boolean r = vh.weakCompareAndSetRelease(array, ci, '\u0123', '\u4567');
 813             });
 814 
 815             checkIOOBE(() -> {
 816                 char o = (char) vh.getAndSet(array, ci, '\u0123');
 817             });
 818 
 819             checkIOOBE(() -> {
 820                 char o = (char) vh.getAndAdd(array, ci, '\u89AB');
 821             });
 822 
 823             checkIOOBE(() -> {
 824                 char o = (char) vh.addAndGet(array, ci, '\u89AB');
 825             });
 826         }
 827     }
 828 }
 829 
< prev index next >