< prev index next >

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


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

 356             assertEquals(o, 0x01234567, "getAndBitwiseXorAcquire int");
 357             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 358             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorAcquire int value");
 359         }
 360 
 361         {
 362             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 363 
 364             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, 0x89ABCDEF);
 365             assertEquals(o, 0x01234567, "getAndBitwiseXorRelease int");
 366             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 367             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorRelease int value");
 368         }
 369     }
 370 
 371     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessInt recv, Handles hs) throws Throwable {
 372 
 373 
 374     }
 375 


















 376 
 377     static void testStaticField(Handles hs) throws Throwable {
 378         // Plain
 379         {
 380             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 381             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 382             assertEquals(x, 0x01234567, "set int value");
 383         }
 384 
 385 
 386         // Volatile
 387         {
 388             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(0x89ABCDEF);
 389             int x = (int) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 390             assertEquals(x, 0x89ABCDEF, "setVolatile int value");
 391         }
 392 
 393         // Lazy
 394         {
 395             hs.get(TestAccessMode.SET_RELEASE).invokeExact(0x01234567);

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

 370             assertEquals(o, 0x01234567, "getAndBitwiseXorAcquire int");
 371             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 372             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorAcquire int value");
 373         }
 374 
 375         {
 376             hs.get(TestAccessMode.SET).invokeExact(recv, 0x01234567);
 377 
 378             int o = (int) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, 0x89ABCDEF);
 379             assertEquals(o, 0x01234567, "getAndBitwiseXorRelease int");
 380             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 381             assertEquals(x, (int)(0x01234567 ^ 0x89ABCDEF), "getAndBitwiseXorRelease int value");
 382         }
 383     }
 384 
 385     static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessInt recv, Handles hs) throws Throwable {
 386 
 387 
 388     }
 389 
 390     static void testValueTypeField(Value recv, Handles hs) throws Throwable {
 391         // Plain
 392         {
 393             int x = (int) hs.get(TestAccessMode.GET).invokeExact(recv);
 394             assertEquals(x, 0x01234567, "get int value");
 395         }
 396     }
 397 
 398     static void testValueTypeFieldUnsupported(Value recv, Handles hs) throws Throwable {
 399         // Plain
 400         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 401             checkUOE(am, () -> {
 402                 hs.get(am).invokeExact(recv, 0x01234567);
 403             });
 404         }
 405 
 406 
 407     }
 408 
 409     static void testStaticField(Handles hs) throws Throwable {
 410         // Plain
 411         {
 412             hs.get(TestAccessMode.SET).invokeExact(0x01234567);
 413             int x = (int) hs.get(TestAccessMode.GET).invokeExact();
 414             assertEquals(x, 0x01234567, "set int value");
 415         }
 416 
 417 
 418         // Volatile
 419         {
 420             hs.get(TestAccessMode.SET_VOLATILE).invokeExact(0x89ABCDEF);
 421             int x = (int) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
 422             assertEquals(x, 0x89ABCDEF, "setVolatile int value");
 423         }
 424 
 425         // Lazy
 426         {
 427             hs.get(TestAccessMode.SET_RELEASE).invokeExact(0x01234567);
< prev index next >