blob: fcb17817642b5f9ba9ea2290cd47244efaed6a7e [file] [log] [blame]
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001/*
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 Sandoz1af28062016-04-26 18:30:00 -070026 * @bug 8154556
Paul Sandoz9fb30a32016-03-24 11:21:21 +010027 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestByteArrayAsFloat
28 * @run testng/othervm -Diters=20000 VarHandleTestByteArrayAsFloat
29 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestByteArrayAsFloat
30 */
31
32import org.testng.annotations.DataProvider;
33import org.testng.annotations.Test;
34
35import java.lang.invoke.MethodHandles;
36import java.lang.invoke.VarHandle;
37import java.nio.ByteBuffer;
38import java.nio.ByteOrder;
39import java.util.ArrayList;
40import java.util.Arrays;
41import java.util.EnumSet;
42import java.util.List;
43
44import static org.testng.Assert.*;
45
46public class VarHandleTestByteArrayAsFloat extends VarHandleBaseByteArrayTest {
47 static final int SIZE = Float.BYTES;
48
49 static final float VALUE_1 = 0x01020304;
50
51 static final float VALUE_2 = 0x11121314;
52
53 static final float VALUE_3 = 0x21222324;
54
55
56 @Override
57 public void setupVarHandleSources() {
58 // Combinations of VarHandle byte[] or ByteBuffer
59 vhss = new ArrayList<>();
60 for (MemoryMode endianess : Arrays.asList(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) {
Paul Sandoz1af28062016-04-26 18:30:00 -070061
62 ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN
63 ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
Paul Sandoz9fb30a32016-03-24 11:21:21 +010064 VarHandleSource aeh = new VarHandleSource(
Paul Sandoz1af28062016-04-26 18:30:00 -070065 MethodHandles.byteArrayViewVarHandle(float[].class, bo),
Paul Sandoz9fb30a32016-03-24 11:21:21 +010066 endianess, MemoryMode.READ_WRITE);
67 vhss.add(aeh);
68
69 VarHandleSource bbh = new VarHandleSource(
Paul Sandoz1af28062016-04-26 18:30:00 -070070 MethodHandles.byteBufferViewVarHandle(float[].class, bo),
Paul Sandoz9fb30a32016-03-24 11:21:21 +010071 endianess, MemoryMode.READ_WRITE);
72 vhss.add(bbh);
73 }
74 }
75
76
77 @Test(dataProvider = "varHandlesProvider")
78 public void testIsAccessModeSupported(VarHandleSource vhs) {
79 VarHandle vh = vhs.s;
80
Paul Sandoza7aff442016-04-13 15:05:48 +020081 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
82 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
Paul Sandoz9fb30a32016-03-24 11:21:21 +010083
Paul Sandoza7aff442016-04-13 15:05:48 +020084 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
85 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
86 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
87 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
88 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
89 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
Paul Sandoz9fb30a32016-03-24 11:21:21 +010090
Paul Sandoza7aff442016-04-13 15:05:48 +020091 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
92 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
93 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
94 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
Paul Sandoza7aff442016-04-13 15:05:48 +020098 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
Paul Sandoz9fb30a32016-03-24 11:21:21 +010099
Paul Sandoza7aff442016-04-13 15:05:48 +0200100 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
101 assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100102 }
103
104 @Test(dataProvider = "typesProvider")
105 public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) {
106 assertEquals(vh.varType(), float.class);
107
108 assertEquals(vh.coordinateTypes(), pts);
109
110 testTypes(vh);
111 }
112
113
114 @DataProvider
115 public Object[][] accessTestCaseProvider() throws Exception {
116 List<AccessTestCase<?>> cases = new ArrayList<>();
117
118 for (ByteArrayViewSource<?> bav : bavss) {
119 for (VarHandleSource vh : vhss) {
120 if (vh.matches(bav)) {
121 if (bav instanceof ByteArraySource) {
122 ByteArraySource bas = (ByteArraySource) bav;
123
124 cases.add(new VarHandleSourceAccessTestCase(
125 "read write", bav, vh, h -> testArrayReadWrite(bas, h),
126 true));
127 cases.add(new VarHandleSourceAccessTestCase(
128 "unsupported", bav, vh, h -> testArrayUnsupported(bas, h),
129 false));
130 cases.add(new VarHandleSourceAccessTestCase(
131 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h),
132 false));
133 cases.add(new VarHandleSourceAccessTestCase(
134 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h),
135 false));
136 }
137 else {
138 ByteBufferSource bbs = (ByteBufferSource) bav;
139
140 if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) {
141 cases.add(new VarHandleSourceAccessTestCase(
142 "read write", bav, vh, h -> testArrayReadWrite(bbs, h),
143 true));
144 }
145 else {
146 cases.add(new VarHandleSourceAccessTestCase(
147 "read only", bav, vh, h -> testArrayReadOnly(bbs, h),
148 true));
149 }
150
151 cases.add(new VarHandleSourceAccessTestCase(
152 "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h),
153 false));
154 cases.add(new VarHandleSourceAccessTestCase(
155 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h),
156 false));
157 cases.add(new VarHandleSourceAccessTestCase(
158 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h),
159 false));
160 }
161 }
162 }
163 }
164
165 // Work around issue with jtreg summary reporting which truncates
166 // the String result of Object.toString to 30 characters, hence
167 // the first dummy argument
168 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
169 }
170
171 @Test(dataProvider = "accessTestCaseProvider")
172 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
173 T t = atc.get();
174 int iters = atc.requiresLoop() ? ITERS : 1;
175 for (int c = 0; c < iters; c++) {
176 atc.testAccess(t);
177 }
178 }
179
180
181 static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) {
182 VarHandle vh = vhs.s;
183 byte[] array = bs.s;
184 int ci = 1;
185
186
187 checkUOE(() -> {
188 float o = (float) vh.getAndAdd(array, ci, VALUE_1);
189 });
190
191 checkUOE(() -> {
192 float o = (float) vh.addAndGet(array, ci, VALUE_1);
193 });
194 }
195
196 static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) {
197 VarHandle vh = vhs.s;
198 ByteBuffer array = bs.s;
199 int ci = 0;
200 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
201
202 if (readOnly) {
203 checkROBE(() -> {
204 vh.set(array, ci, VALUE_1);
205 });
206 }
207
208 if (readOnly) {
209 checkROBE(() -> {
210 vh.setVolatile(array, ci, VALUE_1);
211 });
212
213 checkROBE(() -> {
214 vh.setRelease(array, ci, VALUE_1);
215 });
216
217 checkROBE(() -> {
218 vh.setOpaque(array, ci, VALUE_1);
219 });
220
221 checkROBE(() -> {
222 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
223 });
224
225 checkROBE(() -> {
226 float r = (float) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
227 });
228
229 checkROBE(() -> {
230 float r = (float) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
231 });
232
233 checkROBE(() -> {
234 float r = (float) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
235 });
236
237 checkROBE(() -> {
238 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
239 });
240
241 checkROBE(() -> {
242 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
243 });
244
245 checkROBE(() -> {
246 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
247 });
248
249 checkROBE(() -> {
250 float o = (float) vh.getAndSet(array, ci, VALUE_1);
251 });
252 checkUOE(() -> {
253 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
254 });
255
256 checkUOE(() -> {
257 float o = (float) vh.getAndAdd(array, ci, VALUE_1);
258 });
259
260 checkUOE(() -> {
261 float o = (float) vh.addAndGet(array, ci, VALUE_1);
262 });
263 }
264 else {
265 checkUOE(() -> {
266 float o = (float) vh.getAndAdd(array, ci, VALUE_1);
267 });
268
269 checkUOE(() -> {
270 float o = (float) vh.addAndGet(array, ci, VALUE_1);
271 });
272 }
273 }
274
275
276 static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
277 VarHandle vh = vhs.s;
278 byte[] array = bs.s;
279
280 int length = array.length - SIZE + 1;
281 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
282 final int ci = i;
283
284 checkIOOBE(() -> {
285 float x = (float) vh.get(array, ci);
286 });
287
288 checkIOOBE(() -> {
289 vh.set(array, ci, VALUE_1);
290 });
291
292 checkIOOBE(() -> {
293 float x = (float) vh.getVolatile(array, ci);
294 });
295
296 checkIOOBE(() -> {
297 float x = (float) vh.getAcquire(array, ci);
298 });
299
300 checkIOOBE(() -> {
301 float x = (float) vh.getOpaque(array, ci);
302 });
303
304 checkIOOBE(() -> {
305 vh.setVolatile(array, ci, VALUE_1);
306 });
307
308 checkIOOBE(() -> {
309 vh.setRelease(array, ci, VALUE_1);
310 });
311
312 checkIOOBE(() -> {
313 vh.setOpaque(array, ci, VALUE_1);
314 });
315
316 checkIOOBE(() -> {
317 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
318 });
319
320 checkIOOBE(() -> {
321 float r = (float) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
322 });
323
324 checkIOOBE(() -> {
325 float r = (float) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
326 });
327
328 checkIOOBE(() -> {
329 float r = (float) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
330 });
331
332 checkIOOBE(() -> {
333 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
334 });
335
336 checkIOOBE(() -> {
337 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
338 });
339
340 checkIOOBE(() -> {
341 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
342 });
343
344 checkIOOBE(() -> {
345 float o = (float) vh.getAndSet(array, ci, VALUE_1);
346 });
347
348
349 }
350 }
351
352 static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
353 VarHandle vh = vhs.s;
354 ByteBuffer array = bs.s;
355
356 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
357
358 int length = array.limit() - SIZE + 1;
359 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
360 final int ci = i;
361
362 checkIOOBE(() -> {
363 float x = (float) vh.get(array, ci);
364 });
365
366 if (!readOnly) {
367 checkIOOBE(() -> {
368 vh.set(array, ci, VALUE_1);
369 });
370 }
371
372 checkIOOBE(() -> {
373 float x = (float) vh.getVolatile(array, ci);
374 });
375
376 checkIOOBE(() -> {
377 float x = (float) vh.getAcquire(array, ci);
378 });
379
380 checkIOOBE(() -> {
381 float x = (float) vh.getOpaque(array, ci);
382 });
383
384 if (!readOnly) {
385 checkIOOBE(() -> {
386 vh.setVolatile(array, ci, VALUE_1);
387 });
388
389 checkIOOBE(() -> {
390 vh.setRelease(array, ci, VALUE_1);
391 });
392
393 checkIOOBE(() -> {
394 vh.setOpaque(array, ci, VALUE_1);
395 });
396
397 checkIOOBE(() -> {
398 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
399 });
400
401 checkIOOBE(() -> {
402 float r = (float) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
403 });
404
405 checkIOOBE(() -> {
406 float r = (float) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
407 });
408
409 checkIOOBE(() -> {
410 float r = (float) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
411 });
412
413 checkIOOBE(() -> {
414 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
415 });
416
417 checkIOOBE(() -> {
418 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
419 });
420
421 checkIOOBE(() -> {
422 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
423 });
424
425 checkIOOBE(() -> {
426 float o = (float) vh.getAndSet(array, ci, VALUE_1);
427 });
428
429 }
430 }
431 }
432
433 static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
434 VarHandle vh = vhs.s;
435 byte[] array = bs.s;
436
437 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
438
439 int length = array.length - SIZE + 1;
440 for (int i = 0; i < length; i++) {
441 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
442 final int ci = i;
443
444 if (!iAligned) {
445 checkISE(() -> {
446 float x = (float) vh.getVolatile(array, ci);
447 });
448
449 checkISE(() -> {
450 float x = (float) vh.getAcquire(array, ci);
451 });
452
453 checkISE(() -> {
454 float x = (float) vh.getOpaque(array, ci);
455 });
456
457 checkISE(() -> {
458 vh.setVolatile(array, ci, VALUE_1);
459 });
460
461 checkISE(() -> {
462 vh.setRelease(array, ci, VALUE_1);
463 });
464
465 checkISE(() -> {
466 vh.setOpaque(array, ci, VALUE_1);
467 });
468
469 checkISE(() -> {
470 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
471 });
472
473 checkISE(() -> {
474 float r = (float) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
475 });
476
477 checkISE(() -> {
478 float r = (float) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
479 });
480
481 checkISE(() -> {
482 float r = (float) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
483 });
484
485 checkISE(() -> {
486 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
487 });
488
489 checkISE(() -> {
490 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
491 });
492
493 checkISE(() -> {
494 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
495 });
496
497 checkISE(() -> {
498 float o = (float) vh.getAndSet(array, ci, VALUE_1);
499 });
500
501
502 }
503 }
504 }
505
506 static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
507 VarHandle vh = vhs.s;
508 ByteBuffer array = bs.s;
509
510 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
511 int misalignmentAtZero = array.alignmentOffset(0, SIZE);
512
513 int length = array.limit() - SIZE + 1;
514 for (int i = 0; i < length; i++) {
515 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
516 final int ci = i;
517
518 if (!iAligned) {
519 checkISE(() -> {
520 float x = (float) vh.getVolatile(array, ci);
521 });
522
523 checkISE(() -> {
524 float x = (float) vh.getAcquire(array, ci);
525 });
526
527 checkISE(() -> {
528 float x = (float) vh.getOpaque(array, ci);
529 });
530
531 if (!readOnly) {
532 checkISE(() -> {
533 vh.setVolatile(array, ci, VALUE_1);
534 });
535
536 checkISE(() -> {
537 vh.setRelease(array, ci, VALUE_1);
538 });
539
540 checkISE(() -> {
541 vh.setOpaque(array, ci, VALUE_1);
542 });
543
544 checkISE(() -> {
545 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
546 });
547
548 checkISE(() -> {
549 float r = (float) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
550 });
551
552 checkISE(() -> {
553 float r = (float) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
554 });
555
556 checkISE(() -> {
557 float r = (float) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
558 });
559
560 checkISE(() -> {
561 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
562 });
563
564 checkISE(() -> {
565 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
566 });
567
568 checkISE(() -> {
569 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
570 });
571
572 checkISE(() -> {
573 float o = (float) vh.getAndSet(array, ci, VALUE_1);
574 });
575
576 }
577 }
578 }
579 }
580
581 static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) {
582 VarHandle vh = vhs.s;
583 byte[] array = bs.s;
584
585 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
586
587 bs.fill((byte) 0xff);
588 int length = array.length - SIZE + 1;
589 for (int i = 0; i < length; i++) {
590 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
591
592 // Plain
593 {
594 vh.set(array, i, VALUE_1);
595 float x = (float) vh.get(array, i);
596 assertEquals(x, VALUE_1, "get float value");
597 }
598
599
600 if (iAligned) {
601 // Volatile
602 {
603 vh.setVolatile(array, i, VALUE_2);
604 float x = (float) vh.getVolatile(array, i);
605 assertEquals(x, VALUE_2, "setVolatile float value");
606 }
607
608 // Lazy
609 {
610 vh.setRelease(array, i, VALUE_1);
611 float x = (float) vh.getAcquire(array, i);
612 assertEquals(x, VALUE_1, "setRelease float value");
613 }
614
615 // Opaque
616 {
617 vh.setOpaque(array, i, VALUE_2);
618 float x = (float) vh.getOpaque(array, i);
619 assertEquals(x, VALUE_2, "setOpaque float value");
620 }
621
622 vh.set(array, i, VALUE_1);
623
624 // Compare
625 {
626 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
627 assertEquals(r, true, "success compareAndSet float");
628 float x = (float) vh.get(array, i);
629 assertEquals(x, VALUE_2, "success compareAndSet float value");
630 }
631
632 {
633 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
634 assertEquals(r, false, "failing compareAndSet float");
635 float x = (float) vh.get(array, i);
636 assertEquals(x, VALUE_2, "failing compareAndSet float value");
637 }
638
639 {
640 float r = (float) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
641 assertEquals(r, VALUE_2, "success compareAndExchangeVolatile float");
642 float x = (float) vh.get(array, i);
643 assertEquals(x, VALUE_1, "success compareAndExchangeVolatile float value");
644 }
645
646 {
647 float r = (float) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
648 assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile float");
649 float x = (float) vh.get(array, i);
650 assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile float value");
651 }
652
653 {
654 float r = (float) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
655 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire float");
656 float x = (float) vh.get(array, i);
657 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire float value");
658 }
659
660 {
661 float r = (float) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
662 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire float");
663 float x = (float) vh.get(array, i);
664 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire float value");
665 }
666
667 {
668 float r = (float) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
669 assertEquals(r, VALUE_2, "success compareAndExchangeRelease float");
670 float x = (float) vh.get(array, i);
671 assertEquals(x, VALUE_1, "success compareAndExchangeRelease float value");
672 }
673
674 {
675 float r = (float) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
676 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease float");
677 float x = (float) vh.get(array, i);
678 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease float value");
679 }
680
681 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300682 boolean success = false;
683 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
684 success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2);
685 }
686 assertEquals(success, true, "weakCompareAndSet float");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100687 float x = (float) vh.get(array, i);
688 assertEquals(x, VALUE_2, "weakCompareAndSet float value");
689 }
690
691 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300692 boolean success = false;
693 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
694 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
695 }
696 assertEquals(success, true, "weakCompareAndSetAcquire float");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100697 float x = (float) vh.get(array, i);
698 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire float");
699 }
700
701 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300702 boolean success = false;
703 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
704 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
705 }
706 assertEquals(success, true, "weakCompareAndSetRelease float");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100707 float x = (float) vh.get(array, i);
708 assertEquals(x, VALUE_2, "weakCompareAndSetRelease float");
709 }
710
711 // Compare set and get
712 {
713 float o = (float) vh.getAndSet(array, i, VALUE_1);
714 assertEquals(o, VALUE_2, "getAndSet float");
715 float x = (float) vh.get(array, i);
716 assertEquals(x, VALUE_1, "getAndSet float value");
717 }
718
719 }
720 }
721 }
722
723
724 static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) {
725 VarHandle vh = vhs.s;
726 ByteBuffer array = bs.s;
727
728 int misalignmentAtZero = array.alignmentOffset(0, SIZE);
729
730 bs.fill((byte) 0xff);
731 int length = array.limit() - SIZE + 1;
732 for (int i = 0; i < length; i++) {
733 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
734
735 // Plain
736 {
737 vh.set(array, i, VALUE_1);
738 float x = (float) vh.get(array, i);
739 assertEquals(x, VALUE_1, "get float value");
740 }
741
742 if (iAligned) {
743 // Volatile
744 {
745 vh.setVolatile(array, i, VALUE_2);
746 float x = (float) vh.getVolatile(array, i);
747 assertEquals(x, VALUE_2, "setVolatile float value");
748 }
749
750 // Lazy
751 {
752 vh.setRelease(array, i, VALUE_1);
753 float x = (float) vh.getAcquire(array, i);
754 assertEquals(x, VALUE_1, "setRelease float value");
755 }
756
757 // Opaque
758 {
759 vh.setOpaque(array, i, VALUE_2);
760 float x = (float) vh.getOpaque(array, i);
761 assertEquals(x, VALUE_2, "setOpaque float value");
762 }
763
764 vh.set(array, i, VALUE_1);
765
766 // Compare
767 {
768 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
769 assertEquals(r, true, "success compareAndSet float");
770 float x = (float) vh.get(array, i);
771 assertEquals(x, VALUE_2, "success compareAndSet float value");
772 }
773
774 {
775 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
776 assertEquals(r, false, "failing compareAndSet float");
777 float x = (float) vh.get(array, i);
778 assertEquals(x, VALUE_2, "failing compareAndSet float value");
779 }
780
781 {
782 float r = (float) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
783 assertEquals(r, VALUE_2, "success compareAndExchangeVolatile float");
784 float x = (float) vh.get(array, i);
785 assertEquals(x, VALUE_1, "success compareAndExchangeVolatile float value");
786 }
787
788 {
789 float r = (float) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
790 assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile float");
791 float x = (float) vh.get(array, i);
792 assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile float value");
793 }
794
795 {
796 float r = (float) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
797 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire float");
798 float x = (float) vh.get(array, i);
799 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire float value");
800 }
801
802 {
803 float r = (float) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
804 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire float");
805 float x = (float) vh.get(array, i);
806 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire float value");
807 }
808
809 {
810 float r = (float) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
811 assertEquals(r, VALUE_2, "success compareAndExchangeRelease float");
812 float x = (float) vh.get(array, i);
813 assertEquals(x, VALUE_1, "success compareAndExchangeRelease float value");
814 }
815
816 {
817 float r = (float) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
818 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease float");
819 float x = (float) vh.get(array, i);
820 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease float value");
821 }
822
823 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300824 boolean success = false;
825 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
826 success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2);
827 }
828 assertEquals(success, true, "weakCompareAndSet float");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100829 float x = (float) vh.get(array, i);
830 assertEquals(x, VALUE_2, "weakCompareAndSet float value");
831 }
832
833 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300834 boolean success = false;
835 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
836 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
837 }
838 assertEquals(success, true, "weakCompareAndSetAcquire float");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100839 float x = (float) vh.get(array, i);
840 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire float");
841 }
842
843 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300844 boolean success = false;
845 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
846 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
847 }
848 assertEquals(success, true, "weakCompareAndSetRelease float");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100849 float x = (float) vh.get(array, i);
850 assertEquals(x, VALUE_2, "weakCompareAndSetRelease float");
851 }
852
853 // Compare set and get
854 {
855 float o = (float) vh.getAndSet(array, i, VALUE_1);
856 assertEquals(o, VALUE_2, "getAndSet float");
857 float x = (float) vh.get(array, i);
858 assertEquals(x, VALUE_1, "getAndSet float value");
859 }
860
861 }
862 }
863 }
864
865 static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) {
866 VarHandle vh = vhs.s;
867 ByteBuffer array = bs.s;
868
869 int misalignmentAtZero = array.alignmentOffset(0, SIZE);
870
871 ByteBuffer bb = ByteBuffer.allocate(SIZE);
872 bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
873 bs.fill(bb.putFloat(0, VALUE_2).array());
874
875 int length = array.limit() - SIZE + 1;
876 for (int i = 0; i < length; i++) {
877 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
878
879 float v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes)
880 ? rotateLeft(VALUE_2, (i % SIZE) << 3)
881 : rotateRight(VALUE_2, (i % SIZE) << 3);
882 // Plain
883 {
884 float x = (float) vh.get(array, i);
885 assertEquals(x, v, "get float value");
886 }
887
888 if (iAligned) {
889 // Volatile
890 {
891 float x = (float) vh.getVolatile(array, i);
892 assertEquals(x, v, "getVolatile float value");
893 }
894
895 // Lazy
896 {
897 float x = (float) vh.getAcquire(array, i);
898 assertEquals(x, v, "getRelease float value");
899 }
900
901 // Opaque
902 {
903 float x = (float) vh.getOpaque(array, i);
904 assertEquals(x, v, "getOpaque float value");
905 }
906 }
907 }
908 }
909
910}
911