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