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 /*
26 * @test
27 * @enablePreview
28 * @run testng TestLayoutPaths
29 */
30
31 import java.lang.foreign.*;
32 import java.lang.foreign.MemoryLayout.PathElement;
33
34 import org.testng.annotations.*;
35
36 import java.lang.invoke.MethodHandle;
37 import java.lang.invoke.VarHandle;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.function.IntFunction;
41
42 import static java.lang.foreign.MemoryLayout.PathElement.groupElement;
43 import static java.lang.foreign.MemoryLayout.PathElement.sequenceElement;
44 import static java.lang.foreign.ValueLayout.JAVA_INT;
45 import static java.lang.foreign.ValueLayout.JAVA_SHORT;
46 import static org.testng.Assert.*;
47
48 public class TestLayoutPaths {
49
50 @Test(expectedExceptions = IllegalArgumentException.class)
51 public void testBadByteSelectFromSeq() {
52 SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
53 seq.byteOffset(groupElement("foo"));
54 }
55
56 @Test(expectedExceptions = IllegalArgumentException.class)
57 public void testBadByteSelectFromStruct() {
58 GroupLayout g = MemoryLayout.structLayout(JAVA_INT);
59 g.byteOffset(sequenceElement());
60 }
61
62 @Test(expectedExceptions = IllegalArgumentException.class)
63 public void testBadByteSelectFromValue() {
64 SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
119
120 @Test(expectedExceptions = IllegalArgumentException.class)
121 public void testIncompleteAccess() {
122 SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
123 seq.varHandle(sequenceElement());
124 }
125
126 @Test
127 public void testByteOffsetHandleRange() {
128 SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
129 seq.byteOffsetHandle(sequenceElement(0, 1));
130 }
131
132 @Test(expectedExceptions = IllegalArgumentException.class)
133 public void testByteOffsetHandleBadRange() {
134 SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
135 seq.byteOffsetHandle(sequenceElement(5, 1)); // invalid range (starting position is outside the sequence)
136 }
137
138 @Test
139 public void testBadAlignmentOfRoot() throws Throwable {
140 MemoryLayout struct = MemoryLayout.structLayout(
141 JAVA_INT,
142 JAVA_SHORT.withName("x"));
143 assertEquals(struct.byteAlignment(), 4);
144
145 try (Arena arena = Arena.ofConfined()) {
146 MemorySegment seg = arena.allocate(struct.byteSize() + 2, struct.byteAlignment()).asSlice(2);
147 assertEquals(seg.address() % JAVA_SHORT.byteAlignment(), 0); // should be aligned
148 assertNotEquals(seg.address() % struct.byteAlignment(), 0); // should not be aligned
149
150 String expectedMessage = "Target offset incompatible with alignment constraints: " + struct.byteAlignment();
151
152 VarHandle vhX = struct.varHandle(groupElement("x"));
153 IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> {
154 vhX.set(seg, (short) 42);
155 });
156 assertEquals(iae.getMessage(), expectedMessage);
157
158 MethodHandle sliceX = struct.sliceHandle(groupElement("x"));
159 iae = expectThrows(IllegalArgumentException.class, () -> {
160 MemorySegment slice = (MemorySegment) sliceX.invokeExact(seg);
161 });
162 assertEquals(iae.getMessage(), expectedMessage);
163 }
164 }
165
166 @Test
167 public void testBadSequencePathInOffset() {
168 SequenceLayout seq = MemoryLayout.sequenceLayout(10, JAVA_INT);
169 // bad path elements
170 for (PathElement e : List.of( sequenceElement(), sequenceElement(0, 2) )) {
171 try {
172 seq.byteOffset(e);
173 fail();
174 } catch (IllegalArgumentException ex) {
175 assertTrue(true);
176 }
177 }
178 }
179
180 @Test
181 public void testBadSequencePathInSelect() {
182 SequenceLayout seq = MemoryLayout.sequenceLayout(10, JAVA_INT);
183 for (PathElement e : List.of( sequenceElement(0), sequenceElement(0, 2) )) {
184 try {
185 seq.select(e);
254 SequenceLayout g = MemoryLayout.sequenceLayout(4, ValueLayout.JAVA_BYTE);
255
256 // test select
257
258 MemoryLayout selected = g.select(sequenceElement());
259 assertTrue(selected == ValueLayout.JAVA_BYTE);
260
261 // test offset
262
263 for (int i = 0 ; i < 4 ; i++) {
264 long byteOffset = g.byteOffset(sequenceElement(i));
265 assertEquals(offsets[i], byteOffset);
266 }
267 }
268
269 @Test(dataProvider = "testLayouts")
270 public void testOffsetHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
271 long expectedByteOffset) throws Throwable {
272 MethodHandle byteOffsetHandle = layout.byteOffsetHandle(pathElements);
273 byteOffsetHandle = byteOffsetHandle.asSpreader(long[].class, indexes.length);
274 long actualByteOffset = (long) byteOffsetHandle.invokeExact(indexes);
275 assertEquals(actualByteOffset, expectedByteOffset);
276 }
277
278 @DataProvider
279 public static Object[][] testLayouts() {
280 List<Object[]> testCases = new ArrayList<>();
281
282 testCases.add(new Object[] {
283 MemoryLayout.sequenceLayout(10, JAVA_INT),
284 new PathElement[] { sequenceElement() },
285 new long[] { 4 },
286 JAVA_INT.byteSize() * 4
287 });
288 testCases.add(new Object[] {
289 MemoryLayout.sequenceLayout(10, MemoryLayout.structLayout(JAVA_INT, JAVA_INT.withName("y"))),
290 new PathElement[] { sequenceElement(), groupElement("y") },
291 new long[] { 4 },
292 (JAVA_INT.byteSize() * 2) * 4 + JAVA_INT.byteSize()
293 });
294 testCases.add(new Object[] {
343 });
344 testCases.add(new Object[] {
345 complexLayout,
346 new PathElement[] { groupElement("data"), sequenceElement(), sequenceElement(), groupElement("y") },
347 new long[] { 1, 0 },
348 (JAVA_INT.byteSize() * 2) * 10 + JAVA_INT.byteSize()
349 });
350
351 return testCases.toArray(Object[][]::new);
352 }
353
354 @Test(dataProvider = "testLayouts")
355 public void testSliceHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
356 long expectedByteOffset) throws Throwable {
357 MemoryLayout selected = layout.select(pathElements);
358 MethodHandle sliceHandle = layout.sliceHandle(pathElements);
359 sliceHandle = sliceHandle.asSpreader(long[].class, indexes.length);
360
361 try (Arena arena = Arena.ofConfined()) {
362 MemorySegment segment = arena.allocate(layout);
363 MemorySegment slice = (MemorySegment) sliceHandle.invokeExact(segment, indexes);
364 assertEquals(slice.address() - segment.address(), expectedByteOffset);
365 assertEquals(slice.byteSize(), selected.byteSize());
366 }
367 }
368
369 }
|
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 /*
26 * @test
27 * @run testng TestLayoutPaths
28 */
29
30 import java.lang.foreign.*;
31 import java.lang.foreign.MemoryLayout.PathElement;
32
33 import org.testng.annotations.*;
34
35 import java.lang.invoke.MethodHandle;
36 import java.lang.invoke.VarHandle;
37 import java.nio.ByteOrder;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.function.IntFunction;
41
42 import static java.lang.foreign.MemoryLayout.PathElement.groupElement;
43 import static java.lang.foreign.MemoryLayout.PathElement.sequenceElement;
44 import static java.lang.foreign.ValueLayout.JAVA_INT;
45 import static java.lang.foreign.ValueLayout.JAVA_LONG;
46 import static java.lang.foreign.ValueLayout.JAVA_SHORT;
47 import static org.testng.Assert.*;
48
49 public class TestLayoutPaths {
50
51 @Test(expectedExceptions = IllegalArgumentException.class)
52 public void testBadByteSelectFromSeq() {
53 SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
54 seq.byteOffset(groupElement("foo"));
55 }
56
57 @Test(expectedExceptions = IllegalArgumentException.class)
58 public void testBadByteSelectFromStruct() {
59 GroupLayout g = MemoryLayout.structLayout(JAVA_INT);
60 g.byteOffset(sequenceElement());
61 }
62
63 @Test(expectedExceptions = IllegalArgumentException.class)
64 public void testBadByteSelectFromValue() {
65 SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
120
121 @Test(expectedExceptions = IllegalArgumentException.class)
122 public void testIncompleteAccess() {
123 SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
124 seq.varHandle(sequenceElement());
125 }
126
127 @Test
128 public void testByteOffsetHandleRange() {
129 SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
130 seq.byteOffsetHandle(sequenceElement(0, 1));
131 }
132
133 @Test(expectedExceptions = IllegalArgumentException.class)
134 public void testByteOffsetHandleBadRange() {
135 SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
136 seq.byteOffsetHandle(sequenceElement(5, 1)); // invalid range (starting position is outside the sequence)
137 }
138
139 @Test
140 public void testBadAlignmentOfRoot() {
141 MemoryLayout struct = MemoryLayout.structLayout(
142 JAVA_INT,
143 JAVA_SHORT.withName("x"));
144 assertEquals(struct.byteAlignment(), 4);
145
146 try (Arena arena = Arena.ofConfined()) {
147 MemorySegment seg = arena.allocate(struct.byteSize() + 2, struct.byteAlignment()).asSlice(2);
148 assertEquals(seg.address() % JAVA_SHORT.byteAlignment(), 0); // should be aligned
149 assertNotEquals(seg.address() % struct.byteAlignment(), 0); // should not be aligned
150
151 String expectedMessage = "Target offset 0 is incompatible with alignment constraint " + struct.byteAlignment() + " (of [i4s2(x)]) for segment MemorySegment";
152
153 VarHandle vhX = struct.varHandle(groupElement("x"));
154 IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> {
155 vhX.set(seg, 0L, (short) 42);
156 });
157 assertTrue(iae.getMessage().startsWith(expectedMessage));
158
159 MethodHandle sliceX = struct.sliceHandle(groupElement("x"));
160 iae = expectThrows(IllegalArgumentException.class, () -> {
161 MemorySegment slice = (MemorySegment) sliceX.invokeExact(seg, 0L);
162 });
163 assertTrue(iae.getMessage().startsWith(expectedMessage));
164 }
165 }
166
167 @Test
168 public void testWrongTypeRoot() {
169 MemoryLayout struct = MemoryLayout.structLayout(
170 JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN),
171 JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN)
172 );
173
174 var expectedMessage = "Bad layout path: attempting to select a sequence element from a non-sequence layout: [i4i4]";
175
176 IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () ->
177 struct.select(PathElement.sequenceElement()));
178 assertEquals(iae.getMessage(), expectedMessage);
179 }
180
181 @Test
182 public void testWrongTypeEnclosing() {
183 MemoryLayout struct = MemoryLayout.structLayout(
184 MemoryLayout.sequenceLayout(2, MemoryLayout.structLayout(
185 JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN).withName("3a"),
186 JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN).withName("3b")
187 ).withName("2")
188 ).withName("1")
189 ).withName("0");
190
191 var expectedMessage = "Bad layout path: attempting to select a sequence element from a non-sequence layout: " +
192 "[i4(3a)i4(3b)](2), selected from: " +
193 "[2:[i4(3a)i4(3b)](2)](1), selected from: " +
194 "[[2:[i4(3a)i4(3b)](2)](1)](0)";
195
196 IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () ->
197 struct.select(PathElement.groupElement("1"),
198 PathElement.sequenceElement(),
199 PathElement.sequenceElement()));
200 assertEquals(iae.getMessage(), expectedMessage);
201 }
202
203 @Test
204 public void testBadSequencePathInOffset() {
205 SequenceLayout seq = MemoryLayout.sequenceLayout(10, JAVA_INT);
206 // bad path elements
207 for (PathElement e : List.of( sequenceElement(), sequenceElement(0, 2) )) {
208 try {
209 seq.byteOffset(e);
210 fail();
211 } catch (IllegalArgumentException ex) {
212 assertTrue(true);
213 }
214 }
215 }
216
217 @Test
218 public void testBadSequencePathInSelect() {
219 SequenceLayout seq = MemoryLayout.sequenceLayout(10, JAVA_INT);
220 for (PathElement e : List.of( sequenceElement(0), sequenceElement(0, 2) )) {
221 try {
222 seq.select(e);
291 SequenceLayout g = MemoryLayout.sequenceLayout(4, ValueLayout.JAVA_BYTE);
292
293 // test select
294
295 MemoryLayout selected = g.select(sequenceElement());
296 assertTrue(selected == ValueLayout.JAVA_BYTE);
297
298 // test offset
299
300 for (int i = 0 ; i < 4 ; i++) {
301 long byteOffset = g.byteOffset(sequenceElement(i));
302 assertEquals(offsets[i], byteOffset);
303 }
304 }
305
306 @Test(dataProvider = "testLayouts")
307 public void testOffsetHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
308 long expectedByteOffset) throws Throwable {
309 MethodHandle byteOffsetHandle = layout.byteOffsetHandle(pathElements);
310 byteOffsetHandle = byteOffsetHandle.asSpreader(long[].class, indexes.length);
311 long actualByteOffset = (long) byteOffsetHandle.invokeExact(0L, indexes);
312 assertEquals(actualByteOffset, expectedByteOffset);
313 }
314
315 @DataProvider
316 public static Object[][] testLayouts() {
317 List<Object[]> testCases = new ArrayList<>();
318
319 testCases.add(new Object[] {
320 MemoryLayout.sequenceLayout(10, JAVA_INT),
321 new PathElement[] { sequenceElement() },
322 new long[] { 4 },
323 JAVA_INT.byteSize() * 4
324 });
325 testCases.add(new Object[] {
326 MemoryLayout.sequenceLayout(10, MemoryLayout.structLayout(JAVA_INT, JAVA_INT.withName("y"))),
327 new PathElement[] { sequenceElement(), groupElement("y") },
328 new long[] { 4 },
329 (JAVA_INT.byteSize() * 2) * 4 + JAVA_INT.byteSize()
330 });
331 testCases.add(new Object[] {
380 });
381 testCases.add(new Object[] {
382 complexLayout,
383 new PathElement[] { groupElement("data"), sequenceElement(), sequenceElement(), groupElement("y") },
384 new long[] { 1, 0 },
385 (JAVA_INT.byteSize() * 2) * 10 + JAVA_INT.byteSize()
386 });
387
388 return testCases.toArray(Object[][]::new);
389 }
390
391 @Test(dataProvider = "testLayouts")
392 public void testSliceHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
393 long expectedByteOffset) throws Throwable {
394 MemoryLayout selected = layout.select(pathElements);
395 MethodHandle sliceHandle = layout.sliceHandle(pathElements);
396 sliceHandle = sliceHandle.asSpreader(long[].class, indexes.length);
397
398 try (Arena arena = Arena.ofConfined()) {
399 MemorySegment segment = arena.allocate(layout);
400 MemorySegment slice = (MemorySegment) sliceHandle.invokeExact(segment, 0L, indexes);
401 assertEquals(slice.address() - segment.address(), expectedByteOffset);
402 assertEquals(slice.byteSize(), selected.byteSize());
403 }
404 }
405
406 }
|