< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2015, 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                   VarHandleTestAccessLong



  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessLong
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccessLong
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessLong
  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 VarHandleTestAccessLong extends VarHandleBaseTest {
  45     static final long static_final_v = 0x0123456789ABCDEFL;
  46 
  47     static long static_v;
  48 
  49     final long final_v = 0x0123456789ABCDEFL;
  50 
  51     long v;
  52 
  53     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 


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



  78     }
  79 
  80 
  81     @DataProvider
  82     public Object[][] varHandlesProvider() throws Exception {
  83         List<VarHandle> vhs = new ArrayList<>();
  84         vhs.add(vhField);
  85         vhs.add(vhStaticField);
  86         vhs.add(vhArray);
  87 
  88         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
  89     }
  90 
  91     @Test(dataProvider = "varHandlesProvider")
  92     public void testIsAccessModeSupported(VarHandle vh) {
  93         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
  94         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
  95         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
  96         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
  97         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));


 193         cases.add(new VarHandleAccessTestCase("Instance field",
 194                                               vhField, vh -> testInstanceField(this, vh)));
 195         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 196                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 197                                               false));
 198 
 199         cases.add(new VarHandleAccessTestCase("Static field",
 200                                               vhStaticField, VarHandleTestAccessLong::testStaticField));
 201         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 202                                               vhStaticField, VarHandleTestAccessLong::testStaticFieldUnsupported,
 203                                               false));
 204 
 205         cases.add(new VarHandleAccessTestCase("Array",
 206                                               vhArray, VarHandleTestAccessLong::testArray));
 207         cases.add(new VarHandleAccessTestCase("Array unsupported",
 208                                               vhArray, VarHandleTestAccessLong::testArrayUnsupported,
 209                                               false));
 210         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 211                                               vhArray, VarHandleTestAccessLong::testArrayIndexOutOfBounds,
 212                                               false));





 213 
 214         // Work around issue with jtreg summary reporting which truncates
 215         // the String result of Object.toString to 30 characters, hence
 216         // the first dummy argument
 217         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 218     }
 219 
 220     @Test(dataProvider = "accessTestCaseProvider")
 221     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 222         T t = atc.get();
 223         int iters = atc.requiresLoop() ? ITERS : 1;
 224         for (int c = 0; c < iters; c++) {
 225             atc.testAccess(t);
 226         }
 227     }
 228 
 229 
 230 
 231 
 232     static void testInstanceFinalField(VarHandleTestAccessLong recv, VarHandle vh) {


 260         checkUOE(() -> {
 261             vh.set(recv, 0xCAFEBABECAFEBABEL);
 262         });
 263 
 264         checkUOE(() -> {
 265             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 266         });
 267 
 268         checkUOE(() -> {
 269             vh.setRelease(recv, 0xCAFEBABECAFEBABEL);
 270         });
 271 
 272         checkUOE(() -> {
 273             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 274         });
 275 
 276 
 277 
 278     }
 279 













 280 
 281     static void testStaticFinalField(VarHandle vh) {
 282         // Plain
 283         {
 284             long x = (long) vh.get();
 285             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 286         }
 287 
 288 
 289         // Volatile
 290         {
 291             long x = (long) vh.getVolatile();
 292             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 293         }
 294 
 295         // Lazy
 296         {
 297             long x = (long) vh.getAcquire();
 298             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 299         }


   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  * @compile -XDenableValueTypes Value.java
  27  * @run testng/othervm -Xverify:none -Diters=10    -Xint                   VarHandleTestAccessLong
  28  */
  29 /* Disabled temporarily for lworld
  30  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessLong
  31  * @run testng/othervm -Diters=20000                         VarHandleTestAccessLong
  32  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessLong
  33  */
  34 
  35 import org.testng.annotations.BeforeClass;
  36 import org.testng.annotations.DataProvider;
  37 import org.testng.annotations.Test;
  38 
  39 import java.lang.invoke.MethodHandles;
  40 import java.lang.invoke.VarHandle;
  41 import java.util.ArrayList;
  42 import java.util.Arrays;
  43 import java.util.List;
  44 
  45 import static org.testng.Assert.*;
  46 
  47 public class VarHandleTestAccessLong extends VarHandleBaseTest {
  48     static final long static_final_v = 0x0123456789ABCDEFL;
  49 
  50     static long static_v;
  51 
  52     final long final_v = 0x0123456789ABCDEFL;
  53 
  54     long v;
  55 
  56     VarHandle vhFinalField;
  57 
  58     VarHandle vhField;
  59 
  60     VarHandle vhStaticField;
  61 
  62     VarHandle vhStaticFinalField;
  63 
  64     VarHandle vhArray;
  65 
  66     VarHandle vhValueTypeField;
  67 
  68     @BeforeClass
  69     public void setup() throws Exception {
  70         vhFinalField = MethodHandles.lookup().findVarHandle(
  71                 VarHandleTestAccessLong.class, "final_v", long.class);
  72 
  73         vhField = MethodHandles.lookup().findVarHandle(
  74                 VarHandleTestAccessLong.class, "v", long.class);
  75 
  76         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  77             VarHandleTestAccessLong.class, "static_final_v", long.class);
  78 
  79         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  80             VarHandleTestAccessLong.class, "static_v", long.class);
  81 
  82         vhArray = MethodHandles.arrayElementVarHandle(long[].class);
  83 
  84         vhValueTypeField = MethodHandles.lookup().findVarHandle(
  85                     Value.class, "long_v", long.class);
  86     }
  87 
  88 
  89     @DataProvider
  90     public Object[][] varHandlesProvider() throws Exception {
  91         List<VarHandle> vhs = new ArrayList<>();
  92         vhs.add(vhField);
  93         vhs.add(vhStaticField);
  94         vhs.add(vhArray);
  95 
  96         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
  97     }
  98 
  99     @Test(dataProvider = "varHandlesProvider")
 100     public void testIsAccessModeSupported(VarHandle vh) {
 101         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 102         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 103         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 104         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 105         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));


 201         cases.add(new VarHandleAccessTestCase("Instance field",
 202                                               vhField, vh -> testInstanceField(this, vh)));
 203         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 204                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 205                                               false));
 206 
 207         cases.add(new VarHandleAccessTestCase("Static field",
 208                                               vhStaticField, VarHandleTestAccessLong::testStaticField));
 209         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 210                                               vhStaticField, VarHandleTestAccessLong::testStaticFieldUnsupported,
 211                                               false));
 212 
 213         cases.add(new VarHandleAccessTestCase("Array",
 214                                               vhArray, VarHandleTestAccessLong::testArray));
 215         cases.add(new VarHandleAccessTestCase("Array unsupported",
 216                                               vhArray, VarHandleTestAccessLong::testArrayUnsupported,
 217                                               false));
 218         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 219                                               vhArray, VarHandleTestAccessLong::testArrayIndexOutOfBounds,
 220                                               false));
 221         cases.add(new VarHandleAccessTestCase("Value type field",
 222                                               vhValueTypeField, vh -> testValueTypeField(Value.VT, vh)));
 223         cases.add(new VarHandleAccessTestCase("Value type field unsupported",
 224                                               vhValueTypeField, vh -> testValueTypeFieldUnsupported(Value.VT, vh),
 225                                               false));
 226 
 227         // Work around issue with jtreg summary reporting which truncates
 228         // the String result of Object.toString to 30 characters, hence
 229         // the first dummy argument
 230         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 231     }
 232 
 233     @Test(dataProvider = "accessTestCaseProvider")
 234     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 235         T t = atc.get();
 236         int iters = atc.requiresLoop() ? ITERS : 1;
 237         for (int c = 0; c < iters; c++) {
 238             atc.testAccess(t);
 239         }
 240     }
 241 
 242 
 243 
 244 
 245     static void testInstanceFinalField(VarHandleTestAccessLong recv, VarHandle vh) {


 273         checkUOE(() -> {
 274             vh.set(recv, 0xCAFEBABECAFEBABEL);
 275         });
 276 
 277         checkUOE(() -> {
 278             vh.setVolatile(recv, 0xCAFEBABECAFEBABEL);
 279         });
 280 
 281         checkUOE(() -> {
 282             vh.setRelease(recv, 0xCAFEBABECAFEBABEL);
 283         });
 284 
 285         checkUOE(() -> {
 286             vh.setOpaque(recv, 0xCAFEBABECAFEBABEL);
 287         });
 288 
 289 
 290 
 291     }
 292 
 293     static void testValueTypeField(Value recv, VarHandle vh) {
 294         // Plain
 295         {
 296             long x = (long) vh.get(recv);
 297             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 298         }
 299     }
 300 
 301     static void testValueTypeFieldUnsupported(Value recv, VarHandle vh) {
 302         checkUOE(() -> {
 303             vh.set(recv, 0xCAFEBABECAFEBABEL);
 304         });
 305     }
 306 
 307     static void testStaticFinalField(VarHandle vh) {
 308         // Plain
 309         {
 310             long x = (long) vh.get();
 311             assertEquals(x, 0x0123456789ABCDEFL, "get long value");
 312         }
 313 
 314 
 315         // Volatile
 316         {
 317             long x = (long) vh.getVolatile();
 318             assertEquals(x, 0x0123456789ABCDEFL, "getVolatile long value");
 319         }
 320 
 321         // Lazy
 322         {
 323             long x = (long) vh.getAcquire();
 324             assertEquals(x, 0x0123456789ABCDEFL, "getRelease long value");
 325         }


< prev index next >