1 /*
2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 // Android-added: package for test.
25 package test.java.lang.invoke.VarHandles;
26 
27 /*
28  * @test
29  * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessDouble
30  */
31 
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35 
36 import java.lang.invoke.MethodHandles;
37 import java.lang.invoke.VarHandle;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 
42 import static org.testng.Assert.*;
43 
44 public class VarHandleTestMethodHandleAccessDouble extends VarHandleBaseTest {
45     static final double static_final_v = 1.0d;
46 
47     static double static_v;
48 
49     final double final_v = 1.0d;
50 
51     double v;
52 
53     VarHandle vhFinalField;
54 
55     VarHandle vhField;
56 
57     VarHandle vhStaticField;
58 
59     VarHandle vhStaticFinalField;
60 
61     VarHandle vhArray;
62 
63     @BeforeClass
setup()64     public void setup() throws Exception {
65         vhFinalField = MethodHandles.lookup().findVarHandle(
66                 VarHandleTestMethodHandleAccessDouble.class, "final_v", double.class);
67 
68         vhField = MethodHandles.lookup().findVarHandle(
69                 VarHandleTestMethodHandleAccessDouble.class, "v", double.class);
70 
71         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
72             VarHandleTestMethodHandleAccessDouble.class, "static_final_v", double.class);
73 
74         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
75             VarHandleTestMethodHandleAccessDouble.class, "static_v", double.class);
76 
77         vhArray = MethodHandles.arrayElementVarHandle(double[].class);
78     }
79 
80 
81     @DataProvider
accessTestCaseProvider()82     public Object[][] accessTestCaseProvider() throws Exception {
83         List<AccessTestCase<?>> cases = new ArrayList<>();
84 
85         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
86             cases.add(new MethodHandleAccessTestCase("Instance field",
87                                                      vhField, f, hs -> testInstanceField(this, hs)));
88             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
89                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
90                                                      false));
91 
92             cases.add(new MethodHandleAccessTestCase("Static field",
93                                                      vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticField));
94             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
95                                                      vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticFieldUnsupported,
96                                                      false));
97 
98             cases.add(new MethodHandleAccessTestCase("Array",
99                                                      vhArray, f, VarHandleTestMethodHandleAccessDouble::testArray));
100             cases.add(new MethodHandleAccessTestCase("Array unsupported",
101                                                      vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayUnsupported,
102                                                      false));
103             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
104                                                      vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayIndexOutOfBounds,
105                                                      false));
106         }
107 
108         // Work around issue with jtreg summary reporting which truncates
109         // the String result of Object.toString to 30 characters, hence
110         // the first dummy argument
111         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
112     }
113 
114     @Test(dataProvider = "accessTestCaseProvider")
testAccess(String desc, AccessTestCase<T> atc)115     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
116         T t = atc.get();
117         int iters = atc.requiresLoop() ? ITERS : 1;
118         for (int c = 0; c < iters; c++) {
119             atc.testAccess(t);
120         }
121     }
122 
123 
testInstanceField(VarHandleTestMethodHandleAccessDouble recv, Handles hs)124     static void testInstanceField(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable {
125         // Plain
126         {
127             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
128             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
129             assertEquals(x, 1.0d, "set double value");
130         }
131 
132 
133         // Volatile
134         {
135             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0d);
136             double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
137             assertEquals(x, 2.0d, "setVolatile double value");
138         }
139 
140         // Lazy
141         {
142             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0d);
143             double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
144             assertEquals(x, 1.0d, "setRelease double value");
145         }
146 
147         // Opaque
148         {
149             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0d);
150             double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
151             assertEquals(x, 2.0d, "setOpaque double value");
152         }
153 
154         hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
155 
156         // Compare
157         {
158             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0d, 2.0d);
159             assertEquals(r, true, "success compareAndSet double");
160             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
161             assertEquals(x, 2.0d, "success compareAndSet double value");
162         }
163 
164         {
165             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0d, 3.0d);
166             assertEquals(r, false, "failing compareAndSet double");
167             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
168             assertEquals(x, 2.0d, "failing compareAndSet double value");
169         }
170 
171         {
172             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0d, 1.0d);
173             assertEquals(r, 2.0d, "success compareAndExchange double");
174             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
175             assertEquals(x, 1.0d, "success compareAndExchange double value");
176         }
177 
178         {
179             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0d, 3.0d);
180             assertEquals(r, 1.0d, "failing compareAndExchange double");
181             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
182             assertEquals(x, 1.0d, "failing compareAndExchange double value");
183         }
184 
185         {
186             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0d, 2.0d);
187             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
188             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
189             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
190         }
191 
192         {
193             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0d, 3.0d);
194             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
195             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
196             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
197         }
198 
199         {
200             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0d, 1.0d);
201             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
202             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
203             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
204         }
205 
206         {
207             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0d, 3.0d);
208             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
209             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
210             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
211         }
212 
213         {
214             boolean success = false;
215             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
216                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 1.0d, 2.0d);
217             }
218             assertEquals(success, true, "weakCompareAndSetPlain double");
219             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
220             assertEquals(x, 2.0d, "weakCompareAndSetPlain double value");
221         }
222 
223         {
224             boolean success = false;
225             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
226                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2.0d, 1.0d);
227             }
228             assertEquals(success, true, "weakCompareAndSetAcquire double");
229             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
230             assertEquals(x, 1.0d, "weakCompareAndSetAcquire double");
231         }
232 
233         {
234             boolean success = false;
235             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
236                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1.0d, 2.0d);
237             }
238             assertEquals(success, true, "weakCompareAndSetRelease double");
239             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
240             assertEquals(x, 2.0d, "weakCompareAndSetRelease double");
241         }
242 
243         {
244             boolean success = false;
245             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
246                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 2.0d, 1.0d);
247             }
248             assertEquals(success, true, "weakCompareAndSet double");
249             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
250             assertEquals(x, 1.0d, "weakCompareAndSet double");
251         }
252 
253         // Compare set and get
254         {
255             double o = (double) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2.0d);
256             assertEquals(o, 1.0d, "getAndSet double");
257             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
258             assertEquals(x, 2.0d, "getAndSet double value");
259         }
260 
261         // get and add, add and get
262         {
263             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
264 
265             double o = (double) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 2.0d);
266             assertEquals(o, 1.0d, "getAndAdd double");
267             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
268             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
269         }
270 
271         {
272             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
273 
274             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 2.0d);
275             assertEquals(o, 1.0d, "getAndAddAcquire double");
276             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
277             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
278         }
279 
280         {
281             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
282 
283             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0d);
284             assertEquals(o, 1.0d, "getAndAddRelease double");
285             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
286             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
287         }
288 
289     }
290 
testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessDouble recv, Handles hs)291     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable {
292 
293 
294         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
295             checkUOE(am, () -> {
296                 double r = (double) hs.get(am).invokeExact(recv, 1.0d);
297             });
298         }
299     }
300 
301 
testStaticField(Handles hs)302     static void testStaticField(Handles hs) throws Throwable {
303         // Plain
304         {
305             hs.get(TestAccessMode.SET).invokeExact(1.0d);
306             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
307             assertEquals(x, 1.0d, "set double value");
308         }
309 
310 
311         // Volatile
312         {
313             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0d);
314             double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
315             assertEquals(x, 2.0d, "setVolatile double value");
316         }
317 
318         // Lazy
319         {
320             hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0d);
321             double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
322             assertEquals(x, 1.0d, "setRelease double value");
323         }
324 
325         // Opaque
326         {
327             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0d);
328             double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
329             assertEquals(x, 2.0d, "setOpaque double value");
330         }
331 
332         hs.get(TestAccessMode.SET).invokeExact(1.0d);
333 
334         // Compare
335         {
336             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0d, 2.0d);
337             assertEquals(r, true, "success compareAndSet double");
338             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
339             assertEquals(x, 2.0d, "success compareAndSet double value");
340         }
341 
342         {
343             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0d, 3.0d);
344             assertEquals(r, false, "failing compareAndSet double");
345             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
346             assertEquals(x, 2.0d, "failing compareAndSet double value");
347         }
348 
349         {
350             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0d, 1.0d);
351             assertEquals(r, 2.0d, "success compareAndExchange double");
352             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
353             assertEquals(x, 1.0d, "success compareAndExchange double value");
354         }
355 
356         {
357             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0d, 3.0d);
358             assertEquals(r, 1.0d, "failing compareAndExchange double");
359             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
360             assertEquals(x, 1.0d, "failing compareAndExchange double value");
361         }
362 
363         {
364             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0d, 2.0d);
365             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
366             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
367             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
368         }
369 
370         {
371             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0d, 3.0d);
372             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
373             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
374             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
375         }
376 
377         {
378             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0d, 1.0d);
379             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
380             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
381             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
382         }
383 
384         {
385             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0d, 3.0d);
386             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
387             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
388             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
389         }
390 
391         {
392             boolean success = false;
393             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
394                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(1.0d, 2.0d);
395             }
396             assertEquals(success, true, "weakCompareAndSetPlain double");
397             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
398             assertEquals(x, 2.0d, "weakCompareAndSetPlain double value");
399         }
400 
401         {
402             boolean success = false;
403             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
404                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(2.0d, 1.0d);
405             }
406             assertEquals(success, true, "weakCompareAndSetAcquire double");
407             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
408             assertEquals(x, 1.0d, "weakCompareAndSetAcquire double");
409         }
410 
411         {
412             boolean success = false;
413             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
414                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1.0d, 2.0d);
415             }
416             assertEquals(success, true, "weakCompareAndSetRelease double");
417             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
418             assertEquals(x, 2.0d, "weakCompareAndSetRelease double");
419         }
420 
421         {
422             boolean success = false;
423             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
424                 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(2.0d, 1.0d);
425             }
426             assertEquals(success, true, "weakCompareAndSet double");
427             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
428             assertEquals(x, 1.0d, "weakCompareAndSet double");
429         }
430 
431         // Compare set and get
432         {
433             hs.get(TestAccessMode.SET).invokeExact(1.0d);
434 
435             double o = (double) hs.get(TestAccessMode.GET_AND_SET).invokeExact(2.0d);
436             assertEquals(o, 1.0d, "getAndSet double");
437             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
438             assertEquals(x, 2.0d, "getAndSet double value");
439         }
440 
441         // Compare set and get
442         {
443             hs.get(TestAccessMode.SET).invokeExact(1.0d);
444 
445             double o = (double) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(2.0d);
446             assertEquals(o, 1.0d, "getAndSetAcquire double");
447             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
448             assertEquals(x, 2.0d, "getAndSetAcquire double value");
449         }
450 
451         // Compare set and get
452         {
453             hs.get(TestAccessMode.SET).invokeExact(1.0d);
454 
455             double o = (double) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(2.0d);
456             assertEquals(o, 1.0d, "getAndSetRelease double");
457             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
458             assertEquals(x, 2.0d, "getAndSetRelease double value");
459         }
460 
461         // get and add, add and get
462         {
463             hs.get(TestAccessMode.SET).invokeExact(1.0d);
464 
465             double o = (double) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(2.0d);
466             assertEquals(o, 1.0d, "getAndAdd double");
467             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
468             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
469         }
470 
471         {
472             hs.get(TestAccessMode.SET).invokeExact(1.0d);
473 
474             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(2.0d);
475             assertEquals(o, 1.0d, "getAndAddAcquire double");
476             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
477             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
478         }
479 
480         {
481             hs.get(TestAccessMode.SET).invokeExact(1.0d);
482 
483             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(2.0d);
484             assertEquals(o, 1.0d, "getAndAddRelease double");
485             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
486             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
487         }
488 
489     }
490 
testStaticFieldUnsupported(Handles hs)491     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
492 
493 
494         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
495             checkUOE(am, () -> {
496                 double r = (double) hs.get(am).invokeExact(1.0d);
497             });
498         }
499     }
500 
501 
testArray(Handles hs)502     static void testArray(Handles hs) throws Throwable {
503         double[] array = new double[10];
504 
505         for (int i = 0; i < array.length; i++) {
506             // Plain
507             {
508                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
509                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
510                 assertEquals(x, 1.0d, "get double value");
511             }
512 
513 
514             // Volatile
515             {
516                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0d);
517                 double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
518                 assertEquals(x, 2.0d, "setVolatile double value");
519             }
520 
521             // Lazy
522             {
523                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0d);
524                 double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
525                 assertEquals(x, 1.0d, "setRelease double value");
526             }
527 
528             // Opaque
529             {
530                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0d);
531                 double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
532                 assertEquals(x, 2.0d, "setOpaque double value");
533             }
534 
535             hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
536 
537             // Compare
538             {
539                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0d, 2.0d);
540                 assertEquals(r, true, "success compareAndSet double");
541                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
542                 assertEquals(x, 2.0d, "success compareAndSet double value");
543             }
544 
545             {
546                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0d, 3.0d);
547                 assertEquals(r, false, "failing compareAndSet double");
548                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
549                 assertEquals(x, 2.0d, "failing compareAndSet double value");
550             }
551 
552             {
553                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0d, 1.0d);
554                 assertEquals(r, 2.0d, "success compareAndExchange double");
555                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
556                 assertEquals(x, 1.0d, "success compareAndExchange double value");
557             }
558 
559             {
560                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0d, 3.0d);
561                 assertEquals(r, 1.0d, "failing compareAndExchange double");
562                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
563                 assertEquals(x, 1.0d, "failing compareAndExchange double value");
564             }
565 
566             {
567                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0d, 2.0d);
568                 assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
569                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
570                 assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
571             }
572 
573             {
574                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0d, 3.0d);
575                 assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
576                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
577                 assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
578             }
579 
580             {
581                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0d, 1.0d);
582                 assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
583                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
584                 assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
585             }
586 
587             {
588                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0d, 3.0d);
589                 assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
590                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
591                 assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
592             }
593 
594             {
595                 boolean success = false;
596                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
597                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 1.0d, 2.0d);
598                 }
599                 assertEquals(success, true, "weakCompareAndSetPlain double");
600                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
601                 assertEquals(x, 2.0d, "weakCompareAndSetPlain double value");
602             }
603 
604             {
605                 boolean success = false;
606                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
607                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2.0d, 1.0d);
608                 }
609                 assertEquals(success, true, "weakCompareAndSetAcquire double");
610                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
611                 assertEquals(x, 1.0d, "weakCompareAndSetAcquire double");
612             }
613 
614             {
615                 boolean success = false;
616                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
617                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, 1.0d, 2.0d);
618                 }
619                 assertEquals(success, true, "weakCompareAndSetRelease double");
620                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
621                 assertEquals(x, 2.0d, "weakCompareAndSetRelease double");
622             }
623 
624             {
625                 boolean success = false;
626                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
627                     success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 2.0d, 1.0d);
628                 }
629                 assertEquals(success, true, "weakCompareAndSet double");
630                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
631                 assertEquals(x, 1.0d, "weakCompareAndSet double");
632             }
633 
634             // Compare set and get
635             {
636                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
637 
638                 double o = (double) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2.0d);
639                 assertEquals(o, 1.0d, "getAndSet double");
640                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
641                 assertEquals(x, 2.0d, "getAndSet double value");
642             }
643 
644             {
645                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
646 
647                 double o = (double) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 2.0d);
648                 assertEquals(o, 1.0d, "getAndSetAcquire double");
649                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
650                 assertEquals(x, 2.0d, "getAndSetAcquire double value");
651             }
652 
653             {
654                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
655 
656                 double o = (double) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 2.0d);
657                 assertEquals(o, 1.0d, "getAndSetRelease double");
658                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
659                 assertEquals(x, 2.0d, "getAndSetRelease double value");
660             }
661 
662             // get and add, add and get
663             {
664                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
665 
666                 double o = (double) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 2.0d);
667                 assertEquals(o, 1.0d, "getAndAdd double");
668                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
669                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
670             }
671 
672             {
673                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
674 
675                 double o = (double) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 2.0d);
676                 assertEquals(o, 1.0d, "getAndAddAcquire double");
677                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
678                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
679             }
680 
681             {
682                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
683 
684                 double o = (double) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 2.0d);
685                 assertEquals(o, 1.0d, "getAndAddRelease double");
686                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
687                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
688             }
689 
690         }
691     }
692 
testArrayUnsupported(Handles hs)693     static void testArrayUnsupported(Handles hs) throws Throwable {
694         double[] array = new double[10];
695 
696         final int i = 0;
697 
698 
699         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
700             checkUOE(am, () -> {
701                 double o = (double) hs.get(am).invokeExact(array, i, 1.0d);
702             });
703         }
704     }
705 
testArrayIndexOutOfBounds(Handles hs)706     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
707         double[] array = new double[10];
708 
709         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
710             final int ci = i;
711 
712             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
713                 checkIOOBE(am, () -> {
714                     double x = (double) hs.get(am).invokeExact(array, ci);
715                 });
716             }
717 
718             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
719                 checkIOOBE(am, () -> {
720                     hs.get(am).invokeExact(array, ci, 1.0d);
721                 });
722             }
723 
724             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
725                 checkIOOBE(am, () -> {
726                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1.0d, 2.0d);
727                 });
728             }
729 
730             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
731                 checkIOOBE(am, () -> {
732                     double r = (double) hs.get(am).invokeExact(array, ci, 2.0d, 1.0d);
733                 });
734             }
735 
736             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
737                 checkIOOBE(am, () -> {
738                     double o = (double) hs.get(am).invokeExact(array, ci, 1.0d);
739                 });
740             }
741 
742             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
743                 checkIOOBE(am, () -> {
744                     double o = (double) hs.get(am).invokeExact(array, ci, 3.0d);
745                 });
746             }
747 
748         }
749     }
750 }
751 
752