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