1 /*
  2  * Copyright (c) 2015, 2024, 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 // -- This file was mechanically generated: Do not edit! -- //
 25 
 26 /*
 27  * @test
 28  * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
 29  *          to hit compilation thresholds
 30  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 VarHandleTestMethodHandleAccessDouble
 31  */
 32 
 33 import org.testng.annotations.BeforeClass;
 34 import org.testng.annotations.DataProvider;
 35 import org.testng.annotations.Test;
 36 
 37 import java.lang.invoke.MethodHandle;
 38 import java.lang.invoke.MethodHandles;
 39 import java.lang.invoke.VarHandle;
 40 import java.util.ArrayList;
 41 import java.util.Arrays;
 42 import java.util.List;
 43 
 44 import static org.testng.Assert.*;
 45 
 46 public class VarHandleTestMethodHandleAccessDouble extends VarHandleBaseTest {
 47     static final double static_final_v = 1.0d;
 48 
 49     static double static_v;
 50 
 51     final double final_v = 1.0d;
 52 
 53     double v;
 54 
 55     VarHandle vhFinalField;
 56 
 57     VarHandle vhField;
 58 
 59     VarHandle vhStaticField;
 60 
 61     VarHandle vhStaticFinalField;
 62 
 63     VarHandle vhArray;
 64 
 65     @BeforeClass
 66     public void setup() throws Exception {
 67         vhFinalField = MethodHandles.lookup().findVarHandle(
 68                 VarHandleTestMethodHandleAccessDouble.class, "final_v", double.class);
 69 
 70         vhField = MethodHandles.lookup().findVarHandle(
 71                 VarHandleTestMethodHandleAccessDouble.class, "v", double.class);
 72 
 73         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 74             VarHandleTestMethodHandleAccessDouble.class, "static_final_v", double.class);
 75 
 76         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 77             VarHandleTestMethodHandleAccessDouble.class, "static_v", double.class);
 78 
 79         vhArray = MethodHandles.arrayElementVarHandle(double[].class);
 80     }
 81 
 82 
 83     @DataProvider
 84     public Object[][] accessTestCaseProvider() throws Exception {
 85         List<AccessTestCase<?>> cases = new ArrayList<>();
 86 
 87         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
 88             cases.add(new MethodHandleAccessTestCase("Instance field",
 89                                                      vhField, f, hs -> testInstanceField(this, hs)));
 90             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
 91                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
 92                                                      false));
 93 
 94             cases.add(new MethodHandleAccessTestCase("Static field",
 95                                                      vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticField));
 96             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
 97                                                      vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticFieldUnsupported,
 98                                                      false));
 99 
