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