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 VarHandleTestAccessByte |
| 27 | * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessByte |
| 28 | * @run testng/othervm -Diters=20000 VarHandleTestAccessByte |
| 29 | * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccessByte |
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 VarHandleTestAccessByte |
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 VarHandleTestAccessByte extends VarHandleBaseTest { |
| 46 | static final byte static_final_v = (byte)1; |
| 47 | |
| 48 | static byte static_v; |
| 49 | |
| 50 | final byte final_v = (byte)1; |
| 51 | |
| 52 | byte 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 | VarHandleTestAccessByte.class, "final_v", byte.class); |
| 68 | |
| 69 | vhField = MethodHandles.lookup().findVarHandle( |
| 70 | VarHandleTestAccessByte.class, "v", byte.class); |
| 71 | |
| 72 | vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( |
| 73 | VarHandleTestAccessByte.class, "static_final_v", byte.class); |
| 74 | |
| 75 | vhStaticField = MethodHandles.lookup().findStaticVarHandle( |
| 76 | VarHandleTestAccessByte.class, "static_v", byte.class); |
| 77 | |
| 78 | vhArray = MethodHandles.arrayElementVarHandle(byte[].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 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); |
| 104 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE)); |
| 105 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); |
| 106 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); |
| 107 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 108 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE)); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 109 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); |
| 110 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 111 | assertFalse(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 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); |
| 114 | assertFalse(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(VarHandleTestAccessByte.class)}); |
| 122 | types.add(new Object[] {vhStaticField, Arrays.asList()}); |
| 123 | types.add(new Object[] {vhArray, Arrays.asList(byte[].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(), byte.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 | VarHandleTestAccessByte.class, "final_v", byte.class); |
| 143 | }); |
| 144 | |
| 145 | checkIAE("Lookup of static field to instance field", () -> { |
| 146 | MethodHandles.lookup().findStaticVarHandle( |
| 147 | VarHandleTestAccessByte.class, "v", byte.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 | VarHandleTestAccessByte.class, "static_final_v", byte.class); |
| 156 | }); |
| 157 | |
| 158 | checkIAE("Lookup of instance field to static field", () -> { |
| 159 | vhStaticField = MethodHandles.lookup().findVarHandle( |
| 160 | VarHandleTestAccessByte.class, "static_v", byte.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, VarHandleTestAccessByte::testStaticFinalField)); |
| 177 | cases.add(new VarHandleAccessTestCase("Static final field unsupported", |
| 178 | vhStaticFinalField, VarHandleTestAccessByte::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, VarHandleTestAccessByte::testStaticField)); |
| 189 | cases.add(new VarHandleAccessTestCase("Static field unsupported", |
| 190 | vhStaticField, VarHandleTestAccessByte::testStaticFieldUnsupported, |
| 191 | false)); |
| 192 | |
| 193 | cases.add(new VarHandleAccessTestCase("Array", |
| 194 | vhArray, VarHandleTestAccessByte::testArray)); |
| 195 | cases.add(new VarHandleAccessTestCase("Array unsupported", |
| 196 | vhArray, VarHandleTestAccessByte::testArrayUnsupported, |
| 197 | false)); |
| 198 | cases.add(new VarHandleAccessTestCase("Array index out of bounds", |
| 199 | vhArray, VarHandleTestAccessByte::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(VarHandleTestAccessByte recv, VarHandle vh) { |
| 221 | // Plain |
| 222 | { |
| 223 | byte x = (byte) vh.get(recv); |
| 224 | assertEquals(x, (byte)1, "get byte value"); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | // Volatile |
| 229 | { |
| 230 | byte x = (byte) vh.getVolatile(recv); |
| 231 | assertEquals(x, (byte)1, "getVolatile byte value"); |
| 232 | } |
| 233 | |
| 234 | // Lazy |
| 235 | { |
| 236 | byte x = (byte) vh.getAcquire(recv); |
| 237 | assertEquals(x, (byte)1, "getRelease byte value"); |
| 238 | } |
| 239 | |
| 240 | // Opaque |
| 241 | { |
| 242 | byte x = (byte) vh.getOpaque(recv); |
| 243 | assertEquals(x, (byte)1, "getOpaque byte value"); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static void testInstanceFinalFieldUnsupported(VarHandleTestAccessByte recv, VarHandle vh) { |
| 248 | checkUOE(() -> { |
| 249 | vh.set(recv, (byte)2); |
| 250 | }); |
| 251 | |
| 252 | checkUOE(() -> { |
| 253 | vh.setVolatile(recv, (byte)2); |
| 254 | }); |
| 255 | |
| 256 | checkUOE(() -> { |
| 257 | vh.setRelease(recv, (byte)2); |
| 258 | }); |
| 259 | |
| 260 | checkUOE(() -> { |
| 261 | vh.setOpaque(recv, (byte)2); |
| 262 | }); |
| 263 | |
| 264 | checkUOE(() -> { |
| 265 | boolean r = vh.compareAndSet(recv, (byte)1, (byte)2); |
| 266 | }); |
| 267 | |
| 268 | checkUOE(() -> { |
| 269 | byte r = (byte) vh.compareAndExchangeVolatile(recv, (byte)1, (byte)2); |
| 270 | }); |
| 271 | |
| 272 | checkUOE(() -> { |
| 273 | byte r = (byte) vh.compareAndExchangeAcquire(recv, (byte)1, (byte)2); |
| 274 | }); |
| 275 | |
| 276 | checkUOE(() -> { |
| 277 | byte r = (byte) vh.compareAndExchangeRelease(recv, (byte)1, (byte)2); |
| 278 | }); |
| 279 | |
| 280 | checkUOE(() -> { |
| 281 | boolean r = vh.weakCompareAndSet(recv, (byte)1, (byte)2); |
| 282 | }); |
| 283 | |
| 284 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 285 | boolean r = vh.weakCompareAndSetVolatile(recv, (byte)1, (byte)2); |
| 286 | }); |
| 287 | |
| 288 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 289 | boolean r = vh.weakCompareAndSetAcquire(recv, (byte)1, (byte)2); |
| 290 | }); |
| 291 | |
| 292 | checkUOE(() -> { |
| 293 | boolean r = vh.weakCompareAndSetRelease(recv, (byte)1, (byte)2); |
| 294 | }); |
| 295 | |
| 296 | checkUOE(() -> { |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 297 | byte r = (byte) vh.getAndSet(recv, (byte)1); |
| 298 | }); |
| 299 | |
| 300 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 301 | byte o = (byte) vh.getAndAdd(recv, (byte)1); |
| 302 | }); |
| 303 | |
| 304 | checkUOE(() -> { |
| 305 | byte o = (byte) vh.addAndGet(recv, (byte)1); |
| 306 | }); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | static void testStaticFinalField(VarHandle vh) { |
| 311 | // Plain |
| 312 | { |
| 313 | byte x = (byte) vh.get(); |
| 314 | assertEquals(x, (byte)1, "get byte value"); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | // Volatile |
| 319 | { |
| 320 | byte x = (byte) vh.getVolatile(); |
| 321 | assertEquals(x, (byte)1, "getVolatile byte value"); |
| 322 | } |
| 323 | |
| 324 | // Lazy |
| 325 | { |
| 326 | byte x = (byte) vh.getAcquire(); |
| 327 | assertEquals(x, (byte)1, "getRelease byte value"); |
| 328 | } |
| 329 | |
| 330 | // Opaque |
| 331 | { |
| 332 | byte x = (byte) vh.getOpaque(); |
| 333 | assertEquals(x, (byte)1, "getOpaque byte value"); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static void testStaticFinalFieldUnsupported(VarHandle vh) { |
| 338 | checkUOE(() -> { |
| 339 | vh.set((byte)2); |
| 340 | }); |
| 341 | |
| 342 | checkUOE(() -> { |
| 343 | vh.setVolatile((byte)2); |
| 344 | }); |
| 345 | |
| 346 | checkUOE(() -> { |
| 347 | vh.setRelease((byte)2); |
| 348 | }); |
| 349 | |
| 350 | checkUOE(() -> { |
| 351 | vh.setOpaque((byte)2); |
| 352 | }); |
| 353 | |
| 354 | checkUOE(() -> { |
| 355 | boolean r = vh.compareAndSet((byte)1, (byte)2); |
| 356 | }); |
| 357 | |
| 358 | checkUOE(() -> { |
| 359 | byte r = (byte) vh.compareAndExchangeVolatile((byte)1, (byte)2); |
| 360 | }); |
| 361 | |
| 362 | checkUOE(() -> { |
| 363 | byte r = (byte) vh.compareAndExchangeAcquire((byte)1, (byte)2); |
| 364 | }); |
| 365 | |
| 366 | checkUOE(() -> { |
| 367 | byte r = (byte) vh.compareAndExchangeRelease((byte)1, (byte)2); |
| 368 | }); |
| 369 | |
| 370 | checkUOE(() -> { |
| 371 | boolean r = vh.weakCompareAndSet((byte)1, (byte)2); |
| 372 | }); |
| 373 | |
| 374 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 375 | boolean r = vh.weakCompareAndSetVolatile((byte)1, (byte)2); |
| 376 | }); |
| 377 | |
| 378 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 379 | boolean r = vh.weakCompareAndSetAcquire((byte)1, (byte)2); |
| 380 | }); |
| 381 | |
| 382 | checkUOE(() -> { |
| 383 | boolean r = vh.weakCompareAndSetRelease((byte)1, (byte)2); |
| 384 | }); |
| 385 | |
| 386 | checkUOE(() -> { |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 387 | byte r = (byte) vh.getAndSet((byte)1); |
| 388 | }); |
| 389 | |
| 390 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 391 | byte o = (byte) vh.getAndAdd((byte)1); |
| 392 | }); |
| 393 | |
| 394 | checkUOE(() -> { |
| 395 | byte o = (byte) vh.addAndGet((byte)1); |
| 396 | }); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | static void testInstanceField(VarHandleTestAccessByte recv, VarHandle vh) { |
| 401 | // Plain |
| 402 | { |
| 403 | vh.set(recv, (byte)1); |
| 404 | byte x = (byte) vh.get(recv); |
| 405 | assertEquals(x, (byte)1, "set byte value"); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | // Volatile |
| 410 | { |
| 411 | vh.setVolatile(recv, (byte)2); |
| 412 | byte x = (byte) vh.getVolatile(recv); |
| 413 | assertEquals(x, (byte)2, "setVolatile byte value"); |
| 414 | } |
| 415 | |
| 416 | // Lazy |
| 417 | { |
| 418 | vh.setRelease(recv, (byte)1); |
| 419 | byte x = (byte) vh.getAcquire(recv); |
| 420 | assertEquals(x, (byte)1, "setRelease byte value"); |
| 421 | } |
| 422 | |
| 423 | // Opaque |
| 424 | { |
| 425 | vh.setOpaque(recv, (byte)2); |
| 426 | byte x = (byte) vh.getOpaque(recv); |
| 427 | assertEquals(x, (byte)2, "setOpaque byte value"); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | } |
| 432 | |
| 433 | static void testInstanceFieldUnsupported(VarHandleTestAccessByte recv, VarHandle vh) { |
| 434 | checkUOE(() -> { |
| 435 | boolean r = vh.compareAndSet(recv, (byte)1, (byte)2); |
| 436 | }); |
| 437 | |
| 438 | checkUOE(() -> { |
| 439 | byte r = (byte) vh.compareAndExchangeVolatile(recv, (byte)1, (byte)2); |
| 440 | }); |
| 441 | |
| 442 | checkUOE(() -> { |
| 443 | byte r = (byte) vh.compareAndExchangeAcquire(recv, (byte)1, (byte)2); |
| 444 | }); |
| 445 | |
| 446 | checkUOE(() -> { |
| 447 | byte r = (byte) vh.compareAndExchangeRelease(recv, (byte)1, (byte)2); |
| 448 | }); |
| 449 | |
| 450 | checkUOE(() -> { |
| 451 | boolean r = vh.weakCompareAndSet(recv, (byte)1, (byte)2); |
| 452 | }); |
| 453 | |
| 454 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 455 | boolean r = vh.weakCompareAndSetVolatile(recv, (byte)1, (byte)2); |
| 456 | }); |
| 457 | |
| 458 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 459 | boolean r = vh.weakCompareAndSetAcquire(recv, (byte)1, (byte)2); |
| 460 | }); |
| 461 | |
| 462 | checkUOE(() -> { |
| 463 | boolean r = vh.weakCompareAndSetRelease(recv, (byte)1, (byte)2); |
| 464 | }); |
| 465 | |
| 466 | checkUOE(() -> { |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 467 | byte r = (byte) vh.getAndSet(recv, (byte)1); |
| 468 | }); |
| 469 | |
| 470 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 471 | byte o = (byte) vh.getAndAdd(recv, (byte)1); |
| 472 | }); |
| 473 | |
| 474 | checkUOE(() -> { |
| 475 | byte o = (byte) vh.addAndGet(recv, (byte)1); |
| 476 | }); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | static void testStaticField(VarHandle vh) { |
| 481 | // Plain |
| 482 | { |
| 483 | vh.set((byte)1); |
| 484 | byte x = (byte) vh.get(); |
| 485 | assertEquals(x, (byte)1, "set byte value"); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | // Volatile |
| 490 | { |
| 491 | vh.setVolatile((byte)2); |
| 492 | byte x = (byte) vh.getVolatile(); |
| 493 | assertEquals(x, (byte)2, "setVolatile byte value"); |
| 494 | } |
| 495 | |
| 496 | // Lazy |
| 497 | { |
| 498 | vh.setRelease((byte)1); |
| 499 | byte x = (byte) vh.getAcquire(); |
| 500 | assertEquals(x, (byte)1, "setRelease byte value"); |
| 501 | } |
| 502 | |
| 503 | // Opaque |
| 504 | { |
| 505 | vh.setOpaque((byte)2); |
| 506 | byte x = (byte) vh.getOpaque(); |
| 507 | assertEquals(x, (byte)2, "setOpaque byte value"); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | } |
| 512 | |
| 513 | static void testStaticFieldUnsupported(VarHandle vh) { |
| 514 | checkUOE(() -> { |
| 515 | boolean r = vh.compareAndSet((byte)1, (byte)2); |
| 516 | }); |
| 517 | |
| 518 | checkUOE(() -> { |
| 519 | byte r = (byte) vh.compareAndExchangeVolatile((byte)1, (byte)2); |
| 520 | }); |
| 521 | |
| 522 | checkUOE(() -> { |
| 523 | byte r = (byte) vh.compareAndExchangeAcquire((byte)1, (byte)2); |
| 524 | }); |
| 525 | |
| 526 | checkUOE(() -> { |
| 527 | byte r = (byte) vh.compareAndExchangeRelease((byte)1, (byte)2); |
| 528 | }); |
| 529 | |
| 530 | checkUOE(() -> { |
| 531 | boolean r = vh.weakCompareAndSet((byte)1, (byte)2); |
| 532 | }); |
| 533 | |
| 534 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 535 | boolean r = vh.weakCompareAndSetVolatile((byte)1, (byte)2); |
| 536 | }); |
| 537 | |
| 538 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 539 | boolean r = vh.weakCompareAndSetAcquire((byte)1, (byte)2); |
| 540 | }); |
| 541 | |
| 542 | checkUOE(() -> { |
| 543 | boolean r = vh.weakCompareAndSetRelease((byte)1, (byte)2); |
| 544 | }); |
| 545 | |
| 546 | checkUOE(() -> { |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 547 | byte r = (byte) vh.getAndSet((byte)1); |
| 548 | }); |
| 549 | |
| 550 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 551 | byte o = (byte) vh.getAndAdd((byte)1); |
| 552 | }); |
| 553 | |
| 554 | checkUOE(() -> { |
| 555 | byte o = (byte) vh.addAndGet((byte)1); |
| 556 | }); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | static void testArray(VarHandle vh) { |
| 561 | byte[] array = new byte[10]; |
| 562 | |
| 563 | for (int i = 0; i < array.length; i++) { |
| 564 | // Plain |
| 565 | { |
| 566 | vh.set(array, i, (byte)1); |
| 567 | byte x = (byte) vh.get(array, i); |
| 568 | assertEquals(x, (byte)1, "get byte value"); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | // Volatile |
| 573 | { |
| 574 | vh.setVolatile(array, i, (byte)2); |
| 575 | byte x = (byte) vh.getVolatile(array, i); |
| 576 | assertEquals(x, (byte)2, "setVolatile byte value"); |
| 577 | } |
| 578 | |
| 579 | // Lazy |
| 580 | { |
| 581 | vh.setRelease(array, i, (byte)1); |
| 582 | byte x = (byte) vh.getAcquire(array, i); |
| 583 | assertEquals(x, (byte)1, "setRelease byte value"); |
| 584 | } |
| 585 | |
| 586 | // Opaque |
| 587 | { |
| 588 | vh.setOpaque(array, i, (byte)2); |
| 589 | byte x = (byte) vh.getOpaque(array, i); |
| 590 | assertEquals(x, (byte)2, "setOpaque byte value"); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | static void testArrayUnsupported(VarHandle vh) { |
| 598 | byte[] array = new byte[10]; |
| 599 | |
| 600 | int i = 0; |
| 601 | checkUOE(() -> { |
| 602 | boolean r = vh.compareAndSet(array, i, (byte)1, (byte)2); |
| 603 | }); |
| 604 | |
| 605 | checkUOE(() -> { |
| 606 | byte r = (byte) vh.compareAndExchangeVolatile(array, i, (byte)1, (byte)2); |
| 607 | }); |
| 608 | |
| 609 | checkUOE(() -> { |
| 610 | byte r = (byte) vh.compareAndExchangeAcquire(array, i, (byte)1, (byte)2); |
| 611 | }); |
| 612 | |
| 613 | checkUOE(() -> { |
| 614 | byte r = (byte) vh.compareAndExchangeRelease(array, i, (byte)1, (byte)2); |
| 615 | }); |
| 616 | |
| 617 | checkUOE(() -> { |
| 618 | boolean r = vh.weakCompareAndSet(array, i, (byte)1, (byte)2); |
| 619 | }); |
| 620 | |
| 621 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 622 | boolean r = vh.weakCompareAndSetVolatile(array, i, (byte)1, (byte)2); |
| 623 | }); |
| 624 | |
| 625 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 626 | boolean r = vh.weakCompareAndSetAcquire(array, i, (byte)1, (byte)2); |
| 627 | }); |
| 628 | |
| 629 | checkUOE(() -> { |
| 630 | boolean r = vh.weakCompareAndSetRelease(array, i, (byte)1, (byte)2); |
| 631 | }); |
| 632 | |
| 633 | checkUOE(() -> { |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 634 | byte r = (byte) vh.getAndSet(array, i, (byte)1); |
| 635 | }); |
| 636 | |
| 637 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 638 | byte o = (byte) vh.getAndAdd(array, i, (byte)1); |
| 639 | }); |
| 640 | |
| 641 | checkUOE(() -> { |
| 642 | byte o = (byte) vh.addAndGet(array, i, (byte)1); |
| 643 | }); |
| 644 | } |
| 645 | |
| 646 | static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable { |
| 647 | byte[] array = new byte[10]; |
| 648 | |
| 649 | for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { |
| 650 | final int ci = i; |
| 651 | |
| 652 | checkIOOBE(() -> { |
| 653 | byte x = (byte) vh.get(array, ci); |
| 654 | }); |
| 655 | |
| 656 | checkIOOBE(() -> { |
| 657 | vh.set(array, ci, (byte)1); |
| 658 | }); |
| 659 | |
| 660 | checkIOOBE(() -> { |
| 661 | byte x = (byte) vh.getVolatile(array, ci); |
| 662 | }); |
| 663 | |
| 664 | checkIOOBE(() -> { |
| 665 | vh.setVolatile(array, ci, (byte)1); |
| 666 | }); |
| 667 | |
| 668 | checkIOOBE(() -> { |
| 669 | byte x = (byte) vh.getAcquire(array, ci); |
| 670 | }); |
| 671 | |
| 672 | checkIOOBE(() -> { |
| 673 | vh.setRelease(array, ci, (byte)1); |
| 674 | }); |
| 675 | |
| 676 | checkIOOBE(() -> { |
| 677 | byte x = (byte) vh.getOpaque(array, ci); |
| 678 | }); |
| 679 | |
| 680 | checkIOOBE(() -> { |
| 681 | vh.setOpaque(array, ci, (byte)1); |
| 682 | }); |
| 683 | |
| 684 | |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |