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 VarHandleTestMethodHandleAccessLong 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 VarHandleTestMethodHandleAccessLong extends VarHandleBaseTest { 42 static final long static_final_v = 0x0123456789ABCDEFL; 43 44 static long static_v; 45 46 final long final_v = 0x0123456789ABCDEFL; 47 48 long 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 VarHandleTestMethodHandleAccessLong.class, "final_v", long.class); 64 65 vhField = MethodHandles.lookup().findVarHandle( 66 VarHandleTestMethodHandleAccessLong.class, "v", long.class); 67 68 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( 69 VarHandleTestMethodHandleAccessLong.class, "static_final_v", long.class); 70 71 vhStaticField = MethodHandles.lookup().findStaticVarHandle( 72 VarHandleTestMethodHandleAccessLong.class, "static_v", long.class); 73 74 vhArray = MethodHandles.arrayElementVarHandle(long[].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, VarHandleTestMethodHandleAccessLong::testStaticField)); 91 cases.add(new MethodHandleAccessTestCase("Static field unsupported", 92 vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticFieldUnsupported, 93 false)); 94 95 cases.add(new MethodHandleAccessTestCase("Array", 96 vhArray, f, VarHandleTestMethodHandleAccessLong::testArray)); 97 cases.add(new MethodHandleAccessTestCase("Array unsupported", 98 vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayUnsupported, 99 false)); 100 cases.add(new MethodHandleAccessTestCase("Array index out of bounds", 101 vhArray, f, VarHandleTestMethodHandleAccessLong::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(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable { 122 // Plain 356 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long"); 357 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 358 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value"); 359 } 360 361 { 362 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 363 364 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 365 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long"); 366 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 367 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value"); 368 } 369 } 370 371 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessLong 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(0x0123456789ABCDEFL); 381 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 382 assertEquals(x, 0x0123456789ABCDEFL, "set long value"); 383 } 384 385 386 // Volatile 387 { 388 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(0xCAFEBABECAFEBABEL); 389 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); 390 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value"); 391 } 392 393 // Lazy 394 { 395 hs.get(TestAccessMode.SET_RELEASE).invokeExact(0x0123456789ABCDEFL); | 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 VarHandleTestMethodHandleAccessLong 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 VarHandleTestMethodHandleAccessLong extends VarHandleBaseTest { 42 static final long static_final_v = 0x0123456789ABCDEFL; 43 44 static long static_v; 45 46 final long final_v = 0x0123456789ABCDEFL; 47 48 long 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 VarHandleTestMethodHandleAccessLong.class, "final_v", long.class); 66 67 vhField = MethodHandles.lookup().findVarHandle( 68 VarHandleTestMethodHandleAccessLong.class, "v", long.class); 69 70 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( 71 VarHandleTestMethodHandleAccessLong.class, "static_final_v", long.class); 72 73 vhStaticField = MethodHandles.lookup().findStaticVarHandle( 74 VarHandleTestMethodHandleAccessLong.class, "static_v", long.class); 75 76 vhArray = MethodHandles.arrayElementVarHandle(long[].class); 77 78 vhValueTypeField = MethodHandles.lookup().findVarHandle( 79 Value.class, "long_v", long.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, VarHandleTestMethodHandleAccessLong::testStaticField)); 96 cases.add(new MethodHandleAccessTestCase("Static field unsupported", 97 vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticFieldUnsupported, 98 false)); 99 100 cases.add(new MethodHandleAccessTestCase("Array", 101 vhArray, f, VarHandleTestMethodHandleAccessLong::testArray)); 102 cases.add(new MethodHandleAccessTestCase("Array unsupported", 103 vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayUnsupported, 104 false)); 105 cases.add(new MethodHandleAccessTestCase("Array index out of bounds", 106 vhArray, f, VarHandleTestMethodHandleAccessLong::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(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable { 132 // Plain 366 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorAcquire long"); 367 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 368 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorAcquire long value"); 369 } 370 371 { 372 hs.get(TestAccessMode.SET).invokeExact(recv, 0x0123456789ABCDEFL); 373 374 long o = (long) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, 0xCAFEBABECAFEBABEL); 375 assertEquals(o, 0x0123456789ABCDEFL, "getAndBitwiseXorRelease long"); 376 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 377 assertEquals(x, (long)(0x0123456789ABCDEFL ^ 0xCAFEBABECAFEBABEL), "getAndBitwiseXorRelease long value"); 378 } 379 } 380 381 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable { 382 383 384 } 385 386 static void testValueTypeField(Value recv, Handles hs) throws Throwable { 387 // Plain 388 { 389 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); 390 assertEquals(x, 0x0123456789ABCDEFL, "get long value"); 391 } 392 } 393 394 static void testValueTypeFieldUnsupported(Value recv, Handles hs) throws Throwable { 395 // Plain 396 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { 397 checkUOE(am, () -> { 398 hs.get(am).invokeExact(recv, 0x0123456789ABCDEFL); 399 }); 400 } 401 402 403 } 404 405 static void testStaticField(Handles hs) throws Throwable { 406 // Plain 407 { 408 hs.get(TestAccessMode.SET).invokeExact(0x0123456789ABCDEFL); 409 long x = (long) hs.get(TestAccessMode.GET).invokeExact(); 410 assertEquals(x, 0x0123456789ABCDEFL, "set long value"); 411 } 412 413 414 // Volatile 415 { 416 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(0xCAFEBABECAFEBABEL); 417 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); 418 assertEquals(x, 0xCAFEBABECAFEBABEL, "setVolatile long value"); 419 } 420 421 // Lazy 422 { 423 hs.get(TestAccessMode.SET_RELEASE).invokeExact(0x0123456789ABCDEFL); |