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