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 VarHandleTestAccessBoolean 30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessBoolean 31 * @run testng/othervm -Diters=20000 VarHandleTestAccessBoolean 32 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccessBoolean 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 VarHandleTestAccessBoolean extends VarHandleBaseTest { 48 static final boolean static_final_v = true; 49 50 static boolean static_v; 51 52 final boolean final_v = true; 53 54 boolean 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 VarHandleTestAccessBoolean.class, "final_v", boolean.class); 71 72 vhField = MethodHandles.lookup().findVarHandle( 73 VarHandleTestAccessBoolean.class, "v", boolean.class); 74 75 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( 76 VarHandleTestAccessBoolean.class, "static_final_v", boolean.class); 77 78 vhStaticField = MethodHandles.lookup().findStaticVarHandle( 79 VarHandleTestAccessBoolean.class, "static_v", boolean.class); 80 81 vhArray = MethodHandles.arrayElementVarHandle(boolean[].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 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); 119 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)); 120 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE)); 121 122 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR)); 123 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE)); 124 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE)); 125 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND)); 126 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE)); 127 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE)); 128 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR)); 129 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE)); 130 assertTrue(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(VarHandleTestAccessBoolean.class)}); 138 types.add(new Object[] {vhStaticField, Arrays.asList()}); 139 types.add(new Object[] {vhArray, Arrays.asList(boolean[].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(), boolean.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 VarHandleTestAccessBoolean.class, "final_v", boolean.class); 159 }); 160 161 checkIAE("Lookup of static field to instance field", () -> { 162 MethodHandles.lookup().findStaticVarHandle( 163 VarHandleTestAccessBoolean.class, "v", boolean.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 VarHandleTestAccessBoolean.class, "static_final_v", boolean.class); 172 }); 173 174 checkIAE("Lookup of instance field to static field", () -> { 175 vhStaticField = MethodHandles.lookup().findVarHandle( 176 VarHandleTestAccessBoolean.class, "static_v", boolean.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, VarHandleTestAccessBoolean::testStaticFinalField)); 193 cases.add(new VarHandleAccessTestCase("Static final field unsupported", 194 vhStaticFinalField, VarHandleTestAccessBoolean::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, VarHandleTestAccessBoolean::testStaticField)); 205 cases.add(new VarHandleAccessTestCase("Static field unsupported", 206 vhStaticField, VarHandleTestAccessBoolean::testStaticFieldUnsupported, 207 false)); 208 209 cases.add(new VarHandleAccessTestCase("Array", 210 vhArray, VarHandleTestAccessBoolean::testArray)); 211 cases.add(new VarHandleAccessTestCase("Array unsupported", 212 vhArray, VarHandleTestAccessBoolean::testArrayUnsupported, 213 false)); 214 cases.add(new VarHandleAccessTestCase("Array index out of bounds", 215 vhArray, VarHandleTestAccessBoolean::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(VarHandleTestAccessBoolean recv, VarHandle vh)235 static void testInstanceFinalField(VarHandleTestAccessBoolean recv, VarHandle vh) { 236 // Plain 237 { 238 boolean x = (boolean) vh.get(recv); 239 assertEquals(x, true, "get boolean value"); 240 } 241 242 243 // Volatile 244 { 245 boolean x = (boolean) vh.getVolatile(recv); 246 assertEquals(x, true, "getVolatile boolean value"); 247 } 248 249 // Lazy 250 { 251 boolean x = (boolean) vh.getAcquire(recv); 252 assertEquals(x, true, "getRelease boolean value"); 253 } 254 255 // Opaque 256 { 257 boolean x = (boolean) vh.getOpaque(recv); 258 assertEquals(x, true, "getOpaque boolean value"); 259 } 260 } 261 testInstanceFinalFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh)262 static void testInstanceFinalFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) { 263 checkUOE(() -> { 264 vh.set(recv, false); 265 }); 266 267 checkUOE(() -> { 268 vh.setVolatile(recv, false); 269 }); 270 271 checkUOE(() -> { 272 vh.setRelease(recv, false); 273 }); 274 275 checkUOE(() -> { 276 vh.setOpaque(recv, false); 277 }); 278 279 280 checkUOE(() -> { 281 boolean o = (boolean) vh.getAndAdd(recv, true); 282 }); 283 284 checkUOE(() -> { 285 boolean o = (boolean) vh.getAndAddAcquire(recv, true); 286 }); 287 288 checkUOE(() -> { 289 boolean o = (boolean) vh.getAndAddRelease(recv, true); 290 }); 291 292 } 293 294 testStaticFinalField(VarHandle vh)295 static void testStaticFinalField(VarHandle vh) { 296 // Plain 297 { 298 boolean x = (boolean) vh.get(); 299 assertEquals(x, true, "get boolean value"); 300 } 301 302 303 // Volatile 304 { 305 boolean x = (boolean) vh.getVolatile(); 306 assertEquals(x, true, "getVolatile boolean value"); 307 } 308 309 // Lazy 310 { 311 boolean x = (boolean) vh.getAcquire(); 312 assertEquals(x, true, "getRelease boolean value"); 313 } 314 315 // Opaque 316 { 317 boolean x = (boolean) vh.getOpaque(); 318 assertEquals(x, true, "getOpaque boolean value"); 319 } 320 } 321 testStaticFinalFieldUnsupported(VarHandle vh)322 static void testStaticFinalFieldUnsupported(VarHandle vh) { 323 checkUOE(() -> { 324 vh.set(false); 325 }); 326 327 checkUOE(() -> { 328 vh.setVolatile(false); 329 }); 330 331 checkUOE(() -> { 332 vh.setRelease(false); 333 }); 334 335 checkUOE(() -> { 336 vh.setOpaque(false); 337 }); 338 339 340 checkUOE(() -> { 341 boolean o = (boolean) vh.getAndAdd(true); 342 }); 343 344 checkUOE(() -> { 345 boolean o = (boolean) vh.getAndAddAcquire(true); 346 }); 347 348 checkUOE(() -> { 349 boolean o = (boolean) vh.getAndAddRelease(true); 350 }); 351 352 } 353 354 testInstanceField(VarHandleTestAccessBoolean recv, VarHandle vh)355 static void testInstanceField(VarHandleTestAccessBoolean recv, VarHandle vh) { 356 // Plain 357 { 358 vh.set(recv, true); 359 boolean x = (boolean) vh.get(recv); 360 assertEquals(x, true, "set boolean value"); 361 } 362 363 364 // Volatile 365 { 366 vh.setVolatile(recv, false); 367 boolean x = (boolean) vh.getVolatile(recv); 368 assertEquals(x, false, "setVolatile boolean value"); 369 } 370 371 // Lazy 372 { 373 vh.setRelease(recv, true); 374 boolean x = (boolean) vh.getAcquire(recv); 375 assertEquals(x, true, "setRelease boolean value"); 376 } 377 378 // Opaque 379 { 380 vh.setOpaque(recv, false); 381 boolean x = (boolean) vh.getOpaque(recv); 382 assertEquals(x, false, "setOpaque boolean value"); 383 } 384 385 vh.set(recv, true); 386 387 // Compare 388 { 389 boolean r = vh.compareAndSet(recv, true, false); 390 assertEquals(r, true, "success compareAndSet boolean"); 391 boolean x = (boolean) vh.get(recv); 392 assertEquals(x, false, "success compareAndSet boolean value"); 393 } 394 395 { 396 boolean r = vh.compareAndSet(recv, true, false); 397 assertEquals(r, false, "failing compareAndSet boolean"); 398 boolean x = (boolean) vh.get(recv); 399 assertEquals(x, false, "failing compareAndSet boolean value"); 400 } 401 402 { 403 boolean r = (boolean) vh.compareAndExchange(recv, false, true); 404 assertEquals(r, false, "success compareAndExchange boolean"); 405 boolean x = (boolean) vh.get(recv); 406 assertEquals(x, true, "success compareAndExchange boolean value"); 407 } 408 409 { 410 boolean r = (boolean) vh.compareAndExchange(recv, false, false); 411 assertEquals(r, true, "failing compareAndExchange boolean"); 412 boolean x = (boolean) vh.get(recv); 413 assertEquals(x, true, "failing compareAndExchange boolean value"); 414 } 415 416 { 417 boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false); 418 assertEquals(r, true, "success compareAndExchangeAcquire boolean"); 419 boolean x = (boolean) vh.get(recv); 420 assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); 421 } 422 423 { 424 boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false); 425 assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); 426 boolean x = (boolean) vh.get(recv); 427 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); 428 } 429 430 { 431 boolean r = (boolean) vh.compareAndExchangeRelease(recv, false, true); 432 assertEquals(r, false, "success compareAndExchangeRelease boolean"); 433 boolean x = (boolean) vh.get(recv); 434 assertEquals(x, true, "success compareAndExchangeRelease boolean value"); 435 } 436 437 { 438 boolean r = (boolean) vh.compareAndExchangeRelease(recv, false, false); 439 assertEquals(r, true, "failing compareAndExchangeRelease boolean"); 440 boolean x = (boolean) vh.get(recv); 441 assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); 442 } 443 444 { 445 boolean success = false; 446 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 447 success = vh.weakCompareAndSetPlain(recv, true, false); 448 } 449 assertEquals(success, true, "weakCompareAndSetPlain boolean"); 450 boolean x = (boolean) vh.get(recv); 451 assertEquals(x, false, "weakCompareAndSetPlain boolean value"); 452 } 453 454 { 455 boolean success = false; 456 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 457 success = vh.weakCompareAndSetAcquire(recv, false, true); 458 } 459 assertEquals(success, true, "weakCompareAndSetAcquire boolean"); 460 boolean x = (boolean) vh.get(recv); 461 assertEquals(x, true, "weakCompareAndSetAcquire boolean"); 462 } 463 464 { 465 boolean success = false; 466 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 467 success = vh.weakCompareAndSetRelease(recv, true, false); 468 } 469 assertEquals(success, true, "weakCompareAndSetRelease boolean"); 470 boolean x = (boolean) vh.get(recv); 471 assertEquals(x, false, "weakCompareAndSetRelease boolean"); 472 } 473 474 { 475 boolean success = false; 476 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 477 success = vh.weakCompareAndSet(recv, false, true); 478 } 479 assertEquals(success, true, "weakCompareAndSet boolean"); 480 boolean x = (boolean) vh.get(recv); 481 assertEquals(x, true, "weakCompareAndSet boolean value"); 482 } 483 484 // Compare set and get 485 { 486 vh.set(recv, true); 487 488 boolean o = (boolean) vh.getAndSet(recv, false); 489 assertEquals(o, true, "getAndSet boolean"); 490 boolean x = (boolean) vh.get(recv); 491 assertEquals(x, false, "getAndSet boolean value"); 492 } 493 494 { 495 vh.set(recv, true); 496 497 boolean o = (boolean) vh.getAndSetAcquire(recv, false); 498 assertEquals(o, true, "getAndSetAcquire boolean"); 499 boolean x = (boolean) vh.get(recv); 500 assertEquals(x, false, "getAndSetAcquire boolean value"); 501 } 502 503 { 504 vh.set(recv, true); 505 506 boolean o = (boolean) vh.getAndSetRelease(recv, false); 507 assertEquals(o, true, "getAndSetRelease boolean"); 508 boolean x = (boolean) vh.get(recv); 509 assertEquals(x, false, "getAndSetRelease boolean value"); 510 } 511 512 513 // get and bitwise or 514 { 515 vh.set(recv, true); 516 517 boolean o = (boolean) vh.getAndBitwiseOr(recv, false); 518 assertEquals(o, true, "getAndBitwiseOr boolean"); 519 boolean x = (boolean) vh.get(recv); 520 assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value"); 521 } 522 523 { 524 vh.set(recv, true); 525 526 boolean o = (boolean) vh.getAndBitwiseOrAcquire(recv, false); 527 assertEquals(o, true, "getAndBitwiseOrAcquire boolean"); 528 boolean x = (boolean) vh.get(recv); 529 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value"); 530 } 531 532 { 533 vh.set(recv, true); 534 535 boolean o = (boolean) vh.getAndBitwiseOrRelease(recv, false); 536 assertEquals(o, true, "getAndBitwiseOrRelease boolean"); 537 boolean x = (boolean) vh.get(recv); 538 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value"); 539 } 540 541 // get and bitwise and 542 { 543 vh.set(recv, true); 544 545 boolean o = (boolean) vh.getAndBitwiseAnd(recv, false); 546 assertEquals(o, true, "getAndBitwiseAnd boolean"); 547 boolean x = (boolean) vh.get(recv); 548 assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value"); 549 } 550 551 { 552 vh.set(recv, true); 553 554 boolean o = (boolean) vh.getAndBitwiseAndAcquire(recv, false); 555 assertEquals(o, true, "getAndBitwiseAndAcquire boolean"); 556 boolean x = (boolean) vh.get(recv); 557 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value"); 558 } 559 560 { 561 vh.set(recv, true); 562 563 boolean o = (boolean) vh.getAndBitwiseAndRelease(recv, false); 564 assertEquals(o, true, "getAndBitwiseAndRelease boolean"); 565 boolean x = (boolean) vh.get(recv); 566 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value"); 567 } 568 569 // get and bitwise xor 570 { 571 vh.set(recv, true); 572 573 boolean o = (boolean) vh.getAndBitwiseXor(recv, false); 574 assertEquals(o, true, "getAndBitwiseXor boolean"); 575 boolean x = (boolean) vh.get(recv); 576 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value"); 577 } 578 579 { 580 vh.set(recv, true); 581 582 boolean o = (boolean) vh.getAndBitwiseXorAcquire(recv, false); 583 assertEquals(o, true, "getAndBitwiseXorAcquire boolean"); 584 boolean x = (boolean) vh.get(recv); 585 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value"); 586 } 587 588 { 589 vh.set(recv, true); 590 591 boolean o = (boolean) vh.getAndBitwiseXorRelease(recv, false); 592 assertEquals(o, true, "getAndBitwiseXorRelease boolean"); 593 boolean x = (boolean) vh.get(recv); 594 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value"); 595 } 596 } 597 testInstanceFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh)598 static void testInstanceFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) { 599 600 checkUOE(() -> { 601 boolean o = (boolean) vh.getAndAdd(recv, true); 602 }); 603 604 checkUOE(() -> { 605 boolean o = (boolean) vh.getAndAddAcquire(recv, true); 606 }); 607 608 checkUOE(() -> { 609 boolean o = (boolean) vh.getAndAddRelease(recv, true); 610 }); 611 612 } 613 614 testStaticField(VarHandle vh)615 static void testStaticField(VarHandle vh) { 616 // Plain 617 { 618 vh.set(true); 619 boolean x = (boolean) vh.get(); 620 assertEquals(x, true, "set boolean value"); 621 } 622 623 624 // Volatile 625 { 626 vh.setVolatile(false); 627 boolean x = (boolean) vh.getVolatile(); 628 assertEquals(x, false, "setVolatile boolean value"); 629 } 630 631 // Lazy 632 { 633 vh.setRelease(true); 634 boolean x = (boolean) vh.getAcquire(); 635 assertEquals(x, true, "setRelease boolean value"); 636 } 637 638 // Opaque 639 { 640 vh.setOpaque(false); 641 boolean x = (boolean) vh.getOpaque(); 642 assertEquals(x, false, "setOpaque boolean value"); 643 } 644 645 vh.set(true); 646 647 // Compare 648 { 649 boolean r = vh.compareAndSet(true, false); 650 assertEquals(r, true, "success compareAndSet boolean"); 651 boolean x = (boolean) vh.get(); 652 assertEquals(x, false, "success compareAndSet boolean value"); 653 } 654 655 { 656 boolean r = vh.compareAndSet(true, false); 657 assertEquals(r, false, "failing compareAndSet boolean"); 658 boolean x = (boolean) vh.get(); 659 assertEquals(x, false, "failing compareAndSet boolean value"); 660 } 661 662 { 663 boolean r = (boolean) vh.compareAndExchange(false, true); 664 assertEquals(r, false, "success compareAndExchange boolean"); 665 boolean x = (boolean) vh.get(); 666 assertEquals(x, true, "success compareAndExchange boolean value"); 667 } 668 669 { 670 boolean r = (boolean) vh.compareAndExchange(false, false); 671 assertEquals(r, true, "failing compareAndExchange boolean"); 672 boolean x = (boolean) vh.get(); 673 assertEquals(x, true, "failing compareAndExchange boolean value"); 674 } 675 676 { 677 boolean r = (boolean) vh.compareAndExchangeAcquire(true, false); 678 assertEquals(r, true, "success compareAndExchangeAcquire boolean"); 679 boolean x = (boolean) vh.get(); 680 assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); 681 } 682 683 { 684 boolean r = (boolean) vh.compareAndExchangeAcquire(true, false); 685 assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); 686 boolean x = (boolean) vh.get(); 687 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); 688 } 689 690 { 691 boolean r = (boolean) vh.compareAndExchangeRelease(false, true); 692 assertEquals(r, false, "success compareAndExchangeRelease boolean"); 693 boolean x = (boolean) vh.get(); 694 assertEquals(x, true, "success compareAndExchangeRelease boolean value"); 695 } 696 697 { 698 boolean r = (boolean) vh.compareAndExchangeRelease(false, false); 699 assertEquals(r, true, "failing compareAndExchangeRelease boolean"); 700 boolean x = (boolean) vh.get(); 701 assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); 702 } 703 704 { 705 boolean success = false; 706 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 707 success = vh.weakCompareAndSetPlain(true, false); 708 } 709 assertEquals(success, true, "weakCompareAndSetPlain boolean"); 710 boolean x = (boolean) vh.get(); 711 assertEquals(x, false, "weakCompareAndSetPlain boolean value"); 712 } 713 714 { 715 boolean success = false; 716 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 717 success = vh.weakCompareAndSetAcquire(false, true); 718 } 719 assertEquals(success, true, "weakCompareAndSetAcquire boolean"); 720 boolean x = (boolean) vh.get(); 721 assertEquals(x, true, "weakCompareAndSetAcquire boolean"); 722 } 723 724 { 725 boolean success = false; 726 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 727 success = vh.weakCompareAndSetRelease(true, false); 728 } 729 assertEquals(success, true, "weakCompareAndSetRelease boolean"); 730 boolean x = (boolean) vh.get(); 731 assertEquals(x, false, "weakCompareAndSetRelease boolean"); 732 } 733 734 { 735 boolean success = false; 736 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 737 success = vh.weakCompareAndSet(false, true); 738 } 739 assertEquals(success, true, "weakCompareAndSet boolean"); 740 boolean x = (boolean) vh.get(); 741 assertEquals(x, true, "weakCompareAndSet boolean"); 742 } 743 744 // Compare set and get 745 { 746 vh.set(true); 747 748 boolean o = (boolean) vh.getAndSet(false); 749 assertEquals(o, true, "getAndSet boolean"); 750 boolean x = (boolean) vh.get(); 751 assertEquals(x, false, "getAndSet boolean value"); 752 } 753 754 { 755 vh.set(true); 756 757 boolean o = (boolean) vh.getAndSetAcquire(false); 758 assertEquals(o, true, "getAndSetAcquire boolean"); 759 boolean x = (boolean) vh.get(); 760 assertEquals(x, false, "getAndSetAcquire boolean value"); 761 } 762 763 { 764 vh.set(true); 765 766 boolean o = (boolean) vh.getAndSetRelease(false); 767 assertEquals(o, true, "getAndSetRelease boolean"); 768 boolean x = (boolean) vh.get(); 769 assertEquals(x, false, "getAndSetRelease boolean value"); 770 } 771 772 773 // get and bitwise or 774 { 775 vh.set(true); 776 777 boolean o = (boolean) vh.getAndBitwiseOr(false); 778 assertEquals(o, true, "getAndBitwiseOr boolean"); 779 boolean x = (boolean) vh.get(); 780 assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value"); 781 } 782 783 { 784 vh.set(true); 785 786 boolean o = (boolean) vh.getAndBitwiseOrAcquire(false); 787 assertEquals(o, true, "getAndBitwiseOrAcquire boolean"); 788 boolean x = (boolean) vh.get(); 789 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value"); 790 } 791 792 { 793 vh.set(true); 794 795 boolean o = (boolean) vh.getAndBitwiseOrRelease(false); 796 assertEquals(o, true, "getAndBitwiseOrRelease boolean"); 797 boolean x = (boolean) vh.get(); 798 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value"); 799 } 800 801 // get and bitwise and 802 { 803 vh.set(true); 804 805 boolean o = (boolean) vh.getAndBitwiseAnd(false); 806 assertEquals(o, true, "getAndBitwiseAnd boolean"); 807 boolean x = (boolean) vh.get(); 808 assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value"); 809 } 810 811 { 812 vh.set(true); 813 814 boolean o = (boolean) vh.getAndBitwiseAndAcquire(false); 815 assertEquals(o, true, "getAndBitwiseAndAcquire boolean"); 816 boolean x = (boolean) vh.get(); 817 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value"); 818 } 819 820 { 821 vh.set(true); 822 823 boolean o = (boolean) vh.getAndBitwiseAndRelease(false); 824 assertEquals(o, true, "getAndBitwiseAndRelease boolean"); 825 boolean x = (boolean) vh.get(); 826 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value"); 827 } 828 829 // get and bitwise xor 830 { 831 vh.set(true); 832 833 boolean o = (boolean) vh.getAndBitwiseXor(false); 834 assertEquals(o, true, "getAndBitwiseXor boolean"); 835 boolean x = (boolean) vh.get(); 836 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value"); 837 } 838 839 { 840 vh.set(true); 841 842 boolean o = (boolean) vh.getAndBitwiseXorAcquire(false); 843 assertEquals(o, true, "getAndBitwiseXorAcquire boolean"); 844 boolean x = (boolean) vh.get(); 845 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value"); 846 } 847 848 { 849 vh.set(true); 850 851 boolean o = (boolean) vh.getAndBitwiseXorRelease(false); 852 assertEquals(o, true, "getAndBitwiseXorRelease boolean"); 853 boolean x = (boolean) vh.get(); 854 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value"); 855 } 856 } 857 testStaticFieldUnsupported(VarHandle vh)858 static void testStaticFieldUnsupported(VarHandle vh) { 859 860 checkUOE(() -> { 861 boolean o = (boolean) vh.getAndAdd(true); 862 }); 863 864 checkUOE(() -> { 865 boolean o = (boolean) vh.getAndAddAcquire(true); 866 }); 867 868 checkUOE(() -> { 869 boolean o = (boolean) vh.getAndAddRelease(true); 870 }); 871 872 } 873 874 testArray(VarHandle vh)875 static void testArray(VarHandle vh) { 876 boolean[] array = new boolean[10]; 877 878 for (int i = 0; i < array.length; i++) { 879 // Plain 880 { 881 vh.set(array, i, true); 882 boolean x = (boolean) vh.get(array, i); 883 assertEquals(x, true, "get boolean value"); 884 } 885 886 887 // Volatile 888 { 889 vh.setVolatile(array, i, false); 890 boolean x = (boolean) vh.getVolatile(array, i); 891 assertEquals(x, false, "setVolatile boolean value"); 892 } 893 894 // Lazy 895 { 896 vh.setRelease(array, i, true); 897 boolean x = (boolean) vh.getAcquire(array, i); 898 assertEquals(x, true, "setRelease boolean value"); 899 } 900 901 // Opaque 902 { 903 vh.setOpaque(array, i, false); 904 boolean x = (boolean) vh.getOpaque(array, i); 905 assertEquals(x, false, "setOpaque boolean value"); 906 } 907 908 vh.set(array, i, true); 909 910 // Compare 911 { 912 boolean r = vh.compareAndSet(array, i, true, false); 913 assertEquals(r, true, "success compareAndSet boolean"); 914 boolean x = (boolean) vh.get(array, i); 915 assertEquals(x, false, "success compareAndSet boolean value"); 916 } 917 918 { 919 boolean r = vh.compareAndSet(array, i, true, false); 920 assertEquals(r, false, "failing compareAndSet boolean"); 921 boolean x = (boolean) vh.get(array, i); 922 assertEquals(x, false, "failing compareAndSet boolean value"); 923 } 924 925 { 926 boolean r = (boolean) vh.compareAndExchange(array, i, false, true); 927 assertEquals(r, false, "success compareAndExchange boolean"); 928 boolean x = (boolean) vh.get(array, i); 929 assertEquals(x, true, "success compareAndExchange boolean value"); 930 } 931 932 { 933 boolean r = (boolean) vh.compareAndExchange(array, i, false, false); 934 assertEquals(r, true, "failing compareAndExchange boolean"); 935 boolean x = (boolean) vh.get(array, i); 936 assertEquals(x, true, "failing compareAndExchange boolean value"); 937 } 938 939 { 940 boolean r = (boolean) vh.compareAndExchangeAcquire(array, i, true, false); 941 assertEquals(r, true, "success compareAndExchangeAcquire boolean"); 942 boolean x = (boolean) vh.get(array, i); 943 assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); 944 } 945 946 { 947 boolean r = (boolean) vh.compareAndExchangeAcquire(array, i, true, false); 948 assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); 949 boolean x = (boolean) vh.get(array, i); 950 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); 951 } 952 953 { 954 boolean r = (boolean) vh.compareAndExchangeRelease(array, i, false, true); 955 assertEquals(r, false, "success compareAndExchangeRelease boolean"); 956 boolean x = (boolean) vh.get(array, i); 957 assertEquals(x, true, "success compareAndExchangeRelease boolean value"); 958 } 959 960 { 961 boolean r = (boolean) vh.compareAndExchangeRelease(array, i, false, false); 962 assertEquals(r, true, "failing compareAndExchangeRelease boolean"); 963 boolean x = (boolean) vh.get(array, i); 964 assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); 965 } 966 967 { 968 boolean success = false; 969 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 970 success = vh.weakCompareAndSetPlain(array, i, true, false); 971 } 972 assertEquals(success, true, "weakCompareAndSetPlain boolean"); 973 boolean x = (boolean) vh.get(array, i); 974 assertEquals(x, false, "weakCompareAndSetPlain boolean value"); 975 } 976 977 { 978 boolean success = false; 979 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 980 success = vh.weakCompareAndSetAcquire(array, i, false, true); 981 } 982 assertEquals(success, true, "weakCompareAndSetAcquire boolean"); 983 boolean x = (boolean) vh.get(array, i); 984 assertEquals(x, true, "weakCompareAndSetAcquire boolean"); 985 } 986 987 { 988 boolean success = false; 989 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 990 success = vh.weakCompareAndSetRelease(array, i, true, false); 991 } 992 assertEquals(success, true, "weakCompareAndSetRelease boolean"); 993 boolean x = (boolean) vh.get(array, i); 994 assertEquals(x, false, "weakCompareAndSetRelease boolean"); 995 } 996 997 { 998 boolean success = false; 999 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1000 success = vh.weakCompareAndSet(array, i, false, true); 1001 } 1002 assertEquals(success, true, "weakCompareAndSet boolean"); 1003 boolean x = (boolean) vh.get(array, i); 1004 assertEquals(x, true, "weakCompareAndSet boolean"); 1005 } 1006 1007 // Compare set and get 1008 { 1009 vh.set(array, i, true); 1010 1011 boolean o = (boolean) vh.getAndSet(array, i, false); 1012 assertEquals(o, true, "getAndSet boolean"); 1013 boolean x = (boolean) vh.get(array, i); 1014 assertEquals(x, false, "getAndSet boolean value"); 1015 } 1016 1017 { 1018 vh.set(array, i, true); 1019 1020 boolean o = (boolean) vh.getAndSetAcquire(array, i, false); 1021 assertEquals(o, true, "getAndSetAcquire boolean"); 1022 boolean x = (boolean) vh.get(array, i); 1023 assertEquals(x, false, "getAndSetAcquire boolean value"); 1024 } 1025 1026 { 1027 vh.set(array, i, true); 1028 1029 boolean o = (boolean) vh.getAndSetRelease(array, i, false); 1030 assertEquals(o, true, "getAndSetRelease boolean"); 1031 boolean x = (boolean) vh.get(array, i); 1032 assertEquals(x, false, "getAndSetRelease boolean value"); 1033 } 1034 1035 1036 // get and bitwise or 1037 { 1038 vh.set(array, i, true); 1039 1040 boolean o = (boolean) vh.getAndBitwiseOr(array, i, false); 1041 assertEquals(o, true, "getAndBitwiseOr boolean"); 1042 boolean x = (boolean) vh.get(array, i); 1043 assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value"); 1044 } 1045 1046 { 1047 vh.set(array, i, true); 1048 1049 boolean o = (boolean) vh.getAndBitwiseOrAcquire(array, i, false); 1050 assertEquals(o, true, "getAndBitwiseOrAcquire boolean"); 1051 boolean x = (boolean) vh.get(array, i); 1052 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value"); 1053 } 1054 1055 { 1056 vh.set(array, i, true); 1057 1058 boolean o = (boolean) vh.getAndBitwiseOrRelease(array, i, false); 1059 assertEquals(o, true, "getAndBitwiseOrRelease boolean"); 1060 boolean x = (boolean) vh.get(array, i); 1061 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value"); 1062 } 1063 1064 // get and bitwise and 1065 { 1066 vh.set(array, i, true); 1067 1068 boolean o = (boolean) vh.getAndBitwiseAnd(array, i, false); 1069 assertEquals(o, true, "getAndBitwiseAnd boolean"); 1070 boolean x = (boolean) vh.get(array, i); 1071 assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value"); 1072 } 1073 1074 { 1075 vh.set(array, i, true); 1076 1077 boolean o = (boolean) vh.getAndBitwiseAndAcquire(array, i, false); 1078 assertEquals(o, true, "getAndBitwiseAndAcquire boolean"); 1079 boolean x = (boolean) vh.get(array, i); 1080 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value"); 1081 } 1082 1083 { 1084 vh.set(array, i, true); 1085 1086 boolean o = (boolean) vh.getAndBitwiseAndRelease(array, i, false); 1087 assertEquals(o, true, "getAndBitwiseAndRelease boolean"); 1088 boolean x = (boolean) vh.get(array, i); 1089 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value"); 1090 } 1091 1092 // get and bitwise xor 1093 { 1094 vh.set(array, i, true); 1095 1096 boolean o = (boolean) vh.getAndBitwiseXor(array, i, false); 1097 assertEquals(o, true, "getAndBitwiseXor boolean"); 1098 boolean x = (boolean) vh.get(array, i); 1099 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value"); 1100 } 1101 1102 { 1103 vh.set(array, i, true); 1104 1105 boolean o = (boolean) vh.getAndBitwiseXorAcquire(array, i, false); 1106 assertEquals(o, true, "getAndBitwiseXorAcquire boolean"); 1107 boolean x = (boolean) vh.get(array, i); 1108 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value"); 1109 } 1110 1111 { 1112 vh.set(array, i, true); 1113 1114 boolean o = (boolean) vh.getAndBitwiseXorRelease(array, i, false); 1115 assertEquals(o, true, "getAndBitwiseXorRelease boolean"); 1116 boolean x = (boolean) vh.get(array, i); 1117 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value"); 1118 } 1119 } 1120 } 1121 testArrayUnsupported(VarHandle vh)1122 static void testArrayUnsupported(VarHandle vh) { 1123 boolean[] array = new boolean[10]; 1124 1125 int i = 0; 1126 1127 checkUOE(() -> { 1128 boolean o = (boolean) vh.getAndAdd(array, i, true); 1129 }); 1130 1131 checkUOE(() -> { 1132 boolean o = (boolean) vh.getAndAddAcquire(array, i, true); 1133 }); 1134 1135 checkUOE(() -> { 1136 boolean o = (boolean) vh.getAndAddRelease(array, i, true); 1137 }); 1138 1139 } 1140 testArrayIndexOutOfBounds(VarHandle vh)1141 static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable { 1142 boolean[] array = new boolean[10]; 1143 1144 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { 1145 final int ci = i; 1146 1147 checkIOOBE(() -> { 1148 boolean x = (boolean) vh.get(array, ci); 1149 }); 1150 1151 checkIOOBE(() -> { 1152 vh.set(array, ci, true); 1153 }); 1154 1155 checkIOOBE(() -> { 1156 boolean x = (boolean) vh.getVolatile(array, ci); 1157 }); 1158 1159 checkIOOBE(() -> { 1160 vh.setVolatile(array, ci, true); 1161 }); 1162 1163 checkIOOBE(() -> { 1164 boolean x = (boolean) vh.getAcquire(array, ci); 1165 }); 1166 1167 checkIOOBE(() -> { 1168 vh.setRelease(array, ci, true); 1169 }); 1170 1171 checkIOOBE(() -> { 1172 boolean x = (boolean) vh.getOpaque(array, ci); 1173 }); 1174 1175 checkIOOBE(() -> { 1176 vh.setOpaque(array, ci, true); 1177 }); 1178 1179 checkIOOBE(() -> { 1180 boolean r = vh.compareAndSet(array, ci, true, false); 1181 }); 1182 1183 checkIOOBE(() -> { 1184 boolean r = (boolean) vh.compareAndExchange(array, ci, false, true); 1185 }); 1186 1187 checkIOOBE(() -> { 1188 boolean r = (boolean) vh.compareAndExchangeAcquire(array, ci, false, true); 1189 }); 1190 1191 checkIOOBE(() -> { 1192 boolean r = (boolean) vh.compareAndExchangeRelease(array, ci, false, true); 1193 }); 1194 1195 checkIOOBE(() -> { 1196 boolean r = vh.weakCompareAndSetPlain(array, ci, true, false); 1197 }); 1198 1199 checkIOOBE(() -> { 1200 boolean r = vh.weakCompareAndSet(array, ci, true, false); 1201 }); 1202 1203 checkIOOBE(() -> { 1204 boolean r = vh.weakCompareAndSetAcquire(array, ci, true, false); 1205 }); 1206 1207 checkIOOBE(() -> { 1208 boolean r = vh.weakCompareAndSetRelease(array, ci, true, false); 1209 }); 1210 1211 checkIOOBE(() -> { 1212 boolean o = (boolean) vh.getAndSet(array, ci, true); 1213 }); 1214 1215 checkIOOBE(() -> { 1216 boolean o = (boolean) vh.getAndSetAcquire(array, ci, true); 1217 }); 1218 1219 checkIOOBE(() -> { 1220 boolean o = (boolean) vh.getAndSetRelease(array, ci, true); 1221 }); 1222 1223 1224 checkIOOBE(() -> { 1225 boolean o = (boolean) vh.getAndBitwiseOr(array, ci, true); 1226 }); 1227 1228 checkIOOBE(() -> { 1229 boolean o = (boolean) vh.getAndBitwiseOrAcquire(array, ci, true); 1230 }); 1231 1232 checkIOOBE(() -> { 1233 boolean o = (boolean) vh.getAndBitwiseOrRelease(array, ci, true); 1234 }); 1235 1236 checkIOOBE(() -> { 1237 boolean o = (boolean) vh.getAndBitwiseAnd(array, ci, true); 1238 }); 1239 1240 checkIOOBE(() -> { 1241 boolean o = (boolean) vh.getAndBitwiseAndAcquire(array, ci, true); 1242 }); 1243 1244 checkIOOBE(() -> { 1245 boolean o = (boolean) vh.getAndBitwiseAndRelease(array, ci, true); 1246 }); 1247 1248 checkIOOBE(() -> { 1249 boolean o = (boolean) vh.getAndBitwiseXor(array, ci, true); 1250 }); 1251 1252 checkIOOBE(() -> { 1253 boolean o = (boolean) vh.getAndBitwiseXorAcquire(array, ci, true); 1254 }); 1255 1256 checkIOOBE(() -> { 1257 boolean o = (boolean) vh.getAndBitwiseXorRelease(array, ci, true); 1258 }); 1259 } 1260 } 1261 1262 } 1263 1264