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