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