X-VarHandleTestByteArrayView.java.template revision 15541:e2d28c613133
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
26 * @bug 8154556
27 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestByteArrayAs$Type$
28 * @run testng/othervm -Diters=20000                         VarHandleTestByteArrayAs$Type$
29 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestByteArrayAs$Type$
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 VarHandleTestByteArrayAs$Type$ extends VarHandleBaseByteArrayTest {
47    static final int SIZE = $BoxType$.BYTES;
48
49    static final $type$ VALUE_1 = $value1$;
50
51    static final $type$ VALUE_2 = $value2$;
52
53    static final $type$ VALUE_3 = $value3$;
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)) {
61
62            ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN
63                    ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
64            VarHandleSource aeh = new VarHandleSource(
65                    MethodHandles.byteArrayViewVarHandle($type$[].class, bo),
66                    endianess, MemoryMode.READ_WRITE);
67            vhss.add(aeh);
68
69            VarHandleSource bbh = new VarHandleSource(
70                    MethodHandles.byteBufferViewVarHandle($type$[].class, bo),
71                    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
81        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
82        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
83
84        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));
90
91#if[CAS]
92        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
93        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
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_PLAIN));
97        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
98        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
99        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
100        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
101        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
102        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
103#else[CAS]
104        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
105        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
106        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
107        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
108        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
109        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
110        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
111        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
112        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
113        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
114        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
115#end[CAS]
116
117#if[AtomicAdd]
118        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
119        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
120        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
121#else[AtomicAdd]
122        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
123        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
124        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
125#end[AtomicAdd]
126
127#if[Bitwise]
128        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
129        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
130        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
131        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
132        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
133        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
134        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
135        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
136        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
137#else[Bitwise]
138        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
139        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
140        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
141        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
142        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
143        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
144        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
145        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
146        assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
147#end[Bitwise]
148    }
149
150    @Test(dataProvider = "typesProvider")
151    public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) {
152        assertEquals(vh.varType(), $type$.class);
153
154        assertEquals(vh.coordinateTypes(), pts);
155
156        testTypes(vh);
157    }
158
159
160    @DataProvider
161    public Object[][] accessTestCaseProvider() throws Exception {
162        List<AccessTestCase<?>> cases = new ArrayList<>();
163
164        for (ByteArrayViewSource<?> bav : bavss) {
165            for (VarHandleSource vh : vhss) {
166                if (vh.matches(bav)) {
167                    if (bav instanceof ByteArraySource) {
168                        ByteArraySource bas = (ByteArraySource) bav;
169
170                        cases.add(new VarHandleSourceAccessTestCase(
171                                "read write", bav, vh, h -> testArrayReadWrite(bas, h),
172                                true));
173                        cases.add(new VarHandleSourceAccessTestCase(
174                                "unsupported", bav, vh, h -> testArrayUnsupported(bas, h),
175                                false));
176                        cases.add(new VarHandleSourceAccessTestCase(
177                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h),
178                                false));
179                        cases.add(new VarHandleSourceAccessTestCase(
180                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h),
181                                false));
182                    }
183                    else {
184                        ByteBufferSource bbs = (ByteBufferSource) bav;
185
186                        if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) {
187                            cases.add(new VarHandleSourceAccessTestCase(
188                                    "read write", bav, vh, h -> testArrayReadWrite(bbs, h),
189                                    true));
190                        }
191                        else {
192                            cases.add(new VarHandleSourceAccessTestCase(
193                                    "read only", bav, vh, h -> testArrayReadOnly(bbs, h),
194                                    true));
195                        }
196
197                        cases.add(new VarHandleSourceAccessTestCase(
198                                "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h),
199                                false));
200                        cases.add(new VarHandleSourceAccessTestCase(
201                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h),
202                                false));
203                        cases.add(new VarHandleSourceAccessTestCase(
204                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h),
205                                false));
206                    }
207                }
208            }
209        }
210
211        // Work around issue with jtreg summary reporting which truncates
212        // the String result of Object.toString to 30 characters, hence
213        // the first dummy argument
214        return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
215    }
216
217    @Test(dataProvider = "accessTestCaseProvider")
218    public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
219        T t = atc.get();
220        int iters = atc.requiresLoop() ? ITERS : 1;
221        for (int c = 0; c < iters; c++) {
222            atc.testAccess(t);
223        }
224    }
225
226
227    static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) {
228        VarHandle vh = vhs.s;
229        byte[] array = bs.s;
230        int ci = 1;
231
232#if[!CAS]
233        checkUOE(() -> {
234            boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
235        });
236
237        checkUOE(() -> {
238            $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
239        });
240
241        checkUOE(() -> {
242            $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
243        });
244
245        checkUOE(() -> {
246            $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
247        });
248
249        checkUOE(() -> {
250            boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
251        });
252
253        checkUOE(() -> {
254            boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
255        });
256
257        checkUOE(() -> {
258            boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
259        });
260
261        checkUOE(() -> {
262            boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
263        });
264
265        checkUOE(() -> {
266            $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
267        });
268
269        checkUOE(() -> {
270            $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
271        });
272
273        checkUOE(() -> {
274            $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
275        });
276#end[CAS]
277
278#if[!AtomicAdd]
279        checkUOE(() -> {
280            $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
281        });
282
283        checkUOE(() -> {
284            $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
285        });
286
287        checkUOE(() -> {
288            $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
289        });
290#end[AtomicAdd]
291
292#if[!Bitwise]
293        checkUOE(() -> {
294            $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
295        });
296
297        checkUOE(() -> {
298            $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
299        });
300
301        checkUOE(() -> {
302            $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
303        });
304
305        checkUOE(() -> {
306            $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
307        });
308
309        checkUOE(() -> {
310            $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
311        });
312
313        checkUOE(() -> {
314            $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
315        });
316
317        checkUOE(() -> {
318            $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
319        });
320
321        checkUOE(() -> {
322            $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
323        });
324
325        checkUOE(() -> {
326            $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
327        });
328#end[Bitwise]
329    }
330
331    static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) {
332        VarHandle vh = vhs.s;
333        ByteBuffer array = bs.s;
334        int ci = 0;
335        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
336
337        if (readOnly) {
338            checkROBE(() -> {
339                vh.set(array, ci, VALUE_1);
340            });
341        }
342
343        if (readOnly) {
344            checkROBE(() -> {
345                vh.setVolatile(array, ci, VALUE_1);
346            });
347
348            checkROBE(() -> {
349                vh.setRelease(array, ci, VALUE_1);
350            });
351
352            checkROBE(() -> {
353                vh.setOpaque(array, ci, VALUE_1);
354            });
355#if[CAS]
356
357            checkROBE(() -> {
358                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
359            });
360
361            checkROBE(() -> {
362                $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
363            });
364
365            checkROBE(() -> {
366                $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
367            });
368
369            checkROBE(() -> {
370                $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
371            });
372
373            checkROBE(() -> {
374                boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
375            });
376
377            checkROBE(() -> {
378                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
379            });
380
381            checkROBE(() -> {
382                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
383            });
384
385            checkROBE(() -> {
386                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
387            });
388
389            checkROBE(() -> {
390                $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
391            });
392
393            checkROBE(() -> {
394                $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
395            });
396
397            checkROBE(() -> {
398                $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
399            });
400
401#else[CAS]
402            checkUOE(() -> {
403                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
404            });
405
406            checkUOE(() -> {
407                $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
408            });
409
410            checkUOE(() -> {
411                $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
412            });
413
414            checkUOE(() -> {
415                $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
416            });
417
418            checkUOE(() -> {
419                boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
420            });
421
422            checkUOE(() -> {
423                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
424            });
425
426            checkUOE(() -> {
427                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
428            });
429
430            checkUOE(() -> {
431                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
432            });
433
434            checkUOE(() -> {
435                $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
436            });
437
438            checkUOE(() -> {
439                $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
440            });
441
442            checkUOE(() -> {
443                $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
444            });
445#end[CAS]
446
447#if[AtomicAdd]
448            checkROBE(() -> {
449                $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
450            });
451
452            checkROBE(() -> {
453                $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
454            });
455
456            checkROBE(() -> {
457                $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
458            });
459#else[AtomicAdd]
460            checkUOE(() -> {
461                $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
462            });
463
464            checkUOE(() -> {
465                $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
466            });
467
468            checkUOE(() -> {
469                $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
470            });
471#end[AtomicAdd]
472
473#if[Bitwise]
474            checkROBE(() -> {
475                $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
476            });
477
478            checkROBE(() -> {
479                $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
480            });
481
482            checkROBE(() -> {
483                $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
484            });
485
486            checkROBE(() -> {
487                $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
488            });
489
490            checkROBE(() -> {
491                $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
492            });
493
494            checkROBE(() -> {
495                $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
496            });
497
498            checkROBE(() -> {
499                $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
500            });
501
502            checkROBE(() -> {
503                $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
504            });
505
506            checkROBE(() -> {
507                $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
508            });
509#else[Bitwise]
510            checkUOE(() -> {
511                $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
512            });
513
514            checkUOE(() -> {
515                $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
516            });
517
518            checkUOE(() -> {
519                $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
520            });
521
522            checkUOE(() -> {
523                $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
524            });
525
526            checkUOE(() -> {
527                $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
528            });
529
530            checkUOE(() -> {
531                $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
532            });
533
534            checkUOE(() -> {
535                $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
536            });
537
538            checkUOE(() -> {
539                $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
540            });
541
542            checkUOE(() -> {
543                $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
544            });
545#end[Bitwise]
546        }
547        else {
548#if[!CAS]
549            checkUOE(() -> {
550                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
551            });
552
553            checkUOE(() -> {
554                $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
555            });
556
557            checkUOE(() -> {
558                $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
559            });
560
561            checkUOE(() -> {
562                $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
563            });
564
565            checkUOE(() -> {
566                boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
567            });
568
569            checkUOE(() -> {
570                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
571            });
572
573            checkUOE(() -> {
574                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
575            });
576
577            checkUOE(() -> {
578                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
579            });
580
581            checkUOE(() -> {
582                $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
583            });
584
585            checkUOE(() -> {
586                $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
587            });
588
589            checkUOE(() -> {
590                $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
591            });
592#end[CAS]
593#if[!AtomicAdd]
594            checkUOE(() -> {
595                $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
596            });
597
598            checkUOE(() -> {
599                $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
600            });
601
602            checkUOE(() -> {
603                $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
604            });
605#end[AtomicAdd]
606#if[!Bitwise]
607            checkUOE(() -> {
608                $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
609            });
610
611            checkUOE(() -> {
612                $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
613            });
614
615            checkUOE(() -> {
616                $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
617            });
618
619            checkUOE(() -> {
620                $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
621            });
622
623            checkUOE(() -> {
624                $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
625            });
626
627            checkUOE(() -> {
628                $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
629            });
630
631            checkUOE(() -> {
632                $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
633            });
634
635            checkUOE(() -> {
636                $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
637            });
638
639            checkUOE(() -> {
640                $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
641            });
642#end[Bitwise]
643        }
644    }
645
646
647    static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
648        VarHandle vh = vhs.s;
649        byte[] array = bs.s;
650
651        int length = array.length - SIZE + 1;
652        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
653            final int ci = i;
654
655            checkIOOBE(() -> {
656                $type$ x = ($type$) vh.get(array, ci);
657            });
658
659            checkIOOBE(() -> {
660                vh.set(array, ci, VALUE_1);
661            });
662
663            checkIOOBE(() -> {
664                $type$ x = ($type$) vh.getVolatile(array, ci);
665            });
666
667            checkIOOBE(() -> {
668                $type$ x = ($type$) vh.getAcquire(array, ci);
669            });
670
671            checkIOOBE(() -> {
672                $type$ x = ($type$) vh.getOpaque(array, ci);
673            });
674
675            checkIOOBE(() -> {
676                vh.setVolatile(array, ci, VALUE_1);
677            });
678
679            checkIOOBE(() -> {
680                vh.setRelease(array, ci, VALUE_1);
681            });
682
683            checkIOOBE(() -> {
684                vh.setOpaque(array, ci, VALUE_1);
685            });
686#if[CAS]
687
688            checkIOOBE(() -> {
689                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
690            });
691
692            checkIOOBE(() -> {
693                $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
694            });
695
696            checkIOOBE(() -> {
697                $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
698            });
699
700            checkIOOBE(() -> {
701                $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
702            });
703
704            checkIOOBE(() -> {
705                boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
706            });
707
708            checkIOOBE(() -> {
709                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
710            });
711
712            checkIOOBE(() -> {
713                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
714            });
715
716            checkIOOBE(() -> {
717                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
718            });
719
720            checkIOOBE(() -> {
721                $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
722            });
723
724            checkIOOBE(() -> {
725                $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
726            });
727
728            checkIOOBE(() -> {
729                $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
730            });
731#end[CAS]
732
733#if[AtomicAdd]
734            checkIOOBE(() -> {
735                $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
736            });
737
738            checkIOOBE(() -> {
739                $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
740            });
741
742            checkIOOBE(() -> {
743                $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
744            });
745#end[AtomicAdd]
746
747#if[Bitwise]
748            checkIOOBE(() -> {
749                $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
750            });
751
752            checkIOOBE(() -> {
753                $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
754            });
755
756            checkIOOBE(() -> {
757                $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
758            });
759
760            checkIOOBE(() -> {
761                $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
762            });
763
764            checkIOOBE(() -> {
765                $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
766            });
767
768            checkIOOBE(() -> {
769                $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
770            });
771
772            checkIOOBE(() -> {
773                $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
774            });
775
776            checkIOOBE(() -> {
777                $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
778            });
779
780            checkIOOBE(() -> {
781                $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
782            });
783#end[Bitwise]
784
785        }
786    }
787
788    static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
789        VarHandle vh = vhs.s;
790        ByteBuffer array = bs.s;
791
792        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
793
794        int length = array.limit() - SIZE + 1;
795        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
796            final int ci = i;
797
798            checkIOOBE(() -> {
799                $type$ x = ($type$) vh.get(array, ci);
800            });
801
802            if (!readOnly) {
803                checkIOOBE(() -> {
804                    vh.set(array, ci, VALUE_1);
805                });
806            }
807
808            checkIOOBE(() -> {
809                $type$ x = ($type$) vh.getVolatile(array, ci);
810            });
811
812            checkIOOBE(() -> {
813                $type$ x = ($type$) vh.getAcquire(array, ci);
814            });
815
816            checkIOOBE(() -> {
817                $type$ x = ($type$) vh.getOpaque(array, ci);
818            });
819
820            if (!readOnly) {
821                checkIOOBE(() -> {
822                    vh.setVolatile(array, ci, VALUE_1);
823                });
824
825                checkIOOBE(() -> {
826                    vh.setRelease(array, ci, VALUE_1);
827                });
828
829                checkIOOBE(() -> {
830                    vh.setOpaque(array, ci, VALUE_1);
831                });
832
833#if[CAS]
834                checkIOOBE(() -> {
835                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
836                });
837
838                checkIOOBE(() -> {
839                    $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
840                });
841
842                checkIOOBE(() -> {
843                    $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
844                });
845
846                checkIOOBE(() -> {
847                    $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
848                });
849
850                checkIOOBE(() -> {
851                    boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
852                });
853
854                checkIOOBE(() -> {
855                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
856                });
857
858                checkIOOBE(() -> {
859                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
860                });
861
862                checkIOOBE(() -> {
863                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
864                });
865
866                checkIOOBE(() -> {
867                    $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
868                });
869
870                checkIOOBE(() -> {
871                    $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
872                });
873
874                checkIOOBE(() -> {
875                    $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
876                });
877#end[CAS]
878
879#if[AtomicAdd]
880                checkIOOBE(() -> {
881                    $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
882                });
883
884                checkIOOBE(() -> {
885                    $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
886                });
887
888                checkIOOBE(() -> {
889                    $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
890                });
891#end[AtomicAdd]
892
893#if[Bitwise]
894                checkIOOBE(() -> {
895                    $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
896                });
897
898                checkIOOBE(() -> {
899                    $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
900                });
901
902                checkIOOBE(() -> {
903                    $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
904                });
905
906                checkIOOBE(() -> {
907                    $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
908                });
909
910                checkIOOBE(() -> {
911                    $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
912                });
913
914                checkIOOBE(() -> {
915                    $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
916                });
917
918                checkIOOBE(() -> {
919                    $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
920                });
921
922                checkIOOBE(() -> {
923                    $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
924                });
925
926                checkIOOBE(() -> {
927                    $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
928                });
929#end[Bitwise]
930            }
931        }
932    }
933
934    static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
935        VarHandle vh = vhs.s;
936        byte[] array = bs.s;
937
938        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
939
940        int length = array.length - SIZE + 1;
941        for (int i = 0; i < length; i++) {
942            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
943            final int ci = i;
944
945            if (!iAligned) {
946                checkISE(() -> {
947                    $type$ x = ($type$) vh.getVolatile(array, ci);
948                });
949
950                checkISE(() -> {
951                    $type$ x = ($type$) vh.getAcquire(array, ci);
952                });
953
954                checkISE(() -> {
955                    $type$ x = ($type$) vh.getOpaque(array, ci);
956                });
957
958                checkISE(() -> {
959                    vh.setVolatile(array, ci, VALUE_1);
960                });
961
962                checkISE(() -> {
963                    vh.setRelease(array, ci, VALUE_1);
964                });
965
966                checkISE(() -> {
967                    vh.setOpaque(array, ci, VALUE_1);
968                });
969#if[CAS]
970
971                checkISE(() -> {
972                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
973                });
974
975                checkISE(() -> {
976                    $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
977                });
978
979                checkISE(() -> {
980                    $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
981                });
982
983                checkISE(() -> {
984                    $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
985                });
986
987                checkISE(() -> {
988                    boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
989                });
990
991                checkISE(() -> {
992                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
993                });
994
995                checkISE(() -> {
996                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
997                });
998
999                checkISE(() -> {
1000                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
1001                });
1002
1003                checkISE(() -> {
1004                    $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
1005                });
1006
1007                checkISE(() -> {
1008                    $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
1009                });
1010
1011                checkISE(() -> {
1012                    $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
1013                });
1014#end[CAS]
1015
1016#if[AtomicAdd]
1017                checkISE(() -> {
1018                    $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
1019                });
1020
1021                checkISE(() -> {
1022                    $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
1023                });
1024
1025                checkISE(() -> {
1026                    $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
1027                });
1028#end[AtomicAdd]
1029
1030#if[Bitwise]
1031                checkISE(() -> {
1032                    $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
1033                });
1034
1035                checkISE(() -> {
1036                    $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
1037                });
1038
1039                checkISE(() -> {
1040                    $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
1041                });
1042
1043                checkISE(() -> {
1044                    $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
1045                });
1046
1047                checkISE(() -> {
1048                    $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
1049                });
1050
1051                checkISE(() -> {
1052                    $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
1053                });
1054
1055                checkISE(() -> {
1056                    $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
1057                });
1058
1059                checkISE(() -> {
1060                    $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
1061                });
1062
1063                checkISE(() -> {
1064                    $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
1065                });
1066#end[Bitwise]
1067            }
1068        }
1069    }
1070
1071    static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
1072        VarHandle vh = vhs.s;
1073        ByteBuffer array = bs.s;
1074
1075        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
1076        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
1077
1078        int length = array.limit() - SIZE + 1;
1079        for (int i = 0; i < length; i++) {
1080            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
1081            final int ci = i;
1082
1083            if (!iAligned) {
1084                checkISE(() -> {
1085                    $type$ x = ($type$) vh.getVolatile(array, ci);
1086                });
1087
1088                checkISE(() -> {
1089                    $type$ x = ($type$) vh.getAcquire(array, ci);
1090                });
1091
1092                checkISE(() -> {
1093                    $type$ x = ($type$) vh.getOpaque(array, ci);
1094                });
1095
1096                if (!readOnly) {
1097                    checkISE(() -> {
1098                        vh.setVolatile(array, ci, VALUE_1);
1099                    });
1100
1101                    checkISE(() -> {
1102                        vh.setRelease(array, ci, VALUE_1);
1103                    });
1104
1105                    checkISE(() -> {
1106                        vh.setOpaque(array, ci, VALUE_1);
1107                    });
1108
1109#if[CAS]
1110                    checkISE(() -> {
1111                        boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
1112                    });
1113
1114                    checkISE(() -> {
1115                        $type$ r = ($type$) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1);
1116                    });
1117
1118                    checkISE(() -> {
1119                        $type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
1120                    });
1121
1122                    checkISE(() -> {
1123                        $type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
1124                    });
1125
1126                    checkISE(() -> {
1127                        boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2);
1128                    });
1129
1130                    checkISE(() -> {
1131                        boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
1132                    });
1133
1134                    checkISE(() -> {
1135                        boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
1136                    });
1137
1138                    checkISE(() -> {
1139                        boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
1140                    });
1141
1142                    checkISE(() -> {
1143                        $type$ o = ($type$) vh.getAndSet(array, ci, VALUE_1);
1144                    });
1145
1146                    checkISE(() -> {
1147                        $type$ o = ($type$) vh.getAndSetAcquire(array, ci, VALUE_1);
1148                    });
1149
1150                    checkISE(() -> {
1151                        $type$ o = ($type$) vh.getAndSetRelease(array, ci, VALUE_1);
1152                    });
1153#end[CAS]
1154
1155#if[AtomicAdd]
1156                    checkISE(() -> {
1157                        $type$ o = ($type$) vh.getAndAdd(array, ci, VALUE_1);
1158                    });
1159
1160                    checkISE(() -> {
1161                        $type$ o = ($type$) vh.getAndAddAcquire(array, ci, VALUE_1);
1162                    });
1163
1164                    checkISE(() -> {
1165                        $type$ o = ($type$) vh.getAndAddRelease(array, ci, VALUE_1);
1166                    });
1167#end[AtomicAdd]
1168
1169#if[Bitwise]
1170                    checkISE(() -> {
1171                        $type$ o = ($type$) vh.getAndBitwiseOr(array, ci, VALUE_1);
1172                    });
1173
1174                    checkISE(() -> {
1175                        $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1);
1176                    });
1177
1178                    checkISE(() -> {
1179                        $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, ci, VALUE_1);
1180                    });
1181
1182                    checkISE(() -> {
1183                        $type$ o = ($type$) vh.getAndBitwiseAnd(array, ci, VALUE_1);
1184                    });
1185
1186                    checkISE(() -> {
1187                        $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1);
1188                    });
1189
1190                    checkISE(() -> {
1191                        $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, VALUE_1);
1192                    });
1193
1194                    checkISE(() -> {
1195                        $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, VALUE_1);
1196                    });
1197
1198                    checkISE(() -> {
1199                        $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1);
1200                    });
1201
1202                    checkISE(() -> {
1203                        $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, VALUE_1);
1204                    });
1205#end[Bitwise]
1206                }
1207            }
1208        }
1209    }
1210
1211    static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) {
1212        VarHandle vh = vhs.s;
1213        byte[] array = bs.s;
1214
1215        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
1216
1217        bs.fill((byte) 0xff);
1218        int length = array.length - SIZE + 1;
1219        for (int i = 0; i < length; i++) {
1220            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
1221
1222            // Plain
1223            {
1224                vh.set(array, i, VALUE_1);
1225                $type$ x = ($type$) vh.get(array, i);
1226                assertEquals(x, VALUE_1, "get $type$ value");
1227            }
1228
1229
1230            if (iAligned) {
1231                // Volatile
1232                {
1233                    vh.setVolatile(array, i, VALUE_2);
1234                    $type$ x = ($type$) vh.getVolatile(array, i);
1235                    assertEquals(x, VALUE_2, "setVolatile $type$ value");
1236                }
1237
1238                // Lazy
1239                {
1240                    vh.setRelease(array, i, VALUE_1);
1241                    $type$ x = ($type$) vh.getAcquire(array, i);
1242                    assertEquals(x, VALUE_1, "setRelease $type$ value");
1243                }
1244
1245                // Opaque
1246                {
1247                    vh.setOpaque(array, i, VALUE_2);
1248                    $type$ x = ($type$) vh.getOpaque(array, i);
1249                    assertEquals(x, VALUE_2, "setOpaque $type$ value");
1250                }
1251#if[CAS]
1252
1253                vh.set(array, i, VALUE_1);
1254
1255                // Compare
1256                {
1257                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
1258                    assertEquals(r, true, "success compareAndSet $type$");
1259                    $type$ x = ($type$) vh.get(array, i);
1260                    assertEquals(x, VALUE_2, "success compareAndSet $type$ value");
1261                }
1262
1263                {
1264                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
1265                    assertEquals(r, false, "failing compareAndSet $type$");
1266                    $type$ x = ($type$) vh.get(array, i);
1267                    assertEquals(x, VALUE_2, "failing compareAndSet $type$ value");
1268                }
1269
1270                {
1271                    $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_1);
1272                    assertEquals(r, VALUE_2, "success compareAndExchange $type$");
1273                    $type$ x = ($type$) vh.get(array, i);
1274                    assertEquals(x, VALUE_1, "success compareAndExchange $type$ value");
1275                }
1276
1277                {
1278                    $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_3);
1279                    assertEquals(r, VALUE_1, "failing compareAndExchange $type$");
1280                    $type$ x = ($type$) vh.get(array, i);
1281                    assertEquals(x, VALUE_1, "failing compareAndExchange $type$ value");
1282                }
1283
1284                {
1285                    $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
1286                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire $type$");
1287                    $type$ x = ($type$) vh.get(array, i);
1288                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire $type$ value");
1289                }
1290
1291                {
1292                    $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
1293                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire $type$");
1294                    $type$ x = ($type$) vh.get(array, i);
1295                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire $type$ value");
1296                }
1297
1298                {
1299                    $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
1300                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease $type$");
1301                    $type$ x = ($type$) vh.get(array, i);
1302                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease $type$ value");
1303                }
1304
1305                {
1306                    $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
1307                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease $type$");
1308                    $type$ x = ($type$) vh.get(array, i);
1309                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease $type$ value");
1310                }
1311
1312                {
1313                    boolean success = false;
1314                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1315                        success = vh.weakCompareAndSetPlain(array, i, VALUE_1, VALUE_2);
1316                    }
1317                    assertEquals(success, true, "weakCompareAndSetPlain $type$");
1318                    $type$ x = ($type$) vh.get(array, i);
1319                    assertEquals(x, VALUE_2, "weakCompareAndSetPlain $type$ value");
1320                }
1321
1322                {
1323                    boolean success = false;
1324                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1325                        success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
1326                    }
1327                    assertEquals(success, true, "weakCompareAndSetAcquire $type$");
1328                    $type$ x = ($type$) vh.get(array, i);
1329                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire $type$");
1330                }
1331
1332                {
1333                    boolean success = false;
1334                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1335                        success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
1336                    }
1337                    assertEquals(success, true, "weakCompareAndSetRelease $type$");
1338                    $type$ x = ($type$) vh.get(array, i);
1339                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease $type$");
1340                }
1341
1342                {
1343                    boolean success = false;
1344                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1345                        success = vh.weakCompareAndSet(array, i, VALUE_2, VALUE_1);
1346                    }
1347                    assertEquals(success, true, "weakCompareAndSet $type$");
1348                    $type$ x = ($type$) vh.get(array, i);
1349                    assertEquals(x, VALUE_1, "weakCompareAndSet $type$");
1350                }
1351
1352                // Compare set and get
1353                {
1354                    vh.set(array, i, VALUE_1);
1355
1356                    $type$ o = ($type$) vh.getAndSet(array, i, VALUE_2);
1357                    assertEquals(o, VALUE_1, "getAndSet $type$");
1358                    $type$ x = ($type$) vh.get(array, i);
1359                    assertEquals(x, VALUE_2, "getAndSet $type$ value");
1360                }
1361
1362                {
1363                    vh.set(array, i, VALUE_1);
1364
1365                    $type$ o = ($type$) vh.getAndSetAcquire(array, i, VALUE_2);
1366                    assertEquals(o, VALUE_1, "getAndSetAcquire $type$");
1367                    $type$ x = ($type$) vh.get(array, i);
1368                    assertEquals(x, VALUE_2, "getAndSetAcquire $type$ value");
1369                }
1370
1371                {
1372                    vh.set(array, i, VALUE_1);
1373
1374                    $type$ o = ($type$) vh.getAndSetRelease(array, i, VALUE_2);
1375                    assertEquals(o, VALUE_1, "getAndSetRelease $type$");
1376                    $type$ x = ($type$) vh.get(array, i);
1377                    assertEquals(x, VALUE_2, "getAndSetRelease $type$ value");
1378                }
1379#end[CAS]
1380
1381#if[AtomicAdd]
1382                // get and add, add and get
1383                {
1384                    vh.set(array, i, VALUE_1);
1385
1386                    $type$ o = ($type$) vh.getAndAdd(array, i, VALUE_2);
1387                    assertEquals(o, VALUE_1, "getAndAdd $type$");
1388                    $type$ x = ($type$) vh.get(array, i);
1389                    assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd $type$ value");
1390                }
1391
1392                {
1393                    vh.set(array, i, VALUE_1);
1394
1395                    $type$ o = ($type$) vh.getAndAddAcquire(array, i, VALUE_2);
1396                    assertEquals(o, VALUE_1, "getAndAddAcquire $type$");
1397                    $type$ x = ($type$) vh.get(array, i);
1398                    assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire $type$ value");
1399                }
1400
1401                {
1402                    vh.set(array, i, VALUE_1);
1403
1404                    $type$ o = ($type$) vh.getAndAddRelease(array, i, VALUE_2);
1405                    assertEquals(o, VALUE_1, "getAndAddRelease $type$");
1406                    $type$ x = ($type$) vh.get(array, i);
1407                    assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease $type$ value");
1408                }
1409#end[AtomicAdd]
1410
1411#if[Bitwise]
1412                // get and bitwise or
1413                {
1414                    vh.set(array, i, VALUE_1);
1415
1416                    $type$ o = ($type$) vh.getAndBitwiseOr(array, i, VALUE_2);
1417                    assertEquals(o, VALUE_1, "getAndBitwiseOr $type$");
1418                    $type$ x = ($type$) vh.get(array, i);
1419                    assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr $type$ value");
1420                }
1421
1422                {
1423                    vh.set(array, i, VALUE_1);
1424
1425                    $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, VALUE_2);
1426                    assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire $type$");
1427                    $type$ x = ($type$) vh.get(array, i);
1428                    assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire $type$ value");
1429                }
1430
1431                {
1432                    vh.set(array, i, VALUE_1);
1433
1434                    $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, VALUE_2);
1435                    assertEquals(o, VALUE_1, "getAndBitwiseOrRelease $type$");
1436                    $type$ x = ($type$) vh.get(array, i);
1437                    assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease $type$ value");
1438                }
1439
1440                // get and bitwise and
1441                {
1442                    vh.set(array, i, VALUE_1);
1443
1444                    $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, VALUE_2);
1445                    assertEquals(o, VALUE_1, "getAndBitwiseAnd $type$");
1446                    $type$ x = ($type$) vh.get(array, i);
1447                    assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd $type$ value");
1448                }
1449
1450                {
1451                    vh.set(array, i, VALUE_1);
1452
1453                    $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, VALUE_2);
1454                    assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire $type$");
1455                    $type$ x = ($type$) vh.get(array, i);
1456                    assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire $type$ value");
1457                }
1458
1459                {
1460                    vh.set(array, i, VALUE_1);
1461
1462                    $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, VALUE_2);
1463                    assertEquals(o, VALUE_1, "getAndBitwiseAndRelease $type$");
1464                    $type$ x = ($type$) vh.get(array, i);
1465                    assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease $type$ value");
1466                }
1467
1468                // get and bitwise xor
1469                {
1470                    vh.set(array, i, VALUE_1);
1471
1472                    $type$ o = ($type$) vh.getAndBitwiseXor(array, i, VALUE_2);
1473                    assertEquals(o, VALUE_1, "getAndBitwiseXor $type$");
1474                    $type$ x = ($type$) vh.get(array, i);
1475                    assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor $type$ value");
1476                }
1477
1478                {
1479                    vh.set(array, i, VALUE_1);
1480
1481                    $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, VALUE_2);
1482                    assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire $type$");
1483                    $type$ x = ($type$) vh.get(array, i);
1484                    assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire $type$ value");
1485                }
1486
1487                {
1488                    vh.set(array, i, VALUE_1);
1489
1490                    $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, VALUE_2);
1491                    assertEquals(o, VALUE_1, "getAndBitwiseXorRelease $type$");
1492                    $type$ x = ($type$) vh.get(array, i);
1493                    assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease $type$ value");
1494                }
1495#end[Bitwise]
1496            }
1497        }
1498    }
1499
1500
1501    static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) {
1502        VarHandle vh = vhs.s;
1503        ByteBuffer array = bs.s;
1504
1505        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
1506
1507        bs.fill((byte) 0xff);
1508        int length = array.limit() - SIZE + 1;
1509        for (int i = 0; i < length; i++) {
1510            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
1511
1512            // Plain
1513            {
1514                vh.set(array, i, VALUE_1);
1515                $type$ x = ($type$) vh.get(array, i);
1516                assertEquals(x, VALUE_1, "get $type$ value");
1517            }
1518
1519            if (iAligned) {
1520                // Volatile
1521                {
1522                    vh.setVolatile(array, i, VALUE_2);
1523                    $type$ x = ($type$) vh.getVolatile(array, i);
1524                    assertEquals(x, VALUE_2, "setVolatile $type$ value");
1525                }
1526
1527                // Lazy
1528                {
1529                    vh.setRelease(array, i, VALUE_1);
1530                    $type$ x = ($type$) vh.getAcquire(array, i);
1531                    assertEquals(x, VALUE_1, "setRelease $type$ value");
1532                }
1533
1534                // Opaque
1535                {
1536                    vh.setOpaque(array, i, VALUE_2);
1537                    $type$ x = ($type$) vh.getOpaque(array, i);
1538                    assertEquals(x, VALUE_2, "setOpaque $type$ value");
1539                }
1540#if[CAS]
1541
1542                vh.set(array, i, VALUE_1);
1543
1544                // Compare
1545                {
1546                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
1547                    assertEquals(r, true, "success compareAndSet $type$");
1548                    $type$ x = ($type$) vh.get(array, i);
1549                    assertEquals(x, VALUE_2, "success compareAndSet $type$ value");
1550                }
1551
1552                {
1553                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
1554                    assertEquals(r, false, "failing compareAndSet $type$");
1555                    $type$ x = ($type$) vh.get(array, i);
1556                    assertEquals(x, VALUE_2, "failing compareAndSet $type$ value");
1557                }
1558
1559                {
1560                    $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_1);
1561                    assertEquals(r, VALUE_2, "success compareAndExchange $type$");
1562                    $type$ x = ($type$) vh.get(array, i);
1563                    assertEquals(x, VALUE_1, "success compareAndExchange $type$ value");
1564                }
1565
1566                {
1567                    $type$ r = ($type$) vh.compareAndExchange(array, i, VALUE_2, VALUE_3);
1568                    assertEquals(r, VALUE_1, "failing compareAndExchange $type$");
1569                    $type$ x = ($type$) vh.get(array, i);
1570                    assertEquals(x, VALUE_1, "failing compareAndExchange $type$ value");
1571                }
1572
1573                {
1574                    $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
1575                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire $type$");
1576                    $type$ x = ($type$) vh.get(array, i);
1577                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire $type$ value");
1578                }
1579
1580                {
1581                    $type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
1582                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire $type$");
1583                    $type$ x = ($type$) vh.get(array, i);
1584                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire $type$ value");
1585                }
1586
1587                {
1588                    $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
1589                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease $type$");
1590                    $type$ x = ($type$) vh.get(array, i);
1591                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease $type$ value");
1592                }
1593
1594                {
1595                    $type$ r = ($type$) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
1596                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease $type$");
1597                    $type$ x = ($type$) vh.get(array, i);
1598                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease $type$ value");
1599                }
1600
1601                {
1602                    boolean success = false;
1603                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1604                        success = vh.weakCompareAndSetPlain(array, i, VALUE_1, VALUE_2);
1605                    }
1606                    assertEquals(success, true, "weakCompareAndSetPlain $type$");
1607                    $type$ x = ($type$) vh.get(array, i);
1608                    assertEquals(x, VALUE_2, "weakCompareAndSetPlain $type$ value");
1609                }
1610
1611                {
1612                    boolean success = false;
1613                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1614                        success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
1615                    }
1616                    assertEquals(success, true, "weakCompareAndSetAcquire $type$");
1617                    $type$ x = ($type$) vh.get(array, i);
1618                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire $type$");
1619                }
1620
1621                {
1622                    boolean success = false;
1623                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1624                        success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
1625                    }
1626                    assertEquals(success, true, "weakCompareAndSetRelease $type$");
1627                    $type$ x = ($type$) vh.get(array, i);
1628                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease $type$");
1629                }
1630
1631                {
1632                    boolean success = false;
1633                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1634                        success = vh.weakCompareAndSet(array, i, VALUE_2, VALUE_1);
1635                    }
1636                    assertEquals(success, true, "weakCompareAndSet $type$");
1637                    $type$ x = ($type$) vh.get(array, i);
1638                    assertEquals(x, VALUE_1, "weakCompareAndSet $type$");
1639                }
1640
1641                // Compare set and get
1642                {
1643                    vh.set(array, i, VALUE_1);
1644
1645                    $type$ o = ($type$) vh.getAndSet(array, i, VALUE_2);
1646                    assertEquals(o, VALUE_1, "getAndSet $type$");
1647                    $type$ x = ($type$) vh.get(array, i);
1648                    assertEquals(x, VALUE_2, "getAndSet $type$ value");
1649                }
1650
1651                {
1652                    vh.set(array, i, VALUE_1);
1653
1654                    $type$ o = ($type$) vh.getAndSetAcquire(array, i, VALUE_2);
1655                    assertEquals(o, VALUE_1, "getAndSetAcquire $type$");
1656                    $type$ x = ($type$) vh.get(array, i);
1657                    assertEquals(x, VALUE_2, "getAndSetAcquire $type$ value");
1658                }
1659
1660                {
1661                    vh.set(array, i, VALUE_1);
1662
1663                    $type$ o = ($type$) vh.getAndSetRelease(array, i, VALUE_2);
1664                    assertEquals(o, VALUE_1, "getAndSetRelease $type$");
1665                    $type$ x = ($type$) vh.get(array, i);
1666                    assertEquals(x, VALUE_2, "getAndSetRelease $type$ value");
1667                }
1668#end[CAS]
1669
1670#if[AtomicAdd]
1671                // get and add, add and get
1672                {
1673                    vh.set(array, i, VALUE_1);
1674
1675                    $type$ o = ($type$) vh.getAndAdd(array, i, VALUE_2);
1676                    assertEquals(o, VALUE_1, "getAndAdd $type$");
1677                    $type$ x = ($type$) vh.get(array, i);
1678                    assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd $type$ value");
1679                }
1680
1681                {
1682                    vh.set(array, i, VALUE_1);
1683
1684                    $type$ o = ($type$) vh.getAndAddAcquire(array, i, VALUE_2);
1685                    assertEquals(o, VALUE_1, "getAndAddAcquire $type$");
1686                    $type$ x = ($type$) vh.get(array, i);
1687                    assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire $type$ value");
1688                }
1689
1690                {
1691                    vh.set(array, i, VALUE_1);
1692
1693                    $type$ o = ($type$) vh.getAndAddRelease(array, i, VALUE_2);
1694                    assertEquals(o, VALUE_1, "getAndAddRelease $type$");
1695                    $type$ x = ($type$) vh.get(array, i);
1696                    assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease $type$ value");
1697                }
1698#end[AtomicAdd]
1699
1700#if[Bitwise]
1701                // get and bitwise or
1702                {
1703                    vh.set(array, i, VALUE_1);
1704
1705                    $type$ o = ($type$) vh.getAndBitwiseOr(array, i, VALUE_2);
1706                    assertEquals(o, VALUE_1, "getAndBitwiseOr $type$");
1707                    $type$ x = ($type$) vh.get(array, i);
1708                    assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr $type$ value");
1709                }
1710
1711                {
1712                    vh.set(array, i, VALUE_1);
1713
1714                    $type$ o = ($type$) vh.getAndBitwiseOrAcquire(array, i, VALUE_2);
1715                    assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire $type$");
1716                    $type$ x = ($type$) vh.get(array, i);
1717                    assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire $type$ value");
1718                }
1719
1720                {
1721                    vh.set(array, i, VALUE_1);
1722
1723                    $type$ o = ($type$) vh.getAndBitwiseOrRelease(array, i, VALUE_2);
1724                    assertEquals(o, VALUE_1, "getAndBitwiseOrRelease $type$");
1725                    $type$ x = ($type$) vh.get(array, i);
1726                    assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease $type$ value");
1727                }
1728
1729                // get and bitwise and
1730                {
1731                    vh.set(array, i, VALUE_1);
1732
1733                    $type$ o = ($type$) vh.getAndBitwiseAnd(array, i, VALUE_2);
1734                    assertEquals(o, VALUE_1, "getAndBitwiseAnd $type$");
1735                    $type$ x = ($type$) vh.get(array, i);
1736                    assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd $type$ value");
1737                }
1738
1739                {
1740                    vh.set(array, i, VALUE_1);
1741
1742                    $type$ o = ($type$) vh.getAndBitwiseAndAcquire(array, i, VALUE_2);
1743                    assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire $type$");
1744                    $type$ x = ($type$) vh.get(array, i);
1745                    assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire $type$ value");
1746                }
1747
1748                {
1749                    vh.set(array, i, VALUE_1);
1750
1751                    $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, i, VALUE_2);
1752                    assertEquals(o, VALUE_1, "getAndBitwiseAndRelease $type$");
1753                    $type$ x = ($type$) vh.get(array, i);
1754                    assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease $type$ value");
1755                }
1756
1757                // get and bitwise xor
1758                {
1759                    vh.set(array, i, VALUE_1);
1760
1761                    $type$ o = ($type$) vh.getAndBitwiseXor(array, i, VALUE_2);
1762                    assertEquals(o, VALUE_1, "getAndBitwiseXor $type$");
1763                    $type$ x = ($type$) vh.get(array, i);
1764                    assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor $type$ value");
1765                }
1766
1767                {
1768                    vh.set(array, i, VALUE_1);
1769
1770                    $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, i, VALUE_2);
1771                    assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire $type$");
1772                    $type$ x = ($type$) vh.get(array, i);
1773                    assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire $type$ value");
1774                }
1775
1776                {
1777                    vh.set(array, i, VALUE_1);
1778
1779                    $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, i, VALUE_2);
1780                    assertEquals(o, VALUE_1, "getAndBitwiseXorRelease $type$");
1781                    $type$ x = ($type$) vh.get(array, i);
1782                    assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease $type$ value");
1783                }
1784#end[Bitwise]
1785            }
1786        }
1787    }
1788
1789    static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) {
1790        VarHandle vh = vhs.s;
1791        ByteBuffer array = bs.s;
1792
1793        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
1794
1795        ByteBuffer bb = ByteBuffer.allocate(SIZE);
1796        bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
1797        bs.fill(bb.put$Type$(0, VALUE_2).array());
1798
1799        int length = array.limit() - SIZE + 1;
1800        for (int i = 0; i < length; i++) {
1801            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
1802
1803            $type$ v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes)
1804                    ? rotateLeft(VALUE_2, (i % SIZE) << 3)
1805                    : rotateRight(VALUE_2, (i % SIZE) << 3);
1806            // Plain
1807            {
1808                $type$ x = ($type$) vh.get(array, i);
1809                assertEquals(x, v, "get $type$ value");
1810            }
1811
1812            if (iAligned) {
1813                // Volatile
1814                {
1815                    $type$ x = ($type$) vh.getVolatile(array, i);
1816                    assertEquals(x, v, "getVolatile $type$ value");
1817                }
1818
1819                // Lazy
1820                {
1821                    $type$ x = ($type$) vh.getAcquire(array, i);
1822                    assertEquals(x, v, "getRelease $type$ value");
1823                }
1824
1825                // Opaque
1826                {
1827                    $type$ x = ($type$) vh.getOpaque(array, i);
1828                    assertEquals(x, v, "getOpaque $type$ value");
1829                }
1830            }
1831        }
1832    }
1833
1834}
1835
1836