< prev index next >

test/jdk/java/lang/invoke/VarHandles/VarHandleTestMethodHandleAccessFloat.java

Print this page

  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=20000 VarHandleTestMethodHandleAccessFloat
 27  */
 28 
 29 import org.testng.annotations.BeforeClass;
 30 import org.testng.annotations.DataProvider;
 31 import org.testng.annotations.Test;
 32 
 33 import java.lang.invoke.MethodHandles;
 34 import java.lang.invoke.VarHandle;
 35 import java.util.ArrayList;
 36 import java.util.Arrays;
 37 import java.util.List;
 38 
 39 import static org.testng.Assert.*;
 40 
 41 public class VarHandleTestMethodHandleAccessFloat extends VarHandleBaseTest {


 42     static final float static_final_v = 1.0f;
 43 
 44     static float static_v;
 45 
 46     final float final_v = 1.0f;
 47 
 48     float v;
 49 
 50     VarHandle vhFinalField;
 51 
 52     VarHandle vhField;
 53 
 54     VarHandle vhStaticField;
 55 
 56     VarHandle vhStaticFinalField;
 57 
 58     VarHandle vhArray;
 59 


 60     @BeforeClass
 61     public void setup() throws Exception {
 62         vhFinalField = MethodHandles.lookup().findVarHandle(
 63                 VarHandleTestMethodHandleAccessFloat.class, "final_v", float.class);
 64 
 65         vhField = MethodHandles.lookup().findVarHandle(
 66                 VarHandleTestMethodHandleAccessFloat.class, "v", float.class);
 67 
 68         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 69             VarHandleTestMethodHandleAccessFloat.class, "static_final_v", float.class);
 70 
 71         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 72             VarHandleTestMethodHandleAccessFloat.class, "static_v", float.class);
 73 
 74         vhArray = MethodHandles.arrayElementVarHandle(float[].class);



 75     }
 76 
 77 
 78     @DataProvider
 79     public Object[][] accessTestCaseProvider() throws Exception {
 80         List<AccessTestCase<?>> cases = new ArrayList<>();
 81 
 82         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
 83             cases.add(new MethodHandleAccessTestCase("Instance field",
 84                                                      vhField, f, hs -> testInstanceField(this, hs)));
 85             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
 86                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
 87                                                      false));
 88 
 89             cases.add(new MethodHandleAccessTestCase("Static field",
 90                                                      vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticField));
 91             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
 92                                                      vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticFieldUnsupported,
 93                                                      false));
 94 
 95             cases.add(new MethodHandleAccessTestCase("Array",
 96                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArray));
 97             cases.add(new MethodHandleAccessTestCase("Array unsupported",
 98                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayUnsupported,
 99                                                      false));
100             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
101                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayIndexOutOfBounds,
102                                                      false));





103         }
104 
105         // Work around issue with jtreg summary reporting which truncates
106         // the String result of Object.toString to 30 characters, hence
107         // the first dummy argument
108         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
109     }
110 
111     @Test(dataProvider = "accessTestCaseProvider")
112     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
113         T t = atc.get();
114         int iters = atc.requiresLoop() ? ITERS : 1;
115         for (int c = 0; c < iters; c++) {
116             atc.testAccess(t);
117         }
118     }
119 
120 
121     static void testInstanceField(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
122         // Plain

278             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
279 
280             float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0f);
281             assertEquals(o, 1.0f, "getAndAddRelease float");
282             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
283             assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value");
284         }
285 
286     }
287 
288     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
289 
290 
291         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
292             checkUOE(am, () -> {
293                 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
294             });
295         }
296     }
297 























