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  * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
  27  *          to hit compilation thresholds
  28  * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 VarHandleTestMethodHandleAccessShort
  29  */
  30 
  31 import java.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.invoke.VarHandle;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 
  37 import static org.junit.jupiter.api.Assertions.*;
  38 import org.junit.jupiter.api.BeforeAll;
  39 import org.junit.jupiter.api.TestInstance;
  40 import org.junit.jupiter.params.ParameterizedTest;
  41 import org.junit.jupiter.params.provider.MethodSource;
  42 
  43 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
  44 public class VarHandleTestMethodHandleAccessShort 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     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63     @BeforeAll
  64     public void setup() throws Exception {
  65         vhFinalField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestMethodHandleAccessShort.class, "final_v", short.class);
  67 
  68         vhField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestMethodHandleAccessShort.class, "v", short.class);
  70 
  71         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  72             VarHandleTestMethodHandleAccessShort.class, "static_final_v", short.class);
  73 
  74         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  75             VarHandleTestMethodHandleAccessShort.class, "static_v", short.class);
  76 
  77         vhArray = MethodHandles.arrayElementVarHandle(short[].class);
  78     }
  79 
  80     public Object[][] accessTestCaseProvider() throws Exception {
  81         List<AccessTestCase<?>> cases = new ArrayList<>();
  82 
  83         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
  84             cases.add(new MethodHandleAccessTestCase("Instance field",
  85                                                      vhField, f, hs -> testInstanceField(this, hs)));
  86             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
  87                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
  88                                                      false));
  89 
  90             cases.add(new MethodHandleAccessTestCase("Static field",
  91                                                      vhStaticField, f, VarHandleTestMethodHandleAccessShort::testStaticField));
  92             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
  93                                                      vhStaticField, f, VarHandleTestMethodHandleAccessShort::testStaticFieldUnsupported,
  94                                                      false));
  95 
  96             cases.add(new MethodHandleAccessTestCase("Array",
  97                                                      vhArray, f, VarHandleTestMethodHandleAccessShort::testArray));
  98             cases.add(new MethodHandleAccessTestCase("Array unsupported",
  99                                                      vhArray, f, VarHandleTestMethodHandleAccessShort::testArrayUnsupported,
 100                                                      false));
 101             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
 102                                                      vhArray, f, VarHandleTestMethodHandleAccessShort::testArrayIndexOutOfBounds,
 103                                                      false));
 104         }
 105 
 106         // Work around issue with jtreg summary reporting which truncates
 107         // the String result of Object.toString to 30 characters, hence
 108         // the first dummy argument
 109         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 110     }
 111 
 112     @ParameterizedTest
 113     @MethodSource("accessTestCaseProvider")
 114     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 115         T t = atc.get();
 116         int iters = atc.requiresLoop() ? ITERS : 1;
 117         for (int c = 0; c < iters; c++) {
 118             atc.testAccess(t);
 119         }
 120     }
 121 
 122     static void testInstanceField(VarHandleTestMethodHandleAccessShort recv, Handles hs) throws Throwable {
 123         // Plain
 124         {
 125             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 126             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 127             assertEquals((short)0x0123, x, "set short value");
 128         }
 129 
 130 
 131         // Volatile
 132         {
 133             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, (short)0x4567);
 134             short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
 135             assertEquals((short)0x4567, x, "setVolatile short value");
 136         }
 137 
 138         // Lazy
 139         {
 140             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, (short)0x0123);
 141             short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
 142             assertEquals((short)0x0123, x, "setRelease short value");
 143         }
 144 
 145         // Opaque
 146         {
 147             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, (short)0x4567);
 148             short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
 149             assertEquals((short)0x4567, x, "setOpaque short value");
 150         }
 151 
 152         hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 153 
 154         // Compare
 155         {
 156             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, (short)0x0123, (short)0x4567);
 157             assertEquals(r, true, "success compareAndSet short");
 158             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 159             assertEquals((short)0x4567, x, "success compareAndSet short value");
 160         }
 161 
 162         {
 163             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, (short)0x0123, (short)0x89AB);
 164             assertEquals(r, false, "failing compareAndSet short");
 165             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 166             assertEquals((short)0x4567, x, "failing compareAndSet short value");
 167         }
 168 
 169         {
 170             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, (short)0x4567, (short)0x0123);
 171             assertEquals(r, (short)0x4567, "success compareAndExchange short");
 172             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 173             assertEquals((short)0x0123, x, "success compareAndExchange short value");
 174         }
 175 
 176         {
 177             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, (short)0x4567, (short)0x89AB);
 178             assertEquals(r, (short)0x0123, "failing compareAndExchange short");
 179             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 180             assertEquals((short)0x0123, x, "failing compareAndExchange short value");
 181         }
 182 
 183         {
 184             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, (short)0x0123, (short)0x4567);
 185             assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short");
 186             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 187             assertEquals((short)0x4567, x, "success compareAndExchangeAcquire short value");
 188         }
 189 
 190         {
 191             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, (short)0x0123, (short)0x89AB);
 192             assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short");
 193             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 194             assertEquals((short)0x4567, x, "failing compareAndExchangeAcquire short value");
 195         }
 196 
 197         {
 198             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, (short)0x4567, (short)0x0123);
 199             assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short");
 200             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 201             assertEquals((short)0x0123, x, "success compareAndExchangeRelease short value");
 202         }
 203 
 204         {
 205             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, (short)0x4567, (short)0x89AB);
 206             assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short");
 207             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 208             assertEquals((short)0x0123, x, "failing compareAndExchangeRelease short value");
 209         }
 210 
 211         {
 212             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
 213             boolean success = false;
 214             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 215                 success = (boolean) mh.invokeExact(recv, (short)0x0123, (short)0x4567);
 216                 if (!success) weakDelay();
 217             }
 218             assertEquals(success, true, "success weakCompareAndSetPlain short");
 219             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 220             assertEquals((short)0x4567, x, "success weakCompareAndSetPlain short value");
 221         }
 222 
 223         {
 224             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, (short)0x0123, (short)0x89AB);
 225             assertEquals(success, false, "failing weakCompareAndSetPlain short");
 226             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 227             assertEquals((short)0x4567, x, "failing weakCompareAndSetPlain short value");
 228         }
 229 
 230         {
 231             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 232             boolean success = false;
 233             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 234                 success = (boolean) mh.invokeExact(recv, (short)0x4567, (short)0x0123);
 235                 if (!success) weakDelay();
 236             }
 237             assertEquals(success, true, "success weakCompareAndSetAcquire short");
 238             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 239             assertEquals((short)0x0123, x, "success weakCompareAndSetAcquire short");
 240         }
 241 
 242         {
 243             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, (short)0x4567, (short)0x89AB);
 244             assertEquals(success, false, "failing weakCompareAndSetAcquire short");
 245             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 246             assertEquals((short)0x0123, x, "failing weakCompareAndSetAcquire short value");
 247         }
 248 
 249         {
 250             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
 251             boolean success = false;
 252             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 253                 success = (boolean) mh.invokeExact(recv, (short)0x0123, (short)0x4567);
 254                 if (!success) weakDelay();
 255             }
 256             assertEquals(success, true, "success weakCompareAndSetRelease short");
 257             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 258             assertEquals((short)0x4567, x, "success weakCompareAndSetRelease short");
 259         }
 260 
 261         {
 262             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, (short)0x0123, (short)0x89AB);
 263             assertEquals(success, false, "failing weakCompareAndSetRelease short");
 264             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 265             assertEquals((short)0x4567, x, "failing weakCompareAndSetRelease short value");
 266         }
 267 
 268         {
 269             boolean success = false;
 270             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
 271             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 272                 success = (boolean) mh.invokeExact(recv, (short)0x4567, (short)0x0123);
 273                 if (!success) weakDelay();
 274             }
 275             assertEquals(success, true, "success weakCompareAndSet short");
 276             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 277             assertEquals((short)0x0123, x, "success weakCompareAndSet short");
 278         }
 279 
 280         {
 281             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, (short)0x4567, (short)0x89AB);
 282             assertEquals(success, false, "failing weakCompareAndSet short");
 283             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 284             assertEquals((short)0x0123, x, "failing weakCompareAndSet short value");
 285         }
 286 
 287         // Compare set and get
 288         {
 289             short o = (short) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, (short)0x4567);
 290             assertEquals((short)0x0123, o, "getAndSet short");
 291             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 292             assertEquals((short)0x4567, x, "getAndSet short value");
 293         }
 294 
 295         // get and add, add and get
 296         {
 297             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 298 
 299             short o = (short) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, (short)0x4567);
 300             assertEquals((short)0x0123, o, "getAndAdd short");
 301             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 302             assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAdd short value");
 303         }
 304 
 305         {
 306             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 307 
 308             short o = (short) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, (short)0x4567);
 309             assertEquals((short)0x0123, o, "getAndAddAcquire short");
 310             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 311             assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAddAcquire short value");
 312         }
 313 
 314         {
 315             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 316 
 317             short o = (short) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, (short)0x4567);
 318             assertEquals((short)0x0123, o, "getAndAddRelease short");
 319             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 320             assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAddRelease short value");
 321         }
 322 
 323         // get and bitwise or
 324         {
 325             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 326 
 327             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(recv, (short)0x4567);
 328             assertEquals((short)0x0123, o, "getAndBitwiseOr short");
 329             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 330             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOr short value");
 331         }
 332 
 333         {
 334             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 335 
 336             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(recv, (short)0x4567);
 337             assertEquals((short)0x0123, o, "getAndBitwiseOrAcquire short");
 338             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 339             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOrAcquire short value");
 340         }
 341 
 342         {
 343             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 344 
 345             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(recv, (short)0x4567);
 346             assertEquals((short)0x0123, o, "getAndBitwiseOrRelease short");
 347             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 348             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOrRelease short value");
 349         }
 350 
 351         // get and bitwise and
 352         {
 353             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 354 
 355             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(recv, (short)0x4567);
 356             assertEquals((short)0x0123, o, "getAndBitwiseAnd short");
 357             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 358             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAnd short value");
 359         }
 360 
 361         {
 362             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 363 
 364             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(recv, (short)0x4567);
 365             assertEquals((short)0x0123, o, "getAndBitwiseAndAcquire short");
 366             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 367             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAndAcquire short value");
 368         }
 369 
 370         {
 371             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 372 
 373             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(recv, (short)0x4567);
 374             assertEquals((short)0x0123, o, "getAndBitwiseAndRelease short");
 375             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 376             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAndRelease short value");
 377         }
 378 
 379         // get and bitwise xor
 380         {
 381             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 382 
 383             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(recv, (short)0x4567);
 384             assertEquals((short)0x0123, o, "getAndBitwiseXor short");
 385             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 386             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXor short value");
 387         }
 388 
 389         {
 390             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 391 
 392             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(recv, (short)0x4567);
 393             assertEquals((short)0x0123, o, "getAndBitwiseXorAcquire short");
 394             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 395             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXorAcquire short value");
 396         }
 397 
 398         {
 399             hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123);
 400 
 401             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, (short)0x4567);
 402             assertEquals((short)0x0123, o, "getAndBitwiseXorRelease short");
 403             short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv);
 404             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXorRelease short value");
 405         }
 406     }
 407 
 408     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessShort recv, Handles hs) throws Throwable {
 409 
 410 
 411     }
 412 
 413 
 414     static void testStaticField(Handles hs) throws Throwable {
 415         // Plain
 416         {
 417             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 418             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 419             assertEquals((short)0x0123, x, "set short value");
 420         }
 421 
 422 
 423         // Volatile
 424         {
 425             hs.get(TestAccessMode.SET_VOLATILE).invokeExact((short)0x4567);
 426             short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 427             assertEquals((short)0x4567, x, "setVolatile short value");
 428         }
 429 
 430         // Lazy
 431         {
 432             hs.get(TestAccessMode.SET_RELEASE).invokeExact((short)0x0123);
 433             short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
 434             assertEquals((short)0x0123, x, "setRelease short value");
 435         }
 436 
 437         // Opaque
 438         {
 439             hs.get(TestAccessMode.SET_OPAQUE).invokeExact((short)0x4567);
 440             short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
 441             assertEquals((short)0x4567, x, "setOpaque short value");
 442         }
 443 
 444         hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 445 
 446         // Compare
 447         {
 448             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact((short)0x0123, (short)0x4567);
 449             assertEquals(r, true, "success compareAndSet short");
 450             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 451             assertEquals((short)0x4567, x, "success compareAndSet short value");
 452         }
 453 
 454         {
 455             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact((short)0x0123, (short)0x89AB);
 456             assertEquals(r, false, "failing compareAndSet short");
 457             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 458             assertEquals((short)0x4567, x, "failing compareAndSet short value");
 459         }
 460 
 461         {
 462             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact((short)0x4567, (short)0x0123);
 463             assertEquals(r, (short)0x4567, "success compareAndExchange short");
 464             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 465             assertEquals((short)0x0123, x, "success compareAndExchange short value");
 466         }
 467 
 468         {
 469             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact((short)0x4567, (short)0x89AB);
 470             assertEquals(r, (short)0x0123, "failing compareAndExchange short");
 471             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 472             assertEquals((short)0x0123, x, "failing compareAndExchange short value");
 473         }
 474 
 475         {
 476             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact((short)0x0123, (short)0x4567);
 477             assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short");
 478             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 479             assertEquals((short)0x4567, x, "success compareAndExchangeAcquire short value");
 480         }
 481 
 482         {
 483             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact((short)0x0123, (short)0x89AB);
 484             assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short");
 485             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 486             assertEquals((short)0x4567, x, "failing compareAndExchangeAcquire short value");
 487         }
 488 
 489         {
 490             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact((short)0x4567, (short)0x0123);
 491             assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short");
 492             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 493             assertEquals((short)0x0123, x, "success compareAndExchangeRelease short value");
 494         }
 495 
 496         {
 497             short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact((short)0x4567, (short)0x89AB);
 498             assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short");
 499             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 500             assertEquals((short)0x0123, x, "failing compareAndExchangeRelease short value");
 501         }
 502 
 503         {
 504             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
 505             boolean success = false;
 506             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 507                 success = (boolean) mh.invokeExact((short)0x0123, (short)0x4567);
 508                 if (!success) weakDelay();
 509             }
 510             assertEquals(success, true, "success weakCompareAndSetPlain short");
 511             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 512             assertEquals((short)0x4567, x, "success weakCompareAndSetPlain short value");
 513         }
 514 
 515         {
 516             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact((short)0x0123, (short)0x89AB);
 517             assertEquals(success, false, "failing weakCompareAndSetPlain short");
 518             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 519             assertEquals((short)0x4567, x, "failing weakCompareAndSetPlain short value");
 520         }
 521 
 522         {
 523             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 524             boolean success = false;
 525             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 526                 success = (boolean) mh.invokeExact((short)0x4567, (short)0x0123);
 527                 if (!success) weakDelay();
 528             }
 529             assertEquals(success, true, "success weakCompareAndSetAcquire short");
 530             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 531             assertEquals((short)0x0123, x, "success weakCompareAndSetAcquire short");
 532         }
 533 
 534         {
 535             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 536             boolean success = (boolean) mh.invokeExact((short)0x4567, (short)0x89AB);
 537             assertEquals(success, false, "failing weakCompareAndSetAcquire short");
 538             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 539             assertEquals((short)0x0123, x, "failing weakCompareAndSetAcquire short value");
 540         }
 541 
 542         {
 543             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
 544             boolean success = false;
 545             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 546                 success = (boolean) mh.invokeExact((short)0x0123, (short)0x4567);
 547                 if (!success) weakDelay();
 548             }
 549             assertEquals(success, true, "success weakCompareAndSetRelease short");
 550             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 551             assertEquals((short)0x4567, x, "success weakCompareAndSetRelease short");
 552         }
 553 
 554         {
 555             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact((short)0x0123, (short)0x89AB);
 556             assertEquals(success, false, "failing weakCompareAndSetRelease short");
 557             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 558             assertEquals((short)0x4567, x, "failing weakCompareAndSetRelease short value");
 559         }
 560 
 561         {
 562             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
 563             boolean success = false;
 564             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 565                 success = (boolean) mh.invokeExact((short)0x4567, (short)0x0123);
 566                 if (!success) weakDelay();
 567             }
 568             assertEquals(success, true, "success weakCompareAndSet short");
 569             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 570             assertEquals((short)0x0123, x, "success weakCompareAndSet short");
 571         }
 572 
 573         {
 574             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact((short)0x4567, (short)0x89AB);
 575             assertEquals(success, false, "failing weakCompareAndSet short");
 576             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 577             assertEquals((short)0x0123, x, "failing weakCompareAndSetRe short value");
 578         }
 579 
 580         // Compare set and get
 581         {
 582             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 583 
 584             short o = (short) hs.get(TestAccessMode.GET_AND_SET).invokeExact((short)0x4567);
 585             assertEquals((short)0x0123, o, "getAndSet short");
 586             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 587             assertEquals((short)0x4567, x, "getAndSet short value");
 588         }
 589 
 590         // Compare set and get
 591         {
 592             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 593 
 594             short o = (short) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact((short)0x4567);
 595             assertEquals((short)0x0123, o, "getAndSetAcquire short");
 596             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 597             assertEquals((short)0x4567, x, "getAndSetAcquire short value");
 598         }
 599 
 600         // Compare set and get
 601         {
 602             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 603 
 604             short o = (short) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact((short)0x4567);
 605             assertEquals((short)0x0123, o, "getAndSetRelease short");
 606             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 607             assertEquals((short)0x4567, x, "getAndSetRelease short value");
 608         }
 609 
 610         // get and add, add and get
 611         {
 612             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 613 
 614             short o = (short) hs.get(TestAccessMode.GET_AND_ADD).invokeExact((short)0x4567);
 615             assertEquals((short)0x0123, o, "getAndAdd short");
 616             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 617             assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAdd short value");
 618         }
 619 
 620         {
 621             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 622 
 623             short o = (short) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact((short)0x4567);
 624             assertEquals((short)0x0123, o, "getAndAddAcquire short");
 625             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 626             assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAddAcquire short value");
 627         }
 628 
 629         {
 630             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 631 
 632             short o = (short) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact((short)0x4567);
 633             assertEquals((short)0x0123, o, "getAndAddRelease short");
 634             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 635             assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAddRelease short value");
 636         }
 637 
 638         // get and bitwise or
 639         {
 640             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 641 
 642             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact((short)0x4567);
 643             assertEquals((short)0x0123, o, "getAndBitwiseOr short");
 644             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 645             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOr short value");
 646         }
 647 
 648         {
 649             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 650 
 651             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact((short)0x4567);
 652             assertEquals((short)0x0123, o, "getAndBitwiseOrAcquire short");
 653             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 654             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOrAcquire short value");
 655         }
 656 
 657         {
 658             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 659 
 660             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact((short)0x4567);
 661             assertEquals((short)0x0123, o, "getAndBitwiseOrRelease short");
 662             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 663             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOrRelease short value");
 664         }
 665 
 666         // get and bitwise and
 667         {
 668             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 669 
 670             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact((short)0x4567);
 671             assertEquals((short)0x0123, o, "getAndBitwiseAnd short");
 672             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 673             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAnd short value");
 674         }
 675 
 676         {
 677             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 678 
 679             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact((short)0x4567);
 680             assertEquals((short)0x0123, o, "getAndBitwiseAndAcquire short");
 681             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 682             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAndAcquire short value");
 683         }
 684 
 685         {
 686             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 687 
 688             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact((short)0x4567);
 689             assertEquals((short)0x0123, o, "getAndBitwiseAndRelease short");
 690             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 691             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAndRelease short value");
 692         }
 693 
 694         // get and bitwise xor
 695         {
 696             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 697 
 698             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact((short)0x4567);
 699             assertEquals((short)0x0123, o, "getAndBitwiseXor short");
 700             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 701             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXor short value");
 702         }
 703 
 704         {
 705             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 706 
 707             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact((short)0x4567);
 708             assertEquals((short)0x0123, o, "getAndBitwiseXorAcquire short");
 709             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 710             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXorAcquire short value");
 711         }
 712 
 713         {
 714             hs.get(TestAccessMode.SET).invokeExact((short)0x0123);
 715 
 716             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact((short)0x4567);
 717             assertEquals((short)0x0123, o, "getAndBitwiseXorRelease short");
 718             short x = (short) hs.get(TestAccessMode.GET).invokeExact();
 719             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXorRelease short value");
 720         }
 721     }
 722 
 723     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
 724 
 725 
 726     }
 727 
 728 
 729     static void testArray(Handles hs) throws Throwable {
 730         short[] array = new short[10];
 731 
 732         for (int i = 0; i < array.length; i++) {
 733             // Plain
 734             {
 735                 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 736                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 737                 assertEquals((short)0x0123, x, "get short value");
 738             }
 739 
 740 
 741             // Volatile
 742             {
 743                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, (short)0x4567);
 744                 short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
 745                 assertEquals((short)0x4567, x, "setVolatile short value");
 746             }
 747 
 748             // Lazy
 749             {
 750                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, (short)0x0123);
 751                 short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
 752                 assertEquals((short)0x0123, x, "setRelease short value");
 753             }
 754 
 755             // Opaque
 756             {
 757                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, (short)0x4567);
 758                 short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
 759                 assertEquals((short)0x4567, x, "setOpaque short value");
 760             }
 761 
 762             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 763 
 764             // Compare
 765             {
 766                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, (short)0x0123, (short)0x4567);
 767                 assertEquals(r, true, "success compareAndSet short");
 768                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 769                 assertEquals((short)0x4567, x, "success compareAndSet short value");
 770             }
 771 
 772             {
 773                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, (short)0x0123, (short)0x89AB);
 774                 assertEquals(r, false, "failing compareAndSet short");
 775                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 776                 assertEquals((short)0x4567, x, "failing compareAndSet short value");
 777             }
 778 
 779             {
 780                 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, (short)0x4567, (short)0x0123);
 781                 assertEquals(r, (short)0x4567, "success compareAndExchange short");
 782                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 783                 assertEquals((short)0x0123, x, "success compareAndExchange short value");
 784             }
 785 
 786             {
 787                 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, (short)0x4567, (short)0x89AB);
 788                 assertEquals(r, (short)0x0123, "failing compareAndExchange short");
 789                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 790                 assertEquals((short)0x0123, x, "failing compareAndExchange short value");
 791             }
 792 
 793             {
 794                 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, (short)0x0123, (short)0x4567);
 795                 assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short");
 796                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 797                 assertEquals((short)0x4567, x, "success compareAndExchangeAcquire short value");
 798             }
 799 
 800             {
 801                 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, (short)0x0123, (short)0x89AB);
 802                 assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short");
 803                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 804                 assertEquals((short)0x4567, x, "failing compareAndExchangeAcquire short value");
 805             }
 806 
 807             {
 808                 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, (short)0x4567, (short)0x0123);
 809                 assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short");
 810                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 811                 assertEquals((short)0x0123, x, "success compareAndExchangeRelease short value");
 812             }
 813 
 814             {
 815                 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, (short)0x4567, (short)0x89AB);
 816                 assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short");
 817                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 818                 assertEquals((short)0x0123, x, "failing compareAndExchangeRelease short value");
 819             }
 820 
 821             {
 822                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
 823                 boolean success = false;
 824                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 825                     success = (boolean) mh.invokeExact(array, i, (short)0x0123, (short)0x4567);
 826                     if (!success) weakDelay();
 827                 }
 828                 assertEquals(success, true, "success weakCompareAndSetPlain short");
 829                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 830                 assertEquals((short)0x4567, x, "success weakCompareAndSetPlain short value");
 831             }
 832 
 833             {
 834                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, (short)0x0123, (short)0x89AB);
 835                 assertEquals(success, false, "failing weakCompareAndSetPlain short");
 836                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 837                 assertEquals((short)0x4567, x, "failing weakCompareAndSetPlain short value");
 838             }
 839 
 840             {
 841                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
 842                 boolean success = false;
 843                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 844                     success = (boolean) mh.invokeExact(array, i, (short)0x4567, (short)0x0123);
 845                     if (!success) weakDelay();
 846                 }
 847                 assertEquals(success, true, "success weakCompareAndSetAcquire short");
 848                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 849                 assertEquals((short)0x0123, x, "success weakCompareAndSetAcquire short");
 850             }
 851 
 852             {
 853                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, (short)0x4567, (short)0x89AB);
 854                 assertEquals(success, false, "failing weakCompareAndSetAcquire short");
 855                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 856                 assertEquals((short)0x0123, x, "failing weakCompareAndSetAcquire short value");
 857             }
 858 
 859             {
 860                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
 861                 boolean success = false;
 862                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 863                     success = (boolean) mh.invokeExact(array, i, (short)0x0123, (short)0x4567);
 864                     if (!success) weakDelay();
 865                 }
 866                 assertEquals(success, true, "success weakCompareAndSetRelease short");
 867                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 868                 assertEquals((short)0x4567, x, "success weakCompareAndSetRelease short");
 869             }
 870 
 871             {
 872                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, (short)0x0123, (short)0x89AB);
 873                 assertEquals(success, false, "failing weakCompareAndSetAcquire short");
 874                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 875                 assertEquals((short)0x4567, x, "failing weakCompareAndSetAcquire short value");
 876             }
 877 
 878             {
 879                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
 880                 boolean success = false;
 881                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 882                     success = (boolean) mh.invokeExact(array, i, (short)0x4567, (short)0x0123);
 883                     if (!success) weakDelay();
 884                 }
 885                 assertEquals(success, true, "success weakCompareAndSet short");
 886                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 887                 assertEquals((short)0x0123, x, "success weakCompareAndSet short");
 888             }
 889 
 890             {
 891                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, (short)0x4567, (short)0x89AB);
 892                 assertEquals(success, false, "failing weakCompareAndSet short");
 893                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 894                 assertEquals((short)0x0123, x, "failing weakCompareAndSet short value");
 895             }
 896 
 897             // Compare set and get
 898             {
 899                 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 900 
 901                 short o = (short) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, (short)0x4567);
 902                 assertEquals((short)0x0123, o, "getAndSet short");
 903                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 904                 assertEquals((short)0x4567, x, "getAndSet short value");
 905             }
 906 
 907             {
 908                 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 909 
 910                 short o = (short) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, (short)0x4567);
 911                 assertEquals((short)0x0123, o, "getAndSetAcquire short");
 912                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 913                 assertEquals((short)0x4567, x, "getAndSetAcquire short value");
 914             }
 915 
 916             {
 917                 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 918 
 919                 short o = (short) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, (short)0x4567);
 920                 assertEquals((short)0x0123, o, "getAndSetRelease short");
 921                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 922                 assertEquals((short)0x4567, x, "getAndSetRelease short value");
 923             }
 924 
 925             // get and add, add and get
 926             {
 927                 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 928 
 929                 short o = (short) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, (short)0x4567);
 930                 assertEquals((short)0x0123, o, "getAndAdd short");
 931                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 932                 assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAdd short value");
 933             }
 934 
 935             {
 936                 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 937 
 938                 short o = (short) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, (short)0x4567);
 939                 assertEquals((short)0x0123, o, "getAndAddAcquire short");
 940                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 941                 assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAddAcquire short value");
 942             }
 943 
 944             {
 945                 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 946 
 947                 short o = (short) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, (short)0x4567);
 948                 assertEquals((short)0x0123, o, "getAndAddRelease short");
 949                 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 950                 assertEquals((short)((short)0x0123 + (short)0x4567), x, "getAndAddRelease short value");
 951             }
 952 
 953         // get and bitwise or
 954         {
 955             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 956 
 957             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(array, i, (short)0x4567);
 958             assertEquals((short)0x0123, o, "getAndBitwiseOr short");
 959             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 960             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOr short value");
 961         }
 962 
 963         {
 964             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 965 
 966             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(array, i, (short)0x4567);
 967             assertEquals((short)0x0123, o, "getAndBitwiseOrAcquire short");
 968             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 969             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOrAcquire short value");
 970         }
 971 
 972         {
 973             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 974 
 975             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(array, i, (short)0x4567);
 976             assertEquals((short)0x0123, o, "getAndBitwiseOrRelease short");
 977             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 978             assertEquals((short)((short)0x0123 | (short)0x4567), x, "getAndBitwiseOrRelease short value");
 979         }
 980 
 981         // get and bitwise and
 982         {
 983             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 984 
 985             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(array, i, (short)0x4567);
 986             assertEquals((short)0x0123, o, "getAndBitwiseAnd short");
 987             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 988             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAnd short value");
 989         }
 990 
 991         {
 992             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
 993 
 994             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(array, i, (short)0x4567);
 995             assertEquals((short)0x0123, o, "getAndBitwiseAndAcquire short");
 996             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
 997             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAndAcquire short value");
 998         }
 999 