100             cases.add(new MethodHandleAccessTestCase("Array",
101                                                      vhArray, f, VarHandleTestMethodHandleAccessDouble::testArray));
102             cases.add(new MethodHandleAccessTestCase("Array unsupported",
103                                                      vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayUnsupported,
104                                                      false));
105             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
106                                                      vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayIndexOutOfBounds,
107                                                      false));
108         }
109 
110         // Work around issue with jtreg summary reporting which truncates
111         // the String result of Object.toString to 30 characters, hence
112         // the first dummy argument
113         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
114     }
115 
116     @Test(dataProvider = "accessTestCaseProvider")
117     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
118         T t = atc.get();
119         int iters = atc.requiresLoop() ? ITERS : 1;
120         for (int c = 0; c < iters; c++) {
121             atc.testAccess(t);
122         }
123     }
124 
125 
126     static void testInstanceField(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable {
127         // Plain
128         {
129             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
130             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
131             assertEquals(x, 1.0d, "set double value");
132         }
133 
134 
135         // Volatile
136         {
137             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0d);
138             double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
139             assertEquals(x, 2.0d, "setVolatile double value");
140         }
141 
142         // Lazy
143         {
144             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0d);
145             double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
146             assertEquals(x, 1.0d, "setRelease double value");
147         }
148 
149         // Opaque
150         {
151             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0d);
152             double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
153             assertEquals(x, 2.0d, "setOpaque double value");
154         }
155 
156         hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
157 
158         // Compare
159         {
160             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0d, 2.0d);
161             assertEquals(r, true, "success compareAndSet double");
162             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
163             assertEquals(x, 2.0d, "success compareAndSet double value");
164         }
165 
166         {
167             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0d, 3.0d);
168             assertEquals(r, false, "failing compareAndSet double");
169             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
170             assertEquals(x, 2.0d, "failing compareAndSet double value");
171         }
172 
173         {
174             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0d, 1.0d);
175             assertEquals(r, 2.0d, "success compareAndExchange double");
176             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
177             assertEquals(x, 1.0d, "success compareAndExchange double value");
178         }
179 
180         {
181             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0d, 3.0d);
182             assertEquals(r, 1.0d, "failing compareAndExchange double");
183             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
184             assertEquals(x, 1.0d, "failing compareAndExchange double value");
185         }
186 
187         {
188             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0d, 2.0d);
189             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
190             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
191             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
192         }
193 
194         {
195             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0d, 3.0d);
196             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
197             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
198             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
199         }
200 
201         {
202             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0d, 1.0d);
203             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
204             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
205             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
206         }
207 
208         {
209             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0d, 3.0d);
210             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
211             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
212             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
213         }
214 
215         {
216             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
217             boolean success = false;
218             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
219                 success = (boolean) mh.invokeExact(recv, 1.0d, 2.0d);
220                 if (!success) weakDelay();
221             }
222             assertEquals(success, true, "success weakCompareAndSetPlain double");
223             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
224             assertEquals(x, 2.0d, "success weakCompareAndSetPlain double value");
225         }
226 
227         {
228             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 1.0d, 3.0d);
229             assertEquals(success, false, "failing weakCompareAndSetPlain double");
230             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
231             assertEquals(x, 2.0d, "failing weakCompareAndSetPlain double value");
232         }
233 
234         {
235             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
236             boolean success = false;
237             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
238                 success = (boolean) mh.invokeExact(recv, 2.0d, 1.0d);
239                 if (!success) weakDelay();
240             }
241             assertEquals(success, true, "success weakCompareAndSetAcquire double");
242             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
243             assertEquals(x, 1.0d, "success weakCompareAndSetAcquire double");
244         }
245 
246         {
247             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2.0d, 3.0d);
248             assertEquals(success, false, "failing weakCompareAndSetAcquire double");
249             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
250             assertEquals(x, 1.0d, "failing weakCompareAndSetAcquire double value");
251         }
252 
253         {
254             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
255             boolean success = false;
256             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
257                 success = (boolean) mh.invokeExact(recv, 1.0d, 2.0d);
258                 if (!success) weakDelay();
259             }
260             assertEquals(success, true, "success weakCompareAndSetRelease double");
261             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
262             assertEquals(x, 2.0d, "success weakCompareAndSetRelease double");
263         }
264 
265         {
266             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1.0d, 3.0d);
267             assertEquals(success, false, "failing weakCompareAndSetRelease double");
268             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
269             assertEquals(x, 2.0d, "failing weakCompareAndSetRelease double value");
270         }
271 
272         {
273             boolean success = false;
274             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
275             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
276                 success = (boolean) mh.invokeExact(recv, 2.0d, 1.0d);
277                 if (!success) weakDelay();
278             }
279             assertEquals(success, true, "success weakCompareAndSet double");
280             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
281             assertEquals(x, 1.0d, "success weakCompareAndSet double");
282         }
283 
284         {
285             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 2.0d, 3.0d);
286             assertEquals(success, false, "failing weakCompareAndSet double");
287             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
288             assertEquals(x, 1.0d, "failing weakCompareAndSet double value");
289         }
290 
291         // Compare set and get
292         {
293             double o = (double) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2.0d);
294             assertEquals(o, 1.0d, "getAndSet double");
295             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
296             assertEquals(x, 2.0d, "getAndSet double value");
297         }
298 
299         // get and add, add and get
300         {
301             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
302 
303             double o = (double) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 2.0d);
304             assertEquals(o, 1.0d, "getAndAdd double");
305             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
306             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
307         }
308 
309         {
310             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
311 
312             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 2.0d);
313             assertEquals(o, 1.0d, "getAndAddAcquire double");
314             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
315             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
316         }
317 
318         {
319             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
320 
321             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0d);
322             assertEquals(o, 1.0d, "getAndAddRelease double");
323             double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
324             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
325         }
326 
327     }
328 
329     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable {
330 
331 
332         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
333             checkUOE(am, () -> {
334                 double r = (double) hs.get(am).invokeExact(recv, 1.0d);
335             });
336         }
337     }
338 
339 
340     static void testStaticField(Handles hs) throws Throwable {
341         // Plain
342         {
343             hs.get(TestAccessMode.SET).invokeExact(1.0d);
344             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
345             assertEquals(x, 1.0d, "set double value");
346         }
347 
348 
349         // Volatile
350         {
351             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0d);
352             double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
353             assertEquals(x, 2.0d, "setVolatile double value");
354         }
355 
356         // Lazy
357         {
358             hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0d);
359             double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
360             assertEquals(x, 1.0d, "setRelease double value");
361         }
362 
363         // Opaque
364         {
365             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0d);
366             double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
367             assertEquals(x, 2.0d, "setOpaque double value");
368         }
369 
370         hs.get(TestAccessMode.SET).invokeExact(1.0d);
371 
372         // Compare
373         {
374             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0d, 2.0d);
375             assertEquals(r, true, "success compareAndSet double");
376             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
377             assertEquals(x, 2.0d, "success compareAndSet double value");
378         }
379 
380         {
381             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0d, 3.0d);
382             assertEquals(r, false, "failing compareAndSet double");
383             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
384             assertEquals(x, 2.0d, "failing compareAndSet double value");
385         }
386 
387         {
388             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0d, 1.0d);
389             assertEquals(r, 2.0d, "success compareAndExchange double");
390             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
391             assertEquals(x, 1.0d, "success compareAndExchange double value");
392         }
393 
394         {
395             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0d, 3.0d);
396             assertEquals(r, 1.0d, "failing compareAndExchange double");
397             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
398             assertEquals(x, 1.0d, "failing compareAndExchange double value");
399         }
400 
401         {
402             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0d, 2.0d);
403             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
404             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
405             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
406         }
407 
408         {
409             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0d, 3.0d);
410             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
411             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
412             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
413         }
414 
415         {
416             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0d, 1.0d);
417             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
418             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
419             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
420         }
421 
422         {
423             double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0d, 3.0d);
424             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
425             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
426             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
427         }
428 
429         {
430             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
431             boolean success = false;
432             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
433                 success = (boolean) mh.invokeExact(1.0d, 2.0d);
434                 if (!success) weakDelay();
435             }
436             assertEquals(success, true, "success weakCompareAndSetPlain double");
437             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
438             assertEquals(x, 2.0d, "success weakCompareAndSetPlain double value");
439         }
440 
441         {
442             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(1.0d, 3.0d);
443             assertEquals(success, false, "failing weakCompareAndSetPlain double");
444             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
445             assertEquals(x, 2.0d, "failing weakCompareAndSetPlain double value");
446         }
447 
448         {
449             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
450             boolean success = false;
451             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
452                 success = (boolean) mh.invokeExact(2.0d, 1.0d);
453                 if (!success) weakDelay();
454             }
455             assertEquals(success, true, "success weakCompareAndSetAcquire double");
456             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
457             assertEquals(x, 1.0d, "success weakCompareAndSetAcquire double");
458         }
459 
460         {
461             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
462             boolean success = (boolean) mh.invokeExact(2.0d, 3.0d);
463             assertEquals(success, false, "failing weakCompareAndSetAcquire double");
464             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
465             assertEquals(x, 1.0d, "failing weakCompareAndSetAcquire double value");
466         }
467 
468         {
469             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
470             boolean success = false;
471             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
472                 success = (boolean) mh.invokeExact(1.0d, 2.0d);
473                 if (!success) weakDelay();
474             }
475             assertEquals(success, true, "success weakCompareAndSetRelease double");
476             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
477             assertEquals(x, 2.0d, "success weakCompareAndSetRelease double");
478         }
479 
480         {
481             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1.0d, 3.0d);
482             assertEquals(success, false, "failing weakCompareAndSetRelease double");
483             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
484             assertEquals(x, 2.0d, "failing weakCompareAndSetRelease double value");
485         }
486 
487         {
488             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
489             boolean success = false;
490             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
491                 success = (boolean) mh.invokeExact(2.0d, 1.0d);
492                 if (!success) weakDelay();
493             }
494             assertEquals(success, true, "success weakCompareAndSet double");
495             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
496             assertEquals(x, 1.0d, "success weakCompareAndSet double");
497         }
498 
499         {
500             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(2.0d, 3.0d);
501             assertEquals(success, false, "failing weakCompareAndSet double");
502             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
503             assertEquals(x, 1.0d, "failing weakCompareAndSetRe double value");
504         }
505 
506         // Compare set and get
507         {
508             hs.get(TestAccessMode.SET).invokeExact(1.0d);
509 
510             double o = (double) hs.get(TestAccessMode.GET_AND_SET).invokeExact(2.0d);
511             assertEquals(o, 1.0d, "getAndSet double");
512             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
513             assertEquals(x, 2.0d, "getAndSet double value");
514         }
515 
516         // Compare set and get
517         {
518             hs.get(TestAccessMode.SET).invokeExact(1.0d);
519 
520             double o = (double) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(2.0d);
521             assertEquals(o, 1.0d, "getAndSetAcquire double");
522             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
523             assertEquals(x, 2.0d, "getAndSetAcquire double value");
524         }
525 
526         // Compare set and get
527         {
528             hs.get(TestAccessMode.SET).invokeExact(1.0d);
529 
530             double o = (double) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(2.0d);
531             assertEquals(o, 1.0d, "getAndSetRelease double");
532             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
533             assertEquals(x, 2.0d, "getAndSetRelease double value");
534         }
535 
536         // get and add, add and get
537         {
538             hs.get(TestAccessMode.SET).invokeExact(1.0d);
539 
540             double o = (double) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(2.0d);
541             assertEquals(o, 1.0d, "getAndAdd double");
542             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
543             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
544         }
545 
546         {
547             hs.get(TestAccessMode.SET).invokeExact(1.0d);
548 
549             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(2.0d);
550             assertEquals(o, 1.0d, "getAndAddAcquire double");
551             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
552             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
553         }
554 
555         {
556             hs.get(TestAccessMode.SET).invokeExact(1.0d);
557 
558             double o = (double) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(2.0d);
559             assertEquals(o, 1.0d, "getAndAddRelease double");
560             double x = (double) hs.get(TestAccessMode.GET).invokeExact();
561             assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
562         }
563 
564     }
565 
566     static void testStaticFieldUnsupported(Handles hs) throws Throwable {
567 
568 
569         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
570             checkUOE(am, () -> {
571                 double r = (double) hs.get(am).invokeExact(1.0d);
572             });
573         }
574     }
575 
576 
577     static void testArray(Handles hs) throws Throwable {
578         double[] array = new double[10];
579 
580         for (int i = 0; i < array.length; i++) {
581             // Plain
582             {
583                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
584                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
585                 assertEquals(x, 1.0d, "get double value");
586             }
587 
588 
589             // Volatile
590             {
591                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0d);
592                 double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
593                 assertEquals(x, 2.0d, "setVolatile double value");
594             }
595 
596             // Lazy
597             {
598                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0d);
599                 double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
600                 assertEquals(x, 1.0d, "setRelease double value");
601             }
602 
603             // Opaque
604             {
605                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0d);
606                 double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
607                 assertEquals(x, 2.0d, "setOpaque double value");
608             }
609 
610             hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
611 
612             // Compare
613             {
614                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0d, 2.0d);
615                 assertEquals(r, true, "success compareAndSet double");
616                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
617                 assertEquals(x, 2.0d, "success compareAndSet double value");
618             }
619 
620             {
621                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0d, 3.0d);
622                 assertEquals(r, false, "failing compareAndSet double");
623                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
624                 assertEquals(x, 2.0d, "failing compareAndSet double value");
625             }
626 
627             {
628                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0d, 1.0d);
629                 assertEquals(r, 2.0d, "success compareAndExchange double");
630                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
631                 assertEquals(x, 1.0d, "success compareAndExchange double value");
632             }
633 
634             {
635                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0d, 3.0d);
636                 assertEquals(r, 1.0d, "failing compareAndExchange double");
637                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
638                 assertEquals(x, 1.0d, "failing compareAndExchange double value");
639             }
640 
641             {
642                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0d, 2.0d);
643                 assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
644                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
645                 assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
646             }
647 
648             {
649                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0d, 3.0d);
650                 assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
651                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
652                 assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
653             }
654 
655             {
656                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0d, 1.0d);
657                 assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
658                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
659                 assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
660             }
661 
662             {
663                 double r = (double) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0d, 3.0d);
664                 assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
665                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
666                 assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
667             }
668 
669             {
670                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN);
671                 boolean success = false;
672                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
673                     success = (boolean) mh.invokeExact(array, i, 1.0d, 2.0d);
674                     if (!success) weakDelay();
675                 }
676                 assertEquals(success, true, "success weakCompareAndSetPlain double");
677                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
678                 assertEquals(x, 2.0d, "success weakCompareAndSetPlain double value");
679             }
680 
681             {
682                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 1.0d, 3.0d);
683                 assertEquals(success, false, "failing weakCompareAndSetPlain double");
684                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
685                 assertEquals(x, 2.0d, "failing weakCompareAndSetPlain double value");
686             }
687 
688             {
689                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
690                 boolean success = false;
691                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
692                     success = (boolean) mh.invokeExact(array, i, 2.0d, 1.0d);
693                     if (!success) weakDelay();
694                 }
695                 assertEquals(success, true, "success weakCompareAndSetAcquire double");
696                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
697                 assertEquals(x, 1.0d, "success weakCompareAndSetAcquire double");
698             }
699 
700             {
701                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2.0d, 3.0d);
702                 assertEquals(success, false, "failing weakCompareAndSetAcquire double");
703                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
704                 assertEquals(x, 1.0d, "failing weakCompareAndSetAcquire double value");
705             }
706 
707             {
708                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE);
709                 boolean success = false;
710                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
711                     success = (boolean) mh.invokeExact(array, i, 1.0d, 2.0d);
712                     if (!success) weakDelay();
713                 }
714                 assertEquals(success, true, "success weakCompareAndSetRelease double");
715                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
716                 assertEquals(x, 2.0d, "success weakCompareAndSetRelease double");
717             }
718 
719             {
720                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 1.0d, 3.0d);
721                 assertEquals(success, false, "failing weakCompareAndSetAcquire double");
722                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
723                 assertEquals(x, 2.0d, "failing weakCompareAndSetAcquire double value");
724             }
725 
726             {
727                 MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET);
728                 boolean success = false;
729                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
730                     success = (boolean) mh.invokeExact(array, i, 2.0d, 1.0d);
731                     if (!success) weakDelay();
732                 }
733                 assertEquals(success, true, "success weakCompareAndSet double");
734                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
735                 assertEquals(x, 1.0d, "success weakCompareAndSet double");
736             }
737 
738             {
739                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 2.0d, 3.0d);
740                 assertEquals(success, false, "failing weakCompareAndSet double");
741                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
742                 assertEquals(x, 1.0d, "failing weakCompareAndSet double value");
743             }
744 
745             // Compare set and get
746             {
747                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
748 
749                 double o = (double) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2.0d);
750                 assertEquals(o, 1.0d, "getAndSet double");
751                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
752                 assertEquals(x, 2.0d, "getAndSet double value");
753             }
754 
755             {
756                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
757 
758                 double o = (double) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 2.0d);
759                 assertEquals(o, 1.0d, "getAndSetAcquire double");
760                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
761                 assertEquals(x, 2.0d, "getAndSetAcquire double value");
762             }
763 
764             {
765                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
766 
767                 double o = (double) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 2.0d);
768                 assertEquals(o, 1.0d, "getAndSetRelease double");
769                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
770                 assertEquals(x, 2.0d, "getAndSetRelease double value");
771             }
772 
773             // get and add, add and get
774             {
775                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
776 
777                 double o = (double) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 2.0d);
778                 assertEquals(o, 1.0d, "getAndAdd double");
779                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
780                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double value");
781             }
782 
783             {
784                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
785 
786                 double o = (double) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 2.0d);
787                 assertEquals(o, 1.0d, "getAndAddAcquire double");
788                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
789                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAddAcquire double value");
790             }
791 
792             {
793                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
794 
795                 double o = (double) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 2.0d);
796                 assertEquals(o, 1.0d, "getAndAddRelease double");
797                 double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
798                 assertEquals(x, (double)(1.0d + 2.0d), "getAndAddRelease double value");
799             }
800 
801         }
802     }
803 
804     static void testArrayUnsupported(Handles hs) throws Throwable {
805         double[] array = new double[10];
806 
807         final int i = 0;
808 
809 
810         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
811             checkUOE(am, () -> {
812                 double o = (double) hs.get(am).invokeExact(array, i, 1.0d);
813             });
814         }
815     }
816 
817     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
818         double[] array = new double[10];
819 
820         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
821             final int ci = i;
822 
823             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
824                 checkAIOOBE(am, () -> {
825                     double x = (double) hs.get(am).invokeExact(array, ci);
826                 });
827             }
828 
829             for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
830                 checkAIOOBE(am, () -> {
831                     hs.get(am).invokeExact(array, ci, 1.0d);
832                 });
833             }
834 
835             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
836                 checkAIOOBE(am, () -> {
837                     boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1.0d, 2.0d);
838                 });
839             }
840 
841             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
842                 checkAIOOBE(am, () -> {
843                     double r = (double) hs.get(am).invokeExact(array, ci, 2.0d, 1.0d);
844                 });
845             }
846 
847             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
848                 checkAIOOBE(am, () -> {
849                     double o = (double) hs.get(am).invokeExact(array, ci, 1.0d);
850                 });
851             }
852 
853             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
854                 checkAIOOBE(am, () -> {
855                     double o = (double) hs.get(am).invokeExact(array, ci, 3.0d);
856                 });
857             }
858 
859         }
860     }
861 }
862