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