< prev index next >

test/jdk/java/lang/invoke/VarHandles/VarHandleTestMethodHandleAccessBoolean.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 VarHandleTestMethodHandleAccessBoolean
 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 VarHandleTestMethodHandleAccessBoolean extends VarHandleBaseTest {


 42     static final boolean static_final_v = true;
 43 
 44     static boolean static_v;
 45 
 46     final boolean final_v = true;
 47 
 48     boolean 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                 VarHandleTestMethodHandleAccessBoolean.class, "final_v", boolean.class);
 64 
 65         vhField = MethodHandles.lookup().findVarHandle(
 66                 VarHandleTestMethodHandleAccessBoolean.class, "v", boolean.class);
 67 
 68         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 69             VarHandleTestMethodHandleAccessBoolean.class, "static_final_v", boolean.class);
 70 
 71         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 72             VarHandleTestMethodHandleAccessBoolean.class, "static_v", boolean.class);
 73 
 74         vhArray = MethodHandles.arrayElementVarHandle(boolean[].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, VarHandleTestMethodHandleAccessBoolean::testStaticField));
 91             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
 92                                                      vhStaticField, f, VarHandleTestMethodHandleAccessBoolean::testStaticFieldUnsupported,
 93                                                      false));
 94 
 95             cases.add(new MethodHandleAccessTestCase("Array",
 96                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArray));
 97             cases.add(new MethodHandleAccessTestCase("Array unsupported",
 98                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArrayUnsupported,
 99                                                      false));
100             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
101                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::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(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable {
122         // Plain

334         {
335             hs.get(TestAccessMode.SET).invokeExact(recv, true);
336 
337             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, false);
338             assertEquals(o, true, "getAndBitwiseXorRelease boolean");
339             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
340             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value");
341         }
342     }
343 
344     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable {
345 
346         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
347             checkUOE(am, () -> {
348                 boolean r = (boolean) hs.get(am).invokeExact(recv, true);
349             });
350         }
351 
352     }
353 























354 
355     static void testStaticField(Handles hs) throws Throwable {
356         // Plain
357         {
358             hs.get(TestAccessMode.SET).invokeExact(true);
359             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
360             assertEquals(x, true, "set boolean value");
361         }
362 
363 
364         // Volatile
365         {
366             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(false);
367             boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
368             assertEquals(x, false, "setVolatile boolean value");
369         }
370 
371         // Lazy
372         {
373             hs.get(TestAccessMode.SET_RELEASE).invokeExact(true);

  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 VarHandleTestMethodHandleAccessBoolean
 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 VarHandleTestMethodHandleAccessBoolean extends VarHandleBaseTest {
 44     static final Class<?> type = boolean.class;
 45 
 46     static final boolean static_final_v = true;
 47 
 48     static boolean static_v;
 49 
 50     final boolean final_v = true;
 51 
 52     boolean 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                 VarHandleTestMethodHandleAccessBoolean.class, "final_v", type);
 70 
 71         vhField = MethodHandles.lookup().findVarHandle(
 72                 VarHandleTestMethodHandleAccessBoolean.class, "v", type);
 73 
 74         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 75             VarHandleTestMethodHandleAccessBoolean.class, "static_final_v", type);
 76 
 77         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 78             VarHandleTestMethodHandleAccessBoolean.class, "static_v", type);
 79 
 80         vhArray = MethodHandles.arrayElementVarHandle(boolean[].class);
 81 
 82         vhValueTypeField = MethodHandles.lookup().findVarHandle(
 83                     Value.class, "boolean_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, VarHandleTestMethodHandleAccessBoolean::testStaticField));
100             cases.add(new MethodHandleAccessTestCase("Static field unsupported",
101                                                      vhStaticField, f, VarHandleTestMethodHandleAccessBoolean::testStaticFieldUnsupported,
102                                                      false));
103 
104             cases.add(new MethodHandleAccessTestCase("Array",
105                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArray));
106             cases.add(new MethodHandleAccessTestCase("Array unsupported",
107                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::testArrayUnsupported,
108                                                      false));
109             cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
110                                                      vhArray, f, VarHandleTestMethodHandleAccessBoolean::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(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable {
136         // Plain

348         {
349             hs.get(TestAccessMode.SET).invokeExact(recv, true);
350 
351             boolean o = (boolean) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, false);
352             assertEquals(o, true, "getAndBitwiseXorRelease boolean");
353             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
354             assertEquals(x, (boolean)(true ^ false), "getAndBitwiseXorRelease boolean value");
355         }
356     }
357 
358     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessBoolean recv, Handles hs) throws Throwable {
359 
360         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
361             checkUOE(am, () -> {
362                 boolean r = (boolean) hs.get(am).invokeExact(recv, true);
363             });
364         }
365 
366     }
367 
368     static void testValueTypeField(Value recv, Handles hs) throws Throwable {
369         // Plain
370         {
371             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact(recv);
372             assertEquals(x, true, "get boolean value");
373         }
374     }
375 
376     static void testValueTypeFieldUnsupported(Value recv, Handles hs) throws Throwable {
377         // Plain
378         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
379             checkUOE(am, () -> {
380                 hs.get(am).invokeExact(recv, true);
381             });
382         }
383 
384         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
385             checkUOE(am, () -> {
386                 boolean r = (boolean) hs.get(am).invokeExact(recv, true);
387             });
388         }
389 
390     }
391 
392     static void testStaticField(Handles hs) throws Throwable {
393         // Plain
394         {
395             hs.get(TestAccessMode.SET).invokeExact(true);
396             boolean x = (boolean) hs.get(TestAccessMode.GET).invokeExact();
397             assertEquals(x, true, "set boolean value");
398         }
399 
400 
401         // Volatile
402         {
403             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(false);
404             boolean x = (boolean) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
405             assertEquals(x, false, "setVolatile boolean value");
406         }
407 
408         // Lazy
409         {
410             hs.get(TestAccessMode.SET_RELEASE).invokeExact(true);
< prev index next >