1 /*
  2  * Copyright (c) 2020, 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 import jdk.incubator.foreign.MemoryHandles;
 26 import jdk.incubator.foreign.MemoryLayout;
 27 import jdk.incubator.foreign.MemoryLayout.PathElement;
 28 import jdk.incubator.foreign.MemoryLayouts;
 29 import jdk.incubator.foreign.MemorySegment;
 30 import java.lang.invoke.VarHandle;
 31 import java.util.Arrays;
 32 import java.util.stream.IntStream;
 33 import java.util.stream.LongStream;
 34 
 35 import jdk.incubator.foreign.ResourceScope;
 36 import org.testng.annotations.*;
 37 import static java.nio.ByteOrder.BIG_ENDIAN;
 38 import static org.testng.Assert.*;
 39 
 40 /*
 41  * @test
 42  * @run testng TestMemoryHandleAsUnsigned
 43  */
 44 
 45 public class TestMemoryHandleAsUnsigned {
 46 
 47     @DataProvider(name = "unsignedIntToByteData")
 48     public Object[][] unsignedIntToByteData() {
 49         return IntStream.range(0, 256)
 50                 .mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);
 51     }
 52 
 53     @Test(dataProvider = "unsignedIntToByteData")
 54     public void testUnsignedIntToByte(int intValue) {
 55         byte byteValue = (byte) (intValue & 0xFF);
 56 
 57         MemoryLayout layout = MemoryLayouts.BITS_8_BE;
 58         VarHandle byteHandle = layout.varHandle(byte.class);
 59         VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);
 60 
 61         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 62             MemorySegment segment = MemorySegment.allocateNative(layout, scope);
 63             intHandle.set(segment, intValue);
 64             int expectedIntValue = Byte.toUnsignedInt(byteValue);
 65             assertEquals((int) intHandle.get(segment), expectedIntValue);
 66             assertEquals((byte) byteHandle.get(segment), byteValue);
 67         }
 68     }
 69 
 70     @DataProvider(name = "unsignedLongToByteData")
 71     public Object[][] unsignedLongToByteData() {
 72         return LongStream.range(0L, 256L)
 73                 .mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);
 74     }
 75 
 76     @Test(dataProvider = "unsignedLongToByteData")
 77     public void testUnsignedLongToByte(long longValue) {
 78         byte byteValue = (byte) (longValue & 0xFFL);
 79 
 80         MemoryLayout layout = MemoryLayouts.BITS_8_BE;
 81         VarHandle byteHandle = layout.varHandle(byte.class);
 82         VarHandle longHandle = MemoryHandles.asUnsigned(byteHandle, long.class);
 83 
 84         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
 85             MemorySegment segment = MemorySegment.allocateNative(layout, scope);
 86             longHandle.set(segment, longValue);
 87             long expectedLongValue = Byte.toUnsignedLong(byteValue);
 88             assertEquals((long) longHandle.get(segment), expectedLongValue);
 89             assertEquals((byte) byteHandle.get(segment), byteValue);
 90         }
 91     }
 92 
 93     @DataProvider(name = "unsignedIntToShortData")
 94     public Object[][] unsignedIntToShortData() {
 95         return IntStream.range(0, 65_536).filter(i -> i % 99 == 0)
 96                 .mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);
 97     }
 98 
 99     @Test(dataProvider = "unsignedIntToShortData")