298 
299     static void testStaticField(Handles hs) throws Throwable {
300         // Plain
301         {
302             hs.get(TestAccessMode.SET).invokeExact(1.0f);
303             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
304             assertEquals(x, 1.0f, "set float value");
305         }
306 
307 
308         // Volatile
309         {
310             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f);
311             float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
312             assertEquals(x, 2.0f, "setVolatile float value");
313         }
314 
315         // Lazy
316         {
317             hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f);

  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  * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessFloat
 29  */
 30 
 31 import org.testng.annotations.BeforeClass;
 32 import org.testng.annotations.DataProvider;
 33 import org.testng.annotations.Test;
 34 
 35 import java.lang.invoke.MethodHandles;
 36 import java.lang.invoke.VarHandle;
 37 import java.util.ArrayList;
 38 import java.util.Arrays;
 39 import java.util.List;
 40 
 41 import static org.testng.Assert.*;
 42 
 43 public class VarHandleTestMethodHandleAccessFloat extends VarHandleBaseTest {
 44     static final Class<?> type = float.class;
 45 
 46     static final float static_final_v = 1.0f;
 47 
 48     static float static_v;
 49 
 50     final float final_v = 1.0f;
 51 
 52     float v;
 53 
 54     VarHandle vhFinalField;
 55 
 56     VarHandle vhField;
 57 
 58     VarHandle vhStaticField;
 59 
 60     VarHandle vhStaticFinalField;
 61 
 62     VarHandle vhArray;
 63 
 64     VarHandle vhValueTypeField;
 65 
 66     @BeforeClass
 67     public void setup() throws Exception {
 68         vhFinalField = MethodHandles.lookup().findVarHandle(
 69                 VarHandleTestMethodHandleAccessFloat.class, "final_v", type);
 70 
 71         vhField = MethodHandles.lookup().findVarHandle(
 72                 VarHandleTestMethodHandleAccessFloat.class, "v", type);
 73 
 74         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 75             VarHandleTestMethodHandleAccessFloat.class, "static_final_v", type);
 76 
 77         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 78             VarHandleTestMethodHandleAccessFloat.class, "static_v", type);
 79 
 80         vhArray = MethodHandles.arrayElementVarHandle(float[].class);
 81 
 82         vhValueTypeField = MethodHandles.lookup().findVarHandle(
 83                     Value.class, "float_v", type);
 84     }
 85 
 86 
 87     @DataProvider
 88     public Object[][] accessTestCaseProvider() throws Exception {
 89         List<AccessTestCase<?>> cases = new ArrayList<>();
 90 
 91         for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
 92             cases.add(new MethodHandleAccessTestCase("Instance field",
 93                                                      vhField, f, hs -> testInstanceField(this, hs)));
 94             cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
 95                                                      vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
 96                                                      false));
 97 
 98             cases.add(new MethodHandleAccessTestCase("Static field",
 99                                                      vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticField));
100             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
101                                                      vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticFieldUnsupported,
102                                                      false));
103 
104             cases.add(new MethodHandleAccessTestCase("Array",
105                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArray));
106             cases.add(new MethodHandleAccessTestCase("Array unsupported",
107                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayUnsupported,
108                                                      false));
109             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
110                                                      vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayIndexOutOfBounds,
111                                                      false));
112         cases.add(new MethodHandleAccessTestCase("Value type field",
113                                                  vhValueTypeField, f, hs -> testValueTypeField(Value.getInstance(), hs)));
114         cases.add(new MethodHandleAccessTestCase("Value type field unsupported",
115                                                  vhValueTypeField, f, hs -> testValueTypeFieldUnsupported(Value.getInstance(), hs),
116                                                  false));
117         }
118 
119         // Work around issue with jtreg summary reporting which truncates
120         // the String result of Object.toString to 30 characters, hence
121         // the first dummy argument
122         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
123     }
124 
125     @Test(dataProvider = "accessTestCaseProvider")
126     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
127         T t = atc.get();
128         int iters = atc.requiresLoop() ? ITERS : 1;
129         for (int c = 0; c < iters; c++) {
130             atc.testAccess(t);
131         }
132     }
133 
134 
135     static void testInstanceField(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
136         // Plain

292             hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
293 
294             float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0f);
295             assertEquals(o, 1.0f, "getAndAddRelease float");
296             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
297             assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value");
298         }
299 
300     }
301 
302     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
303 
304 
305         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
306             checkUOE(am, () -> {
307                 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
308             });
309         }
310     }
311 
312     static void testValueTypeField(Value recv, Handles hs) throws Throwable {
313         // Plain
314         {
315             float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
316             assertEquals(x, 1.0f, "get float value");
317         }
318     }
319 
320     static void testValueTypeFieldUnsupported(Value recv, Handles hs) throws Throwable {
321         // Plain
322         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
323             checkUOE(am, () -> {
324                 hs.get(am).invokeExact(recv, 1.0f);
325             });
326         }
327 
328 
329         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
330             checkUOE(am, () -> {
331                 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
332             });
333         }
334     }
335 
336     static void testStaticField(Handles hs) throws Throwable {
337         // Plain
338         {
339             hs.get(TestAccessMode.SET).invokeExact(1.0f);
340             float x = (float) hs.get(TestAccessMode.GET).invokeExact();
341             assertEquals(x, 1.0f, "set float value");
342         }
343 
344 
345         // Volatile
346         {
347             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f);
348             float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
349             assertEquals(x, 2.0f, "setVolatile float value");
350         }
351 
352         // Lazy
353         {
354             hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f);
< prev index next >