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