2019-12-12 22:59:57 +00:00
|
|
|
/*
|
2022-05-12 16:17:45 +00:00
|
|
|
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
|
2019-12-12 22:59:57 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
2022-05-12 16:17:45 +00:00
|
|
|
* @enablePreview
|
2019-12-12 22:59:57 +00:00
|
|
|
* @run testng TestLayoutPaths
|
|
|
|
*/
|
|
|
|
|
2022-12-05 13:49:53 +00:00
|
|
|
import java.lang.foreign.Arena;
|
2022-05-12 16:17:45 +00:00
|
|
|
import java.lang.foreign.GroupLayout;
|
|
|
|
import java.lang.foreign.MemoryLayout;
|
|
|
|
import java.lang.foreign.MemoryLayout.PathElement;
|
|
|
|
import java.lang.foreign.MemorySegment;
|
|
|
|
import java.lang.foreign.SequenceLayout;
|
2019-12-12 22:59:57 +00:00
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
import java.lang.foreign.ValueLayout;
|
2021-06-02 10:53:06 +00:00
|
|
|
import org.testng.SkipException;
|
2019-12-12 22:59:57 +00:00
|
|
|
import org.testng.annotations.*;
|
2020-01-15 11:09:00 +00:00
|
|
|
|
2020-12-07 11:08:24 +00:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.util.ArrayList;
|
2020-01-15 11:09:00 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-05-12 16:17:45 +00:00
|
|
|
import static java.lang.foreign.MemoryLayout.PathElement.groupElement;
|
|
|
|
import static java.lang.foreign.MemoryLayout.PathElement.sequenceElement;
|
|
|
|
import static java.lang.foreign.ValueLayout.JAVA_INT;
|
2019-12-12 22:59:57 +00:00
|
|
|
import static org.testng.Assert.*;
|
|
|
|
|
|
|
|
public class TestLayoutPaths {
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
2020-06-03 15:50:03 +00:00
|
|
|
public void testBadBitSelectFromSeq() {
|
2022-05-12 16:17:45 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2021-06-02 10:53:06 +00:00
|
|
|
seq.bitOffset(groupElement("foo"));
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
2020-06-03 15:50:03 +00:00
|
|
|
public void testBadByteSelectFromSeq() {
|
2022-05-12 16:17:45 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2021-06-02 10:53:06 +00:00
|
|
|
seq.byteOffset(groupElement("foo"));
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBadBitSelectFromStruct() {
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
g.bitOffset(sequenceElement());
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBadByteSelectFromStruct() {
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
g.byteOffset(sequenceElement());
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBadBitSelectFromValue() {
|
2022-05-12 16:17:45 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.bitOffset(sequenceElement(), sequenceElement());
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
2020-06-03 15:50:03 +00:00
|
|
|
public void testBadByteSelectFromValue() {
|
2022-05-12 16:17:45 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.byteOffset(sequenceElement(), sequenceElement());
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
2020-06-03 15:50:03 +00:00
|
|
|
public void testUnknownBitStructField() {
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(JAVA_INT);
|
|
|
|
g.bitOffset(groupElement("foo"));
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testUnknownByteStructField() {
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(JAVA_INT);
|
|
|
|
g.byteOffset(groupElement("foo"));
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 15:50:03 +00:00
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBitOutOfBoundsSeqIndex() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.bitOffset(sequenceElement(6));
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
2020-06-03 15:50:03 +00:00
|
|
|
public void testByteOutOfBoundsSeqIndex() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.byteOffset(sequenceElement(6));
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testNegativeSeqIndex() {
|
2020-12-07 11:08:24 +00:00
|
|
|
sequenceElement(-2);
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBitNegativeSeqIndex() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.bitOffset(sequenceElement(-2));
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testByteNegativeSeqIndex() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.byteOffset(sequenceElement(-2));
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testOutOfBoundsSeqRange() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.bitOffset(sequenceElement(6, 2));
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testNegativeSeqRange() {
|
2020-12-07 11:08:24 +00:00
|
|
|
sequenceElement(-2, 2);
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBitNegativeSeqRange() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.bitOffset(sequenceElement(-2, 2));
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testByteNegativeSeqRange() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.byteOffset(sequenceElement(-2, 2));
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testIncompleteAccess() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
|
2021-11-24 11:51:16 +00:00
|
|
|
seq.varHandle(sequenceElement());
|
2020-12-07 11:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBitOffsetHandleBadRange() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.bitOffsetHandle(sequenceElement(0, 1)); // ranges not accepted
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testByteOffsetHandleBadRange() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(5, MemoryLayout.structLayout(JAVA_INT));
|
2020-12-07 11:08:24 +00:00
|
|
|
seq.byteOffsetHandle(sequenceElement(0, 1)); // ranges not accepted
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 15:50:03 +00:00
|
|
|
@Test(expectedExceptions = UnsupportedOperationException.class)
|
|
|
|
public void testBadMultiple() {
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(MemoryLayout.paddingLayout(3), JAVA_INT.withName("foo"));
|
|
|
|
g.byteOffset(groupElement("foo"));
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 11:08:24 +00:00
|
|
|
@Test(expectedExceptions = UnsupportedOperationException.class)
|
|
|
|
public void testBadByteOffsetNoMultipleOf8() {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout layout = MemoryLayout.structLayout(MemoryLayout.paddingLayout(7), JAVA_INT.withName("x"));
|
2020-12-07 11:08:24 +00:00
|
|
|
layout.byteOffset(groupElement("x"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = UnsupportedOperationException.class)
|
|
|
|
public void testBadByteOffsetHandleNoMultipleOf8() throws Throwable {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout layout = MemoryLayout.structLayout(MemoryLayout.paddingLayout(7), JAVA_INT.withName("x"));
|
2020-12-07 11:08:24 +00:00
|
|
|
MethodHandle handle = layout.byteOffsetHandle(groupElement("x"));
|
|
|
|
handle.invoke();
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:59:57 +00:00
|
|
|
@Test
|
|
|
|
public void testBadContainerAlign() {
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(JAVA_INT.withBitAlignment(16).withName("foo")).withBitAlignment(8);
|
2019-12-12 22:59:57 +00:00
|
|
|
try {
|
2021-06-02 10:53:06 +00:00
|
|
|
g.bitOffset(groupElement("foo"));
|
|
|
|
g.byteOffset(groupElement("foo"));
|
2019-12-12 22:59:57 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
throw new AssertionError(ex); // should be ok!
|
|
|
|
}
|
|
|
|
try {
|
2021-11-24 11:51:16 +00:00
|
|
|
g.varHandle(groupElement("foo")); //ok
|
2019-12-12 22:59:57 +00:00
|
|
|
assertTrue(false); //should fail!
|
|
|
|
} catch (UnsupportedOperationException ex) {
|
|
|
|
//ok
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
throw new AssertionError(ex); //should fail!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testBadAlignOffset() {
|
2021-11-24 11:51:16 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(MemoryLayout.paddingLayout(8), JAVA_INT.withBitAlignment(16).withName("foo"));
|
2019-12-12 22:59:57 +00:00
|
|
|
try {
|
2021-06-02 10:53:06 +00:00
|
|
|
g.bitOffset(groupElement("foo"));
|
|
|
|
g.byteOffset(groupElement("foo"));
|
2019-12-12 22:59:57 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
throw new AssertionError(ex); // should be ok!
|
|
|
|
}
|
|
|
|
try {
|
2021-11-24 11:51:16 +00:00
|
|
|
g.varHandle(groupElement("foo")); //ok
|
2019-12-12 22:59:57 +00:00
|
|
|
assertTrue(false); //should fail!
|
|
|
|
} catch (UnsupportedOperationException ex) {
|
|
|
|
//ok
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
throw new AssertionError(ex); //should fail!
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 11:09:00 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testBadSequencePathInOffset() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(10, JAVA_INT);
|
2020-01-15 11:09:00 +00:00
|
|
|
// bad path elements
|
2020-12-07 11:08:24 +00:00
|
|
|
for (PathElement e : List.of( sequenceElement(), sequenceElement(0, 2) )) {
|
2020-01-15 11:09:00 +00:00
|
|
|
try {
|
2020-06-03 15:50:03 +00:00
|
|
|
seq.bitOffset(e);
|
|
|
|
fail();
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
assertTrue(true);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
seq.byteOffset(e);
|
2020-01-15 11:09:00 +00:00
|
|
|
fail();
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
assertTrue(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testBadSequencePathInSelect() {
|
2021-06-02 10:53:06 +00:00
|
|
|
SequenceLayout seq = MemoryLayout.sequenceLayout(10, JAVA_INT);
|
2020-12-07 11:08:24 +00:00
|
|
|
for (PathElement e : List.of( sequenceElement(0), sequenceElement(0, 2) )) {
|
2020-01-15 11:09:00 +00:00
|
|
|
try {
|
|
|
|
seq.select(e);
|
|
|
|
fail();
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
assertTrue(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testStructPaths() {
|
|
|
|
long[] offsets = { 0, 8, 24, 56 };
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.structLayout(
|
2021-11-24 11:51:16 +00:00
|
|
|
ValueLayout.JAVA_BYTE.withName("1"),
|
|
|
|
ValueLayout.JAVA_CHAR.withName("2"),
|
|
|
|
ValueLayout.JAVA_FLOAT.withName("3"),
|
|
|
|
ValueLayout.JAVA_LONG.withName("4")
|
2020-01-15 11:09:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// test select
|
|
|
|
|
|
|
|
for (int i = 1 ; i <= 4 ; i++) {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout selected = g.select(groupElement(String.valueOf(i)));
|
2020-01-15 11:09:00 +00:00
|
|
|
assertTrue(selected == g.memberLayouts().get(i - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// test offset
|
|
|
|
|
|
|
|
for (int i = 1 ; i <= 4 ; i++) {
|
2021-06-02 10:53:06 +00:00
|
|
|
long bitOffset = g.bitOffset(groupElement(String.valueOf(i)));
|
2020-06-03 15:50:03 +00:00
|
|
|
assertEquals(offsets[i - 1], bitOffset);
|
2021-06-02 10:53:06 +00:00
|
|
|
long byteOffset = g.byteOffset(groupElement(String.valueOf(i)));
|
2020-06-03 15:50:03 +00:00
|
|
|
assertEquals((offsets[i - 1]) >>> 3, byteOffset);
|
2020-01-15 11:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testUnionPaths() {
|
|
|
|
long[] offsets = { 0, 0, 0, 0 };
|
2021-06-02 10:53:06 +00:00
|
|
|
GroupLayout g = MemoryLayout.unionLayout(
|
2021-11-24 11:51:16 +00:00
|
|
|
ValueLayout.JAVA_BYTE.withName("1"),
|
|
|
|
ValueLayout.JAVA_CHAR.withName("2"),
|
|
|
|
ValueLayout.JAVA_FLOAT.withName("3"),
|
|
|
|
ValueLayout.JAVA_LONG.withName("4")
|
2020-01-15 11:09:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// test select
|
|
|
|
|
|
|
|
for (int i = 1 ; i <= 4 ; i++) {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout selected = g.select(groupElement(String.valueOf(i)));
|
2020-01-15 11:09:00 +00:00
|
|
|
assertTrue(selected == g.memberLayouts().get(i - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// test offset
|
|
|
|
|
|
|
|
for (int i = 1 ; i <= 4 ; i++) {
|
2021-06-02 10:53:06 +00:00
|
|
|
long bitOffset = g.bitOffset(groupElement(String.valueOf(i)));
|
2020-06-03 15:50:03 +00:00
|
|
|
assertEquals(offsets[i - 1], bitOffset);
|
2021-06-02 10:53:06 +00:00
|
|
|
long byteOffset = g.byteOffset(groupElement(String.valueOf(i)));
|
2020-06-03 15:50:03 +00:00
|
|
|
assertEquals((offsets[i - 1]) >>> 3, byteOffset);
|
2020-01-15 11:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSequencePaths() {
|
|
|
|
long[] offsets = { 0, 8, 16, 24 };
|
2021-11-24 11:51:16 +00:00
|
|
|
SequenceLayout g = MemoryLayout.sequenceLayout(4, ValueLayout.JAVA_BYTE);
|
2020-01-15 11:09:00 +00:00
|
|
|
|
|
|
|
// test select
|
|
|
|
|
2020-12-07 11:08:24 +00:00
|
|
|
MemoryLayout selected = g.select(sequenceElement());
|
2021-11-24 11:51:16 +00:00
|
|
|
assertTrue(selected == ValueLayout.JAVA_BYTE);
|
2020-01-15 11:09:00 +00:00
|
|
|
|
|
|
|
// test offset
|
|
|
|
|
|
|
|
for (int i = 0 ; i < 4 ; i++) {
|
2020-12-07 11:08:24 +00:00
|
|
|
long bitOffset = g.bitOffset(sequenceElement(i));
|
2020-06-03 15:50:03 +00:00
|
|
|
assertEquals(offsets[i], bitOffset);
|
2020-12-07 11:08:24 +00:00
|
|
|
long byteOffset = g.byteOffset(sequenceElement(i));
|
2020-06-03 15:50:03 +00:00
|
|
|
assertEquals((offsets[i]) >>> 3, byteOffset);
|
2020-01-15 11:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 11:08:24 +00:00
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
@Test(dataProvider = "testLayouts")
|
2020-12-07 11:08:24 +00:00
|
|
|
public void testOffsetHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
|
|
|
|
long expectedBitOffset) throws Throwable {
|
|
|
|
MethodHandle bitOffsetHandle = layout.bitOffsetHandle(pathElements);
|
|
|
|
bitOffsetHandle = bitOffsetHandle.asSpreader(long[].class, indexes.length);
|
|
|
|
long actualBitOffset = (long) bitOffsetHandle.invokeExact(indexes);
|
|
|
|
assertEquals(actualBitOffset, expectedBitOffset);
|
|
|
|
if (expectedBitOffset % 8 == 0) {
|
|
|
|
MethodHandle byteOffsetHandle = layout.byteOffsetHandle(pathElements);
|
|
|
|
byteOffsetHandle = byteOffsetHandle.asSpreader(long[].class, indexes.length);
|
|
|
|
long actualByteOffset = (long) byteOffsetHandle.invokeExact(indexes);
|
|
|
|
assertEquals(actualByteOffset, expectedBitOffset / 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@DataProvider
|
2021-06-02 10:53:06 +00:00
|
|
|
public static Object[][] testLayouts() {
|
2020-12-07 11:08:24 +00:00
|
|
|
List<Object[]> testCases = new ArrayList<>();
|
|
|
|
|
|
|
|
testCases.add(new Object[] {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout.sequenceLayout(10, JAVA_INT),
|
2020-12-07 11:08:24 +00:00
|
|
|
new PathElement[] { sequenceElement() },
|
|
|
|
new long[] { 4 },
|
|
|
|
JAVA_INT.bitSize() * 4
|
|
|
|
});
|
|
|
|
testCases.add(new Object[] {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout.sequenceLayout(10, MemoryLayout.structLayout(JAVA_INT, JAVA_INT.withName("y"))),
|
2020-12-07 11:08:24 +00:00
|
|
|
new PathElement[] { sequenceElement(), groupElement("y") },
|
|
|
|
new long[] { 4 },
|
|
|
|
(JAVA_INT.bitSize() * 2) * 4 + JAVA_INT.bitSize()
|
|
|
|
});
|
|
|
|
testCases.add(new Object[] {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout.sequenceLayout(10, MemoryLayout.structLayout(MemoryLayout.paddingLayout(5), JAVA_INT.withName("y"))),
|
2020-12-07 11:08:24 +00:00
|
|
|
new PathElement[] { sequenceElement(), groupElement("y") },
|
|
|
|
new long[] { 4 },
|
|
|
|
(JAVA_INT.bitSize() + 5) * 4 + 5
|
|
|
|
});
|
|
|
|
testCases.add(new Object[] {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout.sequenceLayout(10, JAVA_INT),
|
2020-12-07 11:08:24 +00:00
|
|
|
new PathElement[] { sequenceElement() },
|
|
|
|
new long[] { 4 },
|
|
|
|
JAVA_INT.bitSize() * 4
|
|
|
|
});
|
|
|
|
testCases.add(new Object[] {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout.structLayout(
|
|
|
|
MemoryLayout.sequenceLayout(10, JAVA_INT).withName("data")
|
2020-12-07 11:08:24 +00:00
|
|
|
),
|
|
|
|
new PathElement[] { groupElement("data"), sequenceElement() },
|
|
|
|
new long[] { 4 },
|
|
|
|
JAVA_INT.bitSize() * 4
|
|
|
|
});
|
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
MemoryLayout complexLayout = MemoryLayout.structLayout(
|
|
|
|
MemoryLayout.sequenceLayout(10,
|
|
|
|
MemoryLayout.sequenceLayout(10,
|
|
|
|
MemoryLayout.structLayout(
|
2020-12-07 11:08:24 +00:00
|
|
|
JAVA_INT.withName("x"),
|
|
|
|
JAVA_INT.withName("y")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).withName("data")
|
|
|
|
);
|
|
|
|
|
|
|
|
testCases.add(new Object[] {
|
|
|
|
complexLayout,
|
|
|
|
new PathElement[] { groupElement("data"), sequenceElement(), sequenceElement(), groupElement("x") },
|
|
|
|
new long[] { 0, 1 },
|
|
|
|
(JAVA_INT.bitSize() * 2)
|
|
|
|
});
|
|
|
|
testCases.add(new Object[] {
|
|
|
|
complexLayout,
|
|
|
|
new PathElement[] { groupElement("data"), sequenceElement(), sequenceElement(), groupElement("x") },
|
|
|
|
new long[] { 1, 0 },
|
|
|
|
(JAVA_INT.bitSize() * 2) * 10
|
|
|
|
});
|
|
|
|
testCases.add(new Object[] {
|
|
|
|
complexLayout,
|
|
|
|
new PathElement[] { groupElement("data"), sequenceElement(), sequenceElement(), groupElement("y") },
|
|
|
|
new long[] { 0, 1 },
|
|
|
|
(JAVA_INT.bitSize() * 2) + JAVA_INT.bitSize()
|
|
|
|
});
|
|
|
|
testCases.add(new Object[] {
|
|
|
|
complexLayout,
|
|
|
|
new PathElement[] { groupElement("data"), sequenceElement(), sequenceElement(), groupElement("y") },
|
|
|
|
new long[] { 1, 0 },
|
|
|
|
(JAVA_INT.bitSize() * 2) * 10 + JAVA_INT.bitSize()
|
|
|
|
});
|
|
|
|
|
|
|
|
return testCases.toArray(Object[][]::new);
|
|
|
|
}
|
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
@Test(dataProvider = "testLayouts")
|
|
|
|
public void testSliceHandle(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
|
|
|
|
long expectedBitOffset) throws Throwable {
|
|
|
|
if (expectedBitOffset % 8 != 0)
|
|
|
|
throw new SkipException("Offset not a multiple of 8");
|
|
|
|
|
|
|
|
MemoryLayout selected = layout.select(pathElements);
|
|
|
|
MethodHandle sliceHandle = layout.sliceHandle(pathElements);
|
|
|
|
sliceHandle = sliceHandle.asSpreader(long[].class, indexes.length);
|
|
|
|
|
2022-12-05 13:49:53 +00:00
|
|
|
try (Arena arena = Arena.openConfined()) {
|
|
|
|
MemorySegment segment = MemorySegment.allocateNative(layout, arena.scope());
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment slice = (MemorySegment) sliceHandle.invokeExact(segment, indexes);
|
2022-12-05 13:49:53 +00:00
|
|
|
assertEquals(slice.address() - segment.address(), expectedBitOffset / 8);
|
2021-06-02 10:53:06 +00:00
|
|
|
assertEquals(slice.byteSize(), selected.byteSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = UnsupportedOperationException.class)
|
|
|
|
public void testSliceHandleUOEInvalidOffsetEager() throws Throwable {
|
|
|
|
MemoryLayout layout = MemoryLayout.structLayout(
|
|
|
|
MemoryLayout.paddingLayout(5),
|
2021-11-24 11:51:16 +00:00
|
|
|
JAVA_INT.withName("y") // offset not a multiple of 8
|
2021-06-02 10:53:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
layout.sliceHandle(groupElement("y")); // should throw
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = UnsupportedOperationException.class)
|
|
|
|
public void testSliceHandleUOEInvalidOffsetLate() throws Throwable {
|
|
|
|
MemoryLayout layout = MemoryLayout.sequenceLayout(3,
|
|
|
|
MemoryLayout.structLayout(
|
|
|
|
MemoryLayout.paddingLayout(4),
|
2021-11-24 11:51:16 +00:00
|
|
|
JAVA_INT.withName("y") // offset not a multiple of 8
|
2021-06-02 10:53:06 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
MethodHandle sliceHandle;
|
|
|
|
try {
|
|
|
|
sliceHandle = layout.sliceHandle(sequenceElement(), groupElement("y")); // should work
|
|
|
|
} catch (UnsupportedOperationException uoe) {
|
|
|
|
fail("Unexpected exception", uoe);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-05 13:49:53 +00:00
|
|
|
try (Arena arena = Arena.openConfined()) {
|
|
|
|
MemorySegment segment = MemorySegment.allocateNative(layout, arena.scope());
|
2021-06-02 10:53:06 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
sliceHandle.invokeExact(segment, 1); // should work
|
|
|
|
} catch (UnsupportedOperationException uoe) {
|
|
|
|
fail("Unexpected exception", uoe);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceHandle.invokeExact(segment, 0); // should throw
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|