1 /* 2 * Copyright (c) 2015, 2016, 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=20000 VarHandleTestMethodHandleAccessFloat 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 VarHandleTestMethodHandleAccessFloat extends VarHandleBaseTest { 45 static final float static_final_v = 1.0f; 46 47 static float static_v; 48 49 final float final_v = 1.0f; 50 51 float v; 52 53 VarHandle vhFinalField; 54 55 VarHandle vhField; 56 57 VarHandle vhStaticField; 58 59 VarHandle vhStaticFinalField; 60 61 VarHandle vhArray; 62 63 @BeforeClass setup()64 public void setup() throws Exception { 65 vhFinalField = MethodHandles.lookup().findVarHandle( 66 VarHandleTestMethodHandleAccessFloat.class, "final_v", float.class); 67 68 vhField = MethodHandles.lookup().findVarHandle( 69 VarHandleTestMethodHandleAccessFloat.class, "v", float.class); 70 71 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( 72 VarHandleTestMethodHandleAccessFloat.class, "static_final_v", float.class); 73 74 vhStaticField = MethodHandles.lookup().findStaticVarHandle( 75 VarHandleTestMethodHandleAccessFloat.class, "static_v", float.class); 76 77 vhArray = MethodHandles.arrayElementVarHandle(float[].class); 78 } 79 80 81 @DataProvider accessTestCaseProvider()82 public Object[][] accessTestCaseProvider() throws Exception { 83 List<AccessTestCase<?>> cases = new ArrayList<>(); 84 85 for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) { 86 cases.add(new MethodHandleAccessTestCase("Instance field", 87 vhField, f, hs -> testInstanceField(this, hs))); 88 cases.add(new MethodHandleAccessTestCase("Instance field unsupported", 89 vhField, f, hs -> testInstanceFieldUnsupported(this, hs), 90 false)); 91 92 cases.add(new MethodHandleAccessTestCase("Static field", 93 vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticField)); 94 cases.add(new MethodHandleAccessTestCase("Static field unsupported", 95 vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticFieldUnsupported, 96 false)); 97 98 cases.add(new MethodHandleAccessTestCase("Array", 99 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArray)); 100 cases.add(new MethodHandleAccessTestCase("Array unsupported", 101 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayUnsupported, 102 false)); 103 cases.add(new MethodHandleAccessTestCase("Array index out of bounds", 104 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayIndexOutOfBounds, 105 false)); 106 } 107 108 // Work around issue with jtreg summary reporting which truncates 109 // the String result of Object.toString to 30 characters, hence 110 // the first dummy argument 111 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 112 } 113 114 @Test(dataProvider = "accessTestCaseProvider") testAccess(String desc, AccessTestCase<T> atc)115 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 116 T t = atc.get(); 117 int iters = atc.requiresLoop() ? ITERS : 1; 118 for (int c = 0; c < iters; c++) { 119 atc.testAccess(t); 120 } 121 } 122 123 testInstanceField(VarHandleTestMethodHandleAccessFloat recv, Handles hs)124 static void testInstanceField(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable { 125 // Plain 126 { 127 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f); 128 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 129 assertEquals(x, 1.0f, "set float value"); 130 } 131 132 133 // Volatile 134 { 135 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0f); 136 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); 137 assertEquals(x, 2.0f, "setVolatile float value"); 138 } 139 140 // Lazy 141 { 142 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0f); 143 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); 144 assertEquals(x, 1.0f, "setRelease float value"); 145 } 146 147 // Opaque 148 { 149 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0f); 150 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); 151 assertEquals(x, 2.0f, "setOpaque float value"); 152 } 153 154 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f); 155 156 // Compare 157 { 158 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 2.0f); 159 assertEquals(r, true, "success compareAndSet float"); 160 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 161 assertEquals(x, 2.0f, "success compareAndSet float value"); 162 } 163 164 { 165 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 3.0f); 166 assertEquals(r, false, "failing compareAndSet float"); 167 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 168 assertEquals(x, 2.0f, "failing compareAndSet float value"); 169 } 170 171 { 172 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 1.0f); 173 assertEquals(r, 2.0f, "success compareAndExchange float"); 174 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 175 assertEquals(x, 1.0f, "success compareAndExchange float value"); 176 } 177 178 { 179 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 3.0f); 180 assertEquals(r, 1.0f, "failing compareAndExchange float"); 181 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 182 assertEquals(x, 1.0f, "failing compareAndExchange float value"); 183 } 184 185 { 186 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 2.0f); 187 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float"); 188 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 189 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value"); 190 } 191 192 { 193 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 3.0f); 194 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float"); 195 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 196 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value"); 197 } 198 199 { 200 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 1.0f); 201 assertEquals(r, 2.0f, "success compareAndExchangeRelease float"); 202 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 203 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value"); 204 } 205 206 { 207 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 3.0f); 208 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float"); 209 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 210 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value"); 211 } 212 213 { 214 boolean success = false; 215 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 216 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 1.0f, 2.0f); 217 } 218 assertEquals(success, true, "weakCompareAndSetPlain float"); 219 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 220 assertEquals(x, 2.0f, "weakCompareAndSetPlain float value"); 221 } 222 223 { 224 boolean success = false; 225 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 226 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2.0f, 1.0f); 227 } 228 assertEquals(success, true, "weakCompareAndSetAcquire float"); 229 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 230 assertEquals(x, 1.0f, "weakCompareAndSetAcquire float"); 231 } 232 233 { 234 boolean success = false; 235 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 236 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1.0f, 2.0f); 237 } 238 assertEquals(success, true, "weakCompareAndSetRelease float"); 239 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 240 assertEquals(x, 2.0f, "weakCompareAndSetRelease float"); 241 } 242 243 { 244 boolean success = false; 245 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 246 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 2.0f, 1.0f); 247 } 248 assertEquals(success, true, "weakCompareAndSet float"); 249 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 250 assertEquals(x, 1.0f, "weakCompareAndSet float"); 251 } 252 253 // Compare set and get 254 { 255 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2.0f); 256 assertEquals(o, 1.0f, "getAndSet float"); 257 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 258 assertEquals(x, 2.0f, "getAndSet float value"); 259 } 260 261 // get and add, add and get 262 { 263 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f); 264 265 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 2.0f); 266 assertEquals(o, 1.0f, "getAndAdd float"); 267 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 268 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value"); 269 } 270 271 { 272 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f); 273 274 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 2.0f); 275 assertEquals(o, 1.0f, "getAndAddAcquire float"); 276 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 277 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value"); 278 } 279 280 { 281 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f); 282 283 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0f); 284 assertEquals(o, 1.0f, "getAndAddRelease float"); 285 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); 286 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value"); 287 } 288 289 } 290 testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs)291 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable { 292 293 294 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) { 295 checkUOE(am, () -> { 296 float r = (float) hs.get(am).invokeExact(recv, 1.0f); 297 }); 298 } 299 } 300 301 testStaticField(Handles hs)302 static void testStaticField(Handles hs) throws Throwable { 303 // Plain 304 { 305 hs.get(TestAccessMode.SET).invokeExact(1.0f); 306 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 307 assertEquals(x, 1.0f, "set float value"); 308 } 309 310 311 // Volatile 312 { 313 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f); 314 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); 315 assertEquals(x, 2.0f, "setVolatile float value"); 316 } 317 318 // Lazy 319 { 320 hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f); 321 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); 322 assertEquals(x, 1.0f, "setRelease float value"); 323 } 324 325 // Opaque 326 { 327 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0f); 328 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); 329 assertEquals(x, 2.0f, "setOpaque float value"); 330 } 331 332 hs.get(TestAccessMode.SET).invokeExact(1.0f); 333 334 // Compare 335 { 336 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 2.0f); 337 assertEquals(r, true, "success compareAndSet float"); 338 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 339 assertEquals(x, 2.0f, "success compareAndSet float value"); 340 } 341 342 { 343 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 3.0f); 344 assertEquals(r, false, "failing compareAndSet float"); 345 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 346 assertEquals(x, 2.0f, "failing compareAndSet float value"); 347 } 348 349 { 350 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 1.0f); 351 assertEquals(r, 2.0f, "success compareAndExchange float"); 352 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 353 assertEquals(x, 1.0f, "success compareAndExchange float value"); 354 } 355 356 { 357 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 3.0f); 358 assertEquals(r, 1.0f, "failing compareAndExchange float"); 359 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 360 assertEquals(x, 1.0f, "failing compareAndExchange float value"); 361 } 362 363 { 364 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 2.0f); 365 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float"); 366 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 367 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value"); 368 } 369 370 { 371 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 3.0f); 372 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float"); 373 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 374 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value"); 375 } 376 377 { 378 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 1.0f); 379 assertEquals(r, 2.0f, "success compareAndExchangeRelease float"); 380 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 381 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value"); 382 } 383 384 { 385 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 3.0f); 386 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float"); 387 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 388 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value"); 389 } 390 391 { 392 boolean success = false; 393 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 394 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(1.0f, 2.0f); 395 } 396 assertEquals(success, true, "weakCompareAndSetPlain float"); 397 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 398 assertEquals(x, 2.0f, "weakCompareAndSetPlain float value"); 399 } 400 401 { 402 boolean success = false; 403 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 404 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(2.0f, 1.0f); 405 } 406 assertEquals(success, true, "weakCompareAndSetAcquire float"); 407 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 408 assertEquals(x, 1.0f, "weakCompareAndSetAcquire float"); 409 } 410 411 { 412 boolean success = false; 413 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 414 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1.0f, 2.0f); 415 } 416 assertEquals(success, true, "weakCompareAndSetRelease float"); 417 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 418 assertEquals(x, 2.0f, "weakCompareAndSetRelease float"); 419 } 420 421 { 422 boolean success = false; 423 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 424 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(2.0f, 1.0f); 425 } 426 assertEquals(success, true, "weakCompareAndSet float"); 427 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 428 assertEquals(x, 1.0f, "weakCompareAndSet float"); 429 } 430 431 // Compare set and get 432 { 433 hs.get(TestAccessMode.SET).invokeExact(1.0f); 434 435 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(2.0f); 436 assertEquals(o, 1.0f, "getAndSet float"); 437 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 438 assertEquals(x, 2.0f, "getAndSet float value"); 439 } 440 441 // Compare set and get 442 { 443 hs.get(TestAccessMode.SET).invokeExact(1.0f); 444 445 float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(2.0f); 446 assertEquals(o, 1.0f, "getAndSetAcquire float"); 447 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 448 assertEquals(x, 2.0f, "getAndSetAcquire float value"); 449 } 450 451 // Compare set and get 452 { 453 hs.get(TestAccessMode.SET).invokeExact(1.0f); 454 455 float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(2.0f); 456 assertEquals(o, 1.0f, "getAndSetRelease float"); 457 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 458 assertEquals(x, 2.0f, "getAndSetRelease float value"); 459 } 460 461 // get and add, add and get 462 { 463 hs.get(TestAccessMode.SET).invokeExact(1.0f); 464 465 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(2.0f); 466 assertEquals(o, 1.0f, "getAndAdd float"); 467 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 468 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value"); 469 } 470 471 { 472 hs.get(TestAccessMode.SET).invokeExact(1.0f); 473 474 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(2.0f); 475 assertEquals(o, 1.0f, "getAndAddAcquire float"); 476 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 477 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value"); 478 } 479 480 { 481 hs.get(TestAccessMode.SET).invokeExact(1.0f); 482 483 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(2.0f); 484 assertEquals(o, 1.0f, "getAndAddRelease float"); 485 float x = (float) hs.get(TestAccessMode.GET).invokeExact(); 486 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value"); 487 } 488 489 } 490 testStaticFieldUnsupported(Handles hs)491 static void testStaticFieldUnsupported(Handles hs) throws Throwable { 492 493 494 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) { 495 checkUOE(am, () -> { 496 float r = (float) hs.get(am).invokeExact(1.0f); 497 }); 498 } 499 } 500 501 testArray(Handles hs)502 static void testArray(Handles hs) throws Throwable { 503 float[] array = new float[10]; 504 505 for (int i = 0; i < array.length; i++) { 506 // Plain 507 { 508 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 509 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 510 assertEquals(x, 1.0f, "get float value"); 511 } 512 513 514 // Volatile 515 { 516 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0f); 517 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); 518 assertEquals(x, 2.0f, "setVolatile float value"); 519 } 520 521 // Lazy 522 { 523 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0f); 524 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); 525 assertEquals(x, 1.0f, "setRelease float value"); 526 } 527 528 // Opaque 529 { 530 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0f); 531 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); 532 assertEquals(x, 2.0f, "setOpaque float value"); 533 } 534 535 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 536 537 // Compare 538 { 539 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 2.0f); 540 assertEquals(r, true, "success compareAndSet float"); 541 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 542 assertEquals(x, 2.0f, "success compareAndSet float value"); 543 } 544 545 { 546 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 3.0f); 547 assertEquals(r, false, "failing compareAndSet float"); 548 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 549 assertEquals(x, 2.0f, "failing compareAndSet float value"); 550 } 551 552 { 553 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 1.0f); 554 assertEquals(r, 2.0f, "success compareAndExchange float"); 555 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 556 assertEquals(x, 1.0f, "success compareAndExchange float value"); 557 } 558 559 { 560 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 3.0f); 561 assertEquals(r, 1.0f, "failing compareAndExchange float"); 562 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 563 assertEquals(x, 1.0f, "failing compareAndExchange float value"); 564 } 565 566 { 567 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 2.0f); 568 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float"); 569 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 570 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value"); 571 } 572 573 { 574 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 3.0f); 575 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float"); 576 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 577 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value"); 578 } 579 580 { 581 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 1.0f); 582 assertEquals(r, 2.0f, "success compareAndExchangeRelease float"); 583 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 584 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value"); 585 } 586 587 { 588 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 3.0f); 589 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float"); 590 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 591 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value"); 592 } 593 594 { 595 boolean success = false; 596 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 597 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 1.0f, 2.0f); 598 } 599 assertEquals(success, true, "weakCompareAndSetPlain float"); 600 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 601 assertEquals(x, 2.0f, "weakCompareAndSetPlain float value"); 602 } 603 604 { 605 boolean success = false; 606 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 607 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f, 1.0f); 608 } 609 assertEquals(success, true, "weakCompareAndSetAcquire float"); 610 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 611 assertEquals(x, 1.0f, "weakCompareAndSetAcquire float"); 612 } 613 614 { 615 boolean success = false; 616 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 617 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, 1.0f, 2.0f); 618 } 619 assertEquals(success, true, "weakCompareAndSetRelease float"); 620 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 621 assertEquals(x, 2.0f, "weakCompareAndSetRelease float"); 622 } 623 624 { 625 boolean success = false; 626 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 627 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 2.0f, 1.0f); 628 } 629 assertEquals(success, true, "weakCompareAndSet float"); 630 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 631 assertEquals(x, 1.0f, "weakCompareAndSet float"); 632 } 633 634 // Compare set and get 635 { 636 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 637 638 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2.0f); 639 assertEquals(o, 1.0f, "getAndSet float"); 640 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 641 assertEquals(x, 2.0f, "getAndSet float value"); 642 } 643 644 { 645 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 646 647 float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f); 648 assertEquals(o, 1.0f, "getAndSetAcquire float"); 649 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 650 assertEquals(x, 2.0f, "getAndSetAcquire float value"); 651 } 652 653 { 654 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 655 656 float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 2.0f); 657 assertEquals(o, 1.0f, "getAndSetRelease float"); 658 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 659 assertEquals(x, 2.0f, "getAndSetRelease float value"); 660 } 661 662 // get and add, add and get 663 { 664 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 665 666 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 2.0f); 667 assertEquals(o, 1.0f, "getAndAdd float"); 668 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 669 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value"); 670 } 671 672 { 673 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 674 675 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 2.0f); 676 assertEquals(o, 1.0f, "getAndAddAcquire float"); 677 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 678 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value"); 679 } 680 681 { 682 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); 683 684 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 2.0f); 685 assertEquals(o, 1.0f, "getAndAddRelease float"); 686 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); 687 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value"); 688 } 689 690 } 691 } 692 testArrayUnsupported(Handles hs)693 static void testArrayUnsupported(Handles hs) throws Throwable { 694 float[] array = new float[10]; 695 696 final int i = 0; 697 698 699 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) { 700 checkUOE(am, () -> { 701 float o = (float) hs.get(am).invokeExact(array, i, 1.0f); 702 }); 703 } 704 } 705 testArrayIndexOutOfBounds(Handles hs)706 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable { 707 float[] array = new float[10]; 708 709 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { 710 final int ci = i; 711 712 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { 713 checkIOOBE(am, () -> { 714 float x = (float) hs.get(am).invokeExact(array, ci); 715 }); 716 } 717 718 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { 719 checkIOOBE(am, () -> { 720 hs.get(am).invokeExact(array, ci, 1.0f); 721 }); 722 } 723 724 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { 725 checkIOOBE(am, () -> { 726 boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1.0f, 2.0f); 727 }); 728 } 729 730 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { 731 checkIOOBE(am, () -> { 732 float r = (float) hs.get(am).invokeExact(array, ci, 2.0f, 1.0f); 733 }); 734 } 735 736 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { 737 checkIOOBE(am, () -> { 738 float o = (float) hs.get(am).invokeExact(array, ci, 1.0f); 739 }); 740 } 741 742 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 743 checkIOOBE(am, () -> { 744 float o = (float) hs.get(am).invokeExact(array, ci, 3.0f); 745 }); 746 } 747 748 } 749 } 750 } 751 752