• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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// Android-added: package for test.
25package test.java.lang.invoke.VarHandles;
26
27/*
28 * @test
29 * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccess$Type$
30 */
31
32import org.testng.annotations.BeforeClass;
33import org.testng.annotations.DataProvider;
34import org.testng.annotations.Test;
35
36import java.lang.invoke.MethodHandles;
37import java.lang.invoke.VarHandle;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.List;
41
42import static org.testng.Assert.*;
43
44public class VarHandleTestMethodHandleAccess$Type$ extends VarHandleBaseTest {
45    static final $type$ static_final_v = $value1$;
46
47    static $type$ static_v;
48
49    final $type$ final_v = $value1$;
50
51    $type$ v;
52
53    VarHandle vhFinalField;
54
55    VarHandle vhField;
56
57    VarHandle vhStaticField;
58
59    VarHandle vhStaticFinalField;
60
61    VarHandle vhArray;
62
63    @BeforeClass
64    public void setup() throws Exception {
65        vhFinalField = MethodHandles.lookup().findVarHandle(
66                VarHandleTestMethodHandleAccess$Type$.class, "final_v", $type$.class);
67
68        vhField = MethodHandles.lookup().findVarHandle(
69                VarHandleTestMethodHandleAccess$Type$.class, "v", $type$.class);
70
71        vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
72            VarHandleTestMethodHandleAccess$Type$.class, "static_final_v", $type$.class);
73
74        vhStaticField = MethodHandles.lookup().findStaticVarHandle(
75            VarHandleTestMethodHandleAccess$Type$.class, "static_v", $type$.class);
76
77        vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
78    }
79
80
81    @DataProvider
82    public Object[][] accessTestCaseProvider() throws Exception {
83        List<AccessTestCase<?>> cases = new ArrayList<>();
84
85        for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
86            cases.add(new MethodHandleAccessTestCase("Instance field",
87                                                     vhField, f, hs -> testInstanceField(this, hs)));
88            cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
89                                                     vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
90                                                     false));
91
92            cases.add(new MethodHandleAccessTestCase("Static field",
93                                                     vhStaticField, f, VarHandleTestMethodHandleAccess$Type$::testStaticField));
94            cases.add(new MethodHandleAccessTestCase("Static field unsupported",
95                                                     vhStaticField, f, VarHandleTestMethodHandleAccess$Type$::testStaticFieldUnsupported,
96                                                     false));
97
98            cases.add(new MethodHandleAccessTestCase("Array",
99                                                     vhArray, f, VarHandleTestMethodHandleAccess$Type$::testArray));
100            cases.add(new MethodHandleAccessTestCase("Array unsupported",
101                                                     vhArray, f, VarHandleTestMethodHandleAccess$Type$::testArrayUnsupported,
102                                                     false));
103            cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
104                                                     vhArray, f, VarHandleTestMethodHandleAccess$Type$::testArrayIndexOutOfBounds,
105                                                     false));
106        }
107
108        // Work around issue with jtreg summary reporting which truncates
109        // the String result of Object.toString to 30 characters, hence
110        // the first dummy argument
111        return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
112    }
113
114    @Test(dataProvider = "accessTestCaseProvider")
115    public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
116        T t = atc.get();
117        int iters = atc.requiresLoop() ? ITERS : 1;
118        for (int c = 0; c < iters; c++) {
119            atc.testAccess(t);
120        }
121    }
122
123
124    static void testInstanceField(VarHandleTestMethodHandleAccess$Type$ recv, Handles hs) throws Throwable {
125        // Plain
126        {
127            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
128            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
129            assertEquals(x, $value1$, "set $type$ value");
130        }
131
132
133        // Volatile
134        {
135            hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, $value2$);
136            $type$ x = ($type$) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
137            assertEquals(x, $value2$, "setVolatile $type$ value");
138        }
139
140        // Lazy
141        {
142            hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, $value1$);
143            $type$ x = ($type$) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
144            assertEquals(x, $value1$, "setRelease $type$ value");
145        }
146
147        // Opaque
148        {
149            hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, $value2$);
150            $type$ x = ($type$) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
151            assertEquals(x, $value2$, "setOpaque $type$ value");
152        }
153
154#if[CAS]
155        hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
156
157        // Compare
158        {
159            boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, $value1$, $value2$);
160            assertEquals(r, true, "success compareAndSet $type$");
161            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
162            assertEquals(x, $value2$, "success compareAndSet $type$ value");
163        }
164
165        {
166            boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, $value1$, $value3$);
167            assertEquals(r, false, "failing compareAndSet $type$");
168            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
169            assertEquals(x, $value2$, "failing compareAndSet $type$ value");
170        }
171
172        {
173            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, $value2$, $value1$);
174            assertEquals(r, $value2$, "success compareAndExchange $type$");
175            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
176            assertEquals(x, $value1$, "success compareAndExchange $type$ value");
177        }
178
179        {
180            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, $value2$, $value3$);
181            assertEquals(r, $value1$, "failing compareAndExchange $type$");
182            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
183            assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
184        }
185
186        {
187            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, $value1$, $value2$);
188            assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
189            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
190            assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
191        }
192
193        {
194            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, $value1$, $value3$);
195            assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
196            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
197            assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
198        }
199
200        {
201            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, $value2$, $value1$);
202            assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
203            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
204            assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
205        }
206
207        {
208            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, $value2$, $value3$);
209            assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
210            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
211            assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
212        }
213
214        {
215            boolean success = false;
216            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
217                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, $value1$, $value2$);
218            }
219            assertEquals(success, true, "weakCompareAndSetPlain $type$");
220            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
221            assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
222        }
223
224        {
225            boolean success = false;
226            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
227                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, $value2$, $value1$);
228            }
229            assertEquals(success, true, "weakCompareAndSetAcquire $type$");
230            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
231            assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
232        }
233
234        {
235            boolean success = false;
236            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
237                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, $value1$, $value2$);
238            }
239            assertEquals(success, true, "weakCompareAndSetRelease $type$");
240            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
241            assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
242        }
243
244        {
245            boolean success = false;
246            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
247                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, $value2$, $value1$);
248            }
249            assertEquals(success, true, "weakCompareAndSet $type$");
250            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
251            assertEquals(x, $value1$, "weakCompareAndSet $type$");
252        }
253
254        // Compare set and get
255        {
256            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, $value2$);
257            assertEquals(o, $value1$, "getAndSet $type$");
258            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
259            assertEquals(x, $value2$, "getAndSet $type$ value");
260        }
261#end[CAS]
262
263#if[AtomicAdd]
264        // get and add, add and get
265        {
266            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
267
268            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, $value2$);
269            assertEquals(o, $value1$, "getAndAdd $type$");
270            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
271            assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
272        }
273
274        {
275            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
276
277            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, $value2$);
278            assertEquals(o, $value1$, "getAndAddAcquire $type$");
279            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
280            assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
281        }
282
283        {
284            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
285
286            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, $value2$);
287            assertEquals(o, $value1$, "getAndAddRelease $type$");
288            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
289            assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
290        }
291#end[AtomicAdd]
292
293#if[Bitwise]
294        // get and bitwise or
295        {
296            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
297
298            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(recv, $value2$);
299            assertEquals(o, $value1$, "getAndBitwiseOr $type$");
300            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
301            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
302        }
303
304        {
305            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
306
307            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(recv, $value2$);
308            assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
309            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
310            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
311        }
312
313        {
314            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
315
316            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(recv, $value2$);
317            assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
318            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
319            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
320        }
321
322        // get and bitwise and
323        {
324            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
325
326            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(recv, $value2$);
327            assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
328            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
329            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
330        }
331
332        {
333            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
334
335            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(recv, $value2$);
336            assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
337            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
338            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
339        }
340
341        {
342            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
343
344            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(recv, $value2$);
345            assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
346            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
347            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
348        }
349
350        // get and bitwise xor
351        {
352            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
353
354            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(recv, $value2$);
355            assertEquals(o, $value1$, "getAndBitwiseXor $type$");
356            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
357            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
358        }
359
360        {
361            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
362
363            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(recv, $value2$);
364            assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
365            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
366            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
367        }
368
369        {
370            hs.get(TestAccessMode.SET).invokeExact(recv, $value1$);
371
372            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, $value2$);
373            assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
374            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(recv);
375            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
376        }
377#end[Bitwise]
378    }
379
380    static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccess$Type$ recv, Handles hs) throws Throwable {
381#if[!CAS]
382        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
383            checkUOE(am, () -> {
384                boolean r = (boolean) hs.get(am).invokeExact(recv, $value1$, $value2$);
385            });
386        }
387
388        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
389            checkUOE(am, () -> {
390                $type$ r = ($type$) hs.get(am).invokeExact(recv, $value1$, $value2$);
391            });
392        }
393
394        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
395            checkUOE(am, () -> {
396                $type$ r = ($type$) hs.get(am).invokeExact(recv, $value1$);
397            });
398        }
399#end[CAS]
400
401#if[!AtomicAdd]
402        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
403            checkUOE(am, () -> {
404                $type$ r = ($type$) hs.get(am).invokeExact(recv, $value1$);
405            });
406        }
407#end[AtomicAdd]
408
409#if[!Bitwise]
410        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
411            checkUOE(am, () -> {
412                $type$ r = ($type$) hs.get(am).invokeExact(recv, $value1$);
413            });
414        }
415#end[Bitwise]
416    }
417
418
419    static void testStaticField(Handles hs) throws Throwable {
420        // Plain
421        {
422            hs.get(TestAccessMode.SET).invokeExact($value1$);
423            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
424            assertEquals(x, $value1$, "set $type$ value");
425        }
426
427
428        // Volatile
429        {
430            hs.get(TestAccessMode.SET_VOLATILE).invokeExact($value2$);
431            $type$ x = ($type$) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
432            assertEquals(x, $value2$, "setVolatile $type$ value");
433        }
434
435        // Lazy
436        {
437            hs.get(TestAccessMode.SET_RELEASE).invokeExact($value1$);
438            $type$ x = ($type$) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
439            assertEquals(x, $value1$, "setRelease $type$ value");
440        }
441
442        // Opaque
443        {
444            hs.get(TestAccessMode.SET_OPAQUE).invokeExact($value2$);
445            $type$ x = ($type$) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
446            assertEquals(x, $value2$, "setOpaque $type$ value");
447        }
448
449#if[CAS]
450        hs.get(TestAccessMode.SET).invokeExact($value1$);
451
452        // Compare
453        {
454            boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact($value1$, $value2$);
455            assertEquals(r, true, "success compareAndSet $type$");
456            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
457            assertEquals(x, $value2$, "success compareAndSet $type$ value");
458        }
459
460        {
461            boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact($value1$, $value3$);
462            assertEquals(r, false, "failing compareAndSet $type$");
463            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
464            assertEquals(x, $value2$, "failing compareAndSet $type$ value");
465        }
466
467        {
468            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact($value2$, $value1$);
469            assertEquals(r, $value2$, "success compareAndExchange $type$");
470            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
471            assertEquals(x, $value1$, "success compareAndExchange $type$ value");
472        }
473
474        {
475            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact($value2$, $value3$);
476            assertEquals(r, $value1$, "failing compareAndExchange $type$");
477            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
478            assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
479        }
480
481        {
482            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact($value1$, $value2$);
483            assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
484            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
485            assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
486        }
487
488        {
489            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact($value1$, $value3$);
490            assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
491            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
492            assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
493        }
494
495        {
496            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact($value2$, $value1$);
497            assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
498            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
499            assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
500        }
501
502        {
503            $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact($value2$, $value3$);
504            assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
505            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
506            assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
507        }
508
509        {
510            boolean success = false;
511            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
512                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact($value1$, $value2$);
513            }
514            assertEquals(success, true, "weakCompareAndSetPlain $type$");
515            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
516            assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
517        }
518
519        {
520            boolean success = false;
521            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
522                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact($value2$, $value1$);
523            }
524            assertEquals(success, true, "weakCompareAndSetAcquire $type$");
525            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
526            assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
527        }
528
529        {
530            boolean success = false;
531            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
532                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact($value1$, $value2$);
533            }
534            assertEquals(success, true, "weakCompareAndSetRelease $type$");
535            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
536            assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
537        }
538
539        {
540            boolean success = false;
541            for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
542                success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact($value2$, $value1$);
543            }
544            assertEquals(success, true, "weakCompareAndSet $type$");
545            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
546            assertEquals(x, $value1$, "weakCompareAndSet $type$");
547        }
548
549        // Compare set and get
550        {
551            hs.get(TestAccessMode.SET).invokeExact($value1$);
552
553            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_SET).invokeExact($value2$);
554            assertEquals(o, $value1$, "getAndSet $type$");
555            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
556            assertEquals(x, $value2$, "getAndSet $type$ value");
557        }
558
559        // Compare set and get
560        {
561            hs.get(TestAccessMode.SET).invokeExact($value1$);
562
563            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact($value2$);
564            assertEquals(o, $value1$, "getAndSetAcquire $type$");
565            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
566            assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
567        }
568
569        // Compare set and get
570        {
571            hs.get(TestAccessMode.SET).invokeExact($value1$);
572
573            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact($value2$);
574            assertEquals(o, $value1$, "getAndSetRelease $type$");
575            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
576            assertEquals(x, $value2$, "getAndSetRelease $type$ value");
577        }
578#end[CAS]
579
580#if[AtomicAdd]
581        // get and add, add and get
582        {
583            hs.get(TestAccessMode.SET).invokeExact($value1$);
584
585            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD).invokeExact($value2$);
586            assertEquals(o, $value1$, "getAndAdd $type$");
587            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
588            assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
589        }
590
591        {
592            hs.get(TestAccessMode.SET).invokeExact($value1$);
593
594            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact($value2$);
595            assertEquals(o, $value1$, "getAndAddAcquire $type$");
596            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
597            assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
598        }
599
600        {
601            hs.get(TestAccessMode.SET).invokeExact($value1$);
602
603            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact($value2$);
604            assertEquals(o, $value1$, "getAndAddRelease $type$");
605            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
606            assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
607        }
608#end[AtomicAdd]
609
610#if[Bitwise]
611        // get and bitwise or
612        {
613            hs.get(TestAccessMode.SET).invokeExact($value1$);
614
615            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact($value2$);
616            assertEquals(o, $value1$, "getAndBitwiseOr $type$");
617            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
618            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
619        }
620
621        {
622            hs.get(TestAccessMode.SET).invokeExact($value1$);
623
624            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact($value2$);
625            assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
626            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
627            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
628        }
629
630        {
631            hs.get(TestAccessMode.SET).invokeExact($value1$);
632
633            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact($value2$);
634            assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
635            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
636            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
637        }
638
639        // get and bitwise and
640        {
641            hs.get(TestAccessMode.SET).invokeExact($value1$);
642
643            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact($value2$);
644            assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
645            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
646            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
647        }
648
649        {
650            hs.get(TestAccessMode.SET).invokeExact($value1$);
651
652            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact($value2$);
653            assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
654            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
655            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
656        }
657
658        {
659            hs.get(TestAccessMode.SET).invokeExact($value1$);
660
661            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact($value2$);
662            assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
663            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
664            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
665        }
666
667        // get and bitwise xor
668        {
669            hs.get(TestAccessMode.SET).invokeExact($value1$);
670
671            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact($value2$);
672            assertEquals(o, $value1$, "getAndBitwiseXor $type$");
673            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
674            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
675        }
676
677        {
678            hs.get(TestAccessMode.SET).invokeExact($value1$);
679
680            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact($value2$);
681            assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
682            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
683            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
684        }
685
686        {
687            hs.get(TestAccessMode.SET).invokeExact($value1$);
688
689            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact($value2$);
690            assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
691            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact();
692            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
693        }
694#end[Bitwise]
695    }
696
697    static void testStaticFieldUnsupported(Handles hs) throws Throwable {
698#if[!CAS]
699        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
700            checkUOE(am, () -> {
701                boolean r = (boolean) hs.get(am).invokeExact($value1$, $value2$);
702            });
703        }
704
705        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
706            checkUOE(am, () -> {
707                $type$ r = ($type$) hs.get(am).invokeExact($value1$, $value2$);
708            });
709        }
710
711        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
712            checkUOE(am, () -> {
713                $type$ r = ($type$) hs.get(am).invokeExact($value1$);
714            });
715        }
716#end[CAS]
717
718#if[!AtomicAdd]
719        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
720            checkUOE(am, () -> {
721                $type$ r = ($type$) hs.get(am).invokeExact($value1$);
722            });
723        }
724#end[AtomicAdd]
725
726#if[!Bitwise]
727        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
728            checkUOE(am, () -> {
729                $type$ r = ($type$) hs.get(am).invokeExact($value1$);
730            });
731        }
732#end[Bitwise]
733    }
734
735
736    static void testArray(Handles hs) throws Throwable {
737        $type$[] array = new $type$[10];
738
739        for (int i = 0; i < array.length; i++) {
740            // Plain
741            {
742                hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
743                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
744                assertEquals(x, $value1$, "get $type$ value");
745            }
746
747
748            // Volatile
749            {
750                hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, $value2$);
751                $type$ x = ($type$) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
752                assertEquals(x, $value2$, "setVolatile $type$ value");
753            }
754
755            // Lazy
756            {
757                hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, $value1$);
758                $type$ x = ($type$) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
759                assertEquals(x, $value1$, "setRelease $type$ value");
760            }
761
762            // Opaque
763            {
764                hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, $value2$);
765                $type$ x = ($type$) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
766                assertEquals(x, $value2$, "setOpaque $type$ value");
767            }
768
769#if[CAS]
770            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
771
772            // Compare
773            {
774                boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, $value1$, $value2$);
775                assertEquals(r, true, "success compareAndSet $type$");
776                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
777                assertEquals(x, $value2$, "success compareAndSet $type$ value");
778            }
779
780            {
781                boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, $value1$, $value3$);
782                assertEquals(r, false, "failing compareAndSet $type$");
783                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
784                assertEquals(x, $value2$, "failing compareAndSet $type$ value");
785            }
786
787            {
788                $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, $value2$, $value1$);
789                assertEquals(r, $value2$, "success compareAndExchange $type$");
790                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
791                assertEquals(x, $value1$, "success compareAndExchange $type$ value");
792            }
793
794            {
795                $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, $value2$, $value3$);
796                assertEquals(r, $value1$, "failing compareAndExchange $type$");
797                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
798                assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
799            }
800
801            {
802                $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, $value1$, $value2$);
803                assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
804                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
805                assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
806            }
807
808            {
809                $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, $value1$, $value3$);
810                assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
811                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
812                assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
813            }
814
815            {
816                $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, $value2$, $value1$);
817                assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
818                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
819                assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
820            }
821
822            {
823                $type$ r = ($type$) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, $value2$, $value3$);
824                assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
825                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
826                assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
827            }
828
829            {
830                boolean success = false;
831                for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
832                    success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, $value1$, $value2$);
833                }
834                assertEquals(success, true, "weakCompareAndSetPlain $type$");
835                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
836                assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
837            }
838
839            {
840                boolean success = false;
841                for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
842                    success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, $value2$, $value1$);
843                }
844                assertEquals(success, true, "weakCompareAndSetAcquire $type$");
845                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
846                assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
847            }
848
849            {
850                boolean success = false;
851                for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
852                    success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, $value1$, $value2$);
853                }
854                assertEquals(success, true, "weakCompareAndSetRelease $type$");
855                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
856                assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
857            }
858
859            {
860                boolean success = false;
861                for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
862                    success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, $value2$, $value1$);
863                }
864                assertEquals(success, true, "weakCompareAndSet $type$");
865                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
866                assertEquals(x, $value1$, "weakCompareAndSet $type$");
867            }
868
869            // Compare set and get
870            {
871                hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
872
873                $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, $value2$);
874                assertEquals(o, $value1$, "getAndSet $type$");
875                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
876                assertEquals(x, $value2$, "getAndSet $type$ value");
877            }
878
879            {
880                hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
881
882                $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, $value2$);
883                assertEquals(o, $value1$, "getAndSetAcquire $type$");
884                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
885                assertEquals(x, $value2$, "getAndSetAcquire $type$ value");
886            }
887
888            {
889                hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
890
891                $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, $value2$);
892                assertEquals(o, $value1$, "getAndSetRelease $type$");
893                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
894                assertEquals(x, $value2$, "getAndSetRelease $type$ value");
895            }
896#end[CAS]
897
898#if[AtomicAdd]
899            // get and add, add and get
900            {
901                hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
902
903                $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, $value2$);
904                assertEquals(o, $value1$, "getAndAdd $type$");
905                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
906                assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$ value");
907            }
908
909            {
910                hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
911
912                $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, $value2$);
913                assertEquals(o, $value1$, "getAndAddAcquire $type$");
914                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
915                assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddAcquire $type$ value");
916            }
917
918            {
919                hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
920
921                $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, $value2$);
922                assertEquals(o, $value1$, "getAndAddRelease $type$");
923                $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
924                assertEquals(x, ($type$)($value1$ + $value2$), "getAndAddRelease $type$ value");
925            }
926#end[AtomicAdd]
927
928#if[Bitwise]
929        // get and bitwise or
930        {
931            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
932
933            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR).invokeExact(array, i, $value2$);
934            assertEquals(o, $value1$, "getAndBitwiseOr $type$");
935            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
936            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOr $type$ value");
937        }
938
939        {
940            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
941
942            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR_ACQUIRE).invokeExact(array, i, $value2$);
943            assertEquals(o, $value1$, "getAndBitwiseOrAcquire $type$");
944            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
945            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrAcquire $type$ value");
946        }
947
948        {
949            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
950
951            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_OR_RELEASE).invokeExact(array, i, $value2$);
952            assertEquals(o, $value1$, "getAndBitwiseOrRelease $type$");
953            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
954            assertEquals(x, ($type$)($value1$ | $value2$), "getAndBitwiseOrRelease $type$ value");
955        }
956
957        // get and bitwise and
958        {
959            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
960
961            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND).invokeExact(array, i, $value2$);
962            assertEquals(o, $value1$, "getAndBitwiseAnd $type$");
963            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
964            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAnd $type$ value");
965        }
966
967        {
968            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
969
970            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND_ACQUIRE).invokeExact(array, i, $value2$);
971            assertEquals(o, $value1$, "getAndBitwiseAndAcquire $type$");
972            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
973            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndAcquire $type$ value");
974        }
975
976        {
977            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
978
979            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_AND_RELEASE).invokeExact(array, i, $value2$);
980            assertEquals(o, $value1$, "getAndBitwiseAndRelease $type$");
981            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
982            assertEquals(x, ($type$)($value1$ & $value2$), "getAndBitwiseAndRelease $type$ value");
983        }
984
985        // get and bitwise xor
986        {
987            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
988
989            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR).invokeExact(array, i, $value2$);
990            assertEquals(o, $value1$, "getAndBitwiseXor $type$");
991            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
992            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXor $type$ value");
993        }
994
995        {
996            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
997
998            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_ACQUIRE).invokeExact(array, i, $value2$);
999            assertEquals(o, $value1$, "getAndBitwiseXorAcquire $type$");
1000            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
1001            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorAcquire $type$ value");
1002        }
1003
1004        {
1005            hs.get(TestAccessMode.SET).invokeExact(array, i, $value1$);
1006
1007            $type$ o = ($type$) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(array, i, $value2$);
1008            assertEquals(o, $value1$, "getAndBitwiseXorRelease $type$");
1009            $type$ x = ($type$) hs.get(TestAccessMode.GET).invokeExact(array, i);
1010            assertEquals(x, ($type$)($value1$ ^ $value2$), "getAndBitwiseXorRelease $type$ value");
1011        }
1012#end[Bitwise]
1013        }
1014    }
1015
1016    static void testArrayUnsupported(Handles hs) throws Throwable {
1017        $type$[] array = new $type$[10];
1018
1019        final int i = 0;
1020#if[!CAS]
1021        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1022            checkUOE(am, () -> {
1023                boolean r = (boolean) hs.get(am).invokeExact(array, i, $value1$, $value2$);
1024            });
1025        }
1026
1027        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1028            checkUOE(am, () -> {
1029                $type$ r = ($type$) hs.get(am).invokeExact(array, i, $value1$, $value2$);
1030            });
1031        }
1032
1033        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
1034            checkUOE(am, () -> {
1035                $type$ r = ($type$) hs.get(am).invokeExact(array, i, $value1$);
1036            });
1037        }
1038#end[CAS]
1039
1040#if[!AtomicAdd]
1041        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
1042            checkUOE(am, () -> {
1043                $type$ o = ($type$) hs.get(am).invokeExact(array, i, $value1$);
1044            });
1045        }
1046#end[AtomicAdd]
1047
1048#if[!Bitwise]
1049        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
1050            checkUOE(am, () -> {
1051                $type$ o = ($type$) hs.get(am).invokeExact(array, i, $value1$);
1052            });
1053        }
1054#end[Bitwise]
1055    }
1056
1057    static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
1058        $type$[] array = new $type$[10];
1059
1060        for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1061            final int ci = i;
1062
1063            for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
1064                checkIOOBE(am, () -> {
1065                    $type$ x = ($type$) hs.get(am).invokeExact(array, ci);
1066                });
1067            }
1068
1069            for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
1070                checkIOOBE(am, () -> {
1071                    hs.get(am).invokeExact(array, ci, $value1$);
1072                });
1073            }
1074
1075#if[CAS]
1076            for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1077                checkIOOBE(am, () -> {
1078                    boolean r = (boolean) hs.get(am).invokeExact(array, ci, $value1$, $value2$);
1079                });
1080            }
1081
1082            for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1083                checkIOOBE(am, () -> {
1084                    $type$ r = ($type$) hs.get(am).invokeExact(array, ci, $value2$, $value1$);
1085                });
1086            }
1087
1088            for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
1089                checkIOOBE(am, () -> {
1090                    $type$ o = ($type$) hs.get(am).invokeExact(array, ci, $value1$);
1091                });
1092            }
1093#end[CAS]
1094
1095#if[AtomicAdd]
1096            for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
1097                checkIOOBE(am, () -> {
1098                    $type$ o = ($type$) hs.get(am).invokeExact(array, ci, $value3$);
1099                });
1100            }
1101#end[AtomicAdd]
1102
1103#if[Bitwise]
1104            for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
1105                checkIOOBE(am, () -> {
1106                    $type$ o = ($type$) hs.get(am).invokeExact(array, ci, $value3$);
1107                });
1108            }
1109#end[Bitwise]
1110        }
1111    }
1112}
1113
1114