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                   VarHandleTestAccessDouble
30  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessDouble
31  * @run testng/othervm -Diters=20000                         VarHandleTestAccessDouble
32  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessDouble
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 VarHandleTestAccessDouble extends VarHandleBaseTest {
48     static final double static_final_v = 1.0d;
49 
50     static double static_v;
51 
52     final double final_v = 1.0d;
53 
54     double 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                 VarHandleTestAccessDouble.class, "final_v", double.class);
71 
72         vhField = MethodHandles.lookup().findVarHandle(
73                 VarHandleTestAccessDouble.class, "v", double.class);
74 
75         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
76             VarHandleTestAccessDouble.class, "static_final_v", double.class);
77 
78         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
79             VarHandleTestAccessDouble.class, "static_v", double.class);
80 
81         vhArray = MethodHandles.arrayElementVarHandle(double[].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         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
123         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
124         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
125         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
126         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
127         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
128         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
129         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
130         assertFalse(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(VarHandleTestAccessDouble.class)});
138         types.add(new Object[] {vhStaticField, Arrays.asList()});
139         types.add(new Object[] {vhArray, Arrays.asList(double[].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(), double.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                     VarHandleTestAccessDouble.class, "final_v", double.class);
159         });
160 
161         checkIAE("Lookup of static field to instance field", () -> {
162             MethodHandles.lookup().findStaticVarHandle(
163                     VarHandleTestAccessDouble.class, "v", double.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                 VarHandleTestAccessDouble.class, "static_final_v", double.class);
172         });
173 
174         checkIAE("Lookup of instance field to static field", () -> {
175             vhStaticField = MethodHandles.lookup().findVarHandle(
176                 VarHandleTestAccessDouble.class, "static_v", double.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, VarHandleTestAccessDouble::testStaticFinalField));
193         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
194                                               vhStaticFinalField, VarHandleTestAccessDouble::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, VarHandleTestAccessDouble::testStaticField));
205         cases.add(new VarHandleAccessTestCase("Static field unsupported",
206                                               vhStaticField, VarHandleTestAccessDouble::testStaticFieldUnsupported,
207                                               false));
208 
209         cases.add(new VarHandleAccessTestCase("Array",
210                                               vhArray, VarHandleTestAccessDouble::testArray));
211         cases.add(new VarHandleAccessTestCase("Array unsupported",
212                                               vhArray, VarHandleTestAccessDouble::testArrayUnsupported,
213                                               false));
214         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
215                                               vhArray, VarHandleTestAccessDouble::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(VarHandleTestAccessDouble recv, VarHandle vh)235     static void testInstanceFinalField(VarHandleTestAccessDouble recv, VarHandle vh) {
236         // Plain
237         {
238             double x = (double) vh.get(recv);
239             assertEquals(x, 1.0d, "get double value");
240         }
241 
242 
243         // Volatile
244         {
245             double x = (double) vh.getVolatile(recv);
246             assertEquals(x, 1.0d, "getVolatile double value");
247         }
248 
249         // Lazy
250         {
251             double x = (double) vh.getAcquire(recv);
252             assertEquals(x, 1.0d, "getRelease double value");
253         }
254 
255         // Opaque
256         {
257             double x = (double) vh.getOpaque(recv);
258             assertEquals(x, 1.0d, "getOpaque double value");
259         }
260     }
261 
testInstanceFinalFieldUnsupported(VarHandleTestAccessDouble recv, VarHandle vh)262     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessDouble recv, VarHandle vh) {
263         checkUOE(() -> {
264             vh.set(recv, 2.0d);
265         });
266 
267         checkUOE(() -> {
268             vh.setVolatile(recv, 2.0d);
269         });
270 
271         checkUOE(() -> {
272             vh.setRelease(recv, 2.0d);
273         });
274 
275         checkUOE(() -> {
276             vh.setOpaque(recv, 2.0d);
277         });
278 
279 
280 
281         checkUOE(() -> {
282             double o = (double) vh.getAndBitwiseOr(recv, 1.0d);
283         });
284 
285         checkUOE(() -> {
286             double o = (double) vh.getAndBitwiseOrAcquire(recv, 1.0d);
287         });
288 
289         checkUOE(() -> {
290             double o = (double) vh.getAndBitwiseOrRelease(recv, 1.0d);
291         });
292 
293         checkUOE(() -> {
294             double o = (double) vh.getAndBitwiseAnd(recv, 1.0d);
295         });
296 
297         checkUOE(() -> {
298             double o = (double) vh.getAndBitwiseAndAcquire(recv, 1.0d);
299         });
300 
301         checkUOE(() -> {
302             double o = (double) vh.getAndBitwiseAndRelease(recv, 1.0d);
303         });
304 
305         checkUOE(() -> {
306             double o = (double) vh.getAndBitwiseXor(recv, 1.0d);
307         });
308 
309         checkUOE(() -> {
310             double o = (double) vh.getAndBitwiseXorAcquire(recv, 1.0d);
311         });
312 
313         checkUOE(() -> {
314             double o = (double) vh.getAndBitwiseXorRelease(recv, 1.0d);
315         });
316     }
317 
318 
testStaticFinalField(VarHandle vh)319     static void testStaticFinalField(VarHandle vh) {
320         // Plain
321         {
322             double x = (double) vh.get();
323             assertEquals(x, 1.0d, "get double value");
324         }
325 
326 
327         // Volatile
328         {
329             double x = (double) vh.getVolatile();
330             assertEquals(x, 1.0d, "getVolatile double value");
331         }
332 
333         // Lazy
334         {
335             double x = (double) vh.getAcquire();
336             assertEquals(x, 1.0d, "getRelease double value");
337         }
338 
339         // Opaque
340         {
341             double x = (double) vh.getOpaque();
342             assertEquals(x, 1.0d, "getOpaque double value");
343         }
344     }
345 
testStaticFinalFieldUnsupported(VarHandle vh)346     static void testStaticFinalFieldUnsupported(VarHandle vh) {
347         checkUOE(() -> {
348             vh.set(2.0d);
349         });
350 
351         checkUOE(() -> {
352             vh.setVolatile(2.0d);
353         });
354 
355         checkUOE(() -> {
356             vh.setRelease(2.0d);
357         });
358 
359         checkUOE(() -> {
360             vh.setOpaque(2.0d);
361         });
362 
363 
364 
365         checkUOE(() -> {
366             double o = (double) vh.getAndBitwiseOr(1.0d);
367         });
368 
369         checkUOE(() -> {
370             double o = (double) vh.getAndBitwiseOrAcquire(1.0d);
371         });
372 
373         checkUOE(() -> {
374             double o = (double) vh.getAndBitwiseOrRelease(1.0d);
375         });
376 
377         checkUOE(() -> {
378             double o = (double) vh.getAndBitwiseAnd(1.0d);
379         });
380 
381         checkUOE(() -> {
382             double o = (double) vh.getAndBitwiseAndAcquire(1.0d);
383         });
384 
385         checkUOE(() -> {
386             double o = (double) vh.getAndBitwiseAndRelease(1.0d);
387         });
388 
389         checkUOE(() -> {
390             double o = (double) vh.getAndBitwiseXor(1.0d);
391         });
392 
393         checkUOE(() -> {
394             double o = (double) vh.getAndBitwiseXorAcquire(1.0d);
395         });
396 
397         checkUOE(() -> {
398             double o = (double) vh.getAndBitwiseXorRelease(1.0d);
399         });
400     }
401 
402 
testInstanceField(VarHandleTestAccessDouble recv, VarHandle vh)403     static void testInstanceField(VarHandleTestAccessDouble recv, VarHandle vh) {
404         // Plain
405         {
406             vh.set(recv, 1.0d);
407             double x = (double) vh.get(recv);
408             assertEquals(x, 1.0d, "set double value");
409         }
410 
411 
412         // Volatile
413         {
414             vh.setVolatile(recv, 2.0d);
415             double x = (double) vh.getVolatile(recv);
416             assertEquals(x, 2.0d, "setVolatile double value");
417         }
418 
419         // Lazy
420         {
421             vh.setRelease(recv, 1.0d);
422             double x = (double) vh.getAcquire(recv);
423             assertEquals(x, 1.0d, "setRelease double value");
424         }
425 
426         // Opaque
427         {
428             vh.setOpaque(recv, 2.0d);
429             double x = (double) vh.getOpaque(recv);
430             assertEquals(x, 2.0d, "setOpaque double value");
431         }
432 
433         vh.set(recv, 1.0d);
434 
435         // Compare
436         {
437             boolean r = vh.compareAndSet(recv, 1.0d, 2.0d);
438             assertEquals(r, true, "success compareAndSet double");
439             double x = (double) vh.get(recv);
440             assertEquals(x, 2.0d, "success compareAndSet double value");
441         }
442 
443         {
444             boolean r = vh.compareAndSet(recv, 1.0d, 3.0d);
445             assertEquals(r, false, "failing compareAndSet double");
446             double x = (double) vh.get(recv);
447             assertEquals(x, 2.0d, "failing compareAndSet double value");
448         }
449 
450         {
451             double r = (double) vh.compareAndExchange(recv, 2.0d, 1.0d);
452             assertEquals(r, 2.0d, "success compareAndExchange double");
453             double x = (double) vh.get(recv);
454             assertEquals(x, 1.0d, "success compareAndExchange double value");
455         }
456 
457         {
458             double r = (double) vh.compareAndExchange(recv, 2.0d, 3.0d);
459             assertEquals(r, 1.0d, "failing compareAndExchange double");
460             double x = (double) vh.get(recv);
461             assertEquals(x, 1.0d, "failing compareAndExchange double value");
462         }
463 
464         {
465             double r = (double) vh.compareAndExchangeAcquire(recv, 1.0d, 2.0d);
466             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
467             double x = (double) vh.get(recv);
468             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
469         }
470 
471         {
472             double r = (double) vh.compareAndExchangeAcquire(recv, 1.0d, 3.0d);
473             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
474             double x = (double) vh.get(recv);
475             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
476         }
477 
478         {
479             double r = (double) vh.compareAndExchangeRelease(recv, 2.0d, 1.0d);
480             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
481             double x = (double) vh.get(recv);
482             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
483         }
484 
485         {
486             double r = (double) vh.compareAndExchangeRelease(recv, 2.0d, 3.0d);
487             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
488             double x = (double) vh.get(recv);
489             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
490         }
491 
492         {
493             boolean success = false;
494             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
495                 success = vh.weakCompareAndSetPlain(recv, 1.0d, 2.0d);
496             }
497             assertEquals(success, true, "weakCompareAndSetPlain double");
498             double x = (double) vh.get(recv);
499             assertEquals(x, 2.0d, "weakCompareAndSetPlain double value");
500         }
501 
502         {
503             boolean success = false;
504             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
505                 success = vh.weakCompareAndSetAcquire(recv, 2.0d, 1.0d);
506             }
507             assertEquals(success, true, "weakCompareAndSetAcquire double");
508             double x = (double) vh.get(recv);
509             assertEquals(x, 1.0d, "weakCompareAndSetAcquire double");
510         }
511 
512         {
513             boolean success = false;
514             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
515                 success = vh.weakCompareAndSetRelease(recv, 1.0d, 2.0d);
516             }
517             assertEquals(success, true, "weakCompareAndSetRelease double");
518             double x = (double) vh.get(recv);
519             assertEquals(x, 2.0d, "weakCompareAndSetRelease double");
520         }
521 
522         {
523             boolean success = false;
524             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
525                 success = vh.weakCompareAndSet(recv, 2.0d, 1.0d);
526             }
527             assertEquals(success, true, "weakCompareAndSet double");
528             double x = (double) vh.get(recv);
529             assertEquals(x, 1.0d, "weakCompareAndSet double value");
530         }
531 
532         // Compare set and get
533         {
534             vh.set(recv, 1.0d);
535 
536             double o = (double) vh.getAndSet(recv, 2.0d);
537             assertEquals(o, 1.0d, "getAndSet double");
538             double x = (double) vh.get(recv);
539             assertEquals(x, 2.0d, "getAndSet double value");
540         }
541 
542         {
543             vh.set(recv, 1.0d);
544 
545             double o = (double) vh.getAndSetAcquire(recv, 2.0d);
546             assertEquals(o, 1.0d, "getAndSetAcquire double");
547             double x = (double) vh.get(recv);
548             assertEquals(x, 2.0d, "getAndSetAcquire double value");
549         }
550 
551         {
552             vh.set(recv, 1.0d);
553 
554             double o = (double) vh.getAndSetRelease(recv, 2.0d);
555             assertEquals(o, 1.0d, "getAndSetRelease double");
556             double x = (double) vh.get(recv);
557             assertEquals(x, 2.0d, "getAndSetRelease double value");
558         }
559 
560         // get and add, add and get
561         {
562             vh.set(recv, 1.0d);
563 
564             double o = (double) vh.getAndAdd(recv, 2.0d);
565             assertEquals(o, 1.0d, "getAndAdd double");
566             double x = (double) vh.get(recv);
567             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
568         }
569 
570         {
571             vh.set(recv, 1.0d);
572 
573             double o = (double) vh.getAndAddAcquire(recv, 2.0d);
574             assertEquals(o, 1.0d, "getAndAddAcquire double");
575             double x = (double) vh.get(recv);
576             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
577         }
578 
579         {
580             vh.set(recv, 1.0d);
581 
582             double o = (double) vh.getAndAddRelease(recv, 2.0d);
583             assertEquals(o, 1.0d, "getAndAddReleasedouble");
584             double x = (double) vh.get(recv);
585             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
586         }
587 
588     }
589 
testInstanceFieldUnsupported(VarHandleTestAccessDouble recv, VarHandle vh)590     static void testInstanceFieldUnsupported(VarHandleTestAccessDouble recv, VarHandle vh) {
591 
592 
593         checkUOE(() -> {
594             double o = (double) vh.getAndBitwiseOr(recv, 1.0d);
595         });
596 
597         checkUOE(() -> {
598             double o = (double) vh.getAndBitwiseOrAcquire(recv, 1.0d);
599         });
600 
601         checkUOE(() -> {
602             double o = (double) vh.getAndBitwiseOrRelease(recv, 1.0d);
603         });
604 
605         checkUOE(() -> {
606             double o = (double) vh.getAndBitwiseAnd(recv, 1.0d);
607         });
608 
609         checkUOE(() -> {
610             double o = (double) vh.getAndBitwiseAndAcquire(recv, 1.0d);
611         });
612 
613         checkUOE(() -> {
614             double o = (double) vh.getAndBitwiseAndRelease(recv, 1.0d);
615         });
616 
617         checkUOE(() -> {
618             double o = (double) vh.getAndBitwiseXor(recv, 1.0d);
619         });
620 
621         checkUOE(() -> {
622             double o = (double) vh.getAndBitwiseXorAcquire(recv, 1.0d);
623         });
624 
625         checkUOE(() -> {
626             double o = (double) vh.getAndBitwiseXorRelease(recv, 1.0d);
627         });
628     }
629 
630 
testStaticField(VarHandle vh)631     static void testStaticField(VarHandle vh) {
632         // Plain
633         {
634             vh.set(1.0d);
635             double x = (double) vh.get();
636             assertEquals(x, 1.0d, "set double value");
637         }
638 
639 
640         // Volatile
641         {
642             vh.setVolatile(2.0d);
643             double x = (double) vh.getVolatile();
644             assertEquals(x, 2.0d, "setVolatile double value");
645         }
646 
647         // Lazy
648         {
649             vh.setRelease(1.0d);
650             double x = (double) vh.getAcquire();
651             assertEquals(x, 1.0d, "setRelease double value");
652         }
653 
654         // Opaque
655         {
656             vh.setOpaque(2.0d);
657             double x = (double) vh.getOpaque();
658             assertEquals(x, 2.0d, "setOpaque double value");
659         }
660 
661         vh.set(1.0d);
662 
663         // Compare
664         {
665             boolean r = vh.compareAndSet(1.0d, 2.0d);
666             assertEquals(r, true, "success compareAndSet double");
667             double x = (double) vh.get();
668             assertEquals(x, 2.0d, "success compareAndSet double value");
669         }
670 
671         {
672             boolean r = vh.compareAndSet(1.0d, 3.0d);
673             assertEquals(r, false, "failing compareAndSet double");
674             double x = (double) vh.get();
675             assertEquals(x, 2.0d, "failing compareAndSet double value");
676         }
677 
678         {
679             double r = (double) vh.compareAndExchange(2.0d, 1.0d);
680             assertEquals(r, 2.0d, "success compareAndExchange double");
681             double x = (double) vh.get();
682             assertEquals(x, 1.0d, "success compareAndExchange double value");
683         }
684 
685         {
686             double r = (double) vh.compareAndExchange(2.0d, 3.0d);
687             assertEquals(r, 1.0d, "failing compareAndExchange double");
688             double x = (double) vh.get();
689             assertEquals(x, 1.0d, "failing compareAndExchange double value");
690         }
691 
692         {
693             double r = (double) vh.compareAndExchangeAcquire(1.0d, 2.0d);
694             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
695             double x = (double) vh.get();
696             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
697         }
698 
699         {
700             double r = (double) vh.compareAndExchangeAcquire(1.0d, 3.0d);
701             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
702             double x = (double) vh.get();
703             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
704         }
705 
706         {
707             double r = (double) vh.compareAndExchangeRelease(2.0d, 1.0d);
708             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
709             double x = (double) vh.get();
710             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
711         }
712 
713         {
714             double r = (double) vh.compareAndExchangeRelease(2.0d, 3.0d);
715             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
716             double x = (double) vh.get();
717             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
718         }
719 
720         {
721             boolean success = false;
722             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
723                 success = vh.weakCompareAndSetPlain(1.0d, 2.0d);
724             }
725             assertEquals(success, true, "weakCompareAndSetPlain double");
726             double x = (double) vh.get();
727             assertEquals(x, 2.0d, "weakCompareAndSetPlain double value");
728         }
729 
730         {
731             boolean success = false;
732             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
733                 success = vh.weakCompareAndSetAcquire(2.0d, 1.0d);
734             }
735             assertEquals(success, true, "weakCompareAndSetAcquire double");
736             double x = (double) vh.get();
737             assertEquals(x, 1.0d, "weakCompareAndSetAcquire double");
738         }
739 
740         {
741             boolean success = false;
742             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
743                 success = vh.weakCompareAndSetRelease(1.0d, 2.0d);
744             }
745             assertEquals(success, true, "weakCompareAndSetRelease double");
746             double x = (double) vh.get();
747             assertEquals(x, 2.0d, "weakCompareAndSetRelease double");
748         }
749 
750         {
751             boolean success = false;
752             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
753                 success = vh.weakCompareAndSet(2.0d, 1.0d);
754             }
755             assertEquals(success, true, "weakCompareAndSet double");
756             double x = (double) vh.get();
757             assertEquals(x, 1.0d, "weakCompareAndSet double");
758         }
759 
760         // Compare set and get
761         {
762             vh.set(1.0d);
763 
764             double o = (double) vh.getAndSet(2.0d);
765             assertEquals(o, 1.0d, "getAndSet double");
766             double x = (double) vh.get();
767             assertEquals(x, 2.0d, "getAndSet double value");
768         }
769 
770         {
771             vh.set(1.0d);
772 
773             double o = (double) vh.getAndSetAcquire(2.0d);
774             assertEquals(o, 1.0d, "getAndSetAcquire double");
775             double x = (double) vh.get();
776             assertEquals(x, 2.0d, "getAndSetAcquire double value");
777         }
778 
779         {
780             vh.set(1.0d);
781 
782             double o = (double) vh.getAndSetRelease(2.0d);
783             assertEquals(o, 1.0d, "getAndSetRelease double");
784             double x = (double) vh.get();
785             assertEquals(x, 2.0d, "getAndSetRelease double value");
786         }
787 
788         // get and add, add and get
789         {
790             vh.set(1.0d);
791 
792             double o = (double) vh.getAndAdd(2.0d);
793             assertEquals(o, 1.0d, "getAndAdd double");
794             double x = (double) vh.get();
795             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
796         }
797 
798         {
799             vh.set(1.0d);
800 
801             double o = (double) vh.getAndAddAcquire(2.0d);
802             assertEquals(o, 1.0d, "getAndAddAcquire double");
803             double x = (double) vh.get();
804             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
805         }
806 
807         {
808             vh.set(1.0d);
809 
810             double o = (double) vh.getAndAddRelease(2.0d);
811             assertEquals(o, 1.0d, "getAndAddReleasedouble");
812             double x = (double) vh.get();
813             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
814         }
815 
816     }
817 
testStaticFieldUnsupported(VarHandle vh)818     static void testStaticFieldUnsupported(VarHandle vh) {
819 
820 
821         checkUOE(() -> {
822             double o = (double) vh.getAndBitwiseOr(1.0d);
823         });
824 
825         checkUOE(() -> {
826             double o = (double) vh.getAndBitwiseOrAcquire(1.0d);
827         });
828 
829         checkUOE(() -> {
830             double o = (double) vh.getAndBitwiseOrRelease(1.0d);
831         });
832 
833         checkUOE(() -> {
834             double o = (double) vh.getAndBitwiseAnd(1.0d);
835         });
836 
837         checkUOE(() -> {
838             double o = (double) vh.getAndBitwiseAndAcquire(1.0d);
839         });
840 
841         checkUOE(() -> {
842             double o = (double) vh.getAndBitwiseAndRelease(1.0d);
843         });
844 
845         checkUOE(() -> {
846             double o = (double) vh.getAndBitwiseXor(1.0d);
847         });
848 
849         checkUOE(() -> {
850             double o = (double) vh.getAndBitwiseXorAcquire(1.0d);
851         });
852 
853         checkUOE(() -> {
854             double o = (double) vh.getAndBitwiseXorRelease(1.0d);
855         });
856     }
857 
858 
testArray(VarHandle vh)859     static void testArray(VarHandle vh) {
860         double[] array = new double[10];
861 
862         for (int i = 0; i < array.length; i++) {
863             // Plain
864             {
865                 vh.set(array, i, 1.0d);
866                 double x = (double) vh.get(array, i);
867                 assertEquals(x, 1.0d, "get double value");
868             }
869 
870 
871             // Volatile
872             {
873                 vh.setVolatile(array, i, 2.0d);
874                 double x = (double) vh.getVolatile(array, i);
875                 assertEquals(x, 2.0d, "setVolatile double value");
876             }
877 
878             // Lazy
879             {
880                 vh.setRelease(array, i, 1.0d);
881                 double x = (double) vh.getAcquire(array, i);
882                 assertEquals(x, 1.0d, "setRelease double value");
883             }
884 
885             // Opaque
886             {
887                 vh.setOpaque(array, i, 2.0d);
888                 double x = (double) vh.getOpaque(array, i);
889                 assertEquals(x, 2.0d, "setOpaque double value");
890             }
891 
892             vh.set(array, i, 1.0d);
893 
894             // Compare
895             {
896                 boolean r = vh.compareAndSet(array, i, 1.0d, 2.0d);
897                 assertEquals(r, true, "success compareAndSet double");
898                 double x = (double) vh.get(array, i);
899                 assertEquals(x, 2.0d, "success compareAndSet double value");
900             }
901 
902             {
903                 boolean r = vh.compareAndSet(array, i, 1.0d, 3.0d);
904                 assertEquals(r, false, "failing compareAndSet double");
905                 double x = (double) vh.get(array, i);
906                 assertEquals(x, 2.0d, "failing compareAndSet double value");
907             }
908 
909             {
910                 double r = (double) vh.compareAndExchange(array, i, 2.0d, 1.0d);
911                 assertEquals(r, 2.0d, "success compareAndExchange double");
912                 double x = (double) vh.get(array, i);
913                 assertEquals(x, 1.0d, "success compareAndExchange double value");
914             }
915 
916             {
917                 double r = (double) vh.compareAndExchange(array, i, 2.0d, 3.0d);
918                 assertEquals(r, 1.0d, "failing compareAndExchange double");
919                 double x = (double) vh.get(array, i);
920                 assertEquals(x, 1.0d, "failing compareAndExchange double value");
921             }
922 
923             {
924                 double r = (double) vh.compareAndExchangeAcquire(array, i, 1.0d, 2.0d);
925                 assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
926                 double x = (double) vh.get(array, i);
927                 assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
928             }
929 
930             {
931                 double r = (double) vh.compareAndExchangeAcquire(array, i, 1.0d, 3.0d);
932                 assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
933                 double x = (double) vh.get(array, i);
934                 assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
935             }
936 
937             {
938                 double r = (double) vh.compareAndExchangeRelease(array, i, 2.0d, 1.0d);
939                 assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
940                 double x = (double) vh.get(array, i);
941                 assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
942             }
943 
944             {
945                 double r = (double) vh.compareAndExchangeRelease(array, i, 2.0d, 3.0d);
946                 assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
947                 double x = (double) vh.get(array, i);
948                 assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
949             }
950 
951             {
952                 boolean success = false;
953                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
954                     success = vh.weakCompareAndSetPlain(array, i, 1.0d, 2.0d);
955                 }
956                 assertEquals(success, true, "weakCompareAndSetPlain double");
957                 double x = (double) vh.get(array, i);
958                 assertEquals(x, 2.0d, "weakCompareAndSetPlain double value");
959             }
960 
961             {
962                 boolean success = false;
963                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
964                     success = vh.weakCompareAndSetAcquire(array, i, 2.0d, 1.0d);
965                 }
966                 assertEquals(success, true, "weakCompareAndSetAcquire double");
967                 double x = (double) vh.get(array, i);
968                 assertEquals(x, 1.0d, "weakCompareAndSetAcquire double");
969             }
970 
971             {
972                 boolean success = false;
973                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
974                     success = vh.weakCompareAndSetRelease(array, i, 1.0d, 2.0d);
975                 }
976                 assertEquals(success, true, "weakCompareAndSetRelease double");
977                 double x = (double) vh.get(array, i);
978                 assertEquals(x, 2.0d, "weakCompareAndSetRelease double");
979             }
980 
981             {
982                 boolean success = false;
983                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
984                     success = vh.weakCompareAndSet(array, i, 2.0d, 1.0d);
985                 }
986                 assertEquals(success, true, "weakCompareAndSet double");
987                 double x = (double) vh.get(array, i);
988                 assertEquals(x, 1.0d, "weakCompareAndSet double");
989             }
990 
991             // Compare set and get
992             {
993                 vh.set(array, i, 1.0d);
994 
995                 double o = (double) vh.getAndSet(array, i, 2.0d);
996                 assertEquals(o, 1.0d, "getAndSet double");
997                 double x = (double) vh.get(array, i);
998                 assertEquals(x, 2.0d, "getAndSet double value");
999             }
1000 
1001             {
1002                 vh.set(array, i, 1.0d);
1003 
1004                 double o = (double) vh.getAndSetAcquire(array, i, 2.0d);
1005                 assertEquals(o, 1.0d, "getAndSetAcquire double");
1006                 double x = (double) vh.get(array, i);
1007                 assertEquals(x, 2.0d, "getAndSetAcquire double value");
1008             }
1009 
1010             {
1011                 vh.set(array, i, 1.0d);
1012 
1013                 double o = (double) vh.getAndSetRelease(array, i, 2.0d);
1014                 assertEquals(o, 1.0d, "getAndSetRelease double");
1015                 double x = (double) vh.get(array, i);
1016                 assertEquals(x, 2.0d, "getAndSetRelease double value");
1017             }
1018 
1019             // get and add, add and get
1020             {
1021                 vh.set(array, i, 1.0d);
1022 
1023                 double o = (double) vh.getAndAdd(array, i, 2.0d);
1024                 assertEquals(o, 1.0d, "getAndAdd double");
1025                 double x = (double) vh.get(array, i);
1026                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
1027             }
1028 
1029             {
1030                 vh.set(array, i, 1.0d);
1031 
1032                 double o = (double) vh.getAndAddAcquire(array, i, 2.0d);
1033                 assertEquals(o, 1.0d, "getAndAddAcquire double");
1034                 double x = (double) vh.get(array, i);
1035                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
1036             }
1037 
1038             {
1039                 vh.set(array, i, 1.0d);
1040 
1041                 double o = (double) vh.getAndAddRelease(array, i, 2.0d);
1042                 assertEquals(o, 1.0d, "getAndAddReleasedouble");
1043                 double x = (double) vh.get(array, i);
1044                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
1045             }
1046 
1047         }
1048     }
1049 
testArrayUnsupported(VarHandle vh)1050     static void testArrayUnsupported(VarHandle vh) {
1051         double[] array = new double[10];
1052 
1053         int i = 0;
1054 
1055 
1056         checkUOE(() -> {
1057             double o = (double) vh.getAndBitwiseOr(array, i, 1.0d);
1058         });
1059 
1060         checkUOE(() -> {
1061             double o = (double) vh.getAndBitwiseOrAcquire(array, i, 1.0d);
1062         });
1063 
1064         checkUOE(() -> {
1065             double o = (double) vh.getAndBitwiseOrRelease(array, i, 1.0d);
1066         });
1067 
1068         checkUOE(() -> {
1069             double o = (double) vh.getAndBitwiseAnd(array, i, 1.0d);
1070         });
1071 
1072         checkUOE(() -> {
1073             double o = (double) vh.getAndBitwiseAndAcquire(array, i, 1.0d);
1074         });
1075 
1076         checkUOE(() -> {
1077             double o = (double) vh.getAndBitwiseAndRelease(array, i, 1.0d);
1078         });
1079 
1080         checkUOE(() -> {
1081             double o = (double) vh.getAndBitwiseXor(array, i, 1.0d);
1082         });
1083 
1084         checkUOE(() -> {
1085             double o = (double) vh.getAndBitwiseXorAcquire(array, i, 1.0d);
1086         });
1087 
1088         checkUOE(() -> {
1089             double o = (double) vh.getAndBitwiseXorRelease(array, i, 1.0d);
1090         });
1091     }
1092 
testArrayIndexOutOfBounds(VarHandle vh)1093     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1094         double[] array = new double[10];
1095 
1096         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1097             final int ci = i;
1098 
1099             checkIOOBE(() -> {
1100                 double x = (double) vh.get(array, ci);
1101             });
1102 
1103             checkIOOBE(() -> {
1104                 vh.set(array, ci, 1.0d);
1105             });
1106 
1107             checkIOOBE(() -> {
1108                 double x = (double) vh.getVolatile(array, ci);
1109             });
1110 
1111             checkIOOBE(() -> {
1112                 vh.setVolatile(array, ci, 1.0d);
1113             });
1114 
1115             checkIOOBE(() -> {
1116                 double x = (double) vh.getAcquire(array, ci);
1117             });
1118 
1119             checkIOOBE(() -> {
1120                 vh.setRelease(array, ci, 1.0d);
1121             });
1122 
1123             checkIOOBE(() -> {
1124                 double x = (double) vh.getOpaque(array, ci);
1125             });
1126 
1127             checkIOOBE(() -> {
1128                 vh.setOpaque(array, ci, 1.0d);
1129             });
1130 
1131             checkIOOBE(() -> {
1132                 boolean r = vh.compareAndSet(array, ci, 1.0d, 2.0d);
1133             });
1134 
1135             checkIOOBE(() -> {
1136                 double r = (double) vh.compareAndExchange(array, ci, 2.0d, 1.0d);
1137             });
1138 
1139             checkIOOBE(() -> {
1140                 double r = (double) vh.compareAndExchangeAcquire(array, ci, 2.0d, 1.0d);
1141             });
1142 
1143             checkIOOBE(() -> {
1144                 double r = (double) vh.compareAndExchangeRelease(array, ci, 2.0d, 1.0d);
1145             });
1146 
1147             checkIOOBE(() -> {
1148                 boolean r = vh.weakCompareAndSetPlain(array, ci, 1.0d, 2.0d);
1149             });
1150 
1151             checkIOOBE(() -> {
1152                 boolean r = vh.weakCompareAndSet(array, ci, 1.0d, 2.0d);
1153             });
1154 
1155             checkIOOBE(() -> {
1156                 boolean r = vh.weakCompareAndSetAcquire(array, ci, 1.0d, 2.0d);
1157             });
1158 
1159             checkIOOBE(() -> {
1160                 boolean r = vh.weakCompareAndSetRelease(array, ci, 1.0d, 2.0d);
1161             });
1162 
1163             checkIOOBE(() -> {
1164                 double o = (double) vh.getAndSet(array, ci, 1.0d);
1165             });
1166 
1167             checkIOOBE(() -> {
1168                 double o = (double) vh.getAndSetAcquire(array, ci, 1.0d);
1169             });
1170 
1171             checkIOOBE(() -> {
1172                 double o = (double) vh.getAndSetRelease(array, ci, 1.0d);
1173             });
1174 
1175             checkIOOBE(() -> {
1176                 double o = (double) vh.getAndAdd(array, ci, 1.0d);
1177             });
1178 
1179             checkIOOBE(() -> {
1180                 double o = (double) vh.getAndAddAcquire(array, ci, 1.0d);
1181             });
1182 
1183             checkIOOBE(() -> {
1184                 double o = (double) vh.getAndAddRelease(array, ci, 1.0d);
1185             });
1186 
1187         }
1188     }
1189 
1190 }
1191 
1192