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