< prev index next >

test/java/lang/invoke/VarHandles/VarHandleTestAccessLong.java

Print this page




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


 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(VarHandleTestAccessLong recv, VarHandle vh) {
 219         // Plain
 220         {
 221             long x = (long) vh.get(recv);
 222             assertEquals(x, 1L, "get long value");
 223         }
 224 
 225 
 226         // Volatile
 227         {
 228             long x = (long) vh.getVolatile(recv);
 229             assertEquals(x, 1L, "getVolatile long value");
 230         }
 231 
 232         // Lazy
 233         {
 234             long x = (long) vh.getAcquire(recv);
 235             assertEquals(x, 1L, "getRelease long value");
 236         }
 237 
 238         // Opaque
 239         {
 240             long x = (long) vh.getOpaque(recv);
 241             assertEquals(x, 1L, "getOpaque long value");
 242         }
 243     }
 244 
 245     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 246         checkUOE(() -> {
 247             vh.set(recv, 2L);
 248         });
 249 
 250         checkUOE(() -> {
 251             vh.setVolatile(recv, 2L);
 252         });
 253 
 254         checkUOE(() -> {
 255             vh.setRelease(recv, 2L);
 256         });
 257 
 258         checkUOE(() -> {
 259             vh.setOpaque(recv, 2L);
 260         });
 261 
 262 
 263     }
 264 
 265 
 266     static void testStaticFinalField(VarHandle vh) {
 267         // Plain
 268         {
 269             long x = (long) vh.get();
 270             assertEquals(x, 1L, "get long value");
 271         }
 272 
 273 
 274         // Volatile
 275         {
 276             long x = (long) vh.getVolatile();
 277             assertEquals(x, 1L, "getVolatile long value");
 278         }
 279 
 280         // Lazy
 281         {
 282             long x = (long) vh.getAcquire();
 283             assertEquals(x, 1L, "getRelease long value");
 284         }
 285 
 286         // Opaque
 287         {
 288             long x = (long) vh.getOpaque();
 289             assertEquals(x, 1L, "getOpaque long value");
 290         }
 291     }
 292 
 293     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 294         checkUOE(() -> {
 295             vh.set(2L);
 296         });
 297 
 298         checkUOE(() -> {
 299             vh.setVolatile(2L);
 300         });
 301 
 302         checkUOE(() -> {
 303             vh.setRelease(2L);
 304         });
 305 
 306         checkUOE(() -> {
 307             vh.setOpaque(2L);
 308         });
 309 
 310 
 311     }
 312 
 313 
 314     static void testInstanceField(VarHandleTestAccessLong recv, VarHandle vh) {
 315         // Plain
 316         {
 317             vh.set(recv, 1L);
 318             long x = (long) vh.get(recv);
 319             assertEquals(x, 1L, "set long value");
 320         }
 321 
 322 
 323         // Volatile
 324         {
 325             vh.setVolatile(recv, 2L);
 326             long x = (long) vh.getVolatile(recv);
 327             assertEquals(x, 2L, "setVolatile long value");
 328         }
 329 
 330         // Lazy
 331         {
 332             vh.setRelease(recv, 1L);
 333             long x = (long) vh.getAcquire(recv);
 334             assertEquals(x, 1L, "setRelease long value");
 335         }
 336 
 337         // Opaque
 338         {
 339             vh.setOpaque(recv, 2L);
 340             long x = (long) vh.getOpaque(recv);
 341             assertEquals(x, 2L, "setOpaque long value");
 342         }
 343 
 344         vh.set(recv, 1L);
 345 
 346         // Compare
 347         {
 348             boolean r = vh.compareAndSet(recv, 1L, 2L);
 349             assertEquals(r, true, "success compareAndSet long");
 350             long x = (long) vh.get(recv);
 351             assertEquals(x, 2L, "success compareAndSet long value");
 352         }
 353 
 354         {
 355             boolean r = vh.compareAndSet(recv, 1L, 3L);
 356             assertEquals(r, false, "failing compareAndSet long");
 357             long x = (long) vh.get(recv);
 358             assertEquals(x, 2L, "failing compareAndSet long value");
 359         }
 360 
 361         {
 362             long r = (long) vh.compareAndExchangeVolatile(recv, 2L, 1L);
 363             assertEquals(r, 2L, "success compareAndExchangeVolatile long");
 364             long x = (long) vh.get(recv);
 365             assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
 366         }
 367 
 368         {
 369             long r = (long) vh.compareAndExchangeVolatile(recv, 2L, 3L);
 370             assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
 371             long x = (long) vh.get(recv);
 372             assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
 373         }
 374 
 375         {
 376             long r = (long) vh.compareAndExchangeAcquire(recv, 1L, 2L);
 377             assertEquals(r, 1L, "success compareAndExchangeAcquire long");
 378             long x = (long) vh.get(recv);
 379             assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
 380         }
 381 
 382         {
 383             long r = (long) vh.compareAndExchangeAcquire(recv, 1L, 3L);
 384             assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
 385             long x = (long) vh.get(recv);
 386             assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
 387         }
 388 
 389         {
 390             long r = (long) vh.compareAndExchangeRelease(recv, 2L, 1L);
 391             assertEquals(r, 2L, "success compareAndExchangeRelease long");
 392             long x = (long) vh.get(recv);
 393             assertEquals(x, 1L, "success compareAndExchangeRelease long value");
 394         }
 395 
 396         {
 397             long r = (long) vh.compareAndExchangeRelease(recv, 2L, 3L);
 398             assertEquals(r, 1L, "failing compareAndExchangeRelease long");
 399             long x = (long) vh.get(recv);
 400             assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
 401         }
 402 
 403         {
 404             boolean success = false;
 405             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 406                 success = vh.weakCompareAndSet(recv, 1L, 2L);
 407             }
 408             assertEquals(success, true, "weakCompareAndSet long");
 409             long x = (long) vh.get(recv);
 410             assertEquals(x, 2L, "weakCompareAndSet long value");
 411         }
 412 
 413         {
 414             boolean success = false;
 415             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 416                 success = vh.weakCompareAndSetAcquire(recv, 2L, 1L);
 417             }
 418             assertEquals(success, true, "weakCompareAndSetAcquire long");
 419             long x = (long) vh.get(recv);
 420             assertEquals(x, 1L, "weakCompareAndSetAcquire long");
 421         }
 422 
 423         {
 424             boolean success = false;
 425             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 426                 success = vh.weakCompareAndSetRelease(recv, 1L, 2L);
 427             }
 428             assertEquals(success, true, "weakCompareAndSetRelease long");
 429             long x = (long) vh.get(recv);
 430             assertEquals(x, 2L, "weakCompareAndSetRelease long");
 431         }
 432 
 433         // Compare set and get
 434         {
 435             long o = (long) vh.getAndSet(recv, 1L);
 436             assertEquals(o, 2L, "getAndSet long");
 437             long x = (long) vh.get(recv);
 438             assertEquals(x, 1L, "getAndSet long value");
 439         }
 440 
 441         vh.set(recv, 1L);
 442 
 443         // get and add, add and get
 444         {
 445             long o = (long) vh.getAndAdd(recv, 3L);
 446             assertEquals(o, 1L, "getAndAdd long");
 447             long c = (long) vh.addAndGet(recv, 3L);
 448             assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
 449         }
 450     }
 451 
 452     static void testInstanceFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 453 
 454     }
 455 
 456 
 457     static void testStaticField(VarHandle vh) {
 458         // Plain
 459         {
 460             vh.set(1L);
 461             long x = (long) vh.get();
 462             assertEquals(x, 1L, "set long value");
 463         }
 464 
 465 
 466         // Volatile
 467         {
 468             vh.setVolatile(2L);
 469             long x = (long) vh.getVolatile();
 470             assertEquals(x, 2L, "setVolatile long value");
 471         }
 472 
 473         // Lazy
 474         {
 475             vh.setRelease(1L);
 476             long x = (long) vh.getAcquire();
 477             assertEquals(x, 1L, "setRelease long value");
 478         }
 479 
 480         // Opaque
 481         {
 482             vh.setOpaque(2L);
 483             long x = (long) vh.getOpaque();
 484             assertEquals(x, 2L, "setOpaque long value");
 485         }
 486 
 487         vh.set(1L);
 488 
 489         // Compare
 490         {
 491             boolean r = vh.compareAndSet(1L, 2L);
 492             assertEquals(r, true, "success compareAndSet long");
 493             long x = (long) vh.get();
 494             assertEquals(x, 2L, "success compareAndSet long value");
 495         }
 496 
 497         {
 498             boolean r = vh.compareAndSet(1L, 3L);
 499             assertEquals(r, false, "failing compareAndSet long");
 500             long x = (long) vh.get();
 501             assertEquals(x, 2L, "failing compareAndSet long value");
 502         }
 503 
 504         {
 505             long r = (long) vh.compareAndExchangeVolatile(2L, 1L);
 506             assertEquals(r, 2L, "success compareAndExchangeVolatile long");
 507             long x = (long) vh.get();
 508             assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
 509         }
 510 
 511         {
 512             long r = (long) vh.compareAndExchangeVolatile(2L, 3L);
 513             assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
 514             long x = (long) vh.get();
 515             assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
 516         }
 517 
 518         {
 519             long r = (long) vh.compareAndExchangeAcquire(1L, 2L);
 520             assertEquals(r, 1L, "success compareAndExchangeAcquire long");
 521             long x = (long) vh.get();
 522             assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
 523         }
 524 
 525         {
 526             long r = (long) vh.compareAndExchangeAcquire(1L, 3L);
 527             assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
 528             long x = (long) vh.get();
 529             assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
 530         }
 531 
 532         {
 533             long r = (long) vh.compareAndExchangeRelease(2L, 1L);
 534             assertEquals(r, 2L, "success compareAndExchangeRelease long");
 535             long x = (long) vh.get();
 536             assertEquals(x, 1L, "success compareAndExchangeRelease long value");
 537         }
 538 
 539         {
 540             long r = (long) vh.compareAndExchangeRelease(2L, 3L);
 541             assertEquals(r, 1L, "failing compareAndExchangeRelease long");
 542             long x = (long) vh.get();
 543             assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
 544         }
 545 
 546         {
 547             boolean success = false;
 548             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 549                 success = vh.weakCompareAndSet(1L, 2L);
 550             }
 551             assertEquals(success, true, "weakCompareAndSet long");
 552             long x = (long) vh.get();
 553             assertEquals(x, 2L, "weakCompareAndSet long value");
 554         }
 555 
 556         {
 557             boolean success = false;
 558             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 559                 success = vh.weakCompareAndSetAcquire(2L, 1L);
 560             }
 561             assertEquals(success, true, "weakCompareAndSetAcquire long");
 562             long x = (long) vh.get();
 563             assertEquals(x, 1L, "weakCompareAndSetAcquire long");
 564         }
 565 
 566         {
 567             boolean success = false;
 568             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 569                 success = vh.weakCompareAndSetRelease(1L, 2L);
 570             }
 571             assertEquals(success, true, "weakCompareAndSetRelease long");
 572             long x = (long) vh.get();
 573             assertEquals(x, 2L, "weakCompareAndSetRelease long");
 574         }
 575 
 576         // Compare set and get
 577         {
 578             long o = (long) vh.getAndSet( 1L);
 579             assertEquals(o, 2L, "getAndSet long");
 580             long x = (long) vh.get();
 581             assertEquals(x, 1L, "getAndSet long value");
 582         }
 583 
 584         vh.set(1L);
 585 
 586         // get and add, add and get
 587         {
 588             long o = (long) vh.getAndAdd( 3L);
 589             assertEquals(o, 1L, "getAndAdd long");
 590             long c = (long) vh.addAndGet(3L);
 591             assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
 592         }
 593     }
 594 
 595     static void testStaticFieldUnsupported(VarHandle vh) {
 596 
 597     }
 598 
 599 
 600     static void testArray(VarHandle vh) {
 601         long[] array = new long[10];
 602 
 603         for (int i = 0; i < array.length; i++) {
 604             // Plain
 605             {
 606                 vh.set(array, i, 1L);
 607                 long x = (long) vh.get(array, i);
 608                 assertEquals(x, 1L, "get long value");
 609             }
 610 
 611 
 612             // Volatile
 613             {
 614                 vh.setVolatile(array, i, 2L);
 615                 long x = (long) vh.getVolatile(array, i);
 616                 assertEquals(x, 2L, "setVolatile long value");
 617             }
 618 
 619             // Lazy
 620             {
 621                 vh.setRelease(array, i, 1L);
 622                 long x = (long) vh.getAcquire(array, i);
 623                 assertEquals(x, 1L, "setRelease long value");
 624             }
 625 
 626             // Opaque
 627             {
 628                 vh.setOpaque(array, i, 2L);
 629                 long x = (long) vh.getOpaque(array, i);
 630                 assertEquals(x, 2L, "setOpaque long value");
 631             }
 632 
 633             vh.set(array, i, 1L);
 634 
 635             // Compare
 636             {
 637                 boolean r = vh.compareAndSet(array, i, 1L, 2L);
 638                 assertEquals(r, true, "success compareAndSet long");
 639                 long x = (long) vh.get(array, i);
 640                 assertEquals(x, 2L, "success compareAndSet long value");
 641             }
 642 
 643             {
 644                 boolean r = vh.compareAndSet(array, i, 1L, 3L);
 645                 assertEquals(r, false, "failing compareAndSet long");
 646                 long x = (long) vh.get(array, i);
 647                 assertEquals(x, 2L, "failing compareAndSet long value");
 648             }
 649 
 650             {
 651                 long r = (long) vh.compareAndExchangeVolatile(array, i, 2L, 1L);
 652                 assertEquals(r, 2L, "success compareAndExchangeVolatile long");
 653                 long x = (long) vh.get(array, i);
 654                 assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
 655             }
 656 
 657             {
 658                 long r = (long) vh.compareAndExchangeVolatile(array, i, 2L, 3L);
 659                 assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
 660                 long x = (long) vh.get(array, i);
 661                 assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
 662             }
 663 
 664             {
 665                 long r = (long) vh.compareAndExchangeAcquire(array, i, 1L, 2L);
 666                 assertEquals(r, 1L, "success compareAndExchangeAcquire long");
 667                 long x = (long) vh.get(array, i);
 668                 assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
 669             }
 670 
 671             {
 672                 long r = (long) vh.compareAndExchangeAcquire(array, i, 1L, 3L);
 673                 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
 674                 long x = (long) vh.get(array, i);
 675                 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
 676             }
 677 
 678             {
 679                 long r = (long) vh.compareAndExchangeRelease(array, i, 2L, 1L);
 680                 assertEquals(r, 2L, "success compareAndExchangeRelease long");
 681                 long x = (long) vh.get(array, i);
 682                 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
 683             }
 684 
 685             {
 686                 long r = (long) vh.compareAndExchangeRelease(array, i, 2L, 3L);
 687                 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
 688                 long x = (long) vh.get(array, i);
 689                 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
 690             }
 691 
 692             {
 693                 boolean success = false;
 694                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 695                     success = vh.weakCompareAndSet(array, i, 1L, 2L);
 696                 }
 697                 assertEquals(success, true, "weakCompareAndSet long");
 698                 long x = (long) vh.get(array, i);
 699                 assertEquals(x, 2L, "weakCompareAndSet long value");
 700             }
 701 
 702             {
 703                 boolean success = false;
 704                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 705                     success = vh.weakCompareAndSetAcquire(array, i, 2L, 1L);
 706                 }
 707                 assertEquals(success, true, "weakCompareAndSetAcquire long");
 708                 long x = (long) vh.get(array, i);
 709                 assertEquals(x, 1L, "weakCompareAndSetAcquire long");
 710             }
 711 
 712             {
 713                 boolean success = false;
 714                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 715                     success = vh.weakCompareAndSetRelease(array, i, 1L, 2L);
 716                 }
 717                 assertEquals(success, true, "weakCompareAndSetRelease long");
 718                 long x = (long) vh.get(array, i);
 719                 assertEquals(x, 2L, "weakCompareAndSetRelease long");
 720             }
 721 
 722             // Compare set and get
 723             {
 724                 long o = (long) vh.getAndSet(array, i, 1L);
 725                 assertEquals(o, 2L, "getAndSet long");
 726                 long x = (long) vh.get(array, i);
 727                 assertEquals(x, 1L, "getAndSet long value");
 728             }
 729 
 730             vh.set(array, i, 1L);
 731 
 732             // get and add, add and get
 733             {
 734                 long o = (long) vh.getAndAdd(array, i, 3L);
 735                 assertEquals(o, 1L, "getAndAdd long");
 736                 long c = (long) vh.addAndGet(array, i, 3L);
 737                 assertEquals(c, 1L + 3L + 3L, "getAndAdd long value");
 738             }
 739         }
 740     }
 741 
 742     static void testArrayUnsupported(VarHandle vh) {
 743         long[] array = new long[10];
 744 
 745         int i = 0;
 746 
 747     }
 748 
 749     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 750         long[] array = new long[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                 long x = (long) vh.get(array, ci);
 757             });
 758 
 759             checkIOOBE(() -> {
 760                 vh.set(array, ci, 1L);
 761             });
 762 
 763             checkIOOBE(() -> {
 764                 long x = (long) vh.getVolatile(array, ci);
 765             });
 766 
 767             checkIOOBE(() -> {
 768                 vh.setVolatile(array, ci, 1L);
 769             });
 770 
 771             checkIOOBE(() -> {
 772                 long x = (long) vh.getAcquire(array, ci);
 773             });
 774 
 775             checkIOOBE(() -> {
 776                 vh.setRelease(array, ci, 1L);
 777             });
 778 
 779             checkIOOBE(() -> {
 780                 long x = (long) vh.getOpaque(array, ci);
 781             });
 782 
 783             checkIOOBE(() -> {
 784                 vh.setOpaque(array, ci, 1L);
 785             });
 786 
 787             checkIOOBE(() -> {
 788                 boolean r = vh.compareAndSet(array, ci, 1L, 2L);
 789             });
 790 
 791             checkIOOBE(() -> {
 792                 long r = (long) vh.compareAndExchangeVolatile(array, ci, 2L, 1L);
 793             });
 794 
 795             checkIOOBE(() -> {
 796                 long r = (long) vh.compareAndExchangeAcquire(array, ci, 2L, 1L);
 797             });
 798 
 799             checkIOOBE(() -> {
 800                 long r = (long) vh.compareAndExchangeRelease(array, ci, 2L, 1L);
 801             });
 802 
 803             checkIOOBE(() -> {
 804                 boolean r = vh.weakCompareAndSet(array, ci, 1L, 2L);
 805             });
 806 
 807             checkIOOBE(() -> {
 808                 boolean r = vh.weakCompareAndSetAcquire(array, ci, 1L, 2L);
 809             });
 810 
 811             checkIOOBE(() -> {
 812                 boolean r = vh.weakCompareAndSetRelease(array, ci, 1L, 2L);
 813             });
 814 
 815             checkIOOBE(() -> {
 816                 long o = (long) vh.getAndSet(array, ci, 1L);
 817             });
 818 
 819             checkIOOBE(() -> {
 820                 long o = (long) vh.getAndAdd(array, ci, 3L);
 821             });
 822 
 823             checkIOOBE(() -> {
 824                 long o = (long) vh.addAndGet(array, ci, 3L);
 825             });
 826         }
 827     }
 828 }
 829 


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


 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(VarHandleTestAccessLong recv, VarHandle vh) {
 219         // Plain
 220         {
 221             long x = (long) vh.get(recv);
 222             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 223         }
 224 
 225 
 226         // Volatile
 227         {
 228             long x = (long) vh.getVolatile(recv);
 229             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 230         }
 231 
 232         // Lazy
 233         {
 234             long x = (long) vh.getAcquire(recv);
 235             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 236         }
 237 
 238         // Opaque
 239         {
 240             long x = (long) vh.getOpaque(recv);
 241             assertEquals(x, 0x0123456789ABCDEFL, "getOpaque long value");
 242         }
 243     }
 244 
 245     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 246         checkUOE(() -> {
 247             vh.set(recv, 0xCAFEBABECAFEBABEL);
 248         });
 249 
 250         checkUOE(() -> {
 251             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 252         });
 253 
 254         checkUOE(() -> {
 255             vh.setRelease(recv, 0xCAFEBABECAFEBABEL);
 256         });
 257 
 258         checkUOE(() -> {
 259             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 260         });
 261 
 262 
 263     }
 264 
 265 
 266     static void testStaticFinalField(VarHandle vh) {
 267         // Plain
 268         {
 269             long x = (long) vh.get();
 270             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 271         }
 272 
 273 
 274         // Volatile
 275         {
 276             long x = (long) vh.getVolatile();
 277             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 278         }
 279 
 280         // Lazy
 281         {
 282             long x = (long) vh.getAcquire();
 283             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 284         }
 285 
 286         // Opaque
 287         {
 288             long x = (long) vh.getOpaque();
 289             assertEquals(x, 0x0123456789ABCDEFL, "getOpaque long value");
 290         }
 291     }
 292 
 293     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 294         checkUOE(() -> {
 295             vh.set(0xCAFEBABECAFEBABEL);
 296         });
 297 
 298         checkUOE(() -> {
 299             vh.setVolatile(0xCAFEBABECAFEBABEL);
 300         });
 301 
 302         checkUOE(() -> {
 303             vh.setRelease(0xCAFEBABECAFEBABEL);
 304         });
 305 
 306         checkUOE(() -> {
 307             vh.setOpaque(0xCAFEBABECAFEBABEL);
 308         });
 309 
 310 
 311     }
 312 
 313 
 314     static void testInstanceField(VarHandleTestAccessLong recv, VarHandle vh) {
 315         // Plain
 316         {
 317             vh.set(recv, 0x0123456789ABCDEFL);
 318             long x = (long) vh.get(recv);
 319             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 320         }
 321 
 322 
 323         // Volatile
 324         {
 325             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 326             long x = (long) vh.getVolatile(recv);
 327             assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 328         }
 329 
 330         // Lazy
 331         {
 332             vh.setRelease(recv, 0x0123456789ABCDEFL);
 333             long x = (long) vh.getAcquire(recv);
 334             assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 335         }
 336 
 337         // Opaque
 338         {
 339             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 340             long x = (long) vh.getOpaque(recv);
 341             assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 342         }
 343 
 344         vh.set(recv, 0x0123456789ABCDEFL);
 345 
 346         // Compare
 347         {
 348             boolean r = vh.compareAndSet(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 349             assertEquals(r, true, "success compareAndSet long");
 350             long x = (long) vh.get(recv);
 351             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 352         }
 353 
 354         {
 355             boolean r = vh.compareAndSet(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 356             assertEquals(r, false, "failing compareAndSet long");
 357             long x = (long) vh.get(recv);
 358             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 359         }
 360 
 361         {
 362             long r = (long) vh.compareAndExchangeVolatile(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 363             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long");
 364             long x = (long) vh.get(recv);
 365             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value");
 366         }
 367 
 368         {
 369             long r = (long) vh.compareAndExchangeVolatile(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 370             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long");
 371             long x = (long) vh.get(recv);
 372             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value");
 373         }
 374 
 375         {
 376             long r = (long) vh.compareAndExchangeAcquire(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 377             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 378             long x = (long) vh.get(recv);
 379             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 380         }
 381 
 382         {
 383             long r = (long) vh.compareAndExchangeAcquire(recv, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 384             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 385             long x = (long) vh.get(recv);
 386             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 387         }
 388 
 389         {
 390             long r = (long) vh.compareAndExchangeRelease(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 391             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 392             long x = (long) vh.get(recv);
 393             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 394         }
 395 
 396         {
 397             long r = (long) vh.compareAndExchangeRelease(recv, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 398             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 399             long x = (long) vh.get(recv);
 400             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 401         }
 402 
 403         {
 404             boolean success = false;
 405             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 406                 success = vh.weakCompareAndSet(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 407             }
 408             assertEquals(success, true, "weakCompareAndSet long");
 409             long x = (long) vh.get(recv);
 410             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSet long value");
 411         }
 412 
 413         {
 414             boolean success = false;
 415             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 416                 success = vh.weakCompareAndSetAcquire(recv, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 417             }
 418             assertEquals(success, true, "weakCompareAndSetAcquire long");
 419             long x = (long) vh.get(recv);
 420             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 421         }
 422 
 423         {
 424             boolean success = false;
 425             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 426                 success = vh.weakCompareAndSetRelease(recv, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 427             }
 428             assertEquals(success, true, "weakCompareAndSetRelease long");
 429             long x = (long) vh.get(recv);
 430             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 431         }
 432 
 433         // Compare set and get
 434         {
 435             long o = (long) vh.getAndSet(recv, 0x0123456789ABCDEFL);
 436             assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 437             long x = (long) vh.get(recv);
 438             assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 439         }
 440 
 441         vh.set(recv, 0x0123456789ABCDEFL);
 442 
 443         // get and add, add and get
 444         {
 445             long o = (long) vh.getAndAdd(recv, 0xDEADBEEFDEADBEEFL);
 446             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 447             long c = (long) vh.addAndGet(recv, 0xDEADBEEFDEADBEEFL);
 448             assertEquals(c, (long)(0x0123456789ABCDEFL + 0xDEADBEEFDEADBEEFL + 0xDEADBEEFDEADBEEFL), "getAndAdd long value");
 449         }
 450     }
 451 
 452     static void testInstanceFieldUnsupported(VarHandleTestAccessLong recv, VarHandle vh) {
 453 
 454     }
 455 
 456 
 457     static void testStaticField(VarHandle vh) {
 458         // Plain
 459         {
 460             vh.set(0x0123456789ABCDEFL);
 461             long x = (long) vh.get();
 462             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 463         }
 464 
 465 
 466         // Volatile
 467         {
 468             vh.setVolatile(0xCAFEBABECAFEBABEL);
 469             long x = (long) vh.getVolatile();
 470             assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 471         }
 472 
 473         // Lazy
 474         {
 475             vh.setRelease(0x0123456789ABCDEFL);
 476             long x = (long) vh.getAcquire();
 477             assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 478         }
 479 
 480         // Opaque
 481         {
 482             vh.setOpaque(0xCAFEBABECAFEBABEL);
 483             long x = (long) vh.getOpaque();
 484             assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 485         }
 486 
 487         vh.set(0x0123456789ABCDEFL);
 488 
 489         // Compare
 490         {
 491             boolean r = vh.compareAndSet(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 492             assertEquals(r, true, "success compareAndSet long");
 493             long x = (long) vh.get();
 494             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 495         }
 496 
 497         {
 498             boolean r = vh.compareAndSet(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 499             assertEquals(r, false, "failing compareAndSet long");
 500             long x = (long) vh.get();
 501             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 502         }
 503 
 504         {
 505             long r = (long) vh.compareAndExchangeVolatile(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 506             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long");
 507             long x = (long) vh.get();
 508             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value");
 509         }
 510 
 511         {
 512             long r = (long) vh.compareAndExchangeVolatile(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 513             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long");
 514             long x = (long) vh.get();
 515             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value");
 516         }
 517 
 518         {
 519             long r = (long) vh.compareAndExchangeAcquire(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 520             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 521             long x = (long) vh.get();
 522             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 523         }
 524 
 525         {
 526             long r = (long) vh.compareAndExchangeAcquire(0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 527             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 528             long x = (long) vh.get();
 529             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 530         }
 531 
 532         {
 533             long r = (long) vh.compareAndExchangeRelease(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 534             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 535             long x = (long) vh.get();
 536             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 537         }
 538 
 539         {
 540             long r = (long) vh.compareAndExchangeRelease(0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 541             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 542             long x = (long) vh.get();
 543             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 544         }
 545 
 546         {
 547             boolean success = false;
 548             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 549                 success = vh.weakCompareAndSet(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 550             }
 551             assertEquals(success, true, "weakCompareAndSet long");
 552             long x = (long) vh.get();
 553             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSet long value");
 554         }
 555 
 556         {
 557             boolean success = false;
 558             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 559                 success = vh.weakCompareAndSetAcquire(0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 560             }
 561             assertEquals(success, true, "weakCompareAndSetAcquire long");
 562             long x = (long) vh.get();
 563             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 564         }
 565 
 566         {
 567             boolean success = false;
 568             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 569                 success = vh.weakCompareAndSetRelease(0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 570             }
 571             assertEquals(success, true, "weakCompareAndSetRelease long");
 572             long x = (long) vh.get();
 573             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 574         }
 575 
 576         // Compare set and get
 577         {
 578             long o = (long) vh.getAndSet( 0x0123456789ABCDEFL);
 579             assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 580             long x = (long) vh.get();
 581             assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 582         }
 583 
 584         vh.set(0x0123456789ABCDEFL);
 585 
 586         // get and add, add and get
 587         {
 588             long o = (long) vh.getAndAdd( 0xDEADBEEFDEADBEEFL);
 589             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 590             long c = (long) vh.addAndGet(0xDEADBEEFDEADBEEFL);
 591             assertEquals(c, (long)(0x0123456789ABCDEFL + 0xDEADBEEFDEADBEEFL + 0xDEADBEEFDEADBEEFL), "getAndAdd long value");
 592         }
 593     }
 594 
 595     static void testStaticFieldUnsupported(VarHandle vh) {
 596 
 597     }
 598 
 599 
 600     static void testArray(VarHandle vh) {
 601         long[] array = new long[10];
 602 
 603         for (int i = 0; i < array.length; i++) {
 604             // Plain
 605             {
 606                 vh.set(array, i, 0x0123456789ABCDEFL);
 607                 long x = (long) vh.get(array, i);
 608                 assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 609             }
 610 
 611 
 612             // Volatile
 613             {
 614                 vh.setVolatile(array, i, 0xCAFEBABECAFEBABEL);
 615                 long x = (long) vh.getVolatile(array, i);
 616                 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value");
 617             }
 618 
 619             // Lazy
 620             {
 621                 vh.setRelease(array, i, 0x0123456789ABCDEFL);
 622                 long x = (long) vh.getAcquire(array, i);
 623                 assertEquals(x, 0x0123456789ABCDEFL, "setRelease long value");
 624             }
 625 
 626             // Opaque
 627             {
 628                 vh.setOpaque(array, i, 0xCAFEBABECAFEBABEL);
 629                 long x = (long) vh.getOpaque(array, i);
 630                 assertEquals(x, 0xCAFEBABECAFEBABEL, "setOpaque long value");
 631             }
 632 
 633             vh.set(array, i, 0x0123456789ABCDEFL);
 634 
 635             // Compare
 636             {
 637                 boolean r = vh.compareAndSet(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 638                 assertEquals(r, true, "success compareAndSet long");
 639                 long x = (long) vh.get(array, i);
 640                 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 641             }
 642 
 643             {
 644                 boolean r = vh.compareAndSet(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 645                 assertEquals(r, false, "failing compareAndSet long");
 646                 long x = (long) vh.get(array, i);
 647                 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 648             }
 649 
 650             {
 651                 long r = (long) vh.compareAndExchangeVolatile(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 652                 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long");
 653                 long x = (long) vh.get(array, i);
 654                 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value");
 655             }
 656 
 657             {
 658                 long r = (long) vh.compareAndExchangeVolatile(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 659                 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long");
 660                 long x = (long) vh.get(array, i);
 661                 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value");
 662             }
 663 
 664             {
 665                 long r = (long) vh.compareAndExchangeAcquire(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 666                 assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 667                 long x = (long) vh.get(array, i);
 668                 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 669             }
 670 
 671             {
 672                 long r = (long) vh.compareAndExchangeAcquire(array, i, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 673                 assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 674                 long x = (long) vh.get(array, i);
 675                 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 676             }
 677 
 678             {
 679                 long r = (long) vh.compareAndExchangeRelease(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 680                 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 681                 long x = (long) vh.get(array, i);
 682                 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 683             }
 684 
 685             {
 686                 long r = (long) vh.compareAndExchangeRelease(array, i, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 687                 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 688                 long x = (long) vh.get(array, i);
 689                 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 690             }
 691 
 692             {
 693                 boolean success = false;
 694                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 695                     success = vh.weakCompareAndSet(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 696                 }
 697                 assertEquals(success, true, "weakCompareAndSet long");
 698                 long x = (long) vh.get(array, i);
 699                 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSet long value");
 700             }
 701 
 702             {
 703                 boolean success = false;
 704                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 705                     success = vh.weakCompareAndSetAcquire(array, i, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 706                 }
 707                 assertEquals(success, true, "weakCompareAndSetAcquire long");
 708                 long x = (long) vh.get(array, i);
 709                 assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 710             }
 711 
 712             {
 713                 boolean success = false;
 714                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 715                     success = vh.weakCompareAndSetRelease(array, i, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 716                 }
 717                 assertEquals(success, true, "weakCompareAndSetRelease long");
 718                 long x = (long) vh.get(array, i);
 719                 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 720             }
 721 
 722             // Compare set and get
 723             {
 724                 long o = (long) vh.getAndSet(array, i, 0x0123456789ABCDEFL);
 725                 assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 726                 long x = (long) vh.get(array, i);
 727                 assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 728             }
 729 
 730             vh.set(array, i, 0x0123456789ABCDEFL);
 731 
 732             // get and add, add and get
 733             {
 734                 long o = (long) vh.getAndAdd(array, i, 0xDEADBEEFDEADBEEFL);
 735                 assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 736                 long c = (long) vh.addAndGet(array, i, 0xDEADBEEFDEADBEEFL);
 737                 assertEquals(c, (long)(0x0123456789ABCDEFL + 0xDEADBEEFDEADBEEFL + 0xDEADBEEFDEADBEEFL), "getAndAdd long value");
 738             }
 739         }
 740     }
 741 
 742     static void testArrayUnsupported(VarHandle vh) {
 743         long[] array = new long[10];
 744 
 745         int i = 0;
 746 
 747     }
 748 
 749     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 750         long[] array = new long[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                 long x = (long) vh.get(array, ci);
 757             });
 758 
 759             checkIOOBE(() -> {
 760                 vh.set(array, ci, 0x0123456789ABCDEFL);
 761             });
 762 
 763             checkIOOBE(() -> {
 764                 long x = (long) vh.getVolatile(array, ci);
 765             });
 766 
 767             checkIOOBE(() -> {
 768                 vh.setVolatile(array, ci, 0x0123456789ABCDEFL);
 769             });
 770 
 771             checkIOOBE(() -> {
 772                 long x = (long) vh.getAcquire(array, ci);
 773             });
 774 
 775             checkIOOBE(() -> {
 776                 vh.setRelease(array, ci, 0x0123456789ABCDEFL);
 777             });
 778 
 779             checkIOOBE(() -> {
 780                 long x = (long) vh.getOpaque(array, ci);
 781             });
 782 
 783             checkIOOBE(() -> {
 784                 vh.setOpaque(array, ci, 0x0123456789ABCDEFL);
 785             });
 786 
 787             checkIOOBE(() -> {
 788                 boolean r = vh.compareAndSet(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 789             });
 790 
 791             checkIOOBE(() -> {
 792                 long r = (long) vh.compareAndExchangeVolatile(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 793             });
 794 
 795             checkIOOBE(() -> {
 796                 long r = (long) vh.compareAndExchangeAcquire(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 797             });
 798 
 799             checkIOOBE(() -> {
 800                 long r = (long) vh.compareAndExchangeRelease(array, ci, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 801             });
 802 
 803             checkIOOBE(() -> {
 804                 boolean r = vh.weakCompareAndSet(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 805             });
 806 
 807             checkIOOBE(() -> {
 808                 boolean r = vh.weakCompareAndSetAcquire(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 809             });
 810 
 811             checkIOOBE(() -> {
 812                 boolean r = vh.weakCompareAndSetRelease(array, ci, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 813             });
 814 
 815             checkIOOBE(() -> {
 816                 long o = (long) vh.getAndSet(array, ci, 0x0123456789ABCDEFL);
 817             });
 818 
 819             checkIOOBE(() -> {
 820                 long o = (long) vh.getAndAdd(array, ci, 0xDEADBEEFDEADBEEFL);
 821             });
 822 
 823             checkIOOBE(() -> {
 824                 long o = (long) vh.addAndGet(array, ci, 0xDEADBEEFDEADBEEFL);
 825             });
 826         }
 827     }
 828 }
 829 
< prev index next >