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