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 VarHandleTestAccessByte 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 VarHandleTestAccessByte 34 * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 VarHandleTestAccessByte 35 * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation VarHandleTestAccessByte 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 VarHandleTestAccessByte extends VarHandleBaseTest { 53 static final byte static_final_v = (byte)0x01; 54 55 static byte static_v; 56 57 final byte final_v = (byte)0x01; 58 59 byte v; 60 61 static final byte static_final_v2 = (byte)0x01; 62 63 static byte static_v2; 64 65 final byte final_v2 = (byte)0x01; 66 67 byte 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 VarHandleTestAccessByte.class, "final_v" + postfix, byte.class); 88 vhs.add(vh); 89 90 vh = MethodHandles.lookup().findVarHandle( 91 VarHandleTestAccessByte.class, "v" + postfix, byte.class); 92 vhs.add(vh); 93 94 vh = MethodHandles.lookup().findStaticVarHandle( 95 VarHandleTestAccessByte.class, "static_final_v" + postfix, byte.class); 96 vhs.add(vh); 97 98 vh = MethodHandles.lookup().findStaticVarHandle( 99 VarHandleTestAccessByte.class, "static_v" + postfix, byte.class); 100 vhs.add(vh); 101 102 if (same) { 103 vh = MethodHandles.arrayElementVarHandle(byte[].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 VarHandleTestAccessByte.class, "final_v", byte.class); 119 120 vhField = MethodHandles.lookup().findVarHandle( 121 VarHandleTestAccessByte.class, "v", byte.class); 122 123 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( 124 VarHandleTestAccessByte.class, "static_final_v", byte.class); 125 126 vhStaticField = MethodHandles.lookup().findStaticVarHandle( 127 VarHandleTestAccessByte.class, "static_v", byte.class); 128 129 vhArray = MethodHandles.arrayElementVarHandle(byte[].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 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); 186 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)); 187 assertTrue(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(VarHandleTestAccessByte.class)}); 203 types.add(new Object[] {vhStaticField, Arrays.asList()}); 204 types.add(new Object[] {vhArray, Arrays.asList(byte[].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(byte.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 VarHandleTestAccessByte.class, "final_v", byte.class); 224 }); 225 226 checkIAE("Lookup of static field to instance field", () -> { 227 MethodHandles.lookup().findStaticVarHandle( 228 VarHandleTestAccessByte.class, "v", byte.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 VarHandleTestAccessByte.class, "static_final_v", byte.class); 237 }); 238 239 checkIAE("Lookup of instance field to static field", () -> { 240 vhStaticField = MethodHandles.lookup().findVarHandle( 241 VarHandleTestAccessByte.class, "static_v", byte.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, VarHandleTestAccessByte::testStaticFinalField)); 256 cases.add(new VarHandleAccessTestCase("Static final field unsupported", 257 vhStaticFinalField, VarHandleTestAccessByte::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, VarHandleTestAccessByte::testStaticField)); 268 cases.add(new VarHandleAccessTestCase("Static field unsupported", 269 vhStaticField, VarHandleTestAccessByte::testStaticFieldUnsupported, 270 false)); 271 272 cases.add(new VarHandleAccessTestCase("Array", 273 vhArray, VarHandleTestAccessByte::testArray)); 274 cases.add(new VarHandleAccessTestCase("Array unsupported", 275 vhArray, VarHandleTestAccessByte::testArrayUnsupported, 276 false)); 277 cases.add(new VarHandleAccessTestCase("Array index out of bounds", 278 vhArray, VarHandleTestAccessByte::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(VarHandleTestAccessByte recv, VarHandle vh) { 297 // Plain 298 { 299 byte x = (byte) vh.get(recv); 300 assertEquals((byte)0x01, x, "get byte value"); 301 } 302 303 304 // Volatile 305 { 306 byte x = (byte) vh.getVolatile(recv); 307 assertEquals((byte)0x01, x, "getVolatile byte value"); 308 } 309 310 // Lazy 311 { 312 byte x = (byte) vh.getAcquire(recv); 313 assertEquals((byte)0x01, x, "getRelease byte value"); 314 } 315 316 // Opaque 317 { 318 byte x = (byte) vh.getOpaque(recv); 319 assertEquals((byte)0x01, x, "getOpaque byte value"); 320 } 321 } 322 323 static void testInstanceFinalFieldUnsupported(VarHandleTestAccessByte recv, VarHandle vh) { 324 checkUOE(() -> { 325 vh.set(recv, (byte)0x23); 326 }); 327 328 checkUOE(() -> { 329 vh.setVolatile(recv, (byte)0x23); 330 }); 331 332 checkUOE(() -> { 333 vh.setRelease(recv, (byte)0x23); 334 }); 335 336 checkUOE(() -> { 337 vh.setOpaque(recv, (byte)0x23); 338 }); 339 340 341 342 } 343 344 345 static void testStaticFinalField(VarHandle vh) { 346 // Plain 347 { 348 byte x = (byte) vh.get(); 349 assertEquals((byte)0x01, x, "get byte value"); 350 } 351 352 353 // Volatile 354 { 355 byte x = (byte) vh.getVolatile(); 356 assertEquals((byte)0x01, x, "getVolatile byte value"); 357 } 358 359 // Lazy 360 { 361 byte x = (byte) vh.getAcquire(); 362 assertEquals((byte)0x01, x, "getRelease byte value"); 363 } 364 365 // Opaque 366 { 367 byte x = (byte) vh.getOpaque(); 368 assertEquals((byte)0x01, x, "getOpaque byte value"); 369 } 370 } 371 372 static void testStaticFinalFieldUnsupported(VarHandle vh) { 373 checkUOE(() -> { 374 vh.set((byte)0x23); 375 }); 376 377 checkUOE(() -> { 378 vh.setVolatile((byte)0x23); 379 }); 380 381 checkUOE(() -> { 382 vh.setRelease((byte)0x23); 383 }); 384 385 checkUOE(() -> { 386 vh.setOpaque((byte)0x23); 387 }); 388 389 390 391 } 392 393 394 static void testInstanceField(VarHandleTestAccessByte recv, VarHandle vh) { 395 // Plain 396 { 397 vh.set(recv, (byte)0x01); 398 byte x = (byte) vh.get(recv); 399 assertEquals((byte)0x01, x, "set byte value"); 400 } 401 402 403 // Volatile 404 { 405 vh.setVolatile(recv, (byte)0x23); 406 byte x = (byte) vh.getVolatile(recv); 407 assertEquals((byte)0x23, x, "setVolatile byte value"); 408 } 409 410 // Lazy 411 { 412 vh.setRelease(recv, (byte)0x01); 413 byte x = (byte) vh.getAcquire(recv); 414 assertEquals((byte)0x01, x, "setRelease byte value"); 415 } 416 417 // Opaque 418 { 419 vh.setOpaque(recv, (byte)0x23); 420 byte x = (byte) vh.getOpaque(recv); 421 assertEquals((byte)0x23, x, "setOpaque byte value"); 422 } 423 424 vh.set(recv, (byte)0x01); 425 426 // Compare 427 { 428 boolean r = vh.compareAndSet(recv, (byte)0x01, (byte)0x23); 429 assertEquals(r, true, "success compareAndSet byte"); 430 byte x = (byte) vh.get(recv); 431 assertEquals((byte)0x23, x, "success compareAndSet byte value"); 432 } 433 434 { 435 boolean r = vh.compareAndSet(recv, (byte)0x01, (byte)0x45); 436 assertEquals(r, false, "failing compareAndSet byte"); 437 byte x = (byte) vh.get(recv); 438 assertEquals((byte)0x23, x, "failing compareAndSet byte value"); 439 } 440 441 { 442 byte r = (byte) vh.compareAndExchange(recv, (byte)0x23, (byte)0x01); 443 assertEquals(r, (byte)0x23, "success compareAndExchange byte"); 444 byte x = (byte) vh.get(recv); 445 assertEquals((byte)0x01, x, "success compareAndExchange byte value"); 446 } 447 448 { 449 byte r = (byte) vh.compareAndExchange(recv, (byte)0x23, (byte)0x45); 450 assertEquals(r, (byte)0x01, "failing compareAndExchange byte"); 451 byte x = (byte) vh.get(recv); 452 assertEquals((byte)0x01, x, "failing compareAndExchange byte value"); 453 } 454 455 { 456 byte r = (byte) vh.compareAndExchangeAcquire(recv, (byte)0x01, (byte)0x23); 457 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte"); 458 byte x = (byte) vh.get(recv); 459 assertEquals((byte)0x23, x, "success compareAndExchangeAcquire byte value"); 460 } 461 462 { 463 byte r = (byte) vh.compareAndExchangeAcquire(recv, (byte)0x01, (byte)0x45); 464 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte"); 465 byte x = (byte) vh.get(recv); 466 assertEquals((byte)0x23, x, "failing compareAndExchangeAcquire byte value"); 467 } 468 469 { 470 byte r = (byte) vh.compareAndExchangeRelease(recv, (byte)0x23, (byte)0x01); 471 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte"); 472 byte x = (byte) vh.get(recv); 473 assertEquals((byte)0x01, x, "success compareAndExchangeRelease byte value"); 474 } 475 476 { 477 byte r = (byte) vh.compareAndExchangeRelease(recv, (byte)0x23, (byte)0x45); 478 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte"); 479 byte x = (byte) vh.get(recv); 480 assertEquals((byte)0x01, x, "failing compareAndExchangeRelease byte value"); 481 } 482 483 { 484 boolean success = false; 485 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 486 success = vh.weakCompareAndSetPlain(recv, (byte)0x01, (byte)0x23); 487 if (!success) weakDelay(); 488 } 489 assertEquals(success, true, "success weakCompareAndSetPlain byte"); 490 byte x = (byte) vh.get(recv); 491 assertEquals((byte)0x23, x, "success weakCompareAndSetPlain byte value"); 492 } 493 494 { 495 boolean success = vh.weakCompareAndSetPlain(recv, (byte)0x01, (byte)0x45); 496 assertEquals(success, false, "failing weakCompareAndSetPlain byte"); 497 byte x = (byte) vh.get(recv); 498 assertEquals((byte)0x23, x, "failing weakCompareAndSetPlain byte value"); 499 } 500 501 { 502 boolean success = false; 503 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 504 success = vh.weakCompareAndSetAcquire(recv, (byte)0x23, (byte)0x01); 505 if (!success) weakDelay(); 506 } 507 assertEquals(success, true, "success weakCompareAndSetAcquire byte"); 508 byte x = (byte) vh.get(recv); 509 assertEquals((byte)0x01, x, "success weakCompareAndSetAcquire byte"); 510 } 511 512 { 513 boolean success = vh.weakCompareAndSetAcquire(recv, (byte)0x23, (byte)0x45); 514 assertEquals(success, false, "failing weakCompareAndSetAcquire byte"); 515 byte x = (byte) vh.get(recv); 516 assertEquals((byte)0x01, x, "failing weakCompareAndSetAcquire byte value"); 517 } 518 519 { 520 boolean success = false; 521 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 522 success = vh.weakCompareAndSetRelease(recv, (byte)0x01, (byte)0x23); 523 if (!success) weakDelay(); 524 } 525 assertEquals(success, true, "success weakCompareAndSetRelease byte"); 526 byte x = (byte) vh.get(recv); 527 assertEquals((byte)0x23, x, "success weakCompareAndSetRelease byte"); 528 } 529 530 { 531 boolean success = vh.weakCompareAndSetRelease(recv, (byte)0x01, (byte)0x45); 532 assertEquals(success, false, "failing weakCompareAndSetRelease byte"); 533 byte x = (byte) vh.get(recv); 534 assertEquals((byte)0x23, x, "failing weakCompareAndSetRelease byte value"); 535 } 536 537 { 538 boolean success = false; 539 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 540 success = vh.weakCompareAndSet(recv, (byte)0x23, (byte)0x01); 541 if (!success) weakDelay(); 542 } 543 assertEquals(success, true, "success weakCompareAndSet byte"); 544 byte x = (byte) vh.get(recv); 545 assertEquals((byte)0x01, x, "success weakCompareAndSet byte value"); 546 } 547 548 { 549 boolean success = vh.weakCompareAndSet(recv, (byte)0x23, (byte)0x45); 550 assertEquals(success, false, "failing weakCompareAndSet byte"); 551 byte x = (byte) vh.get(recv); 552 assertEquals((byte)0x01, x, "failing weakCompareAndSet byte value"); 553 } 554 555 // Compare set and get 556 { 557 vh.set(recv, (byte)0x01); 558 559 byte o = (byte) vh.getAndSet(recv, (byte)0x23); 560 assertEquals((byte)0x01, o, "getAndSet byte"); 561 byte x = (byte) vh.get(recv); 562 assertEquals((byte)0x23, x, "getAndSet byte value"); 563 } 564 565 { 566 vh.set(recv, (byte)0x01); 567 568 byte o = (byte) vh.getAndSetAcquire(recv, (byte)0x23); 569 assertEquals((byte)0x01, o, "getAndSetAcquire byte"); 570 byte x = (byte) vh.get(recv); 571 assertEquals((byte)0x23, x, "getAndSetAcquire byte value"); 572 } 573 574 { 575 vh.set(recv, (byte)0x01); 576 577 byte o = (byte) vh.getAndSetRelease(recv, (byte)0x23); 578 assertEquals((byte)0x01, o, "getAndSetRelease byte"); 579 byte x = (byte) vh.get(recv); 580 assertEquals((byte)0x23, x, "getAndSetRelease byte value"); 581 } 582 583 // get and add, add and get 584 { 585 vh.set(recv, (byte)0x01); 586 587 byte o = (byte) vh.getAndAdd(recv, (byte)0x23); 588 assertEquals((byte)0x01, o, "getAndAdd byte"); 589 byte x = (byte) vh.get(recv); 590 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAdd byte value"); 591 } 592 593 { 594 vh.set(recv, (byte)0x01); 595 596 byte o = (byte) vh.getAndAddAcquire(recv, (byte)0x23); 597 assertEquals((byte)0x01, o, "getAndAddAcquire byte"); 598 byte x = (byte) vh.get(recv); 599 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddAcquire byte value"); 600 } 601 602 { 603 vh.set(recv, (byte)0x01); 604 605 byte o = (byte) vh.getAndAddRelease(recv, (byte)0x23); 606 assertEquals((byte)0x01, o, "getAndAddReleasebyte"); 607 byte x = (byte) vh.get(recv); 608 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddRelease byte value"); 609 } 610 611 // get and bitwise or 612 { 613 vh.set(recv, (byte)0x01); 614 615 byte o = (byte) vh.getAndBitwiseOr(recv, (byte)0x23); 616 assertEquals((byte)0x01, o, "getAndBitwiseOr byte"); 617 byte x = (byte) vh.get(recv); 618 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOr byte value"); 619 } 620 621 { 622 vh.set(recv, (byte)0x01); 623 624 byte o = (byte) vh.getAndBitwiseOrAcquire(recv, (byte)0x23); 625 assertEquals((byte)0x01, o, "getAndBitwiseOrAcquire byte"); 626 byte x = (byte) vh.get(recv); 627 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrAcquire byte value"); 628 } 629 630 { 631 vh.set(recv, (byte)0x01); 632 633 byte o = (byte) vh.getAndBitwiseOrRelease(recv, (byte)0x23); 634 assertEquals((byte)0x01, o, "getAndBitwiseOrRelease byte"); 635 byte x = (byte) vh.get(recv); 636 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrRelease byte value"); 637 } 638 639 // get and bitwise and 640 { 641 vh.set(recv, (byte)0x01); 642 643 byte o = (byte) vh.getAndBitwiseAnd(recv, (byte)0x23); 644 assertEquals((byte)0x01, o, "getAndBitwiseAnd byte"); 645 byte x = (byte) vh.get(recv); 646 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAnd byte value"); 647 } 648 649 { 650 vh.set(recv, (byte)0x01); 651 652 byte o = (byte) vh.getAndBitwiseAndAcquire(recv, (byte)0x23); 653 assertEquals((byte)0x01, o, "getAndBitwiseAndAcquire byte"); 654 byte x = (byte) vh.get(recv); 655 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndAcquire byte value"); 656 } 657 658 { 659 vh.set(recv, (byte)0x01); 660 661 byte o = (byte) vh.getAndBitwiseAndRelease(recv, (byte)0x23); 662 assertEquals((byte)0x01, o, "getAndBitwiseAndRelease byte"); 663 byte x = (byte) vh.get(recv); 664 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndRelease byte value"); 665 } 666 667 // get and bitwise xor 668 { 669 vh.set(recv, (byte)0x01); 670 671 byte o = (byte) vh.getAndBitwiseXor(recv, (byte)0x23); 672 assertEquals((byte)0x01, o, "getAndBitwiseXor byte"); 673 byte x = (byte) vh.get(recv); 674 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXor byte value"); 675 } 676 677 { 678 vh.set(recv, (byte)0x01); 679 680 byte o = (byte) vh.getAndBitwiseXorAcquire(recv, (byte)0x23); 681 assertEquals((byte)0x01, o, "getAndBitwiseXorAcquire byte"); 682 byte x = (byte) vh.get(recv); 683 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorAcquire byte value"); 684 } 685 686 { 687 vh.set(recv, (byte)0x01); 688 689 byte o = (byte) vh.getAndBitwiseXorRelease(recv, (byte)0x23); 690 assertEquals((byte)0x01, o, "getAndBitwiseXorRelease byte"); 691 byte x = (byte) vh.get(recv); 692 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorRelease byte value"); 693 } 694 } 695 696 static void testInstanceFieldUnsupported(VarHandleTestAccessByte recv, VarHandle vh) { 697 698 699 } 700 701 702 static void testStaticField(VarHandle vh) { 703 // Plain 704 { 705 vh.set((byte)0x01); 706 byte x = (byte) vh.get(); 707 assertEquals((byte)0x01, x, "set byte value"); 708 } 709 710 711 // Volatile 712 { 713 vh.setVolatile((byte)0x23); 714 byte x = (byte) vh.getVolatile(); 715 assertEquals((byte)0x23, x, "setVolatile byte value"); 716 } 717 718 // Lazy 719 { 720 vh.setRelease((byte)0x01); 721 byte x = (byte) vh.getAcquire(); 722 assertEquals((byte)0x01, x, "setRelease byte value"); 723 } 724 725 // Opaque 726 { 727 vh.setOpaque((byte)0x23); 728 byte x = (byte) vh.getOpaque(); 729 assertEquals((byte)0x23, x, "setOpaque byte value"); 730 } 731 732 vh.set((byte)0x01); 733 734 // Compare 735 { 736 boolean r = vh.compareAndSet((byte)0x01, (byte)0x23); 737 assertEquals(r, true, "success compareAndSet byte"); 738 byte x = (byte) vh.get(); 739 assertEquals((byte)0x23, x, "success compareAndSet byte value"); 740 } 741 742 { 743 boolean r = vh.compareAndSet((byte)0x01, (byte)0x45); 744 assertEquals(r, false, "failing compareAndSet byte"); 745 byte x = (byte) vh.get(); 746 assertEquals((byte)0x23, x, "failing compareAndSet byte value"); 747 } 748 749 { 750 byte r = (byte) vh.compareAndExchange((byte)0x23, (byte)0x01); 751 assertEquals(r, (byte)0x23, "success compareAndExchange byte"); 752 byte x = (byte) vh.get(); 753 assertEquals((byte)0x01, x, "success compareAndExchange byte value"); 754 } 755 756 { 757 byte r = (byte) vh.compareAndExchange((byte)0x23, (byte)0x45); 758 assertEquals(r, (byte)0x01, "failing compareAndExchange byte"); 759 byte x = (byte) vh.get(); 760 assertEquals((byte)0x01, x, "failing compareAndExchange byte value"); 761 } 762 763 { 764 byte r = (byte) vh.compareAndExchangeAcquire((byte)0x01, (byte)0x23); 765 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte"); 766 byte x = (byte) vh.get(); 767 assertEquals((byte)0x23, x, "success compareAndExchangeAcquire byte value"); 768 } 769 770 { 771 byte r = (byte) vh.compareAndExchangeAcquire((byte)0x01, (byte)0x45); 772 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte"); 773 byte x = (byte) vh.get(); 774 assertEquals((byte)0x23, x, "failing compareAndExchangeAcquire byte value"); 775 } 776 777 { 778 byte r = (byte) vh.compareAndExchangeRelease((byte)0x23, (byte)0x01); 779 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte"); 780 byte x = (byte) vh.get(); 781 assertEquals((byte)0x01, x, "success compareAndExchangeRelease byte value"); 782 } 783 784 { 785 byte r = (byte) vh.compareAndExchangeRelease((byte)0x23, (byte)0x45); 786 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte"); 787 byte x = (byte) vh.get(); 788 assertEquals((byte)0x01, x, "failing compareAndExchangeRelease byte value"); 789 } 790 791 { 792 boolean success = false; 793 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 794 success = vh.weakCompareAndSetPlain((byte)0x01, (byte)0x23); 795 if (!success) weakDelay(); 796 } 797 assertEquals(success, true, "success weakCompareAndSetPlain byte"); 798 byte x = (byte) vh.get(); 799 assertEquals((byte)0x23, x, "success weakCompareAndSetPlain byte value"); 800 } 801 802 { 803 boolean success = vh.weakCompareAndSetPlain((byte)0x01, (byte)0x45); 804 assertEquals(success, false, "failing weakCompareAndSetPlain byte"); 805 byte x = (byte) vh.get(); 806 assertEquals((byte)0x23, x, "failing weakCompareAndSetPlain byte value"); 807 } 808 809 { 810 boolean success = false; 811 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 812 success = vh.weakCompareAndSetAcquire((byte)0x23, (byte)0x01); 813 if (!success) weakDelay(); 814 } 815 assertEquals(success, true, "success weakCompareAndSetAcquire byte"); 816 byte x = (byte) vh.get(); 817 assertEquals((byte)0x01, x, "success weakCompareAndSetAcquire byte"); 818 } 819 820 { 821 boolean success = vh.weakCompareAndSetAcquire((byte)0x23, (byte)0x45); 822 assertEquals(success, false, "failing weakCompareAndSetAcquire byte"); 823 byte x = (byte) vh.get(); 824 assertEquals((byte)0x01, x, "failing weakCompareAndSetAcquire byte value"); 825 } 826 827 { 828 boolean success = false; 829 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 830 success = vh.weakCompareAndSetRelease((byte)0x01, (byte)0x23); 831 if (!success) weakDelay(); 832 } 833 assertEquals(success, true, "success weakCompareAndSetRelease byte"); 834 byte x = (byte) vh.get(); 835 assertEquals((byte)0x23, x, "success weakCompareAndSetRelease byte"); 836 } 837 838 { 839 boolean success = vh.weakCompareAndSetRelease((byte)0x01, (byte)0x45); 840 assertEquals(success, false, "failing weakCompareAndSetRelease byte"); 841 byte x = (byte) vh.get(); 842 assertEquals((byte)0x23, x, "failing weakCompareAndSetRelease byte value"); 843 } 844 845 { 846 boolean success = false; 847 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 848 success = vh.weakCompareAndSet((byte)0x23, (byte)0x01); 849 if (!success) weakDelay(); 850 } 851 assertEquals(success, true, "success weakCompareAndSet byte"); 852 byte x = (byte) vh.get(); 853 assertEquals((byte)0x01, x, "success weakCompareAndSet byte"); 854 } 855 856 { 857 boolean success = vh.weakCompareAndSet((byte)0x23, (byte)0x45); 858 assertEquals(success, false, "failing weakCompareAndSet byte"); 859 byte x = (byte) vh.get(); 860 assertEquals((byte)0x01, x, "failing weakCompareAndSet byte value"); 861 } 862 863 // Compare set and get 864 { 865 vh.set((byte)0x01); 866 867 byte o = (byte) vh.getAndSet((byte)0x23); 868 assertEquals((byte)0x01, o, "getAndSet byte"); 869 byte x = (byte) vh.get(); 870 assertEquals((byte)0x23, x, "getAndSet byte value"); 871 } 872 873 { 874 vh.set((byte)0x01); 875 876 byte o = (byte) vh.getAndSetAcquire((byte)0x23); 877 assertEquals((byte)0x01, o, "getAndSetAcquire byte"); 878 byte x = (byte) vh.get(); 879 assertEquals((byte)0x23, x, "getAndSetAcquire byte value"); 880 } 881 882 { 883 vh.set((byte)0x01); 884 885 byte o = (byte) vh.getAndSetRelease((byte)0x23); 886 assertEquals((byte)0x01, o, "getAndSetRelease byte"); 887 byte x = (byte) vh.get(); 888 assertEquals((byte)0x23, x, "getAndSetRelease byte value"); 889 } 890 891 // get and add, add and get 892 { 893 vh.set((byte)0x01); 894 895 byte o = (byte) vh.getAndAdd((byte)0x23); 896 assertEquals((byte)0x01, o, "getAndAdd byte"); 897 byte x = (byte) vh.get(); 898 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAdd byte value"); 899 } 900 901 { 902 vh.set((byte)0x01); 903 904 byte o = (byte) vh.getAndAddAcquire((byte)0x23); 905 assertEquals((byte)0x01, o, "getAndAddAcquire byte"); 906 byte x = (byte) vh.get(); 907 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddAcquire byte value"); 908 } 909 910 { 911 vh.set((byte)0x01); 912 913 byte o = (byte) vh.getAndAddRelease((byte)0x23); 914 assertEquals((byte)0x01, o, "getAndAddReleasebyte"); 915 byte x = (byte) vh.get(); 916 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddRelease byte value"); 917 } 918 919 // get and bitwise or 920 { 921 vh.set((byte)0x01); 922 923 byte o = (byte) vh.getAndBitwiseOr((byte)0x23); 924 assertEquals((byte)0x01, o, "getAndBitwiseOr byte"); 925 byte x = (byte) vh.get(); 926 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOr byte value"); 927 } 928 929 { 930 vh.set((byte)0x01); 931 932 byte o = (byte) vh.getAndBitwiseOrAcquire((byte)0x23); 933 assertEquals((byte)0x01, o, "getAndBitwiseOrAcquire byte"); 934 byte x = (byte) vh.get(); 935 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrAcquire byte value"); 936 } 937 938 { 939 vh.set((byte)0x01); 940 941 byte o = (byte) vh.getAndBitwiseOrRelease((byte)0x23); 942 assertEquals((byte)0x01, o, "getAndBitwiseOrRelease byte"); 943 byte x = (byte) vh.get(); 944 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrRelease byte value"); 945 } 946 947 // get and bitwise and 948 { 949 vh.set((byte)0x01); 950 951 byte o = (byte) vh.getAndBitwiseAnd((byte)0x23); 952 assertEquals((byte)0x01, o, "getAndBitwiseAnd byte"); 953 byte x = (byte) vh.get(); 954 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAnd byte value"); 955 } 956 957 { 958 vh.set((byte)0x01); 959 960 byte o = (byte) vh.getAndBitwiseAndAcquire((byte)0x23); 961 assertEquals((byte)0x01, o, "getAndBitwiseAndAcquire byte"); 962 byte x = (byte) vh.get(); 963 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndAcquire byte value"); 964 } 965 966 { 967 vh.set((byte)0x01); 968 969 byte o = (byte) vh.getAndBitwiseAndRelease((byte)0x23); 970 assertEquals((byte)0x01, o, "getAndBitwiseAndRelease byte"); 971 byte x = (byte) vh.get(); 972 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndRelease byte value"); 973 } 974 975 // get and bitwise xor 976 { 977 vh.set((byte)0x01); 978 979 byte o = (byte) vh.getAndBitwiseXor((byte)0x23); 980 assertEquals((byte)0x01, o, "getAndBitwiseXor byte"); 981 byte x = (byte) vh.get(); 982 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXor byte value"); 983 } 984 985 { 986 vh.set((byte)0x01); 987 988 byte o = (byte) vh.getAndBitwiseXorAcquire((byte)0x23); 989 assertEquals((byte)0x01, o, "getAndBitwiseXorAcquire byte"); 990 byte x = (byte) vh.get(); 991 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorAcquire byte value"); 992 } 993 994 { 995 vh.set((byte)0x01); 996 997 byte o = (byte) vh.getAndBitwiseXorRelease((byte)0x23); 998 assertEquals((byte)0x01, o, "getAndBitwiseXorRelease byte"); 999 byte x = (byte) vh.get(); 1000 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorRelease byte value"); 1001 } 1002 } 1003 1004 static void testStaticFieldUnsupported(VarHandle vh) { 1005 1006 1007 } 1008 1009 1010 static void testArray(VarHandle vh) { 1011 byte[] array = new byte[10]; 1012 1013 for (int i = 0; i < array.length; i++) { 1014 // Plain 1015 { 1016 vh.set(array, i, (byte)0x01); 1017 byte x = (byte) vh.get(array, i); 1018 assertEquals((byte)0x01, x, "get byte value"); 1019 } 1020 1021 1022 // Volatile 1023 { 1024 vh.setVolatile(array, i, (byte)0x23); 1025 byte x = (byte) vh.getVolatile(array, i); 1026 assertEquals((byte)0x23, x, "setVolatile byte value"); 1027 } 1028 1029 // Lazy 1030 { 1031 vh.setRelease(array, i, (byte)0x01); 1032 byte x = (byte) vh.getAcquire(array, i); 1033 assertEquals((byte)0x01, x, "setRelease byte value"); 1034 } 1035 1036 // Opaque 1037 { 1038 vh.setOpaque(array, i, (byte)0x23); 1039 byte x = (byte) vh.getOpaque(array, i); 1040 assertEquals((byte)0x23, x, "setOpaque byte value"); 1041 } 1042 1043 vh.set(array, i, (byte)0x01); 1044 1045 // Compare 1046 { 1047 boolean r = vh.compareAndSet(array, i, (byte)0x01, (byte)0x23); 1048 assertEquals(r, true, "success compareAndSet byte"); 1049 byte x = (byte) vh.get(array, i); 1050 assertEquals((byte)0x23, x, "success compareAndSet byte value"); 1051 } 1052 1053 { 1054 boolean r = vh.compareAndSet(array, i, (byte)0x01, (byte)0x45); 1055 assertEquals(r, false, "failing compareAndSet byte"); 1056 byte x = (byte) vh.get(array, i); 1057 assertEquals((byte)0x23, x, "failing compareAndSet byte value"); 1058 } 1059 1060 { 1061 byte r = (byte) vh.compareAndExchange(array, i, (byte)0x23, (byte)0x01); 1062 assertEquals(r, (byte)0x23, "success compareAndExchange byte"); 1063 byte x = (byte) vh.get(array, i); 1064 assertEquals((byte)0x01, x, "success compareAndExchange byte value"); 1065 } 1066 1067 { 1068 byte r = (byte) vh.compareAndExchange(array, i, (byte)0x23, (byte)0x45); 1069 assertEquals(r, (byte)0x01, "failing compareAndExchange byte"); 1070 byte x = (byte) vh.get(array, i); 1071 assertEquals((byte)0x01, x, "failing compareAndExchange byte value"); 1072 } 1073 1074 { 1075 byte r = (byte) vh.compareAndExchangeAcquire(array, i, (byte)0x01, (byte)0x23); 1076 assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte"); 1077 byte x = (byte) vh.get(array, i); 1078 assertEquals((byte)0x23, x, "success compareAndExchangeAcquire byte value"); 1079 } 1080 1081 { 1082 byte r = (byte) vh.compareAndExchangeAcquire(array, i, (byte)0x01, (byte)0x45); 1083 assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte"); 1084 byte x = (byte) vh.get(array, i); 1085 assertEquals((byte)0x23, x, "failing compareAndExchangeAcquire byte value"); 1086 } 1087 1088 { 1089 byte r = (byte) vh.compareAndExchangeRelease(array, i, (byte)0x23, (byte)0x01); 1090 assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte"); 1091 byte x = (byte) vh.get(array, i); 1092 assertEquals((byte)0x01, x, "success compareAndExchangeRelease byte value"); 1093 } 1094 1095 { 1096 byte r = (byte) vh.compareAndExchangeRelease(array, i, (byte)0x23, (byte)0x45); 1097 assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte"); 1098 byte x = (byte) vh.get(array, i); 1099 assertEquals((byte)0x01, x, "failing compareAndExchangeRelease byte value"); 1100 } 1101 1102 { 1103 boolean success = false; 1104 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1105 success = vh.weakCompareAndSetPlain(array, i, (byte)0x01, (byte)0x23); 1106 if (!success) weakDelay(); 1107 } 1108 assertEquals(success, true, "success weakCompareAndSetPlain byte"); 1109 byte x = (byte) vh.get(array, i); 1110 assertEquals((byte)0x23, x, "success weakCompareAndSetPlain byte value"); 1111 } 1112 1113 { 1114 boolean success = vh.weakCompareAndSetPlain(array, i, (byte)0x01, (byte)0x45); 1115 assertEquals(success, false, "failing weakCompareAndSetPlain byte"); 1116 byte x = (byte) vh.get(array, i); 1117 assertEquals((byte)0x23, x, "failing weakCompareAndSetPlain byte value"); 1118 } 1119 1120 { 1121 boolean success = false; 1122 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1123 success = vh.weakCompareAndSetAcquire(array, i, (byte)0x23, (byte)0x01); 1124 if (!success) weakDelay(); 1125 } 1126 assertEquals(success, true, "success weakCompareAndSetAcquire byte"); 1127 byte x = (byte) vh.get(array, i); 1128 assertEquals((byte)0x01, x, "success weakCompareAndSetAcquire byte"); 1129 } 1130 1131 { 1132 boolean success = vh.weakCompareAndSetAcquire(array, i, (byte)0x23, (byte)0x45); 1133 assertEquals(success, false, "failing weakCompareAndSetAcquire byte"); 1134 byte x = (byte) vh.get(array, i); 1135 assertEquals((byte)0x01, x, "failing weakCompareAndSetAcquire byte value"); 1136 } 1137 1138 { 1139 boolean success = false; 1140 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1141 success = vh.weakCompareAndSetRelease(array, i, (byte)0x01, (byte)0x23); 1142 if (!success) weakDelay(); 1143 } 1144 assertEquals(success, true, "success weakCompareAndSetRelease byte"); 1145 byte x = (byte) vh.get(array, i); 1146 assertEquals((byte)0x23, x, "success weakCompareAndSetRelease byte"); 1147 } 1148 1149 { 1150 boolean success = vh.weakCompareAndSetRelease(array, i, (byte)0x01, (byte)0x45); 1151 assertEquals(success, false, "failing weakCompareAndSetRelease byte"); 1152 byte x = (byte) vh.get(array, i); 1153 assertEquals((byte)0x23, x, "failing weakCompareAndSetRelease byte value"); 1154 } 1155 1156 { 1157 boolean success = false; 1158 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1159 success = vh.weakCompareAndSet(array, i, (byte)0x23, (byte)0x01); 1160 if (!success) weakDelay(); 1161 } 1162 assertEquals(success, true, "success weakCompareAndSet byte"); 1163 byte x = (byte) vh.get(array, i); 1164 assertEquals((byte)0x01, x, "success weakCompareAndSet byte"); 1165 } 1166 1167 { 1168 boolean success = vh.weakCompareAndSet(array, i, (byte)0x23, (byte)0x45); 1169 assertEquals(success, false, "failing weakCompareAndSet byte"); 1170 byte x = (byte) vh.get(array, i); 1171 assertEquals((byte)0x01, x, "failing weakCompareAndSet byte value"); 1172 } 1173 1174 // Compare set and get 1175 { 1176 vh.set(array, i, (byte)0x01); 1177 1178 byte o = (byte) vh.getAndSet(array, i, (byte)0x23); 1179 assertEquals((byte)0x01, o, "getAndSet byte"); 1180 byte x = (byte) vh.get(array, i); 1181 assertEquals((byte)0x23, x, "getAndSet byte value"); 1182 } 1183 1184 { 1185 vh.set(array, i, (byte)0x01); 1186 1187 byte o = (byte) vh.getAndSetAcquire(array, i, (byte)0x23); 1188 assertEquals((byte)0x01, o, "getAndSetAcquire byte"); 1189 byte x = (byte) vh.get(array, i); 1190 assertEquals((byte)0x23, x, "getAndSetAcquire byte value"); 1191 } 1192 1193 { 1194 vh.set(array, i, (byte)0x01); 1195 1196 byte o = (byte) vh.getAndSetRelease(array, i, (byte)0x23); 1197 assertEquals((byte)0x01, o, "getAndSetRelease byte"); 1198 byte x = (byte) vh.get(array, i); 1199 assertEquals((byte)0x23, x, "getAndSetRelease byte value"); 1200 } 1201 1202 // get and add, add and get 1203 { 1204 vh.set(array, i, (byte)0x01); 1205 1206 byte o = (byte) vh.getAndAdd(array, i, (byte)0x23); 1207 assertEquals((byte)0x01, o, "getAndAdd byte"); 1208 byte x = (byte) vh.get(array, i); 1209 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAdd byte value"); 1210 } 1211 1212 { 1213 vh.set(array, i, (byte)0x01); 1214 1215 byte o = (byte) vh.getAndAddAcquire(array, i, (byte)0x23); 1216 assertEquals((byte)0x01, o, "getAndAddAcquire byte"); 1217 byte x = (byte) vh.get(array, i); 1218 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddAcquire byte value"); 1219 } 1220 1221 { 1222 vh.set(array, i, (byte)0x01); 1223 1224 byte o = (byte) vh.getAndAddRelease(array, i, (byte)0x23); 1225 assertEquals((byte)0x01, o, "getAndAddReleasebyte"); 1226 byte x = (byte) vh.get(array, i); 1227 assertEquals((byte)((byte)0x01 + (byte)0x23), x, "getAndAddRelease byte value"); 1228 } 1229 1230 // get and bitwise or 1231 { 1232 vh.set(array, i, (byte)0x01); 1233 1234 byte o = (byte) vh.getAndBitwiseOr(array, i, (byte)0x23); 1235 assertEquals((byte)0x01, o, "getAndBitwiseOr byte"); 1236 byte x = (byte) vh.get(array, i); 1237 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOr byte value"); 1238 } 1239 1240 { 1241 vh.set(array, i, (byte)0x01); 1242 1243 byte o = (byte) vh.getAndBitwiseOrAcquire(array, i, (byte)0x23); 1244 assertEquals((byte)0x01, o, "getAndBitwiseOrAcquire byte"); 1245 byte x = (byte) vh.get(array, i); 1246 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrAcquire byte value"); 1247 } 1248 1249 { 1250 vh.set(array, i, (byte)0x01); 1251 1252 byte o = (byte) vh.getAndBitwiseOrRelease(array, i, (byte)0x23); 1253 assertEquals((byte)0x01, o, "getAndBitwiseOrRelease byte"); 1254 byte x = (byte) vh.get(array, i); 1255 assertEquals((byte)((byte)0x01 | (byte)0x23), x, "getAndBitwiseOrRelease byte value"); 1256 } 1257 1258 // get and bitwise and 1259 { 1260 vh.set(array, i, (byte)0x01); 1261 1262 byte o = (byte) vh.getAndBitwiseAnd(array, i, (byte)0x23); 1263 assertEquals((byte)0x01, o, "getAndBitwiseAnd byte"); 1264 byte x = (byte) vh.get(array, i); 1265 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAnd byte value"); 1266 } 1267 1268 { 1269 vh.set(array, i, (byte)0x01); 1270 1271 byte o = (byte) vh.getAndBitwiseAndAcquire(array, i, (byte)0x23); 1272 assertEquals((byte)0x01, o, "getAndBitwiseAndAcquire byte"); 1273 byte x = (byte) vh.get(array, i); 1274 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndAcquire byte value"); 1275 } 1276 1277 { 1278 vh.set(array, i, (byte)0x01); 1279 1280 byte o = (byte) vh.getAndBitwiseAndRelease(array, i, (byte)0x23); 1281 assertEquals((byte)0x01, o, "getAndBitwiseAndRelease byte"); 1282 byte x = (byte) vh.get(array, i); 1283 assertEquals((byte)((byte)0x01 & (byte)0x23), x, "getAndBitwiseAndRelease byte value"); 1284 } 1285 1286 // get and bitwise xor 1287 { 1288 vh.set(array, i, (byte)0x01); 1289 1290 byte o = (byte) vh.getAndBitwiseXor(array, i, (byte)0x23); 1291 assertEquals((byte)0x01, o, "getAndBitwiseXor byte"); 1292 byte x = (byte) vh.get(array, i); 1293 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXor byte value"); 1294 } 1295 1296 { 1297 vh.set(array, i, (byte)0x01); 1298 1299 byte o = (byte) vh.getAndBitwiseXorAcquire(array, i, (byte)0x23); 1300 assertEquals((byte)0x01, o, "getAndBitwiseXorAcquire byte"); 1301 byte x = (byte) vh.get(array, i); 1302 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorAcquire byte value"); 1303 } 1304 1305 { 1306 vh.set(array, i, (byte)0x01); 1307 1308 byte o = (byte) vh.getAndBitwiseXorRelease(array, i, (byte)0x23); 1309 assertEquals((byte)0x01, o, "getAndBitwiseXorRelease byte"); 1310 byte x = (byte) vh.get(array, i); 1311 assertEquals((byte)((byte)0x01 ^ (byte)0x23), x, "getAndBitwiseXorRelease byte value"); 1312 } 1313 } 1314 } 1315 1316 static void testArrayUnsupported(VarHandle vh) { 1317 byte[] array = new byte[10]; 1318 1319 int i = 0; 1320 1321 1322 } 1323 1324 static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable { 1325 byte[] array = new byte[10]; 1326 1327 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { 1328 final int ci = i; 1329 1330 checkAIOOBE(() -> { 1331 byte x = (byte) vh.get(array, ci); 1332 }); 1333 1334 checkAIOOBE(() -> { 1335 vh.set(array, ci, (byte)0x01); 1336 }); 1337 1338 checkAIOOBE(() -> { 1339 byte x = (byte) vh.getVolatile(array, ci); 1340 }); 1341 1342 checkAIOOBE(() -> { 1343 vh.setVolatile(array, ci, (byte)0x01); 1344 }); 1345 1346 checkAIOOBE(() -> { 1347 byte x = (byte) vh.getAcquire(array, ci); 1348 }); 1349 1350 checkAIOOBE(() -> { 1351 vh.setRelease(array, ci, (byte)0x01); 1352 }); 1353 1354 checkAIOOBE(() -> { 1355 byte x = (byte) vh.getOpaque(array, ci); 1356 }); 1357 1358 checkAIOOBE(() -> { 1359 vh.setOpaque(array, ci, (byte)0x01); 1360 }); 1361 1362 checkAIOOBE(() -> { 1363 boolean r = vh.compareAndSet(array, ci, (byte)0x01, (byte)0x23); 1364 }); 1365 1366 checkAIOOBE(() -> { 1367 byte r = (byte) vh.compareAndExchange(array, ci, (byte)0x23, (byte)0x01); 1368 }); 1369 1370 checkAIOOBE(() -> { 1371 byte r = (byte) vh.compareAndExchangeAcquire(array, ci, (byte)0x23, (byte)0x01); 1372 }); 1373 1374 checkAIOOBE(() -> { 1375 byte r = (byte) vh.compareAndExchangeRelease(array, ci, (byte)0x23, (byte)0x01); 1376 }); 1377 1378 checkAIOOBE(() -> { 1379 boolean r = vh.weakCompareAndSetPlain(array, ci, (byte)0x01, (byte)0x23); 1380 }); 1381 1382 checkAIOOBE(() -> { 1383 boolean r = vh.weakCompareAndSet(array, ci, (byte)0x01, (byte)0x23); 1384 }); 1385 1386 checkAIOOBE(() -> { 1387 boolean r = vh.weakCompareAndSetAcquire(array, ci, (byte)0x01, (byte)0x23); 1388 }); 1389 1390 checkAIOOBE(() -> { 1391 boolean r = vh.weakCompareAndSetRelease(array, ci, (byte)0x01, (byte)0x23); 1392 }); 1393 1394 checkAIOOBE(() -> { 1395 byte o = (byte) vh.getAndSet(array, ci, (byte)0x01); 1396 }); 1397 1398 checkAIOOBE(() -> { 1399 byte o = (byte) vh.getAndSetAcquire(array, ci, (byte)0x01); 1400 }); 1401 1402 checkAIOOBE(() -> { 1403 byte o = (byte) vh.getAndSetRelease(array, ci, (byte)0x01); 1404 }); 1405 1406 checkAIOOBE(() -> { 1407 byte o = (byte) vh.getAndAdd(array, ci, (byte)0x01); 1408 }); 1409 1410 checkAIOOBE(() -> { 1411 byte o = (byte) vh.getAndAddAcquire(array, ci, (byte)0x01); 1412 }); 1413 1414 checkAIOOBE(() -> { 1415 byte o = (byte) vh.getAndAddRelease(array, ci, (byte)0x01); 1416 }); 1417 1418 checkAIOOBE(() -> { 1419 byte o = (byte) vh.getAndBitwiseOr(array, ci, (byte)0x01); 1420 }); 1421 1422 checkAIOOBE(() -> { 1423 byte o = (byte) vh.getAndBitwiseOrAcquire(array, ci, (byte)0x01); 1424 }); 1425 1426 checkAIOOBE(() -> { 1427 byte o = (byte) vh.getAndBitwiseOrRelease(array, ci, (byte)0x01); 1428 }); 1429 1430 checkAIOOBE(() -> { 1431 byte o = (byte) vh.getAndBitwiseAnd(array, ci, (byte)0x01); 1432 }); 1433 1434 checkAIOOBE(() -> { 1435 byte o = (byte) vh.getAndBitwiseAndAcquire(array, ci, (byte)0x01); 1436 }); 1437 1438 checkAIOOBE(() -> { 1439 byte o = (byte) vh.getAndBitwiseAndRelease(array, ci, (byte)0x01); 1440 }); 1441 1442 checkAIOOBE(() -> { 1443 byte o = (byte) vh.getAndBitwiseXor(array, ci, (byte)0x01); 1444 }); 1445 1446 checkAIOOBE(() -> { 1447 byte o = (byte) vh.getAndBitwiseXorAcquire(array, ci, (byte)0x01); 1448 }); 1449 1450 checkAIOOBE(() -> { 1451 byte o = (byte) vh.getAndBitwiseXorRelease(array, ci, (byte)0x01); 1452 }); 1453 } 1454 } 1455 1456 } 1457 --- EOF ---