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