< prev index next >

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

Print this page


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


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























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


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


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


< prev index next >