1000         {
1001             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
1002 
1003             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(array, i, (short)0x4567);
1004             assertEquals((short)0x0123, o, "getAndBitwiseAndRelease short");
1005             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
1006             assertEquals((short)((short)0x0123 & (short)0x4567), x, "getAndBitwiseAndRelease short value");
1007         }
1008 
1009         // get and bitwise xor
1010         {
1011             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
1012 
1013             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(array, i, (short)0x4567);
1014             assertEquals((short)0x0123, o, "getAndBitwiseXor short");
1015             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
1016             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXor short value");
1017         }
1018 
1019         {
1020             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
1021 
1022             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(array, i, (short)0x4567);
1023             assertEquals((short)0x0123, o, "getAndBitwiseXorAcquire short");
1024             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
1025             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXorAcquire short value");
1026         }
1027 
1028         {
1029             hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123);
1030 
1031             short o = (short) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(array, i, (short)0x4567);
1032             assertEquals((short)0x0123, o, "getAndBitwiseXorRelease short");
1033             short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i);
1034             assertEquals((short)((short)0x0123 ^ (short)0x4567), x, "getAndBitwiseXorRelease short value");
1035         }
1036         }
1037     }
1038 
1039     static void testArrayUnsupported(Handles hs) throws Throwable {
1040         short[] array = new short[10];
1041 
1042         final int i = 0;
1043 
1044 
1045     }
1046 
1047     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
1048         short[] array = new short[10];
1049 
1050         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1051             final int ci = i;
1052 
1053             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
1054                 checkAIOOBE(am, () -> {
1055                     short x = (short) hs.get(am).invokeExact(array, ci);
1056                 });
1057             }
1058 
1059             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
1060                 checkAIOOBE(am, () -> {
1061                     hs.get(am).invokeExact(array, ci, (short)0x0123);
1062                 });
1063             }
1064 
1065             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1066                 checkAIOOBE(am, () -> {
1067                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, (short)0x0123, (short)0x4567);
1068                 });
1069             }
1070 
1071             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1072                 checkAIOOBE(am, () -> {
1073                     short r = (short) hs.get(am).invokeExact(array, ci, (short)0x4567, (short)0x0123);
1074                 });
1075             }
1076 
1077             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
1078                 checkAIOOBE(am, () -> {
1079                     short o = (short) hs.get(am).invokeExact(array, ci, (short)0x0123);
1080                 });
1081             }
1082 
1083             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
1084                 checkAIOOBE(am, () -> {
1085                     short o = (short) hs.get(am).invokeExact(array, ci, (short)0x89AB);
1086                 });
1087             }
1088 
1089             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
1090                 checkAIOOBE(am, () -> {
1091                     short o = (short) hs.get(am).invokeExact(array, ci, (short)0x89AB);
1092                 });
1093             }
1094         }
1095     }
1096 }
1097