100     public void testUnsignedIntToShort(int intValue) {
101         short shortValue = (short) (intValue & 0xFFFF);
102 
103         MemoryLayout layout = MemoryLayouts.BITS_16_BE;
104         VarHandle shortHandle = layout.varHandle(short.class);
105         VarHandle intHandle = MemoryHandles.asUnsigned(shortHandle, int.class);
106 
107         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
108             MemorySegment segment = MemorySegment.allocateNative(layout, scope);
109             intHandle.set(segment, intValue);
110             int expectedIntValue = Short.toUnsignedInt(shortValue);
111             assertEquals((int) intHandle.get(segment), expectedIntValue);
112             assertEquals((short) shortHandle.get(segment), shortValue);
113         }
114     }
115 
116     @DataProvider(name = "unsignedLongToShortData")
117     public Object[][] unsignedLongToShortData() {
118         return LongStream.range(0L, 65_536L).filter(i -> i % 99 == 0)
119                 .mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);
120     }
121 
122     @Test(dataProvider = "unsignedLongToShortData")
123     public void testUnsignedLongToShort(long longValue) {
124         short shortValue = (short) (longValue & 0xFFFFL);
125 
126         MemoryLayout layout = MemoryLayouts.BITS_16_BE;
127         VarHandle shortHandle = layout.varHandle(short.class);
128         VarHandle longHandle = MemoryHandles.asUnsigned(shortHandle, long.class);
129 
130         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
131             MemorySegment segment = MemorySegment.allocateNative(layout, scope);
132             longHandle.set(segment, longValue);
133             long expectedLongValue = Short.toUnsignedLong(shortValue);
134             assertEquals((long) longHandle.get(segment), expectedLongValue);
135             assertEquals((short) shortHandle.get(segment), shortValue);
136         }
137     }
138 
139     @DataProvider(name = "unsignedLongToIntData")
140     public Object[][] unsignedLongToIntData() {
141         // some boundary values
142         long[] l = new long[] { Long.MAX_VALUE, Long.MIN_VALUE,
143                 Short.MAX_VALUE - 1L, Short.MAX_VALUE, Short.MAX_VALUE + 1L,
144                 Short.MIN_VALUE - 1L, Short.MIN_VALUE, Short.MIN_VALUE + 1L, };
145         return LongStream.concat(LongStream.range(-256L, 256L), Arrays.stream(l))
146                 .mapToObj(v -> new Object[] { v }).toArray(Object[][]::new);
147     }
148 
149     @Test(dataProvider = "unsignedLongToIntData")
150     public void testUnsignedLongToInt(long longValue) {
151         int intValue = (int) (longValue & 0xFFFF_FFFFL);
152 
153         MemoryLayout layout = MemoryLayouts.BITS_32_BE;
154         VarHandle intHandle = layout.varHandle(int.class);
155         VarHandle longHandle = MemoryHandles.asUnsigned(intHandle, long.class);
156 
157         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
158             MemorySegment segment = MemorySegment.allocateNative(layout, scope);
159             longHandle.set(segment, longValue);
160             long expectedLongValue = Integer.toUnsignedLong(intValue);
161             assertEquals((long) longHandle.get(segment), expectedLongValue);
162             assertEquals((int) intHandle.get(segment), intValue);
163         }
164     }
165 
166     @Test
167     public void testCoordinatesSequenceLayout() {
168         MemoryLayout layout = MemoryLayout.sequenceLayout(2, MemoryLayouts.BITS_8_BE);
169         VarHandle byteHandle = layout.varHandle(byte.class, PathElement.sequenceElement());
170         VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);
171 
172         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
173             MemorySegment segment = MemorySegment.allocateNative(layout, scope);
174             intHandle.set(segment, 0L, (int) -1);
175             assertEquals((int) intHandle.get(segment, 0L), 255);
176             intHandle.set(segment, 1L, (int) 200);
177             assertEquals((int) intHandle.get(segment, 1L), 200);
178         }
179     }
180 
181     @Test
182     public void testCoordinatesStride() {
183         byte[] arr = { 0, 0, (byte) 129, 0 };
184         MemorySegment segment = MemorySegment.ofArray(arr);
185 
186         {
187             VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
188                     .varHandle(byte.class, PathElement.sequenceElement());
189             VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);
190             assertEquals((int) intHandle.get(segment, 2L), 129);
191         }
192         {
193             VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
194                     .varHandle(byte.class, PathElement.sequenceElement());
195             VarHandle intHandle = MemoryHandles.asUnsigned(byteHandle, int.class);
196             assertEquals((int) intHandle.get(segment, 2L), 129);
197         }
198     }
199 
200     static final Class<NullPointerException> NPE = NullPointerException.class;
201 
202     @Test
203     public void testNull() {
204         VarHandle handle = MemoryHandles.varHandle(byte.class, BIG_ENDIAN);
205         assertThrows(NPE, () -> MemoryHandles.asUnsigned(handle, null));
206         assertThrows(NPE, () -> MemoryHandles.asUnsigned(null, short.class));
207         assertThrows(NPE, () -> MemoryHandles.asUnsigned(null, null));
208     }
209 
210     static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
211 
212     static void assertIllegalArgumentExceptionIllegalCarrier(Class<?> carrier, Class<?> adaptedType) {
213         var vh = MemoryHandles.varHandle(carrier, BIG_ENDIAN);
214         var exception = expectThrows(IAE, () -> MemoryHandles.asUnsigned(vh, adaptedType));
215         var msg = exception.getMessage();
216         assertTrue(msg.contains("illegal carrier"), "Expected \"illegal carrier\" in:[" + msg +"]");
217     }
218 
219     static void assertIllegalArgumentExceptionIllegalAdapter(Class<?> carrier, Class<?> adaptedType) {
220         var vh = MemoryHandles.varHandle(carrier, BIG_ENDIAN);
221         var exception = expectThrows(IAE, () -> MemoryHandles.asUnsigned(vh, adaptedType));
222         var msg = exception.getMessage();
223         assertTrue(msg.contains("illegal adapter type"), "Expected \"illegal adapter type\" in:[" + msg +"]");
224     }
225 
226     static void assertIllegalArgumentExceptionIsNotWiderThan(Class<?> carrier, Class<?> adaptedType) {
227         var vh = MemoryHandles.varHandle(carrier, BIG_ENDIAN);
228         var exception = expectThrows(IAE, () -> MemoryHandles.asUnsigned(vh, adaptedType));
229         var msg = exception.getMessage();
230         assertTrue(msg.contains("is not wider than"), "Expected \"is not wider than\" in:[" + msg +"]");
231     }
232 
233     @Test
234     public void testIllegalArgumentException() {
235         assertIllegalArgumentExceptionIllegalCarrier(char.class,   long.class);
236         assertIllegalArgumentExceptionIllegalCarrier(double.class, long.class);
237         assertIllegalArgumentExceptionIllegalCarrier(float.class,  long.class);
238         assertIllegalArgumentExceptionIllegalCarrier(long.class,   long.class);
239 
240         assertIllegalArgumentExceptionIllegalAdapter(byte.class, void.class);
241         assertIllegalArgumentExceptionIllegalAdapter(byte.class, byte.class);
242         assertIllegalArgumentExceptionIllegalAdapter(byte.class, short.class);
243         assertIllegalArgumentExceptionIllegalAdapter(byte.class, char.class);
244         assertIllegalArgumentExceptionIllegalAdapter(byte.class, double.class);
245         assertIllegalArgumentExceptionIllegalAdapter(byte.class, float.class);
246         assertIllegalArgumentExceptionIllegalAdapter(byte.class, Object.class);
247         assertIllegalArgumentExceptionIllegalAdapter(byte.class, Integer.class);
248         assertIllegalArgumentExceptionIllegalAdapter(byte.class, Long.class);
249         assertIllegalArgumentExceptionIllegalAdapter(byte.class, long[].class);
250         assertIllegalArgumentExceptionIllegalAdapter(byte.class, int[].class);
251         assertIllegalArgumentExceptionIllegalAdapter(byte.class, Integer[].class);
252         assertIllegalArgumentExceptionIllegalAdapter(byte.class, Long[].class);
253 
254         assertIllegalArgumentExceptionIsNotWiderThan(int.class, int.class);
255     }
256 }