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