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