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