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 VarHandleTestByteArrayAsDouble |
| 28 | * @run testng/othervm -Diters=20000 VarHandleTestByteArrayAsDouble |
| 29 | * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestByteArrayAsDouble |
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 VarHandleTestByteArrayAsDouble |
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 VarHandleTestByteArrayAsDouble extends VarHandleBaseByteArrayTest { |
| 48 | static final int SIZE = Double.BYTES; |
| 49 | |
| 50 | static final double VALUE_1 = 0x0102030405060708L; |
| 51 | |
| 52 | static final double VALUE_2 = 0x1112131415161718L; |
| 53 | |
Paul Sandoz | 0bad445 | 2016-05-18 18:46:14 +0200 | [diff] [blame] | 54 | static final double VALUE_3 = 0xFFFEFDFCFBFAF9F8L; |
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(double[].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(double[].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 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); |
| 93 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE)); |
| 94 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); |
| 95 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); |
| 96 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 97 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE)); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 98 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); |
| 99 | assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 100 | assertTrue(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(), double.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 | |
| 189 | checkUOE(() -> { |
| 190 | double o = (double) vh.getAndAdd(array, ci, VALUE_1); |
| 191 | }); |
| 192 | |
| 193 | checkUOE(() -> { |
| 194 | double o = (double) vh.addAndGet(array, ci, VALUE_1); |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) { |
| 199 | VarHandle vh = vhs.s; |
| 200 | ByteBuffer array = bs.s; |
| 201 | int ci = 0; |
| 202 | boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
| 203 | |
| 204 | if (readOnly) { |
| 205 | checkROBE(() -> { |
| 206 | vh.set(array, ci, VALUE_1); |
| 207 | }); |
| 208 | } |
| 209 | |
| 210 | if (readOnly) { |
| 211 | checkROBE(() -> { |
| 212 | vh.setVolatile(array, ci, VALUE_1); |
| 213 | }); |
| 214 | |
| 215 | checkROBE(() -> { |
| 216 | vh.setRelease(array, ci, VALUE_1); |
| 217 | }); |
| 218 | |
| 219 | checkROBE(() -> { |
| 220 | vh.setOpaque(array, ci, VALUE_1); |
| 221 | }); |
| 222 | |
| 223 | checkROBE(() -> { |
| 224 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 225 | }); |
| 226 | |
| 227 | checkROBE(() -> { |
| 228 | double r = (double) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 229 | }); |
| 230 | |
| 231 | checkROBE(() -> { |
| 232 | double r = (double) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 233 | }); |
| 234 | |
| 235 | checkROBE(() -> { |
| 236 | double r = (double) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 237 | }); |
| 238 | |
| 239 | checkROBE(() -> { |
| 240 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 241 | }); |
| 242 | |
| 243 | checkROBE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 244 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 245 | }); |
| 246 | |
| 247 | checkROBE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 248 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 249 | }); |
| 250 | |
| 251 | checkROBE(() -> { |
| 252 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 253 | }); |
| 254 | |
| 255 | checkROBE(() -> { |
| 256 | double o = (double) vh.getAndSet(array, ci, VALUE_1); |
| 257 | }); |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 258 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 259 | |
| 260 | checkUOE(() -> { |
| 261 | double o = (double) vh.getAndAdd(array, ci, VALUE_1); |
| 262 | }); |
| 263 | |
| 264 | checkUOE(() -> { |
| 265 | double o = (double) vh.addAndGet(array, ci, VALUE_1); |
| 266 | }); |
| 267 | } |
| 268 | else { |
| 269 | checkUOE(() -> { |
| 270 | double o = (double) vh.getAndAdd(array, ci, VALUE_1); |
| 271 | }); |
| 272 | |
| 273 | checkUOE(() -> { |
| 274 | double o = (double) vh.addAndGet(array, ci, VALUE_1); |
| 275 | }); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | |
| 280 | static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable { |
| 281 | VarHandle vh = vhs.s; |
| 282 | byte[] array = bs.s; |
| 283 | |
| 284 | int length = array.length - SIZE + 1; |
| 285 | for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { |
| 286 | final int ci = i; |
| 287 | |
| 288 | checkIOOBE(() -> { |
| 289 | double x = (double) vh.get(array, ci); |
| 290 | }); |
| 291 | |
| 292 | checkIOOBE(() -> { |
| 293 | vh.set(array, ci, VALUE_1); |
| 294 | }); |
| 295 | |
| 296 | checkIOOBE(() -> { |
| 297 | double x = (double) vh.getVolatile(array, ci); |
| 298 | }); |
| 299 | |
| 300 | checkIOOBE(() -> { |
| 301 | double x = (double) vh.getAcquire(array, ci); |
| 302 | }); |
| 303 | |
| 304 | checkIOOBE(() -> { |
| 305 | double x = (double) vh.getOpaque(array, ci); |
| 306 | }); |
| 307 | |
| 308 | checkIOOBE(() -> { |
| 309 | vh.setVolatile(array, ci, VALUE_1); |
| 310 | }); |
| 311 | |
| 312 | checkIOOBE(() -> { |
| 313 | vh.setRelease(array, ci, VALUE_1); |
| 314 | }); |
| 315 | |
| 316 | checkIOOBE(() -> { |
| 317 | vh.setOpaque(array, ci, VALUE_1); |
| 318 | }); |
| 319 | |
| 320 | checkIOOBE(() -> { |
| 321 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 322 | }); |
| 323 | |
| 324 | checkIOOBE(() -> { |
| 325 | double r = (double) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 326 | }); |
| 327 | |
| 328 | checkIOOBE(() -> { |
| 329 | double r = (double) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 330 | }); |
| 331 | |
| 332 | checkIOOBE(() -> { |
| 333 | double r = (double) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 334 | }); |
| 335 | |
| 336 | checkIOOBE(() -> { |
| 337 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 338 | }); |
| 339 | |
| 340 | checkIOOBE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 341 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 342 | }); |
| 343 | |
| 344 | checkIOOBE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 345 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 346 | }); |
| 347 | |
| 348 | checkIOOBE(() -> { |
| 349 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 350 | }); |
| 351 | |
| 352 | checkIOOBE(() -> { |
| 353 | double o = (double) vh.getAndSet(array, ci, VALUE_1); |
| 354 | }); |
| 355 | |
| 356 | |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { |
| 361 | VarHandle vh = vhs.s; |
| 362 | ByteBuffer array = bs.s; |
| 363 | |
| 364 | boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
| 365 | |
| 366 | int length = array.limit() - SIZE + 1; |
| 367 | for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { |
| 368 | final int ci = i; |
| 369 | |
| 370 | checkIOOBE(() -> { |
| 371 | double x = (double) vh.get(array, ci); |
| 372 | }); |
| 373 | |
| 374 | if (!readOnly) { |
| 375 | checkIOOBE(() -> { |
| 376 | vh.set(array, ci, VALUE_1); |
| 377 | }); |
| 378 | } |
| 379 | |
| 380 | checkIOOBE(() -> { |
| 381 | double x = (double) vh.getVolatile(array, ci); |
| 382 | }); |
| 383 | |
| 384 | checkIOOBE(() -> { |
| 385 | double x = (double) vh.getAcquire(array, ci); |
| 386 | }); |
| 387 | |
| 388 | checkIOOBE(() -> { |
| 389 | double x = (double) vh.getOpaque(array, ci); |
| 390 | }); |
| 391 | |
| 392 | if (!readOnly) { |
| 393 | checkIOOBE(() -> { |
| 394 | vh.setVolatile(array, ci, VALUE_1); |
| 395 | }); |
| 396 | |
| 397 | checkIOOBE(() -> { |
| 398 | vh.setRelease(array, ci, VALUE_1); |
| 399 | }); |
| 400 | |
| 401 | checkIOOBE(() -> { |
| 402 | vh.setOpaque(array, ci, VALUE_1); |
| 403 | }); |
| 404 | |
| 405 | checkIOOBE(() -> { |
| 406 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 407 | }); |
| 408 | |
| 409 | checkIOOBE(() -> { |
| 410 | double r = (double) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 411 | }); |
| 412 | |
| 413 | checkIOOBE(() -> { |
| 414 | double r = (double) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 415 | }); |
| 416 | |
| 417 | checkIOOBE(() -> { |
| 418 | double r = (double) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 419 | }); |
| 420 | |
| 421 | checkIOOBE(() -> { |
| 422 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 423 | }); |
| 424 | |
| 425 | checkIOOBE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 426 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 427 | }); |
| 428 | |
| 429 | checkIOOBE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 430 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 431 | }); |
| 432 | |
| 433 | checkIOOBE(() -> { |
| 434 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 435 | }); |
| 436 | |
| 437 | checkIOOBE(() -> { |
| 438 | double o = (double) vh.getAndSet(array, ci, VALUE_1); |
| 439 | }); |
| 440 | |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable { |
| 446 | VarHandle vh = vhs.s; |
| 447 | byte[] array = bs.s; |
| 448 | |
| 449 | int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); |
| 450 | |
| 451 | int length = array.length - SIZE + 1; |
| 452 | for (int i = 0; i < length; i++) { |
| 453 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 454 | final int ci = i; |
| 455 | |
| 456 | if (!iAligned) { |
| 457 | checkISE(() -> { |
| 458 | double x = (double) vh.getVolatile(array, ci); |
| 459 | }); |
| 460 | |
| 461 | checkISE(() -> { |
| 462 | double x = (double) vh.getAcquire(array, ci); |
| 463 | }); |
| 464 | |
| 465 | checkISE(() -> { |
| 466 | double x = (double) vh.getOpaque(array, ci); |
| 467 | }); |
| 468 | |
| 469 | checkISE(() -> { |
| 470 | vh.setVolatile(array, ci, VALUE_1); |
| 471 | }); |
| 472 | |
| 473 | checkISE(() -> { |
| 474 | vh.setRelease(array, ci, VALUE_1); |
| 475 | }); |
| 476 | |
| 477 | checkISE(() -> { |
| 478 | vh.setOpaque(array, ci, VALUE_1); |
| 479 | }); |
| 480 | |
| 481 | checkISE(() -> { |
| 482 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 483 | }); |
| 484 | |
| 485 | checkISE(() -> { |
| 486 | double r = (double) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 487 | }); |
| 488 | |
| 489 | checkISE(() -> { |
| 490 | double r = (double) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 491 | }); |
| 492 | |
| 493 | checkISE(() -> { |
| 494 | double r = (double) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 495 | }); |
| 496 | |
| 497 | checkISE(() -> { |
| 498 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 499 | }); |
| 500 | |
| 501 | checkISE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 502 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 503 | }); |
| 504 | |
| 505 | checkISE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 506 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 507 | }); |
| 508 | |
| 509 | checkISE(() -> { |
| 510 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 511 | }); |
| 512 | |
| 513 | checkISE(() -> { |
| 514 | double o = (double) vh.getAndSet(array, ci, VALUE_1); |
| 515 | }); |
| 516 | |
| 517 | |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { |
| 523 | VarHandle vh = vhs.s; |
| 524 | ByteBuffer array = bs.s; |
| 525 | |
| 526 | boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); |
| 527 | int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
| 528 | |
| 529 | int length = array.limit() - SIZE + 1; |
| 530 | for (int i = 0; i < length; i++) { |
| 531 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 532 | final int ci = i; |
| 533 | |
| 534 | if (!iAligned) { |
| 535 | checkISE(() -> { |
| 536 | double x = (double) vh.getVolatile(array, ci); |
| 537 | }); |
| 538 | |
| 539 | checkISE(() -> { |
| 540 | double x = (double) vh.getAcquire(array, ci); |
| 541 | }); |
| 542 | |
| 543 | checkISE(() -> { |
| 544 | double x = (double) vh.getOpaque(array, ci); |
| 545 | }); |
| 546 | |
| 547 | if (!readOnly) { |
| 548 | checkISE(() -> { |
| 549 | vh.setVolatile(array, ci, VALUE_1); |
| 550 | }); |
| 551 | |
| 552 | checkISE(() -> { |
| 553 | vh.setRelease(array, ci, VALUE_1); |
| 554 | }); |
| 555 | |
| 556 | checkISE(() -> { |
| 557 | vh.setOpaque(array, ci, VALUE_1); |
| 558 | }); |
| 559 | |
| 560 | checkISE(() -> { |
| 561 | boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); |
| 562 | }); |
| 563 | |
| 564 | checkISE(() -> { |
| 565 | double r = (double) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1); |
| 566 | }); |
| 567 | |
| 568 | checkISE(() -> { |
| 569 | double r = (double) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); |
| 570 | }); |
| 571 | |
| 572 | checkISE(() -> { |
| 573 | double r = (double) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); |
| 574 | }); |
| 575 | |
| 576 | checkISE(() -> { |
| 577 | boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); |
| 578 | }); |
| 579 | |
| 580 | checkISE(() -> { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 581 | boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2); |
| 582 | }); |
| 583 | |
| 584 | checkISE(() -> { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 585 | boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); |
| 586 | }); |
| 587 | |
| 588 | checkISE(() -> { |
| 589 | boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); |
| 590 | }); |
| 591 | |
| 592 | checkISE(() -> { |
| 593 | double o = (double) vh.getAndSet(array, ci, VALUE_1); |
| 594 | }); |
| 595 | |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) { |
| 602 | VarHandle vh = vhs.s; |
| 603 | byte[] array = bs.s; |
| 604 | |
| 605 | int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); |
| 606 | |
| 607 | bs.fill((byte) 0xff); |
| 608 | int length = array.length - SIZE + 1; |
| 609 | for (int i = 0; i < length; i++) { |
| 610 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 611 | |
| 612 | // Plain |
| 613 | { |
| 614 | vh.set(array, i, VALUE_1); |
| 615 | double x = (double) vh.get(array, i); |
| 616 | assertEquals(x, VALUE_1, "get double value"); |
| 617 | } |
| 618 | |
| 619 | |
| 620 | if (iAligned) { |
| 621 | // Volatile |
| 622 | { |
| 623 | vh.setVolatile(array, i, VALUE_2); |
| 624 | double x = (double) vh.getVolatile(array, i); |
| 625 | assertEquals(x, VALUE_2, "setVolatile double value"); |
| 626 | } |
| 627 | |
| 628 | // Lazy |
| 629 | { |
| 630 | vh.setRelease(array, i, VALUE_1); |
| 631 | double x = (double) vh.getAcquire(array, i); |
| 632 | assertEquals(x, VALUE_1, "setRelease double value"); |
| 633 | } |
| 634 | |
| 635 | // Opaque |
| 636 | { |
| 637 | vh.setOpaque(array, i, VALUE_2); |
| 638 | double x = (double) vh.getOpaque(array, i); |
| 639 | assertEquals(x, VALUE_2, "setOpaque double value"); |
| 640 | } |
| 641 | |
| 642 | vh.set(array, i, VALUE_1); |
| 643 | |
| 644 | // Compare |
| 645 | { |
| 646 | boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); |
| 647 | assertEquals(r, true, "success compareAndSet double"); |
| 648 | double x = (double) vh.get(array, i); |
| 649 | assertEquals(x, VALUE_2, "success compareAndSet double value"); |
| 650 | } |
| 651 | |
| 652 | { |
| 653 | boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); |
| 654 | assertEquals(r, false, "failing compareAndSet double"); |
| 655 | double x = (double) vh.get(array, i); |
| 656 | assertEquals(x, VALUE_2, "failing compareAndSet double value"); |
| 657 | } |
| 658 | |
| 659 | { |
| 660 | double r = (double) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1); |
| 661 | assertEquals(r, VALUE_2, "success compareAndExchangeVolatile double"); |
| 662 | double x = (double) vh.get(array, i); |
| 663 | assertEquals(x, VALUE_1, "success compareAndExchangeVolatile double value"); |
| 664 | } |
| 665 | |
| 666 | { |
| 667 | double r = (double) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3); |
| 668 | assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile double"); |
| 669 | double x = (double) vh.get(array, i); |
| 670 | assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile double value"); |
| 671 | } |
| 672 | |
| 673 | { |
| 674 | double r = (double) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); |
| 675 | assertEquals(r, VALUE_1, "success compareAndExchangeAcquire double"); |
| 676 | double x = (double) vh.get(array, i); |
| 677 | assertEquals(x, VALUE_2, "success compareAndExchangeAcquire double value"); |
| 678 | } |
| 679 | |
| 680 | { |
| 681 | double r = (double) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); |
| 682 | assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire double"); |
| 683 | double x = (double) vh.get(array, i); |
| 684 | assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire double value"); |
| 685 | } |
| 686 | |
| 687 | { |
| 688 | double r = (double) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); |
| 689 | assertEquals(r, VALUE_2, "success compareAndExchangeRelease double"); |
| 690 | double x = (double) vh.get(array, i); |
| 691 | assertEquals(x, VALUE_1, "success compareAndExchangeRelease double value"); |
| 692 | } |
| 693 | |
| 694 | { |
| 695 | double r = (double) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); |
| 696 | assertEquals(r, VALUE_1, "failing compareAndExchangeRelease double"); |
| 697 | double x = (double) vh.get(array, i); |
| 698 | assertEquals(x, VALUE_1, "failing compareAndExchangeRelease double value"); |
| 699 | } |
| 700 | |
| 701 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 702 | boolean success = false; |
| 703 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 704 | success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2); |
| 705 | } |
| 706 | assertEquals(success, true, "weakCompareAndSet double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 707 | double x = (double) vh.get(array, i); |
| 708 | assertEquals(x, VALUE_2, "weakCompareAndSet double value"); |
| 709 | } |
| 710 | |
| 711 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 712 | boolean success = false; |
| 713 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 714 | success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); |
| 715 | } |
| 716 | assertEquals(success, true, "weakCompareAndSetAcquire double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 717 | double x = (double) vh.get(array, i); |
| 718 | assertEquals(x, VALUE_1, "weakCompareAndSetAcquire double"); |
| 719 | } |
| 720 | |
| 721 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 722 | boolean success = false; |
| 723 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 724 | success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); |
| 725 | } |
| 726 | assertEquals(success, true, "weakCompareAndSetRelease double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 727 | double x = (double) vh.get(array, i); |
| 728 | assertEquals(x, VALUE_2, "weakCompareAndSetRelease double"); |
| 729 | } |
| 730 | |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 731 | { |
| 732 | boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1); |
| 733 | assertEquals(r, true, "weakCompareAndSetVolatile double"); |
| 734 | double x = (double) vh.get(array, i); |
| 735 | assertEquals(x, VALUE_1, "weakCompareAndSetVolatile double value"); |
| 736 | } |
| 737 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 738 | // Compare set and get |
| 739 | { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 740 | double o = (double) vh.getAndSet(array, i, VALUE_2); |
| 741 | assertEquals(o, VALUE_1, "getAndSet double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 742 | double x = (double) vh.get(array, i); |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 743 | assertEquals(x, VALUE_2, "getAndSet double value"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | |
| 751 | static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) { |
| 752 | VarHandle vh = vhs.s; |
| 753 | ByteBuffer array = bs.s; |
| 754 | |
| 755 | int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
| 756 | |
| 757 | bs.fill((byte) 0xff); |
| 758 | int length = array.limit() - SIZE + 1; |
| 759 | for (int i = 0; i < length; i++) { |
| 760 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 761 | |
| 762 | // Plain |
| 763 | { |
| 764 | vh.set(array, i, VALUE_1); |
| 765 | double x = (double) vh.get(array, i); |
| 766 | assertEquals(x, VALUE_1, "get double value"); |
| 767 | } |
| 768 | |
| 769 | if (iAligned) { |
| 770 | // Volatile |
| 771 | { |
| 772 | vh.setVolatile(array, i, VALUE_2); |
| 773 | double x = (double) vh.getVolatile(array, i); |
| 774 | assertEquals(x, VALUE_2, "setVolatile double value"); |
| 775 | } |
| 776 | |
| 777 | // Lazy |
| 778 | { |
| 779 | vh.setRelease(array, i, VALUE_1); |
| 780 | double x = (double) vh.getAcquire(array, i); |
| 781 | assertEquals(x, VALUE_1, "setRelease double value"); |
| 782 | } |
| 783 | |
| 784 | // Opaque |
| 785 | { |
| 786 | vh.setOpaque(array, i, VALUE_2); |
| 787 | double x = (double) vh.getOpaque(array, i); |
| 788 | assertEquals(x, VALUE_2, "setOpaque double value"); |
| 789 | } |
| 790 | |
| 791 | vh.set(array, i, VALUE_1); |
| 792 | |
| 793 | // Compare |
| 794 | { |
| 795 | boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); |
| 796 | assertEquals(r, true, "success compareAndSet double"); |
| 797 | double x = (double) vh.get(array, i); |
| 798 | assertEquals(x, VALUE_2, "success compareAndSet double value"); |
| 799 | } |
| 800 | |
| 801 | { |
| 802 | boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); |
| 803 | assertEquals(r, false, "failing compareAndSet double"); |
| 804 | double x = (double) vh.get(array, i); |
| 805 | assertEquals(x, VALUE_2, "failing compareAndSet double value"); |
| 806 | } |
| 807 | |
| 808 | { |
| 809 | double r = (double) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1); |
| 810 | assertEquals(r, VALUE_2, "success compareAndExchangeVolatile double"); |
| 811 | double x = (double) vh.get(array, i); |
| 812 | assertEquals(x, VALUE_1, "success compareAndExchangeVolatile double value"); |
| 813 | } |
| 814 | |
| 815 | { |
| 816 | double r = (double) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3); |
| 817 | assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile double"); |
| 818 | double x = (double) vh.get(array, i); |
| 819 | assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile double value"); |
| 820 | } |
| 821 | |
| 822 | { |
| 823 | double r = (double) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); |
| 824 | assertEquals(r, VALUE_1, "success compareAndExchangeAcquire double"); |
| 825 | double x = (double) vh.get(array, i); |
| 826 | assertEquals(x, VALUE_2, "success compareAndExchangeAcquire double value"); |
| 827 | } |
| 828 | |
| 829 | { |
| 830 | double r = (double) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); |
| 831 | assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire double"); |
| 832 | double x = (double) vh.get(array, i); |
| 833 | assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire double value"); |
| 834 | } |
| 835 | |
| 836 | { |
| 837 | double r = (double) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); |
| 838 | assertEquals(r, VALUE_2, "success compareAndExchangeRelease double"); |
| 839 | double x = (double) vh.get(array, i); |
| 840 | assertEquals(x, VALUE_1, "success compareAndExchangeRelease double value"); |
| 841 | } |
| 842 | |
| 843 | { |
| 844 | double r = (double) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); |
| 845 | assertEquals(r, VALUE_1, "failing compareAndExchangeRelease double"); |
| 846 | double x = (double) vh.get(array, i); |
| 847 | assertEquals(x, VALUE_1, "failing compareAndExchangeRelease double value"); |
| 848 | } |
| 849 | |
| 850 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 851 | boolean success = false; |
| 852 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 853 | success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2); |
| 854 | } |
| 855 | assertEquals(success, true, "weakCompareAndSet double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 856 | double x = (double) vh.get(array, i); |
| 857 | assertEquals(x, VALUE_2, "weakCompareAndSet double value"); |
| 858 | } |
| 859 | |
| 860 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 861 | boolean success = false; |
| 862 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 863 | success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); |
| 864 | } |
| 865 | assertEquals(success, true, "weakCompareAndSetAcquire double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 866 | double x = (double) vh.get(array, i); |
| 867 | assertEquals(x, VALUE_1, "weakCompareAndSetAcquire double"); |
| 868 | } |
| 869 | |
| 870 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 871 | boolean success = false; |
| 872 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 873 | success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); |
| 874 | } |
| 875 | assertEquals(success, true, "weakCompareAndSetRelease double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 876 | double x = (double) vh.get(array, i); |
| 877 | assertEquals(x, VALUE_2, "weakCompareAndSetRelease double"); |
| 878 | } |
| 879 | |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 880 | { |
| 881 | boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1); |
| 882 | assertEquals(r, true, "weakCompareAndSetVolatile double"); |
| 883 | double x = (double) vh.get(array, i); |
| 884 | assertEquals(x, VALUE_1, "weakCompareAndSetVolatile double value"); |
| 885 | } |
| 886 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 887 | // Compare set and get |
| 888 | { |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 889 | double o = (double) vh.getAndSet(array, i, VALUE_2); |
| 890 | assertEquals(o, VALUE_1, "getAndSet double"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 891 | double x = (double) vh.get(array, i); |
Paul Sandoz | e955660 | 2016-04-29 13:46:19 -0700 | [diff] [blame] | 892 | assertEquals(x, VALUE_2, "getAndSet double value"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) { |
| 900 | VarHandle vh = vhs.s; |
| 901 | ByteBuffer array = bs.s; |
| 902 | |
| 903 | int misalignmentAtZero = array.alignmentOffset(0, SIZE); |
| 904 | |
| 905 | ByteBuffer bb = ByteBuffer.allocate(SIZE); |
| 906 | bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); |
| 907 | bs.fill(bb.putDouble(0, VALUE_2).array()); |
| 908 | |
| 909 | int length = array.limit() - SIZE + 1; |
| 910 | for (int i = 0; i < length; i++) { |
| 911 | boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; |
| 912 | |
| 913 | double v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) |
| 914 | ? rotateLeft(VALUE_2, (i % SIZE) << 3) |
| 915 | : rotateRight(VALUE_2, (i % SIZE) << 3); |
| 916 | // Plain |
| 917 | { |
| 918 | double x = (double) vh.get(array, i); |
| 919 | assertEquals(x, v, "get double value"); |
| 920 | } |
| 921 | |
| 922 | if (iAligned) { |
| 923 | // Volatile |
| 924 | { |
| 925 | double x = (double) vh.getVolatile(array, i); |
| 926 | assertEquals(x, v, "getVolatile double value"); |
| 927 | } |
| 928 | |
| 929 | // Lazy |
| 930 | { |
| 931 | double x = (double) vh.getAcquire(array, i); |
| 932 | assertEquals(x, v, "getRelease double value"); |
| 933 | } |
| 934 | |
| 935 | // Opaque |
| 936 | { |
| 937 | double x = (double) vh.getOpaque(array, i); |
| 938 | assertEquals(x, v, "getOpaque double value"); |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | } |
| 945 | |