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