Path: blob/master/test/jdk/java/foreign/TestMemoryHandleAsUnsigned.java
41145 views
/*1* Copyright (c) 2020, 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*/2324import jdk.incubator.foreign.MemoryHandles;25import jdk.incubator.foreign.MemoryLayout;26import jdk.incubator.foreign.MemoryLayout.PathElement;27import jdk.incubator.foreign.MemoryLayouts;28import jdk.incubator.foreign.MemorySegment;29import java.lang.invoke.VarHandle;30import java.util.Arrays;31import java.util.stream.IntStream;32import java.util.stream.LongStream;3334import jdk.incubator.foreign.ResourceScope;35import org.testng.annotations.*;36import static java.nio.ByteOrder.BIG_ENDIAN;37import static org.testng.Assert.*;3839/*40* @test41* @run testng TestMemoryHandleAsUnsigned42*/4344public class TestMemoryHandleAsUnsigned {4546@DataProvider(name = "unsignedIntToByteData")47public Object[][] unsignedIntToByteData() {48return IntStream.range(0, 256)49.mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);50}5152@Test(dataProvider = "unsignedIntToByteData")53public void testUnsignedIntToByte(int intValue) {54byte byteValue = (byte) (intValue & 0xFF);5556MemoryLayout layout = MemoryLayouts.BITS_8_BE;57VarHandle byteHandle = layout.varHandle(byte.class);58VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);5960try (ResourceScope scope = ResourceScope.newConfinedScope()) {61MemorySegment segment = MemorySegment.allocateNative(layout, scope);62intHandle.set(segment, intValue);63int expectedIntValue = Byte.toUnsignedInt(byteValue);64assertEquals((int) intHandle.get(segment), expectedIntValue);65assertEquals((byte) byteHandle.get(segment), byteValue);66}67}6869@DataProvider(name = "unsignedLongToByteData")70public Object[][] unsignedLongToByteData() {71return LongStream.range(0L, 256L)72.mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);73}7475@Test(dataProvider = "unsignedLongToByteData")76public void testUnsignedLongToByte(long longValue) {77byte byteValue = (byte) (longValue & 0xFFL);7879MemoryLayout layout = MemoryLayouts.BITS_8_BE;80VarHandle byteHandle = layout.varHandle(byte.class);81VarHandle longHandle = MemoryHandles.asUnsigned(byteHandle, long.class);8283try (ResourceScope scope = ResourceScope.newConfinedScope()) {84MemorySegment segment = MemorySegment.allocateNative(layout, scope);85longHandle.set(segment, longValue);86long expectedLongValue = Byte.toUnsignedLong(byteValue);87assertEquals((long) longHandle.get(segment), expectedLongValue);88assertEquals((byte) byteHandle.get(segment), byteValue);89}90}9192@DataProvider(name = "unsignedIntToShortData")93public Object[][] unsignedIntToShortData() {94return IntStream.range(0, 65_536).filter(i -> i % 99 == 0)95.mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);96}9798@Test(dataProvider = "unsignedIntToShortData")99public void testUnsignedIntToShort(int intValue) {100short shortValue = (short) (intValue & 0xFFFF);101102MemoryLayout layout = MemoryLayouts.BITS_16_BE;103VarHandle shortHandle = layout.varHandle(short.class);104VarHandle intHandle = MemoryHandles.asUnsigned(shortHandle, int.class);105106try (ResourceScope scope = ResourceScope.newConfinedScope()) {107MemorySegment segment = MemorySegment.allocateNative(layout, scope);108intHandle.set(segment, intValue);109int expectedIntValue = Short.toUnsignedInt(shortValue);110assertEquals((int) intHandle.get(segment), expectedIntValue);111assertEquals((short) shortHandle.get(segment), shortValue);112}113}114115@DataProvider(name = "unsignedLongToShortData")116public Object[][] unsignedLongToShortData() {117return LongStream.range(0L, 65_536L).filter(i -> i % 99 == 0)118.mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);119}120121@Test(dataProvider = "unsignedLongToShortData")122public void testUnsignedLongToShort(long longValue) {123short shortValue = (short) (longValue & 0xFFFFL);124125MemoryLayout layout = MemoryLayouts.BITS_16_BE;126VarHandle shortHandle = layout.varHandle(short.class);127VarHandle longHandle = MemoryHandles.asUnsigned(shortHandle, long.class);128129try (ResourceScope scope = ResourceScope.newConfinedScope()) {130MemorySegment segment = MemorySegment.allocateNative(layout, scope);131longHandle.set(segment, longValue);132long expectedLongValue = Short.toUnsignedLong(shortValue);133assertEquals((long) longHandle.get(segment), expectedLongValue);134assertEquals((short) shortHandle.get(segment), shortValue);135}136}137138@DataProvider(name = "unsignedLongToIntData")139public Object[][] unsignedLongToIntData() {140// some boundary values141long[] l = new long[] { Long.MAX_VALUE, Long.MIN_VALUE,142Short.MAX_VALUE - 1L, Short.MAX_VALUE, Short.MAX_VALUE + 1L,143Short.MIN_VALUE - 1L, Short.MIN_VALUE, Short.MIN_VALUE + 1L, };144return LongStream.concat(LongStream.range(-256L, 256L), Arrays.stream(l))145.mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);146}147148@Test(dataProvider = "unsignedLongToIntData")149public void testUnsignedLongToInt(long longValue) {150int intValue = (int) (longValue & 0xFFFF_FFFFL);151152MemoryLayout layout = MemoryLayouts.BITS_32_BE;153VarHandle intHandle = layout.varHandle(int.class);154VarHandle longHandle = MemoryHandles.asUnsigned(intHandle, long.class);155156try (ResourceScope scope = ResourceScope.newConfinedScope()) {157MemorySegment segment = MemorySegment.allocateNative(layout, scope);158longHandle.set(segment, longValue);159long expectedLongValue = Integer.toUnsignedLong(intValue);160assertEquals((long) longHandle.get(segment), expectedLongValue);161assertEquals((int) intHandle.get(segment), intValue);162}163}164165@Test166public void testCoordinatesSequenceLayout() {167MemoryLayout layout = MemoryLayout.sequenceLayout(2, MemoryLayouts.BITS_8_BE);168VarHandle byteHandle = layout.varHandle(byte.class, PathElement.sequenceElement());169VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);170171try (ResourceScope scope = ResourceScope.newConfinedScope()) {172MemorySegment segment = MemorySegment.allocateNative(layout, scope);173intHandle.set(segment, 0L, (int) -1);174assertEquals((int) intHandle.get(segment, 0L), 255);175intHandle.set(segment, 1L, (int) 200);176assertEquals((int) intHandle.get(segment, 1L), 200);177}178}179180@Test181public void testCoordinatesStride() {182byte[] arr = { 0, 0, (byte) 129, 0 };183MemorySegment segment = MemorySegment.ofArray(arr);184185{186VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)187.varHandle(byte.class, PathElement.sequenceElement());188VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);189assertEquals((int) intHandle.get(segment, 2L), 129);190}191{192VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)193.varHandle(byte.class, PathElement.sequenceElement());194VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);195assertEquals((int) intHandle.get(segment, 2L), 129);196}197}198199static final Class<NullPointerException> NPE = NullPointerException.class;200201@Test202public void testNull() {203VarHandle handle = MemoryHandles.varHandle(byte.class, BIG_ENDIAN);204assertThrows(NPE, () -> MemoryHandles.asUnsigned(handle, null));205assertThrows(NPE, () -> MemoryHandles.asUnsigned(null, short.class));206assertThrows(NPE, () -> MemoryHandles.asUnsigned(null, null));207}208209static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;210211static void assertIllegalArgumentExceptionIllegalCarrier(Class<?> carrier, Class<?> adaptedType) {212var vh = MemoryHandles.varHandle(carrier, BIG_ENDIAN);213var exception = expectThrows(IAE, () -> MemoryHandles.asUnsigned(vh, adaptedType));214var msg = exception.getMessage();215assertTrue(msg.contains("illegal carrier"), "Expected \"illegal carrier\" in:[" + msg +"]");216}217218static void assertIllegalArgumentExceptionIllegalAdapter(Class<?> carrier, Class<?> adaptedType) {219var vh = MemoryHandles.varHandle(carrier, BIG_ENDIAN);220var exception = expectThrows(IAE, () -> MemoryHandles.asUnsigned(vh, adaptedType));221var msg = exception.getMessage();222assertTrue(msg.contains("illegal adapter type"), "Expected \"illegal adapter type\" in:[" + msg +"]");223}224225static void assertIllegalArgumentExceptionIsNotWiderThan(Class<?> carrier, Class<?> adaptedType) {226var vh = MemoryHandles.varHandle(carrier, BIG_ENDIAN);227var exception = expectThrows(IAE, () -> MemoryHandles.asUnsigned(vh, adaptedType));228var msg = exception.getMessage();229assertTrue(msg.contains("is not wider than"), "Expected \"is not wider than\" in:[" + msg +"]");230}231232@Test233public void testIllegalArgumentException() {234assertIllegalArgumentExceptionIllegalCarrier(char.class, long.class);235assertIllegalArgumentExceptionIllegalCarrier(double.class, long.class);236assertIllegalArgumentExceptionIllegalCarrier(float.class, long.class);237assertIllegalArgumentExceptionIllegalCarrier(long.class, long.class);238239assertIllegalArgumentExceptionIllegalAdapter(byte.class, void.class);240assertIllegalArgumentExceptionIllegalAdapter(byte.class, byte.class);241assertIllegalArgumentExceptionIllegalAdapter(byte.class, short.class);242assertIllegalArgumentExceptionIllegalAdapter(byte.class, char.class);243assertIllegalArgumentExceptionIllegalAdapter(byte.class, double.class);244assertIllegalArgumentExceptionIllegalAdapter(byte.class, float.class);245assertIllegalArgumentExceptionIllegalAdapter(byte.class, Object.class);246assertIllegalArgumentExceptionIllegalAdapter(byte.class, Integer.class);247assertIllegalArgumentExceptionIllegalAdapter(byte.class, Long.class);248assertIllegalArgumentExceptionIllegalAdapter(byte.class, long[].class);249assertIllegalArgumentExceptionIllegalAdapter(byte.class, int[].class);250assertIllegalArgumentExceptionIllegalAdapter(byte.class, Integer[].class);251assertIllegalArgumentExceptionIllegalAdapter(byte.class, Long[].class);252253assertIllegalArgumentExceptionIsNotWiderThan(int.class, int.class);254}255}256257258