< prev index next > test/jdk/java/foreign/TestLayouts.java
Print this page
* questions.
*/
/*
* @test
- * @enablePreview
* @run testng TestLayouts
*/
import java.lang.foreign.*;
try (Arena arena = Arena.ofConfined()) {
MemorySegment segment = arena.allocate(seq);;
VarHandle indexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement());
// init segment
for (int i = 0 ; i < 10 ; i++) {
! indexHandle.set(segment, (long)i, i);
}
//check statically indexed handles
for (int i = 0 ; i < 10 ; i++) {
VarHandle preindexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement(i));
! int expected = (int)indexHandle.get(segment, (long)i);
! int found = (int)preindexHandle.get(segment);
assertEquals(expected, found);
}
}
}
try (Arena arena = Arena.ofConfined()) {
MemorySegment segment = arena.allocate(seq);;
VarHandle indexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement());
// init segment
for (int i = 0 ; i < 10 ; i++) {
! indexHandle.set(segment, 0L, (long)i, i);
}
//check statically indexed handles
for (int i = 0 ; i < 10 ; i++) {
VarHandle preindexHandle = seq.varHandle(MemoryLayout.PathElement.sequenceElement(i));
! int expected = (int)indexHandle.get(segment, 0L, (long)i);
! int found = (int)preindexHandle.get(segment, 0L);
assertEquals(expected, found);
}
}
}
public void testSequenceBadCount() {
assertThrows(IllegalArgumentException.class, // negative
() -> MemoryLayout.sequenceLayout(-2, JAVA_SHORT));
}
! @Test(dataProvider = "basicLayouts")
- public void testSequenceInferredCount(MemoryLayout layout) {
- assertEquals(MemoryLayout.sequenceLayout(layout),
- MemoryLayout.sequenceLayout(Long.MAX_VALUE / layout.byteSize(), layout));
- }
-
public void testSequenceNegativeElementCount() {
assertThrows(IllegalArgumentException.class, // negative
() -> MemoryLayout.sequenceLayout(-1, JAVA_SHORT));
}
public void testSequenceBadCount() {
assertThrows(IllegalArgumentException.class, // negative
() -> MemoryLayout.sequenceLayout(-2, JAVA_SHORT));
}
! @Test
public void testSequenceNegativeElementCount() {
assertThrows(IllegalArgumentException.class, // negative
() -> MemoryLayout.sequenceLayout(-1, JAVA_SHORT));
}
}
@Test(dataProvider="layoutsAndAlignments", expectedExceptions = IllegalArgumentException.class)
public void testBadSequenceElementAlignmentTooBig(MemoryLayout layout, long byteAlign) {
layout = layout.withByteAlignment(layout.byteSize() * 2); // hyper-align
! MemoryLayout.sequenceLayout(layout);
}
@Test(dataProvider="layoutsAndAlignments")
public void testBadSequenceElementSizeNotMultipleOfAlignment(MemoryLayout layout, long byteAlign) {
boolean shouldFail = layout.byteSize() % layout.byteAlignment() != 0;
try {
! MemoryLayout.sequenceLayout(layout);
assertFalse(shouldFail);
} catch (IllegalArgumentException ex) {
assertTrue(shouldFail);
}
}
}
@Test(dataProvider="layoutsAndAlignments", expectedExceptions = IllegalArgumentException.class)
public void testBadSequenceElementAlignmentTooBig(MemoryLayout layout, long byteAlign) {
layout = layout.withByteAlignment(layout.byteSize() * 2); // hyper-align
! MemoryLayout.sequenceLayout(1, layout);
}
@Test(dataProvider="layoutsAndAlignments")
public void testBadSequenceElementSizeNotMultipleOfAlignment(MemoryLayout layout, long byteAlign) {
boolean shouldFail = layout.byteSize() % layout.byteAlignment() != 0;
try {
! MemoryLayout.sequenceLayout(1, layout);
assertFalse(shouldFail);
} catch (IllegalArgumentException ex) {
assertTrue(shouldFail);
}
}
} catch (IllegalArgumentException ex) {
assertTrue(shouldFail);
}
}
- @Test(dataProvider="layoutsAndAlignments")
- public void testArrayElementVarHandleBadAlignment(MemoryLayout layout, long byteAlign) {
- if (layout instanceof ValueLayout) {
- assertThrows(UnsupportedOperationException.class, () ->
- ((ValueLayout) layout).withByteAlignment(byteAlign * 2).arrayElementVarHandle());
- }
- }
-
@Test(dataProvider="layoutsAndAlignments", expectedExceptions = IllegalArgumentException.class)
public void testBadStruct(MemoryLayout layout, long byteAlign) {
layout = layout.withByteAlignment(layout.byteSize() * 2); // hyper-align
MemoryLayout.structLayout(layout, layout);
}
SequenceLayout layout = MemoryLayout.sequenceLayout(10, JAVA_INT);
// Step must be != 0
PathElement.sequenceElement(3, 0);
}
+ @Test
+ public void testVarHandleCaching() {
+ assertSame(JAVA_INT.varHandle(), JAVA_INT.varHandle());
+ assertSame(JAVA_INT.withName("foo").varHandle(), JAVA_INT.varHandle());
+
+ assertNotSame(JAVA_INT_UNALIGNED.varHandle(), JAVA_INT.varHandle());
+ assertNotSame(ADDRESS.withTargetLayout(JAVA_INT).varHandle(), ADDRESS.varHandle());
+ }
+
+ @Test(expectedExceptions=IllegalArgumentException.class,
+ expectedExceptionsMessageRegExp=".*Negative offset.*")
+ public void testScaleNegativeOffset() {
+ JAVA_INT.scale(-1, 0);
+ }
+
+ @Test(expectedExceptions=IllegalArgumentException.class,
+ expectedExceptionsMessageRegExp=".*Negative index.*")
+ public void testScaleNegativeIndex() {
+ JAVA_INT.scale(0, -1);
+ }
+
+ @Test(expectedExceptions=ArithmeticException.class)
+ public void testScaleAddOverflow() {
+ JAVA_INT.scale(Long.MAX_VALUE, 1);
+ }
+
+ @Test(expectedExceptions=ArithmeticException.class)
+ public void testScaleMultiplyOverflow() {
+ JAVA_INT.scale(0, Long.MAX_VALUE);
+ }
+
@DataProvider(name = "badAlignments")
public Object[][] layoutsAndBadAlignments() {
LayoutKind[] layoutKinds = LayoutKind.values();
Object[][] values = new Object[layoutKinds.length * 2][2];
for (int i = 0; i < layoutKinds.length ; i++) {
}
static Stream<MemoryLayout> groupLayoutStream() {
return Stream.of(
MemoryLayout.sequenceLayout(10, JAVA_INT),
- MemoryLayout.sequenceLayout(JAVA_INT),
MemoryLayout.structLayout(JAVA_INT, MemoryLayout.paddingLayout(4), JAVA_LONG),
MemoryLayout.unionLayout(JAVA_LONG, JAVA_DOUBLE)
);
}
< prev index next >