1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessChar
  27  */
  28 
  29 import org.testng.annotations.BeforeClass;
  30 import org.testng.annotations.DataProvider;
  31 import org.testng.annotations.Test;
  32 
  33 import java.lang.invoke.MethodHandles;
  34 import java.lang.invoke.VarHandle;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.List;
  38 
  39 import static org.testng.Assert.*;
  40 
  41 public class VarHandleTestMethodHandleAccessChar extends VarHandleBaseTest {
  42     static final char static_final_v = '\u0123';
  43 
  44     static char static_v;
  45 
  46     final char final_v = '\u0123';
  47 
  48     char v;
  49 
  50     VarHandle vhFinalField;
  51 
  52     VarHandle vhField;
  53 
  54     VarHandle vhStaticField;
  55 
  56     VarHandle vhStaticFinalField;
  57 
  58     VarHandle vhArray;
  59 
  60     @BeforeClass
  61     public void setup() throws Exception {
  62         vhFinalField = MethodHandles.lookup().findVarHandle(
  63                 VarHandleTestMethodHandleAccessChar.class, "final_v", char.class);
  64 
  65         vhField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestMethodHandleAccessChar.class, "v", char.class);
  67 
  68         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  69             VarHandleTestMethodHandleAccessChar.class, "static_final_v", char.class);
  70 
  71         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  72             VarHandleTestMethodHandleAccessChar.class, "static_v", char.class);
  73 
  74         vhArray = MethodHandles.arrayElementVarHandle(char[].class);
  75     }
  76 
  77 
  78     @DataProvider
  79     public Object[][] accessTestCaseProvider() throws Exception {
  80         List<AccessTestCase<?>> cases = new ArrayList<>();
  81 
  82         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
  83             cases.add(new MethodHandleAccessTestCase("Instance field",
  84                                                      vhField, f, hs -> testInstanceField(this, hs)));
  85             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
  86                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
  87                                                      false));
  88 
  89             cases.add(new MethodHandleAccessTestCase("Static field",
  90                                                      vhStaticField, f, VarHandleTestMethodHandleAccessChar::testStaticField));
  91             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
  92                                                      vhStaticField, f, VarHandleTestMethodHandleAccessChar::testStaticFieldUnsupported,
  93                                                      false));
  94 
  95             cases.add(new MethodHandleAccessTestCase("Array",
  96                                                      vhArray, f, VarHandleTestMethodHandleAccessChar::testArray));
  97             cases.add(new MethodHandleAccessTestCase("Array unsupported",
  98                                                      vhArray, f, VarHandleTestMethodHandleAccessChar::testArrayUnsupported,
  99                                                      false));
 100             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
 101                                                      vhArray, f, VarHandleTestMethodHandleAccessChar::testArrayIndexOutOfBounds,
 102                                                      false));
 103         }
 104 
 105         // Work around issue with jtreg summary reporting which truncates
 106         // the String result of Object.toString to 30 characters, hence
 107         // the first dummy argument
 108         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 109     }
 110 
 111     @Test(dataProvider = "accessTestCaseProvider")
 112     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 113         T t = atc.get();
 114         int iters = atc.requiresLoop() ? ITERS : 1;
 115         for (int c = 0; c < iters; c++) {
 116             atc.testAccess(t);
 117         }
 118     }
 119 
 120 
 121     static void testInstanceField(VarHandleTestMethodHandleAccessChar recv, Handles hs) throws Throwable {
 122         // Plain
 123         {
 124             hs.get(TestAccessMode.SET).invokeExact(recv, '\u0123');
 125             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 126             assertEquals(x, '\u0123', "set char value");
 127         }
 128 
 129 
 130         // Volatile
 131         {
 132             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, '\u4567');
 133             char x = (char) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
 134             assertEquals(x, '\u4567', "setVolatile char value");
 135         }
 136 
 137         // Lazy
 138         {
 139             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, '\u0123');
 140             char x = (char) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
 141             assertEquals(x, '\u0123', "setRelease char value");
 142         }
 143 
 144         // Opaque
 145         {
 146             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, '\u4567');
 147             char x = (char) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
 148             assertEquals(x, '\u4567', "setOpaque char value");
 149         }
 150 
 151         hs.get(TestAccessMode.SET).invokeExact(recv, '\u0123');
 152 
 153         // Compare
 154         {
 155             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, '\u0123', '\u4567');
 156             assertEquals(r, true, "success compareAndSet char");
 157             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 158             assertEquals(x, '\u4567', "success compareAndSet char value");
 159         }
 160 
 161         {
 162             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, '\u0123', '\u89AB');
 163             assertEquals(r, false, "failing compareAndSet char");
 164             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 165             assertEquals(x, '\u4567', "failing compareAndSet char value");
 166         }
 167 
 168         {
 169             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, '\u4567', '\u0123');
 170             assertEquals(r, '\u4567', "success compareAndExchangeVolatile char");
 171             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 172             assertEquals(x, '\u0123', "success compareAndExchangeVolatile char value");
 173         }
 174 
 175         {
 176             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, '\u4567', '\u89AB');
 177             assertEquals(r, '\u0123', "failing compareAndExchangeVolatile char");
 178             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 179             assertEquals(x, '\u0123', "failing compareAndExchangeVolatile char value");
 180         }
 181 
 182         {
 183             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, '\u0123', '\u4567');
 184             assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 185             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 186             assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 187         }
 188 
 189         {
 190             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, '\u0123', '\u89AB');
 191             assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 192             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 193             assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 194         }
 195 
 196         {
 197             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, '\u4567', '\u0123');
 198             assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 199             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 200             assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 201         }
 202 
 203         {
 204             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, '\u4567', '\u89AB');
 205             assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 206             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 207             assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 208         }
 209 
 210         {
 211             boolean success = false;
 212             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 213                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, '\u0123', '\u4567');
 214             }
 215             assertEquals(success, true, "weakCompareAndSet char");
 216             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 217             assertEquals(x, '\u4567', "weakCompareAndSet char value");
 218         }
 219 
 220         {
 221             boolean success = false;
 222             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 223                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, '\u4567', '\u0123');
 224             }
 225             assertEquals(success, true, "weakCompareAndSetAcquire char");
 226             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 227             assertEquals(x, '\u0123', "weakCompareAndSetAcquire char");
 228         }
 229 
 230         {
 231             boolean success = false;
 232             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 233                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, '\u0123', '\u4567');
 234             }
 235             assertEquals(success, true, "weakCompareAndSetRelease char");
 236             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 237             assertEquals(x, '\u4567', "weakCompareAndSetRelease char");
 238         }
 239 
 240         // Compare set and get
 241         {
 242             char o = (char) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, '\u0123');
 243             assertEquals(o, '\u4567', "getAndSet char");
 244             char x = (char) hs.get(TestAccessMode.GET).invokeExact(recv);
 245             assertEquals(x, '\u0123', "getAndSet char value");
 246         }
 247 
 248         hs.get(TestAccessMode.SET).invokeExact(recv, '\u0123');
 249 
 250         // get and add, add and get
 251         {
 252             char o = (char) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, '\u89AB');
 253             assertEquals(o, '\u0123', "getAndAdd char");
 254             char c = (char) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(recv, '\u89AB');
 255             assertEquals(c, (char)('\u0123' + '\u89AB' + '\u89AB'), "getAndAdd char value");
 256         }
 257     }
 258 
 259     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessChar recv, Handles hs) throws Throwable {
 260 
 261     }
 262 
 263 
 264     static void testStaticField(Handles hs) throws Throwable {
 265         // Plain
 266         {
 267             hs.get(TestAccessMode.SET).invokeExact('\u0123');
 268             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 269             assertEquals(x, '\u0123', "set char value");
 270         }
 271 
 272 
 273         // Volatile
 274         {
 275             hs.get(TestAccessMode.SET_VOLATILE).invokeExact('\u4567');
 276             char x = (char) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 277             assertEquals(x, '\u4567', "setVolatile char value");
 278         }
 279 
 280         // Lazy
 281         {
 282             hs.get(TestAccessMode.SET_RELEASE).invokeExact('\u0123');
 283             char x = (char) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
 284             assertEquals(x, '\u0123', "setRelease char value");
 285         }
 286 
 287         // Opaque
 288         {
 289             hs.get(TestAccessMode.SET_OPAQUE).invokeExact('\u4567');
 290             char x = (char) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
 291             assertEquals(x, '\u4567', "setOpaque char value");
 292         }
 293 
 294         hs.get(TestAccessMode.SET).invokeExact('\u0123');
 295 
 296         // Compare
 297         {
 298             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact('\u0123', '\u4567');
 299             assertEquals(r, true, "success compareAndSet char");
 300             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 301             assertEquals(x, '\u4567', "success compareAndSet char value");
 302         }
 303 
 304         {
 305             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact('\u0123', '\u89AB');
 306             assertEquals(r, false, "failing compareAndSet char");
 307             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 308             assertEquals(x, '\u4567', "failing compareAndSet char value");
 309         }
 310 
 311         {
 312             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact('\u4567', '\u0123');
 313             assertEquals(r, '\u4567', "success compareAndExchangeVolatile char");
 314             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 315             assertEquals(x, '\u0123', "success compareAndExchangeVolatile char value");
 316         }
 317 
 318         {
 319             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact('\u4567', '\u89AB');
 320             assertEquals(r, '\u0123', "failing compareAndExchangeVolatile char");
 321             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 322             assertEquals(x, '\u0123', "failing compareAndExchangeVolatile char value");
 323         }
 324 
 325         {
 326             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact('\u0123', '\u4567');
 327             assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 328             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 329             assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 330         }
 331 
 332         {
 333             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact('\u0123', '\u89AB');
 334             assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 335             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 336             assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 337         }
 338 
 339         {
 340             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact('\u4567', '\u0123');
 341             assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 342             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 343             assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 344         }
 345 
 346         {
 347             char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact('\u4567', '\u89AB');
 348             assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 349             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 350             assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 351         }
 352 
 353         {
 354             boolean success = false;
 355             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 356                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact('\u0123', '\u4567');
 357             }
 358             assertEquals(success, true, "weakCompareAndSet char");
 359             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 360             assertEquals(x, '\u4567', "weakCompareAndSet char value");
 361         }
 362 
 363         {
 364             boolean success = false;
 365             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 366                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact('\u4567', '\u0123');
 367             }
 368             assertEquals(success, true, "weakCompareAndSetAcquire char");
 369             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 370             assertEquals(x, '\u0123', "weakCompareAndSetAcquire char");
 371         }
 372 
 373         {
 374             boolean success = false;
 375             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 376                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact('\u0123', '\u4567');
 377             }
 378             assertEquals(success, true, "weakCompareAndSetRelease char");
 379             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 380             assertEquals(x, '\u4567', "weakCompareAndSetRelease char");
 381         }
 382 
 383         // Compare set and get
 384         {
 385             char o = (char) hs.get(TestAccessMode.GET_AND_SET).invokeExact( '\u0123');
 386             assertEquals(o, '\u4567', "getAndSet char");
 387             char x = (char) hs.get(TestAccessMode.GET).invokeExact();
 388             assertEquals(x, '\u0123', "getAndSet char value");
 389         }
 390 
 391         hs.get(TestAccessMode.SET).invokeExact('\u0123');
 392 
 393         // get and add, add and get
 394         {
 395             char o = (char) hs.get(TestAccessMode.GET_AND_ADD).invokeExact( '\u89AB');
 396             assertEquals(o, '\u0123', "getAndAdd char");
 397             char c = (char) hs.get(TestAccessMode.ADD_AND_GET).invokeExact('\u89AB');
 398             assertEquals(c, (char)('\u0123' + '\u89AB' + '\u89AB'), "getAndAdd char value");
 399         }
 400     }
 401 
 402     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
 403 
 404     }
 405 
 406 
 407     static void testArray(Handles hs) throws Throwable {
 408         char[] array = new char[10];
 409 
 410         for (int i = 0; i < array.length; i++) {
 411             // Plain
 412             {
 413                 hs.get(TestAccessMode.SET).invokeExact(array, i, '\u0123');
 414                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 415                 assertEquals(x, '\u0123', "get char value");
 416             }
 417 
 418 
 419             // Volatile
 420             {
 421                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, '\u4567');
 422                 char x = (char) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
 423                 assertEquals(x, '\u4567', "setVolatile char value");
 424             }
 425 
 426             // Lazy
 427             {
 428                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, '\u0123');
 429                 char x = (char) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
 430                 assertEquals(x, '\u0123', "setRelease char value");
 431             }
 432 
 433             // Opaque
 434             {
 435                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, '\u4567');
 436                 char x = (char) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
 437                 assertEquals(x, '\u4567', "setOpaque char value");
 438             }
 439 
 440             hs.get(TestAccessMode.SET).invokeExact(array, i, '\u0123');
 441 
 442             // Compare
 443             {
 444                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, '\u0123', '\u4567');
 445                 assertEquals(r, true, "success compareAndSet char");
 446                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 447                 assertEquals(x, '\u4567', "success compareAndSet char value");
 448             }
 449 
 450             {
 451                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, '\u0123', '\u89AB');
 452                 assertEquals(r, false, "failing compareAndSet char");
 453                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 454                 assertEquals(x, '\u4567', "failing compareAndSet char value");
 455             }
 456 
 457             {
 458                 char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, '\u4567', '\u0123');
 459                 assertEquals(r, '\u4567', "success compareAndExchangeVolatile char");
 460                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 461                 assertEquals(x, '\u0123', "success compareAndExchangeVolatile char value");
 462             }
 463 
 464             {
 465                 char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, '\u4567', '\u89AB');
 466                 assertEquals(r, '\u0123', "failing compareAndExchangeVolatile char");
 467                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 468                 assertEquals(x, '\u0123', "failing compareAndExchangeVolatile char value");
 469             }
 470 
 471             {
 472                 char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, '\u0123', '\u4567');
 473                 assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 474                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 475                 assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 476             }
 477 
 478             {
 479                 char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, '\u0123', '\u89AB');
 480                 assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 481                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 482                 assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 483             }
 484 
 485             {
 486                 char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, '\u4567', '\u0123');
 487                 assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 488                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 489                 assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 490             }
 491 
 492             {
 493                 char r = (char) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, '\u4567', '\u89AB');
 494                 assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 495                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 496                 assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 497             }
 498 
 499             {
 500                 boolean success = false;
 501                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 502                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, '\u0123', '\u4567');
 503                 }
 504                 assertEquals(success, true, "weakCompareAndSet char");
 505                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 506                 assertEquals(x, '\u4567', "weakCompareAndSet char value");
 507             }
 508 
 509             {
 510                 boolean success = false;
 511                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 512                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, '\u4567', '\u0123');
 513                 }
 514                 assertEquals(success, true, "weakCompareAndSetAcquire char");
 515                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 516                 assertEquals(x, '\u0123', "weakCompareAndSetAcquire char");
 517             }
 518 
 519             {
 520                 boolean success = false;
 521                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 522                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, '\u0123', '\u4567');
 523                 }
 524                 assertEquals(success, true, "weakCompareAndSetRelease char");
 525                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 526                 assertEquals(x, '\u4567', "weakCompareAndSetRelease char");
 527             }
 528 
 529             // Compare set and get
 530             {
 531                 char o = (char) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, '\u0123');
 532                 assertEquals(o, '\u4567', "getAndSet char");
 533                 char x = (char) hs.get(TestAccessMode.GET).invokeExact(array, i);
 534                 assertEquals(x, '\u0123', "getAndSet char value");
 535             }
 536 
 537             hs.get(TestAccessMode.SET).invokeExact(array, i, '\u0123');
 538 
 539             // get and add, add and get
 540             {
 541                 char o = (char) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, '\u89AB');
 542                 assertEquals(o, '\u0123', "getAndAdd char");
 543                 char c = (char) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(array, i, '\u89AB');
 544                 assertEquals(c, (char)('\u0123' + '\u89AB' + '\u89AB'), "getAndAdd char value");
 545             }
 546         }
 547     }
 548 
 549     static void testArrayUnsupported(Handles hs) throws Throwable {
 550         char[] array = new char[10];
 551 
 552         final int i = 0;
 553 
 554     }
 555 
 556     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
 557         char[] array = new char[10];
 558 
 559         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 560             final int ci = i;
 561 
 562             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
 563                 checkIOOBE(am, () -> {
 564                     char x = (char) hs.get(am).invokeExact(array, ci);
 565                 });
 566             }
 567 
 568             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 569                 checkIOOBE(am, () -> {
 570                     hs.get(am).invokeExact(array, ci, '\u0123');
 571                 });
 572             }
 573 
 574             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 575                 checkIOOBE(am, () -> {
 576                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, '\u0123', '\u4567');
 577                 });
 578             }
 579 
 580             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 581                 checkIOOBE(am, () -> {
 582                     char r = (char) hs.get(am).invokeExact(array, ci, '\u4567', '\u0123');
 583                 });
 584             }
 585 
 586             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 587                 checkIOOBE(am, () -> {
 588                     char o = (char) hs.get(am).invokeExact(array, ci, '\u0123');
 589                 });
 590             }
 591 
 592             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
 593                 checkIOOBE(am, () -> {
 594                     char o = (char) hs.get(am).invokeExact(array, ci, '\u89AB');
 595                 });
 596             }
 597         }
 598     }
 599 }
 600