1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 // Android-added: package for test. 25 package test.java.lang.invoke.VarHandles; 26 27 /* 28 * @test 29 * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessBoolean 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 VarHandleTestMethodHandleAccessBoolean 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 setup()64 public void setup() throws Exception { 65 vhFinalField = MethodHandles.lookup().findVarHandle( 66 VarHandleTestMethodHandleAccessBoolean.class, "final_v", boolean.class); 67 68 vhField = MethodHandles.lookup().findVarHandle( 69 VarHandleTestMethodHandleAccessBoolean.class, "v", boolean.class); 70 71 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( 72 VarHandleTestMethodHandleAccessBoolean.class, "static_final_v", boolean.class); 73 74 vhStaticField = MethodHandles.lookup().findStaticVarHandle( 75 VarHandleTestMethodHandleAccessBoolean.class, "static_v", boolean.class); 76 77 vhArray = MethodHandles.arrayElementVarHandle(boolean[].class); 78 } 79 80 81 @DataProvider accessTestCaseProvider()82 public Object[][] accessTestCaseProvider() throws Exception { 83 List<AccessTestCase<?>> cases = new ArrayList<>(); 84 85 for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) { 86 cases.add(new MethodHandleAccessTestCase("Instance field", 87 vhField, f, hs -> testInstanceField(this, hs))); 88 cases.add(new MethodHandleAccessTestCase("Instance field unsupported", 89 vhField, f, hs -> testInstanceFieldUnsupported(this, hs), 90 false)); 91 92 cases.add(new MethodHandleAccessTestCase("Static field", 93 vhStaticField, f, VarHandleTestMethodHandleAccessBoolean::testStaticField)); 94 cases.add(new MethodHandleAccessTestCase("Static field unsupported", 95 vhStaticField, f, VarHandleTestMethodHandleAccessBoolean::testStaticFieldUnsupported, 96 false)); 97 98 cases.add(new MethodHandleAccessTestCase("Array", 99 vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArray)); 100 cases.add(new MethodHandleAccessTestCase("Array unsupported", 101 vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArrayUnsupported, 102 false)); 103 cases.add(new MethodHandleAccessTestCase("Array index out of bounds", 104 vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArrayIndexOutOfBounds, 105 false)); 106 } 107 108 // Work around issue with jtreg summary reporting which truncates 109 // the String result of Object.toString to 30 characters, hence 110 // the first dummy argument 111 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 112 } 113 114 @Test(dataProvider = "accessTestCaseProvider") testAccess(String desc, AccessTestCase<T> atc)115 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 116 T t = atc.get(); 117 int iters = atc.requiresLoop() ? ITERS : 1; 118 for (int c = 0; c < iters; c++) { 119 atc.testAccess(t); 120 } 121 } 122 123 testInstanceField(VarHandleTestMethodHandleAccessBoolean recv, Handles hs)124 static void testInstanceField(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable { 125 // Plain 126 { 127 hs.get(TestAccessMode.SET).invokeExact(recv, true); 128 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 129 assertEquals(x, true, "set boolean value"); 130 } 131 132 133 // Volatile 134 { 135 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, false); 136 boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); 137 assertEquals(x, false, "setVolatile boolean value"); 138 } 139 140 // Lazy 141 { 142 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, true); 143 boolean x = (boolean) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); 144 assertEquals(x, true, "setRelease boolean value"); 145 } 146 147 // Opaque 148 { 149 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, false); 150 boolean x = (boolean) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); 151 assertEquals(x, false, "setOpaque boolean value"); 152 } 153 154 hs.get(TestAccessMode.SET).invokeExact(recv, true); 155 156 // Compare 157 { 158 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, true, false); 159 assertEquals(r, true, "success compareAndSet boolean"); 160 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 161 assertEquals(x, false, "success compareAndSet boolean value"); 162 } 163 164 { 165 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, true, false); 166 assertEquals(r, false, "failing compareAndSet boolean"); 167 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 168 assertEquals(x, false, "failing compareAndSet boolean value"); 169 } 170 171 { 172 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, false, true); 173 assertEquals(r, false, "success compareAndExchange boolean"); 174 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 175 assertEquals(x, true, "success compareAndExchange boolean value"); 176 } 177 178 { 179 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, false, false); 180 assertEquals(r, true, "failing compareAndExchange boolean"); 181 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 182 assertEquals(x, true, "failing compareAndExchange boolean value"); 183 } 184 185 { 186 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, true, false); 187 assertEquals(r, true, "success compareAndExchangeAcquire boolean"); 188 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 189 assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); 190 } 191 192 { 193 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, true, false); 194 assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); 195 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 196 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); 197 } 198 199 { 200 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, false, true); 201 assertEquals(r, false, "success compareAndExchangeRelease boolean"); 202 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 203 assertEquals(x, true, "success compareAndExchangeRelease boolean value"); 204 } 205 206 { 207 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, false, false); 208 assertEquals(r, true, "failing compareAndExchangeRelease boolean"); 209 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 210 assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); 211 } 212 213 { 214 boolean success = false; 215 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 216 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, true, false); 217 } 218 assertEquals(success, true, "weakCompareAndSetPlain boolean"); 219 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 220 assertEquals(x, false, "weakCompareAndSetPlain boolean value"); 221 } 222 223 { 224 boolean success = false; 225 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 226 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, false, true); 227 } 228 assertEquals(success, true, "weakCompareAndSetAcquire boolean"); 229 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 230 assertEquals(x, true, "weakCompareAndSetAcquire boolean"); 231 } 232 233 { 234 boolean success = false; 235 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 236 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, true, false); 237 } 238 assertEquals(success, true, "weakCompareAndSetRelease boolean"); 239 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 240 assertEquals(x, false, "weakCompareAndSetRelease boolean"); 241 } 242 243 { 244 boolean success = false; 245 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 246 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, false, true); 247 } 248 assertEquals(success, true, "weakCompareAndSet boolean"); 249 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 250 assertEquals(x, true, "weakCompareAndSet boolean"); 251 } 252 253 // Compare set and get 254 { 255 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, false); 256 assertEquals(o, true, "getAndSet boolean"); 257 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 258 assertEquals(x, false, "getAndSet boolean value"); 259 } 260 261 262 // get and bitwise or 263 { 264 hs.get(TestAccessMode.SET).invokeExact(recv, true); 265 266 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(recv, false); 267 assertEquals(o, true, "getAndBitwiseOr boolean"); 268 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 269 assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value"); 270 } 271 272 { 273 hs.get(TestAccessMode.SET).invokeExact(recv, true); 274 275 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(recv, false); 276 assertEquals(o, true, "getAndBitwiseOrAcquire boolean"); 277 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 278 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value"); 279 } 280 281 { 282 hs.get(TestAccessMode.SET).invokeExact(recv, true); 283 284 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(recv, false); 285 assertEquals(o, true, "getAndBitwiseOrRelease boolean"); 286 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 287 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value"); 288 } 289 290 // get and bitwise and 291 { 292 hs.get(TestAccessMode.SET).invokeExact(recv, true); 293 294 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(recv, false); 295 assertEquals(o, true, "getAndBitwiseAnd boolean"); 296 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 297 assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value"); 298 } 299 300 { 301 hs.get(TestAccessMode.SET).invokeExact(recv, true); 302 303 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(recv, false); 304 assertEquals(o, true, "getAndBitwiseAndAcquire boolean"); 305 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 306 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value"); 307 } 308 309 { 310 hs.get(TestAccessMode.SET).invokeExact(recv, true); 311 312 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(recv, false); 313 assertEquals(o, true, "getAndBitwiseAndRelease boolean"); 314 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 315 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value"); 316 } 317 318 // get and bitwise xor 319 { 320 hs.get(TestAccessMode.SET).invokeExact(recv, true); 321 322 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(recv, false); 323 assertEquals(o, true, "getAndBitwiseXor boolean"); 324 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 325 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value"); 326 } 327 328 { 329 hs.get(TestAccessMode.SET).invokeExact(recv, true); 330 331 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(recv, false); 332 assertEquals(o, true, "getAndBitwiseXorAcquire boolean"); 333 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 334 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value"); 335 } 336 337 { 338 hs.get(TestAccessMode.SET).invokeExact(recv, true); 339 340 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, false); 341 assertEquals(o, true, "getAndBitwiseXorRelease boolean"); 342 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv); 343 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value"); 344 } 345 } 346 testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessBoolean recv, Handles hs)347 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable { 348 349 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 350 checkUOE(am, () -> { 351 boolean r = (boolean) hs.get(am).invokeExact(recv, true); 352 }); 353 } 354 355 } 356 357 testStaticField(Handles hs)358 static void testStaticField(Handles hs) throws Throwable { 359 // Plain 360 { 361 hs.get(TestAccessMode.SET).invokeExact(true); 362 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 363 assertEquals(x, true, "set boolean value"); 364 } 365 366 367 // Volatile 368 { 369 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(false); 370 boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); 371 assertEquals(x, false, "setVolatile boolean value"); 372 } 373 374 // Lazy 375 { 376 hs.get(TestAccessMode.SET_RELEASE).invokeExact(true); 377 boolean x = (boolean) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); 378 assertEquals(x, true, "setRelease boolean value"); 379 } 380 381 // Opaque 382 { 383 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(false); 384 boolean x = (boolean) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); 385 assertEquals(x, false, "setOpaque boolean value"); 386 } 387 388 hs.get(TestAccessMode.SET).invokeExact(true); 389 390 // Compare 391 { 392 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(true, false); 393 assertEquals(r, true, "success compareAndSet boolean"); 394 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 395 assertEquals(x, false, "success compareAndSet boolean value"); 396 } 397 398 { 399 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(true, false); 400 assertEquals(r, false, "failing compareAndSet boolean"); 401 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 402 assertEquals(x, false, "failing compareAndSet boolean value"); 403 } 404 405 { 406 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(false, true); 407 assertEquals(r, false, "success compareAndExchange boolean"); 408 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 409 assertEquals(x, true, "success compareAndExchange boolean value"); 410 } 411 412 { 413 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(false, false); 414 assertEquals(r, true, "failing compareAndExchange boolean"); 415 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 416 assertEquals(x, true, "failing compareAndExchange boolean value"); 417 } 418 419 { 420 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(true, false); 421 assertEquals(r, true, "success compareAndExchangeAcquire boolean"); 422 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 423 assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); 424 } 425 426 { 427 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(true, false); 428 assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); 429 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 430 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); 431 } 432 433 { 434 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(false, true); 435 assertEquals(r, false, "success compareAndExchangeRelease boolean"); 436 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 437 assertEquals(x, true, "success compareAndExchangeRelease boolean value"); 438 } 439 440 { 441 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(false, false); 442 assertEquals(r, true, "failing compareAndExchangeRelease boolean"); 443 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 444 assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); 445 } 446 447 { 448 boolean success = false; 449 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 450 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(true, false); 451 } 452 assertEquals(success, true, "weakCompareAndSetPlain boolean"); 453 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 454 assertEquals(x, false, "weakCompareAndSetPlain boolean value"); 455 } 456 457 { 458 boolean success = false; 459 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 460 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(false, true); 461 } 462 assertEquals(success, true, "weakCompareAndSetAcquire boolean"); 463 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 464 assertEquals(x, true, "weakCompareAndSetAcquire boolean"); 465 } 466 467 { 468 boolean success = false; 469 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 470 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(true, false); 471 } 472 assertEquals(success, true, "weakCompareAndSetRelease boolean"); 473 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 474 assertEquals(x, false, "weakCompareAndSetRelease boolean"); 475 } 476 477 { 478 boolean success = false; 479 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 480 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(false, true); 481 } 482 assertEquals(success, true, "weakCompareAndSet boolean"); 483 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 484 assertEquals(x, true, "weakCompareAndSet boolean"); 485 } 486 487 // Compare set and get 488 { 489 hs.get(TestAccessMode.SET).invokeExact(true); 490 491 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET).invokeExact(false); 492 assertEquals(o, true, "getAndSet boolean"); 493 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 494 assertEquals(x, false, "getAndSet boolean value"); 495 } 496 497 // Compare set and get 498 { 499 hs.get(TestAccessMode.SET).invokeExact(true); 500 501 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(false); 502 assertEquals(o, true, "getAndSetAcquire boolean"); 503 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 504 assertEquals(x, false, "getAndSetAcquire boolean value"); 505 } 506 507 // Compare set and get 508 { 509 hs.get(TestAccessMode.SET).invokeExact(true); 510 511 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(false); 512 assertEquals(o, true, "getAndSetRelease boolean"); 513 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 514 assertEquals(x, false, "getAndSetRelease boolean value"); 515 } 516 517 518 // get and bitwise or 519 { 520 hs.get(TestAccessMode.SET).invokeExact(true); 521 522 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(false); 523 assertEquals(o, true, "getAndBitwiseOr boolean"); 524 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 525 assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value"); 526 } 527 528 { 529 hs.get(TestAccessMode.SET).invokeExact(true); 530 531 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(false); 532 assertEquals(o, true, "getAndBitwiseOrAcquire boolean"); 533 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 534 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value"); 535 } 536 537 { 538 hs.get(TestAccessMode.SET).invokeExact(true); 539 540 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(false); 541 assertEquals(o, true, "getAndBitwiseOrRelease boolean"); 542 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 543 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value"); 544 } 545 546 // get and bitwise and 547 { 548 hs.get(TestAccessMode.SET).invokeExact(true); 549 550 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(false); 551 assertEquals(o, true, "getAndBitwiseAnd boolean"); 552 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 553 assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value"); 554 } 555 556 { 557 hs.get(TestAccessMode.SET).invokeExact(true); 558 559 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(false); 560 assertEquals(o, true, "getAndBitwiseAndAcquire boolean"); 561 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 562 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value"); 563 } 564 565 { 566 hs.get(TestAccessMode.SET).invokeExact(true); 567 568 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(false); 569 assertEquals(o, true, "getAndBitwiseAndRelease boolean"); 570 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 571 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value"); 572 } 573 574 // get and bitwise xor 575 { 576 hs.get(TestAccessMode.SET).invokeExact(true); 577 578 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(false); 579 assertEquals(o, true, "getAndBitwiseXor boolean"); 580 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 581 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value"); 582 } 583 584 { 585 hs.get(TestAccessMode.SET).invokeExact(true); 586 587 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(false); 588 assertEquals(o, true, "getAndBitwiseXorAcquire boolean"); 589 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 590 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value"); 591 } 592 593 { 594 hs.get(TestAccessMode.SET).invokeExact(true); 595 596 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(false); 597 assertEquals(o, true, "getAndBitwiseXorRelease boolean"); 598 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(); 599 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value"); 600 } 601 } 602 testStaticFieldUnsupported(Handles hs)603 static void testStaticFieldUnsupported(Handles hs) throws Throwable { 604 605 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 606 checkUOE(am, () -> { 607 boolean r = (boolean) hs.get(am).invokeExact(true); 608 }); 609 } 610 611 } 612 613 testArray(Handles hs)614 static void testArray(Handles hs) throws Throwable { 615 boolean[] array = new boolean[10]; 616 617 for (int i = 0; i < array.length; i++) { 618 // Plain 619 { 620 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 621 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 622 assertEquals(x, true, "get boolean value"); 623 } 624 625 626 // Volatile 627 { 628 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, false); 629 boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); 630 assertEquals(x, false, "setVolatile boolean value"); 631 } 632 633 // Lazy 634 { 635 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, true); 636 boolean x = (boolean) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); 637 assertEquals(x, true, "setRelease boolean value"); 638 } 639 640 // Opaque 641 { 642 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, false); 643 boolean x = (boolean) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); 644 assertEquals(x, false, "setOpaque boolean value"); 645 } 646 647 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 648 649 // Compare 650 { 651 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, true, false); 652 assertEquals(r, true, "success compareAndSet boolean"); 653 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 654 assertEquals(x, false, "success compareAndSet boolean value"); 655 } 656 657 { 658 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, true, false); 659 assertEquals(r, false, "failing compareAndSet boolean"); 660 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 661 assertEquals(x, false, "failing compareAndSet boolean value"); 662 } 663 664 { 665 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, false, true); 666 assertEquals(r, false, "success compareAndExchange boolean"); 667 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 668 assertEquals(x, true, "success compareAndExchange boolean value"); 669 } 670 671 { 672 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, false, false); 673 assertEquals(r, true, "failing compareAndExchange boolean"); 674 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 675 assertEquals(x, true, "failing compareAndExchange boolean value"); 676 } 677 678 { 679 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, true, false); 680 assertEquals(r, true, "success compareAndExchangeAcquire boolean"); 681 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 682 assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); 683 } 684 685 { 686 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, true, false); 687 assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); 688 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 689 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); 690 } 691 692 { 693 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, false, true); 694 assertEquals(r, false, "success compareAndExchangeRelease boolean"); 695 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 696 assertEquals(x, true, "success compareAndExchangeRelease boolean value"); 697 } 698 699 { 700 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, false, false); 701 assertEquals(r, true, "failing compareAndExchangeRelease boolean"); 702 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 703 assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); 704 } 705 706 { 707 boolean success = false; 708 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 709 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, true, false); 710 } 711 assertEquals(success, true, "weakCompareAndSetPlain boolean"); 712 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 713 assertEquals(x, false, "weakCompareAndSetPlain boolean value"); 714 } 715 716 { 717 boolean success = false; 718 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 719 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, false, true); 720 } 721 assertEquals(success, true, "weakCompareAndSetAcquire boolean"); 722 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 723 assertEquals(x, true, "weakCompareAndSetAcquire boolean"); 724 } 725 726 { 727 boolean success = false; 728 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 729 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, true, false); 730 } 731 assertEquals(success, true, "weakCompareAndSetRelease boolean"); 732 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 733 assertEquals(x, false, "weakCompareAndSetRelease boolean"); 734 } 735 736 { 737 boolean success = false; 738 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 739 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, false, true); 740 } 741 assertEquals(success, true, "weakCompareAndSet boolean"); 742 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 743 assertEquals(x, true, "weakCompareAndSet boolean"); 744 } 745 746 // Compare set and get 747 { 748 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 749 750 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, false); 751 assertEquals(o, true, "getAndSet boolean"); 752 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 753 assertEquals(x, false, "getAndSet boolean value"); 754 } 755 756 { 757 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 758 759 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, false); 760 assertEquals(o, true, "getAndSetAcquire boolean"); 761 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 762 assertEquals(x, false, "getAndSetAcquire boolean value"); 763 } 764 765 { 766 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 767 768 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, false); 769 assertEquals(o, true, "getAndSetRelease boolean"); 770 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 771 assertEquals(x, false, "getAndSetRelease boolean value"); 772 } 773 774 775 // get and bitwise or 776 { 777 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 778 779 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(array, i, false); 780 assertEquals(o, true, "getAndBitwiseOr boolean"); 781 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 782 assertEquals(x, (boolean)(true | false), "getAndBitwiseOr boolean value"); 783 } 784 785 { 786 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 787 788 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(array, i, false); 789 assertEquals(o, true, "getAndBitwiseOrAcquire boolean"); 790 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 791 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrAcquire boolean value"); 792 } 793 794 { 795 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 796 797 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(array, i, false); 798 assertEquals(o, true, "getAndBitwiseOrRelease boolean"); 799 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 800 assertEquals(x, (boolean)(true | false), "getAndBitwiseOrRelease boolean value"); 801 } 802 803 // get and bitwise and 804 { 805 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 806 807 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(array, i, false); 808 assertEquals(o, true, "getAndBitwiseAnd boolean"); 809 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 810 assertEquals(x, (boolean)(true & false), "getAndBitwiseAnd boolean value"); 811 } 812 813 { 814 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 815 816 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(array, i, false); 817 assertEquals(o, true, "getAndBitwiseAndAcquire boolean"); 818 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 819 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndAcquire boolean value"); 820 } 821 822 { 823 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 824 825 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(array, i, false); 826 assertEquals(o, true, "getAndBitwiseAndRelease boolean"); 827 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 828 assertEquals(x, (boolean)(true & false), "getAndBitwiseAndRelease boolean value"); 829 } 830 831 // get and bitwise xor 832 { 833 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 834 835 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(array, i, false); 836 assertEquals(o, true, "getAndBitwiseXor boolean"); 837 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 838 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXor boolean value"); 839 } 840 841 { 842 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 843 844 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(array, i, false); 845 assertEquals(o, true, "getAndBitwiseXorAcquire boolean"); 846 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 847 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorAcquire boolean value"); 848 } 849 850 { 851 hs.get(TestAccessMode.SET).invokeExact(array, i, true); 852 853 boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(array, i, false); 854 assertEquals(o, true, "getAndBitwiseXorRelease boolean"); 855 boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(array, i); 856 assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value"); 857 } 858 } 859 } 860 testArrayUnsupported(Handles hs)861 static void testArrayUnsupported(Handles hs) throws Throwable { 862 boolean[] array = new boolean[10]; 863 864 final int i = 0; 865 866 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 867 checkUOE(am, () -> { 868 boolean o = (boolean) hs.get(am).invokeExact(array, i, true); 869 }); 870 } 871 872 } 873 testArrayIndexOutOfBounds(Handles hs)874 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable { 875 boolean[] array = new boolean[10]; 876 877 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { 878 final int ci = i; 879 880 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { 881 checkIOOBE(am, () -> { 882 boolean x = (boolean) hs.get(am).invokeExact(array, ci); 883 }); 884 } 885 886 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { 887 checkIOOBE(am, () -> { 888 hs.get(am).invokeExact(array, ci, true); 889 }); 890 } 891 892 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { 893 checkIOOBE(am, () -> { 894 boolean r = (boolean) hs.get(am).invokeExact(array, ci, true, false); 895 }); 896 } 897 898 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { 899 checkIOOBE(am, () -> { 900 boolean r = (boolean) hs.get(am).invokeExact(array, ci, false, true); 901 }); 902 } 903 904 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { 905 checkIOOBE(am, () -> { 906 boolean o = (boolean) hs.get(am).invokeExact(array, ci, true); 907 }); 908 } 909 910 911 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) { 912 checkIOOBE(am, () -> { 913 boolean o = (boolean) hs.get(am).invokeExact(array, ci, false); 914 }); 915 } 916 } 917 } 918 } 919 920