Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
Paul Sandoz | 1af2806 | 2016-04-26 18:30:00 -0700 | [diff] [blame] | 26 | * @bug 8154556 |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 27 | * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestByteArrayAsShort |
| 28 | * @run testng/othervm -Diters=20000 VarHandleTestByteArrayAsShort |
| 29 | * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestByteArrayAsShort |
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 VarHandleTestByteArrayAsShort |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | import org.testng.annotations.DataProvider; |
| 34 | import org.testng.annotations.Test; |
| 35 | |
| 36 | import java.lang.invoke.MethodHandles; |
| 37 | import java.lang.invoke.VarHandle; |
| 38 | import java.nio.ByteBuffer; |
| 39 | import java.nio.ByteOrder; |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.Arrays; |
| 42 | import java.util.EnumSet; |
| 43 | import java.util.List; |
| 44 | |
| 45 | import static org.testng.Assert.*; |
| 46 | |
| 47 | public class VarHandleTestByteArrayAsShort extends VarHandleBaseByteArrayTest { |
| 48 | static final int SIZE = Short.BYTES; |
| 49 | |
| 50 | static final short VALUE_1 = (short)0x0102; |
| 51 | |
| 52 | static final short VALUE_2 = (short)0x1112; |
| 53 | |
Paul Sandoz | 0bad445 | 2016-05-18 18:46:14 +0200 | [diff] [blame] | 54 | static final short VALUE_3 = (short)0xFFFE; |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 55 | |
| 56 | |
| 57 | @Override |
| 58 | public void setupVarHandleSources() { |
| 59 | // Combinations of VarHandle byte[] or ByteBuffer |
| 60 | vhss = new ArrayList<>(); |
| 61 | for (MemoryMode endianess : Arrays.asList(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) { |
Paul Sandoz | 1af2806 | 2016-04-26 18:30:00 -0700 | [diff] [blame] | 62 | |
| 63 | ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN |
| 64 | ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 65 | VarHandleSource aeh = new VarHandleSource( |
Paul Sandoz | 1af2806 | 2016-04-26 18:30:00 -0700 | [diff] [blame] | 66 | MethodHandles.byteArrayViewVarHandle(short[].class, bo), |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 67 | endianess, MemoryMode.READ_WRITE); |
| 68 | vhss.add(aeh); |
| 69 | |
| 70 | VarHandleSource bbh = new VarHandleSource( |
Paul Sandoz | 1af2806 | 2016-04-26 18:30:00 -0700 | [diff] [blame] | 71 | MethodHandles.byteBufferViewVarHandle(short[].class, bo), |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 72 | endianess, MemoryMode.READ_WRITE); |
| 73 | vhss.add(bbh); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | @Test(dataProvider = "varHandlesProvider") |
| 79 | public void testIsAccessModeSupported(VarHandleSource vhs) { |
| 80 | VarHandle vh = vhs.s; |
| 81 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 82 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET)); |
| 83 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET)); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 84 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 85 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE)); |
| 86 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE)); |
| 87 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE)); |
| 88 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE)); |
| 89 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE)); |
| 90 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE)); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 91 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 92 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); |
| 93 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE)); |
| 94 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); |
| 95 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); |
| 96 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 97 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE)); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 98 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); |
| 99 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 100 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 101 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 102 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); |
| 103 | assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET)); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test(dataProvider = "typesProvider") |
| 107 | public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) { |
| 108 | assertEquals(vh.varType(), short.class); |
| 109 | |
| 110 | assertEquals(vh.coordinateTypes(), pts); |
| 111 | |
| 112 | testTypes(vh); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | @DataProvider |
| 117 | public Object[][] accessTestCaseProvider() throws Exception { |
| 118 | List<AccessTestCase<?>> cases = new ArrayList<>(); |
| 119 | |
| 120 | for (ByteArrayViewSource<?> bav : bavss) { |
| 121 | for (VarHandleSource vh : vhss) { |
| 122 | if (vh.matches(bav)) { |
| 123 | if (bav instanceof ByteArraySource) { |
| 124 | ByteArraySource bas = (ByteArraySource) bav; |
| 125 | |
| 126 | cases.add(new VarHandleSourceAccessTestCase( |
| 127 | "read write", bav, vh, h -> testArrayReadWrite(bas, h), |
| 128 | true)); |
| 129 | cases.add(new VarHandleSourceAccessTestCase( |
| 130 | "unsupported", bav, vh, h -> testArrayUnsupported(bas, h), |
| 131 | false)); |
| 132 | cases.add(new VarHandleSourceAccessTestCase( |
| 133 | "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h), |
| 134 | false)); |
| 135 | cases.add(new VarHandleSourceAccessTestCase( |
| 136 | "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h), |
| 137 | false)); |
| 138 | } |
| 139 | else { |
| 140 | ByteBufferSource bbs = (ByteBufferSource) bav; |
| 141 | |
| 142 | if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) { |
| 143 | cases.add(new VarHandleSourceAccessTestCase( |
| 144 | "read write", bav, vh, h -> testArrayReadWrite(bbs, h), |
| 145 | true)); |
| 146 | } |
| 147 | else { |
| 148 | cases.add(new VarHandleSourceAccessTestCase( |
| 149 | "read only", bav, vh, h -> testArrayReadOnly(bbs, h), |
| 150 | true)); |
| 151 | } |
| 152 | |
| 153 | cases.add(new VarHandleSourceAccessTestCase( |
| 154 | "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h), |
| 155 | false)); |
| 156 | cases.add(new VarHandleSourceAccessTestCase( |
| 157 | "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h), |
| 158 | false)); |
| 159 | cases.add(new VarHandleSourceAccessTestCase( |
| 160 | "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h), |
| 161 | false)); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Work around issue with jtreg summary reporting which truncates |
| 168 | // the String result of Object.toString to 30 characters, hence |
| 169 | // the first dummy argument |
| 170 | return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); |
| 171 | } |
| 172 | |
| 173 | @Test(dataProvider = "accessTestCaseProvider") |
| 174 | public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { |
| 175 | T t = atc.get(); |
| 176 | int iters = atc.requiresLoop() ? ITERS : 1; |
| 177 | for (int c = 0; c < iters; c++) { |
| 178 | atc.testAccess(t); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) { |
| 184 | VarHandle vh = vhs.s; |
| 185 | byte[] array = bs.s; |
| 186 | int ci = 1; |
| 187 | |
| 188 | checkUOE(() -> { |
| 189 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 190 | }); |
| 191 | |
| 192 | checkUOE(() -> { |
| 193 | short r = (short) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 194 | }); |
| 195 | |
| 196 | checkUOE(() -> { |
| 197 | short r = (short) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 198 | }); |
| 199 | |
| 200 | checkUOE(() -> { |
| 201 | short r = (short) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 202 | }); |
| 203 | |
| 204 | checkUOE(() -> { |
| 205 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 206 | }); |
| 207 | |
| 208 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 209 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 210 | }); |
| 211 | |
| 212 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 213 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 214 | }); |
| 215 | |
| 216 | checkUOE(() -> { |
| 217 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 218 | }); |
| 219 | |
| 220 | checkUOE(() -> { |
| 221 | short o = (short) vh.getAndSet(array, ci, VALUE_1); |
| 222 | }); |
| 223 | |
| 224 | checkUOE(() -> { |
| 225 | short o = (short) vh.getAndAdd(array, ci, VALUE_1); |
| 226 | }); |
| 227 | |
| 228 | checkUOE(() -> { |
| 229 | short o = (short) vh.addAndGet(array, ci, VALUE_1); |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) { |
| 234 | VarHandle vh = vhs.s; |
| 235 | ByteBuffer array = bs.s; |
| 236 | int ci = 0; |
| 237 | boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
| 238 | |
| 239 | if (readOnly) { |
| 240 | checkROBE(() -> { |
| 241 | vh.set(array, ci, VALUE_1); |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | if (readOnly) { |
| 246 | checkROBE(() -> { |
| 247 | vh.setVolatile(array, ci, VALUE_1); |
| 248 | }); |
| 249 | |
| 250 | checkROBE(() -> { |
| 251 | vh.setRelease(array, ci, VALUE_1); |
| 252 | }); |
| 253 | |
| 254 | checkROBE(() -> { |
| 255 | vh.setOpaque(array, ci, VALUE_1); |
| 256 | }); |
| 257 | checkUOE(() -> { |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 258 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 259 | }); |
| 260 | |
| 261 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 262 | short r = (short) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 263 | }); |
| 264 | |
| 265 | checkUOE(() -> { |
| 266 | short r = (short) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 267 | }); |
| 268 | |
| 269 | checkUOE(() -> { |
| 270 | short r = (short) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 271 | }); |
| 272 | |
| 273 | checkUOE(() -> { |
| 274 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 275 | }); |
| 276 | |
| 277 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 278 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 279 | }); |
| 280 | |
| 281 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 282 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 283 | }); |
| 284 | |
| 285 | checkUOE(() -> { |
| 286 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 287 | }); |
| 288 | |
| 289 | checkUOE(() -> { |
| 290 | short o = (short) vh.getAndSet(array, ci, VALUE_1); |
| 291 | }); |
| 292 | |
| 293 | checkUOE(() -> { |
| 294 | short o = (short) vh.getAndAdd(array, ci, VALUE_1); |
| 295 | }); |
| 296 | |
| 297 | checkUOE(() -> { |
| 298 | short o = (short) vh.addAndGet(array, ci, VALUE_1); |
| 299 | }); |
| 300 | } |
| 301 | else { |
| 302 | checkUOE(() -> { |
| 303 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 304 | }); |
| 305 | |
| 306 | checkUOE(() -> { |
| 307 | short r = (short) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 308 | }); |
| 309 | |
| 310 | checkUOE(() -> { |
| 311 | short r = (short) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 312 | }); |
| 313 | |
| 314 | checkUOE(() -> { |
| 315 | short r = (short) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 316 | }); |
| 317 | |
| 318 | checkUOE(() -> { |
| 319 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 320 | }); |
| 321 | |
| 322 | checkUOE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 323 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 324 | }); |
| 325 | |
| 326 | checkUOE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 327 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 328 | }); |
| 329 | |
| 330 | checkUOE(() -> { |
| 331 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 332 | }); |
| 333 | |
| 334 | checkUOE(() -> { |
| 335 | short o = (short) vh.getAndSet(array, ci, VALUE_1); |
| 336 | }); |
| 337 | checkUOE(() -> { |
| 338 | short o = (short) vh.getAndAdd(array, ci, VALUE_1); |
| 339 | }); |
| 340 | |
| 341 | checkUOE(() -> { |
| 342 | short o = (short) vh.addAndGet(array, ci, VALUE_1); |
| 343 | }); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable { |
| 349 | VarHandle vh = vhs.s; |
| 350 | byte[] array = bs.s; |
| 351 | |
| 352 | int length = array.length - SIZE + 1; |
| 353 | for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { |
| 354 | final int ci = i; |
| 355 | |
| 356 | checkIOOBE(() -> { |
| 357 | short x = (short) vh.get(array, ci); |
| 358 | }); |
| 359 | |
| 360 | checkIOOBE(() -> { |
| 361 | vh.set(array, ci, VALUE_1); |
| 362 | }); |
| 363 | |
| 364 | checkIOOBE(() -> { |
| 365 | short x = (short) vh.getVolatile(array, ci); |
| 366 | }); |
| 367 | |
| 368 | checkIOOBE(() -> { |
| 369 | short x = (short) vh.getAcquire(array, ci); |
| 370 | }); |
| 371 | |
| 372 | checkIOOBE(() -> { |
| 373 | short x = (short) vh.getOpaque(array, ci); |
| 374 | }); |
| 375 | |
| 376 | checkIOOBE(() -> { |
| 377 | vh.setVolatile(array, ci, VALUE_1); |
| 378 | }); |
| 379 | |
| 380 | checkIOOBE(() -> { |
| 381 | vh.setRelease(array, ci, VALUE_1); |
| 382 | }); |
| 383 | |
| 384 | checkIOOBE(() -> { |
| 385 | vh.setOpaque(array, ci, VALUE_1); |
| 386 | }); |
| 387 | |
| 388 | |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { |
| 393 | VarHandle vh = vhs.s; |
| 394 | ByteBuffer array = bs.s; |
| 395 | |
| 396 | boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
| 397 | |
| 398 | int length = array.limit() - SIZE + 1; |
| 399 | for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { |
| 400 | final int ci = i; |
| 401 | |
| 402 | checkIOOBE(() -> { |
| 403 | short x = (short) vh.get(array, ci); |
| 404 | }); |
| 405 | |
| 406 | if (!readOnly) { |
| 407 | checkIOOBE(() -> { |
| 408 | vh.set(array, ci, VALUE_1); |
| 409 | }); |
| 410 | } |
| 411 | |
| 412 | checkIOOBE(() -> { |
| 413 | short x = (short) vh.getVolatile(array, ci); |
| 414 | }); |
| 415 | |
| 416 | checkIOOBE(() -> { |
| 417 | short x = (short) vh.getAcquire(array, ci); |
| 418 | }); |
| 419 | |
| 420 | checkIOOBE(() -> { |
| 421 | short x = (short) vh.getOpaque(array, ci); |
| 422 | }); |
| 423 | |
| 424 | if (!readOnly) { |
| 425 | checkIOOBE(() -> { |
| 426 | vh.setVolatile(array, ci, VALUE_1); |
| 427 | }); |
| 428 | |
| 429 | checkIOOBE(() -> { |
| 430 | vh.setRelease(array, ci, VALUE_1); |
| 431 | }); |
| 432 | |
| 433 | checkIOOBE(() -> { |
| 434 | vh.setOpaque(array, ci, VALUE_1); |
| 435 | }); |
| 436 | |
| 437 | |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable { |
| 443 | VarHandle vh = vhs.s; |
| 444 | byte[] array = bs.s; |
| 445 | |
| 446 | int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); |
| 447 | |
| 448 | int length = array.length - SIZE + 1; |
| 449 | for (int i = 0; i < length; i++) { |
| 450 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 451 | final int ci = i; |
| 452 | |
| 453 | if (!iAligned) { |
| 454 | checkISE(() -> { |
| 455 | short x = (short) vh.getVolatile(array, ci); |
| 456 | }); |
| 457 | |
| 458 | checkISE(() -> { |
| 459 | short x = (short) vh.getAcquire(array, ci); |
| 460 | }); |
| 461 | |
| 462 | checkISE(() -> { |
| 463 | short x = (short) vh.getOpaque(array, ci); |
| 464 | }); |
| 465 | |
| 466 | checkISE(() -> { |
| 467 | vh.setVolatile(array, ci, VALUE_1); |
| 468 | }); |
| 469 | |
| 470 | checkISE(() -> { |
| 471 | vh.setRelease(array, ci, VALUE_1); |
| 472 | }); |
| 473 | |
| 474 | checkISE(() -> { |
| 475 | vh.setOpaque(array, ci, VALUE_1); |
| 476 | }); |
| 477 | |
| 478 | |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { |
| 484 | VarHandle vh = vhs.s; |
| 485 | ByteBuffer array = bs.s; |
| 486 | |
| 487 | boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
| 488 | int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
| 489 | |
| 490 | int length = array.limit() - SIZE + 1; |
| 491 | for (int i = 0; i < length; i++) { |
| 492 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 493 | final int ci = i; |
| 494 | |
| 495 | if (!iAligned) { |
| 496 | checkISE(() -> { |
| 497 | short x = (short) vh.getVolatile(array, ci); |
| 498 | }); |
| 499 | |
| 500 | checkISE(() -> { |
| 501 | short x = (short) vh.getAcquire(array, ci); |
| 502 | }); |
| 503 | |
| 504 | checkISE(() -> { |
| 505 | short x = (short) vh.getOpaque(array, ci); |
| 506 | }); |
| 507 | |
| 508 | if (!readOnly) { |
| 509 | checkISE(() -> { |
| 510 | vh.setVolatile(array, ci, VALUE_1); |
| 511 | }); |
| 512 | |
| 513 | checkISE(() -> { |
| 514 | vh.setRelease(array, ci, VALUE_1); |
| 515 | }); |
| 516 | |
| 517 | checkISE(() -> { |
| 518 | vh.setOpaque(array, ci, VALUE_1); |
| 519 | }); |
| 520 | |
| 521 | |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) { |
| 528 | VarHandle vh = vhs.s; |
| 529 | byte[] array = bs.s; |
| 530 | |
| 531 | int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); |
| 532 | |
| 533 | bs.fill((byte) 0xff); |
| 534 | int length = array.length - SIZE + 1; |
| 535 | for (int i = 0; i < length; i++) { |
| 536 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 537 | |
| 538 | // Plain |
| 539 | { |
| 540 | vh.set(array, i, VALUE_1); |
| 541 | short x = (short) vh.get(array, i); |
| 542 | assertEquals(x, VALUE_1, "get short value"); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | if (iAligned) { |
| 547 | // Volatile |
| 548 | { |
| 549 | vh.setVolatile(array, i, VALUE_2); |
| 550 | short x = (short) vh.getVolatile(array, i); |
| 551 | assertEquals(x, VALUE_2, "setVolatile short value"); |
| 552 | } |
| 553 | |
| 554 | // Lazy |
| 555 | { |
| 556 | vh.setRelease(array, i, VALUE_1); |
| 557 | short x = (short) vh.getAcquire(array, i); |
| 558 | assertEquals(x, VALUE_1, "setRelease short value"); |
| 559 | } |
| 560 | |
| 561 | // Opaque |
| 562 | { |
| 563 | vh.setOpaque(array, i, VALUE_2); |
| 564 | short x = (short) vh.getOpaque(array, i); |
| 565 | assertEquals(x, VALUE_2, "setOpaque short value"); |
| 566 | } |
| 567 | |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | |
| 573 | static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) { |
| 574 | VarHandle vh = vhs.s; |
| 575 | ByteBuffer array = bs.s; |
| 576 | |
| 577 | int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
| 578 | |
| 579 | bs.fill((byte) 0xff); |
| 580 | int length = array.limit() - SIZE + 1; |
| 581 | for (int i = 0; i < length; i++) { |
| 582 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 583 | |
| 584 | // Plain |
| 585 | { |
| 586 | vh.set(array, i, VALUE_1); |
| 587 | short x = (short) vh.get(array, i); |
| 588 | assertEquals(x, VALUE_1, "get short value"); |
| 589 | } |
| 590 | |
| 591 | if (iAligned) { |
| 592 | // Volatile |
| 593 | { |
| 594 | vh.setVolatile(array, i, VALUE_2); |
| 595 | short x = (short) vh.getVolatile(array, i); |
| 596 | assertEquals(x, VALUE_2, "setVolatile short value"); |
| 597 | } |
| 598 | |
| 599 | // Lazy |
| 600 | { |
| 601 | vh.setRelease(array, i, VALUE_1); |
| 602 | short x = (short) vh.getAcquire(array, i); |
| 603 | assertEquals(x, VALUE_1, "setRelease short value"); |
| 604 | } |
| 605 | |
| 606 | // Opaque |
| 607 | { |
| 608 | vh.setOpaque(array, i, VALUE_2); |
| 609 | short x = (short) vh.getOpaque(array, i); |
| 610 | assertEquals(x, VALUE_2, "setOpaque short value"); |
| 611 | } |
| 612 | |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) { |
| 618 | VarHandle vh = vhs.s; |
| 619 | ByteBuffer array = bs.s; |
| 620 | |
| 621 | int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
| 622 | |
| 623 | ByteBuffer bb = ByteBuffer.allocate(SIZE); |
| 624 | bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); |
| 625 | bs.fill(bb.putShort(0, VALUE_2).array()); |
| 626 | |
| 627 | int length = array.limit() - SIZE + 1; |
| 628 | for (int i = 0; i < length; i++) { |
| 629 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 630 | |
| 631 | short v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) |
| 632 | ? rotateLeft(VALUE_2, (i % SIZE) << 3) |
| 633 | : rotateRight(VALUE_2, (i % SIZE) << 3); |
| 634 | // Plain |
| 635 | { |
| 636 | short x = (short) vh.get(array, i); |
| 637 | assertEquals(x, v, "get short value"); |
| 638 | } |
| 639 | |
| 640 | if (iAligned) { |
| 641 | // Volatile |
| 642 | { |
| 643 | short x = (short) vh.getVolatile(array, i); |
| 644 | assertEquals(x, v, "getVolatile short value"); |
| 645 | } |
| 646 | |
| 647 | // Lazy |
| 648 | { |
| 649 | short x = (short) vh.getAcquire(array, i); |
| 650 | assertEquals(x, v, "getRelease short value"); |
| 651 | } |
| 652 | |
| 653 | // Opaque |
| 654 | { |
| 655 | short x = (short) vh.getOpaque(array, i); |
| 656 | assertEquals(x, v, "getOpaque short value"); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | } |
| 663 | |