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