Path: blob/master/test/jdk/java/foreign/TestAddressHandle.java
41145 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAddressHandle27* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAddressHandle28* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAddressHandle29* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAddressHandle30*/3132import java.lang.invoke.*;33import java.nio.ByteOrder;34import jdk.incubator.foreign.*;3536import org.testng.annotations.*;37import static org.testng.Assert.*;3839public class TestAddressHandle {4041static final MethodHandle INT_TO_BOOL;42static final MethodHandle BOOL_TO_INT;43static final MethodHandle INT_TO_STRING;44static final MethodHandle STRING_TO_INT;4546static {47try {48INT_TO_BOOL = MethodHandles.lookup().findStatic(TestAddressHandle.class, "intToBool",49MethodType.methodType(boolean.class, int.class));50BOOL_TO_INT = MethodHandles.lookup().findStatic(TestAddressHandle.class, "boolToInt",51MethodType.methodType(int.class, boolean.class));52INT_TO_STRING = MethodHandles.lookup().findStatic(TestAddressHandle.class, "intToString",53MethodType.methodType(String.class, int.class));54STRING_TO_INT = MethodHandles.lookup().findStatic(TestAddressHandle.class, "stringToInt",55MethodType.methodType(int.class, String.class));56} catch (Throwable ex) {57throw new ExceptionInInitializerError(ex);58}59}6061@Test(dataProvider = "addressHandles")62public void testAddressHandle(VarHandle addrHandle, int byteSize) {63VarHandle longHandle = MemoryLayouts.JAVA_LONG.varHandle(long.class);64try (ResourceScope scope = ResourceScope.newConfinedScope()) {65MemorySegment segment = MemorySegment.allocateNative(8, scope);66MemorySegment target = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ?67segment.asSlice(8 - byteSize) :68segment;69longHandle.set(segment, 42L);70MemoryAddress address = (MemoryAddress)addrHandle.get(target);71assertEquals(address.toRawLongValue(), 42L);72addrHandle.set(target, address.addOffset(1));73long result = (long)longHandle.get(segment);74assertEquals(43L, result);75}76}7778@Test(dataProvider = "addressHandles")79public void testNull(VarHandle addrHandle, int byteSize) {80VarHandle longHandle = MemoryLayouts.JAVA_LONG.varHandle(long.class);81try (ResourceScope scope = ResourceScope.newConfinedScope()) {82MemorySegment segment = MemorySegment.allocateNative(8, scope);83longHandle.set(segment, 0L);84MemoryAddress address = (MemoryAddress)addrHandle.get(segment);85assertTrue(address == MemoryAddress.NULL);86}87}8889@Test(expectedExceptions = IllegalArgumentException.class)90public void testBadAdaptFloat() {91VarHandle floatHandle = MemoryLayouts.JAVA_FLOAT.varHandle(float.class);92MemoryHandles.asAddressVarHandle(floatHandle);93}9495@Test(expectedExceptions = IllegalArgumentException.class)96public void testBadAdaptDouble() {97VarHandle doubleHandle = MemoryLayouts.JAVA_DOUBLE.varHandle(double.class);98MemoryHandles.asAddressVarHandle(doubleHandle);99}100101@Test(expectedExceptions = IllegalArgumentException.class)102public void testBadAdaptBoolean() {103VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);104VarHandle boolHandle = MemoryHandles.filterValue(intHandle, BOOL_TO_INT, INT_TO_BOOL);105MemoryHandles.asAddressVarHandle(boolHandle);106}107108@Test(expectedExceptions = IllegalArgumentException.class)109public void testBadAdaptString() {110VarHandle intHandle = MemoryLayouts.JAVA_INT.varHandle(int.class);111VarHandle stringHandle = MemoryHandles.filterValue(intHandle, STRING_TO_INT, INT_TO_STRING);112MemoryHandles.asAddressVarHandle(stringHandle);113}114115@DataProvider(name = "addressHandles")116static Object[][] addressHandles() {117return new Object[][] {118// long119{ MemoryHandles.asAddressVarHandle(at(MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder()), 0)), 8 },120{ MemoryHandles.asAddressVarHandle(MemoryLayouts.JAVA_LONG.varHandle(long.class)), 8 },121122// int123{ MemoryHandles.asAddressVarHandle(at(MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder()), 0)), 4 },124{ MemoryHandles.asAddressVarHandle(MemoryLayouts.JAVA_INT.varHandle(int.class)), 4 },125126// short127{ MemoryHandles.asAddressVarHandle(at(MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder()), 0)), 2 },128{ MemoryHandles.asAddressVarHandle(MemoryLayouts.JAVA_SHORT.varHandle(short.class)), 2 },129130// char131{ MemoryHandles.asAddressVarHandle(at(MemoryHandles.varHandle(char.class, ByteOrder.nativeOrder()), 0)), 2 },132{ MemoryHandles.asAddressVarHandle(MemoryLayouts.JAVA_CHAR.varHandle(char.class)), 2 },133134// byte135{ MemoryHandles.asAddressVarHandle(at(MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder()), 0)), 1 },136{ MemoryHandles.asAddressVarHandle(MemoryLayouts.JAVA_BYTE.varHandle(byte.class)), 1 }137};138}139140static VarHandle at(VarHandle handle, long offset) {141return MemoryHandles.insertCoordinates(handle, 1, offset);142}143144static int boolToInt(boolean value) {145return value ? 1 : 0;146}147148static boolean intToBool(int value) {149return value != 0;150}151152static int stringToInt(String value) {153return value.length();154}155156static String intToString(int value) {157return String.valueOf(value);158}159}160161162