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