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