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 VarHandleTestMethodHandleAccessFloat
 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 VarHandleTestMethodHandleAccessFloat extends VarHandleBaseTest {
 47     static final float static_final_v = 1.0f;
 48 
 49     static float static_v;
 50 
 51     final float final_v = 1.0f;
 52 
 53     float 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                 VarHandleTestMethodHandleAccessFloat.class, "final_v", float.class);
 69 
 70         vhField = MethodHandles.lookup().findVarHandle(
 71                 VarHandleTestMethodHandleAccessFloat.class, "v", float.class);
 72 
 73         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 74             VarHandleTestMethodHandleAccessFloat.class, "static_final_v", float.class);
 75 
 76         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 77             VarHandleTestMethodHandleAccessFloat.class, "static_v", float.class);
 78 
 79         vhArray = MethodHandles.arrayElementVarHandle(float[].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, VarHandleTestMethodHandleAccessFloat::testStaticField));
 96             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
 97                                                      vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticFieldUnsupported,
 98                                                      false));
 99 
100             cases.add(new MethodHandleAccessTestCase("Array",
101                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArray));
102             cases.add(new MethodHandleAccessTestCase("Array unsupported",
103                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayUnsupported,
104                                                      false));
105             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
106                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::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(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
127         // Plain
128         {
129             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
130             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
131             assertEquals(x, 1.0f, "set float value");
132         }
133 
134 
135         // Volatile
136         {
137             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0f);
138             float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
139             assertEquals(x, 2.0f, "setVolatile float value");
140         }
141 
142         // Lazy
143         {
144             hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0f);
145             float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
146             assertEquals(x, 1.0f, "setRelease float value");
147         }
148 
149         // Opaque
150         {
151             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0f);
152             float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
153             assertEquals(x, 2.0f, "setOpaque float value");
154         }
155 
156         hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
157 
158         // Compare
159         {
160             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 2.0f);
161             assertEquals(r, true, "success compareAndSet float");
162             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
163             assertEquals(x, 2.0f, "success compareAndSet float value");
164         }
165 
166         {
167             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 3.0f);
168             assertEquals(r, false, "failing compareAndSet float");
169             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
170             assertEquals(x, 2.0f, "failing compareAndSet float value");
171         }
172 
173         {
174             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 1.0f);
175             assertEquals(r, 2.0f, "success compareAndExchange float");
176             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
177             assertEquals(x, 1.0f, "success compareAndExchange float value");
178         }
179 
180         {
181             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 3.0f);
182             assertEquals(r, 1.0f, "failing compareAndExchange float");
183             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
184             assertEquals(x, 1.0f, "failing compareAndExchange float value");
185         }
186 
187         {
188             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 2.0f);
189             assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
190             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
191             assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
192         }
193 
194         {
195             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 3.0f);
196             assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
197             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
198             assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
199         }
200 
201         {
202             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 1.0f);
203             assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
204             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
205             assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
206         }
207 
208         {
209             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 3.0f);
210             assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
211             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
212             assertEquals(x, 1.0f, "failing compareAndExchangeRelease float 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.0f, 2.0f);
220                 if (!success) weakDelay();
221             }
222             assertEquals(success, true, "success weakCompareAndSetPlain float");
223             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
224             assertEquals(x, 2.0f, "success weakCompareAndSetPlain float value");
225         }
226 
227         {
228             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 1.0f, 3.0f);
229             assertEquals(success, false, "failing weakCompareAndSetPlain float");
230             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
231             assertEquals(x, 2.0f, "failing weakCompareAndSetPlain float 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.0f, 1.0f);
239                 if (!success) weakDelay();
240             }
241             assertEquals(success, true, "success weakCompareAndSetAcquire float");
242             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
243             assertEquals(x, 1.0f, "success weakCompareAndSetAcquire float");
244         }
245 
246         {
247             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2.0f, 3.0f);
248             assertEquals(success, false, "failing weakCompareAndSetAcquire float");
249             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
250             assertEquals(x, 1.0f, "failing weakCompareAndSetAcquire float 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.0f, 2.0f);
258                 if (!success) weakDelay();
259             }
260             assertEquals(success, true, "success weakCompareAndSetRelease float");
261             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
262             assertEquals(x, 2.0f, "success weakCompareAndSetRelease float");
263         }
264 
265         {
266             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1.0f, 3.0f);
267             assertEquals(success, false, "failing weakCompareAndSetRelease float");
268             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
269             assertEquals(x, 2.0f, "failing weakCompareAndSetRelease float 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.0f, 1.0f);
277                 if (!success) weakDelay();
278             }
279             assertEquals(success, true, "success weakCompareAndSet float");
280             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
281             assertEquals(x, 1.0f, "success weakCompareAndSet float");
282         }
283 
284         {
285             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 2.0f, 3.0f);
286             assertEquals(success, false, "failing weakCompareAndSet float");
287             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
288             assertEquals(x, 1.0f, "failing weakCompareAndSet float value");
289         }
290 
291         // Compare set and get
292         {
293             float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2.0f);
294             assertEquals(o, 1.0f, "getAndSet float");
295             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
296             assertEquals(x, 2.0f, "getAndSet float value");
297         }
298 
299         // get and add, add and get
300         {
301             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
302 
303             float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 2.0f);
304             assertEquals(o, 1.0f, "getAndAdd float");
305             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
306             assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value");
307         }
308 
309         {
310             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
311 
312             float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 2.0f);
313             assertEquals(o, 1.0f, "getAndAddAcquire float");
314             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
315             assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value");
316         }
317 
318         {
319             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
320 
321             float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0f);
322             assertEquals(o, 1.0f, "getAndAddRelease float");
323             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
324             assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value");
325         }
326 
327     }
328 
329     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
330 
331 
332         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
333             checkUOE(am, () -> {
334                 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
335             });
336         }
337     }
338 
339 
340     static void testStaticField(Handles hs) throws Throwable {
341         // Plain
342         {
343             hs.get(TestAccessMode.SET).invokeExact(1.0f);
344             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
345             assertEquals(x, 1.0f, "set float value");
346         }
347 
348 
349         // Volatile
350         {
351             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f);
352             float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
353             assertEquals(x, 2.0f, "setVolatile float value");
354         }
355 
356         // Lazy
357         {
358             hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f);
359             float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
360             assertEquals(x, 1.0f, "setRelease float value");
361         }
362 
363         // Opaque
364         {
365             hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0f);
366             float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
367             assertEquals(x, 2.0f, "setOpaque float value");
368         }
369 
370         hs.get(TestAccessMode.SET).invokeExact(1.0f);
371 
372         // Compare
373         {
374             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 2.0f);
375             assertEquals(r, true, "success compareAndSet float");
376             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
377             assertEquals(x, 2.0f, "success compareAndSet float value");
378         }
379 
380         {
381             boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 3.0f);
382             assertEquals(r, false, "failing compareAndSet float");
383             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
384             assertEquals(x, 2.0f, "failing compareAndSet float value");
385         }
386 
387         {
388             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 1.0f);
389             assertEquals(r, 2.0f, "success compareAndExchange float");
390             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
391             assertEquals(x, 1.0f, "success compareAndExchange float value");
392         }
393 
394         {
395             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 3.0f);
396             assertEquals(r, 1.0f, "failing compareAndExchange float");
397             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
398             assertEquals(x, 1.0f, "failing compareAndExchange float value");
399         }
400 
401         {
402             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 2.0f);
403             assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
404             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
405             assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
406         }
407 
408         {
409             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 3.0f);
410             assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
411             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
412             assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
413         }
414 
415         {
416             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 1.0f);
417             assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
418             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
419             assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
420         }
421 
422         {
423             float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 3.0f);
424             assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
425             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
426             assertEquals(x, 1.0f, "failing compareAndExchangeRelease float 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.0f, 2.0f);
434                 if (!success) weakDelay();
435             }
436             assertEquals(success, true, "success weakCompareAndSetPlain float");
437             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
438             assertEquals(x, 2.0f, "success weakCompareAndSetPlain float value");
439         }
440 
441         {
442             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(1.0f, 3.0f);
443             assertEquals(success, false, "failing weakCompareAndSetPlain float");
444             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
445             assertEquals(x, 2.0f, "failing weakCompareAndSetPlain float 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.0f, 1.0f);
453                 if (!success) weakDelay();
454             }
455             assertEquals(success, true, "success weakCompareAndSetAcquire float");
456             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
457             assertEquals(x, 1.0f, "success weakCompareAndSetAcquire float");
458         }
459 
460         {
461             MethodHandle mh = hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE);
462             boolean success = (boolean) mh.invokeExact(2.0f, 3.0f);
463             assertEquals(success, false, "failing weakCompareAndSetAcquire float");
464             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
465             assertEquals(x, 1.0f, "failing weakCompareAndSetAcquire float 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.0f, 2.0f);
473                 if (!success) weakDelay();
474             }
475             assertEquals(success, true, "success weakCompareAndSetRelease float");
476             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
477             assertEquals(x, 2.0f, "success weakCompareAndSetRelease float");
478         }
479 
480         {
481             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1.0f, 3.0f);
482             assertEquals(success, false, "failing weakCompareAndSetRelease float");
483             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
484             assertEquals(x, 2.0f, "failing weakCompareAndSetRelease float 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.0f, 1.0f);
492                 if (!success) weakDelay();
493             }
494             assertEquals(success, true, "success weakCompareAndSet float");
495             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
496             assertEquals(x, 1.0f, "success weakCompareAndSet float");
497         }
498 
499         {
500             boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(2.0f, 3.0f);
501             assertEquals(success, false, "failing weakCompareAndSet float");
502             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
503             assertEquals(x, 1.0f, "failing weakCompareAndSetRe float value");
504         }
505 
506         // Compare set and get
507         {
508             hs.get(TestAccessMode.SET).invokeExact(1.0f);
509 
510             float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(2.0f);
511             assertEquals(o, 1.0f, "getAndSet float");
512             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
513             assertEquals(x, 2.0f, "getAndSet float value");
514         }
515 
516         // Compare set and get
517         {
518             hs.get(TestAccessMode.SET).invokeExact(1.0f);
519 
520             float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(2.0f);
521             assertEquals(o, 1.0f, "getAndSetAcquire float");
522             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
523             assertEquals(x, 2.0f, "getAndSetAcquire float value");
524         }
525 
526         // Compare set and get
527         {
528             hs.get(TestAccessMode.SET).invokeExact(1.0f);
529 
530             float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(2.0f);
531             assertEquals(o, 1.0f, "getAndSetRelease float");
532             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
533             assertEquals(x, 2.0f, "getAndSetRelease float value");
534         }
535 
536         // get and add, add and get
537         {
538             hs.get(TestAccessMode.SET).invokeExact(1.0f);
539 
540             float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(2.0f);
541             assertEquals(o, 1.0f, "getAndAdd float");
542             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
543             assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value");
544         }
545 
546         {
547             hs.get(TestAccessMode.SET).invokeExact(1.0f);
548 
549             float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(2.0f);
550             assertEquals(o, 1.0f, "getAndAddAcquire float");
551             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
552             assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value");
553         }
554 
555         {
556             hs.get(TestAccessMode.SET).invokeExact(1.0f);
557 
558             float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(2.0f);
559             assertEquals(o, 1.0f, "getAndAddRelease float");
560             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
561             assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float 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                 float r = (float) hs.get(am).invokeExact(1.0f);
572             });
573         }
574     }
575 
576 
577     static void testArray(Handles hs) throws Throwable {
578         float[] array = new float[10];
579 
580         for (int i = 0; i < array.length; i++) {
581             // Plain
582             {
583                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
584                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
585                 assertEquals(x, 1.0f, "get float value");
586             }
587 
588 
589             // Volatile
590             {
591                 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0f);
592                 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
593                 assertEquals(x, 2.0f, "setVolatile float value");
594             }
595 
596             // Lazy
597             {
598                 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0f);
599                 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
600                 assertEquals(x, 1.0f, "setRelease float value");
601             }
602 
603             // Opaque
604             {
605                 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0f);
606                 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
607                 assertEquals(x, 2.0f, "setOpaque float value");
608             }
609 
610             hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
611 
612             // Compare
613             {
614                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 2.0f);
615                 assertEquals(r, true, "success compareAndSet float");
616                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
617                 assertEquals(x, 2.0f, "success compareAndSet float value");
618             }
619 
620             {
621                 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 3.0f);
622                 assertEquals(r, false, "failing compareAndSet float");
623                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
624                 assertEquals(x, 2.0f, "failing compareAndSet float value");
625             }
626 
627             {
628                 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 1.0f);
629                 assertEquals(r, 2.0f, "success compareAndExchange float");
630                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
631                 assertEquals(x, 1.0f, "success compareAndExchange float value");
632             }
633 
634             {
635                 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 3.0f);
636                 assertEquals(r, 1.0f, "failing compareAndExchange float");
637                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
638                 assertEquals(x, 1.0f, "failing compareAndExchange float value");
639             }
640 
641             {
642                 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 2.0f);
643                 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
644                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
645                 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
646             }
647 
648             {
649                 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 3.0f);
650                 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
651                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
652                 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
653             }
654 
655             {
656                 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 1.0f);
657                 assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
658                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
659                 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
660             }
661 
662             {
663                 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 3.0f);
664                 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
665                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
666                 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float 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.0f, 2.0f);
674                     if (!success) weakDelay();
675                 }
676                 assertEquals(success, true, "success weakCompareAndSetPlain float");
677                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
678                 assertEquals(x, 2.0f, "success weakCompareAndSetPlain float value");
679             }
680 
681             {
682                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 1.0f, 3.0f);
683                 assertEquals(success, false, "failing weakCompareAndSetPlain float");
684                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
685                 assertEquals(x, 2.0f, "failing weakCompareAndSetPlain float 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.0f, 1.0f);
693                     if (!success) weakDelay();
694                 }
695                 assertEquals(success, true, "success weakCompareAndSetAcquire float");
696                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
697                 assertEquals(x, 1.0f, "success weakCompareAndSetAcquire float");
698             }
699 
700             {
701                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f, 3.0f);
702                 assertEquals(success, false, "failing weakCompareAndSetAcquire float");
703                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
704                 assertEquals(x, 1.0f, "failing weakCompareAndSetAcquire float 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.0f, 2.0f);
712                     if (!success) weakDelay();
713                 }
714                 assertEquals(success, true, "success weakCompareAndSetRelease float");
715                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
716                 assertEquals(x, 2.0f, "success weakCompareAndSetRelease float");
717             }
718 
719             {
720                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 1.0f, 3.0f);
721                 assertEquals(success, false, "failing weakCompareAndSetAcquire float");
722                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
723                 assertEquals(x, 2.0f, "failing weakCompareAndSetAcquire float 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.0f, 1.0f);
731                     if (!success) weakDelay();
732                 }
733                 assertEquals(success, true, "success weakCompareAndSet float");
734                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
735                 assertEquals(x, 1.0f, "success weakCompareAndSet float");
736             }
737 
738             {
739                 boolean success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 2.0f, 3.0f);
740                 assertEquals(success, false, "failing weakCompareAndSet float");
741                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
742                 assertEquals(x, 1.0f, "failing weakCompareAndSet float value");
743             }
744 
745             // Compare set and get
746             {
747                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
748 
749                 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2.0f);
750                 assertEquals(o, 1.0f, "getAndSet float");
751                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
752                 assertEquals(x, 2.0f, "getAndSet float value");
753             }
754 
755             {
756                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
757 
758                 float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f);
759                 assertEquals(o, 1.0f, "getAndSetAcquire float");
760                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
761                 assertEquals(x, 2.0f, "getAndSetAcquire float value");
762             }
763 
764             {
765                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
766 
767                 float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 2.0f);
768                 assertEquals(o, 1.0f, "getAndSetRelease float");
769                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
770                 assertEquals(x, 2.0f, "getAndSetRelease float value");
771             }
772 
773             // get and add, add and get
774             {
775                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
776 
777                 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 2.0f);
778                 assertEquals(o, 1.0f, "getAndAdd float");
779                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
780                 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value");
781             }
782 
783             {
784                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
785 
786                 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 2.0f);
787                 assertEquals(o, 1.0f, "getAndAddAcquire float");
788                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
789                 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value");
790             }
791 
792             {
793                 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
794 
795                 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 2.0f);
796                 assertEquals(o, 1.0f, "getAndAddRelease float");
797                 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
798                 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value");
799             }
800 
801         }
802     }
803 
804     static void testArrayUnsupported(Handles hs) throws Throwable {
805         float[] array = new float[10];
806 
807         final int i = 0;
808 
809 
810         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
811             checkUOE(am, () -> {
812                 float o = (float) hs.get(am).invokeExact(array, i, 1.0f);
813             });
814         }
815     }
816 
817     static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
818         float[] array = new float[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                     float x = (float) 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.0f);
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.0f, 2.0f);
838                 });
839             }
840 
841             for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
842                 checkAIOOBE(am, () -> {
843                     float r = (float) hs.get(am).invokeExact(array, ci, 2.0f, 1.0f);
844                 });
845             }
846 
847             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
848                 checkAIOOBE(am, () -> {
849                     float o = (float) hs.get(am).invokeExact(array, ci, 1.0f);
850                 });
851             }
852 
853             for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
854                 checkAIOOBE(am, () -> {
855                     float o = (float) hs.get(am).invokeExact(array, ci, 3.0f);
856                 });
857             }
858 
859         }
860     }
861 }
862