Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/jdk/java/foreign/CallGeneratorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
import java.util.stream.Stream;

import jdk.internal.foreign.Utils;
import org.testng.annotations.*;

import org.junit.jupiter.api.TestInstance;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CallGeneratorHelper extends NativeTestHelper {

static final List<MemoryLayout> STACK_PREFIX_LAYOUTS = Stream.concat(
Expand Down Expand Up @@ -151,7 +153,6 @@ static <Z> void generateTest(int i, Stack<Z> combo, Z[] elems, List<List<Z>> res
}
}

@DataProvider(name = "functions")
public static Object[][] functions() {
int functions = 0;
List<Object[]> downcalls = new ArrayList<>();
Expand Down
18 changes: 10 additions & 8 deletions test/jdk/java/foreign/CompositeLookupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,39 @@
* questions.
*/

import org.testng.annotations.Test;

import java.lang.foreign.*;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.testng.annotations.*;

import static org.testng.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/*
* @test
* @run testng CompositeLookupTest
* @run junit CompositeLookupTest
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CompositeLookupTest {

@Test(dataProvider = "testCases")
@ParameterizedTest
@MethodSource("testCases")
public void testLookups(SymbolLookup lookup, List<Result> results) {
for (Result result : results) {
switch (result) {
case Success(String name, long expectedLookupId) -> {
Optional<MemorySegment> symbol = lookup.find(name);
assertTrue(symbol.isPresent());
assertEquals(symbol.get().address(), expectedLookupId);
assertEquals(expectedLookupId, symbol.get().address());
}
case Failure(String name) -> {
Optional<MemorySegment> symbol = lookup.find(name);
assertFalse(symbol.isPresent());
}

}
}
}
Expand All @@ -76,7 +79,6 @@ sealed interface Result { }
record Success(String name, long expectedLookupId) implements Result { }
record Failure(String name) implements Result { }

@DataProvider(name = "testCases")
public Object[][] testCases() {
return new Object[][]{
{
Expand Down
36 changes: 22 additions & 14 deletions test/jdk/java/foreign/LibraryLookupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* questions.
*/

import org.testng.annotations.Test;

import java.io.IOException;
import java.lang.foreign.*;
Expand All @@ -37,19 +36,20 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

/*
* @test id=specialized
* @run testng/othervm/native
* @run junit/othervm/native
* -Djdk.internal.foreign.DowncallLinker.USE_SPEC=true
* --enable-native-access=ALL-UNNAMED
* LibraryLookupTest
*/

/*
* @test id=interpreted
* @run testng/othervm/native
* @run junit/othervm/native
* -Djdk.internal.foreign.DowncallLinker.USE_SPEC=false
* --enable-native-access=ALL-UNNAMED
* LibraryLookupTest
Expand All @@ -73,19 +73,23 @@ void testLoadLibraryConfined() {
}
}

@Test(expectedExceptions = IllegalStateException.class)
@Test
void testLoadLibraryConfinedClosed() {
MemorySegment addr;
try (Arena arena = Arena.ofConfined()) {
addr = loadLibrary(arena);
}
callFunc(addr);
assertThrows(IllegalStateException.class, () -> {
callFunc(addr);
});
}

@Test(expectedExceptions = IllegalArgumentException.class)
@Test
void testLoadLibraryBadName() {
try (Arena arena = Arena.ofConfined()) {
SymbolLookup.libraryLookup(LIB_PATH.toString() + "\u0000", arena);
assertThrows(IllegalArgumentException.class, () -> {
SymbolLookup.libraryLookup(LIB_PATH.toString() + "\u0000", arena);
});
}
}

Expand All @@ -99,7 +103,7 @@ void testLoadLibraryBadLookupName() {

@Test
void testLoadLibraryNonDefaultFileSystem() throws URISyntaxException, IOException {
try (FileSystem customFs = fsFromJarOfClass(org.testng.annotations.Test.class)) {
try (FileSystem customFs = fsFromJarOfClass(Test.class)) {
try (Arena arena = Arena.ofConfined()) {
Path p = customFs.getPath(".");
try {
Expand Down Expand Up @@ -131,7 +135,7 @@ private static FileSystem fsFromJarOfClass(Class<?> clazz) throws URISyntaxExcep
private static MemorySegment loadLibrary(Arena session) {
SymbolLookup lib = SymbolLookup.libraryLookup(LIB_PATH, session);
MemorySegment addr = lib.find("inc").get();
assertEquals(addr.scope(), session.scope());
assertEquals(session.scope(), addr.scope());
return addr;
}

Expand All @@ -149,14 +153,18 @@ private static void callFunc(MemorySegment addr) {
static final int MAX_EXECUTOR_WAIT_SECONDS = 20;
static final int NUM_ACCESSORS = Math.min(10, Runtime.getRuntime().availableProcessors());

@Test(expectedExceptions = IllegalArgumentException.class)
@Test
void testBadLibraryLookupName() {
SymbolLookup.libraryLookup("nonExistent", Arena.global());
assertThrows(IllegalArgumentException.class, () -> {
SymbolLookup.libraryLookup("nonExistent", Arena.global());
});
}

@Test(expectedExceptions = IllegalArgumentException.class)
@Test
void testBadLibraryLookupPath() {
SymbolLookup.libraryLookup(Path.of("nonExistent"), Arena.global());
assertThrows(IllegalArgumentException.class, () -> {
SymbolLookup.libraryLookup(Path.of("nonExistent"), Arena.global());
});
}

@Test
Expand Down
17 changes: 9 additions & 8 deletions test/jdk/java/foreign/MemoryLayoutPrincipalTotalityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@

/*
* @test
* @run testng/othervm MemoryLayoutPrincipalTotalityTest
* @run junit/othervm MemoryLayoutPrincipalTotalityTest
*/

import org.testng.annotations.*;

import java.lang.foreign.*;

import static java.lang.foreign.ValueLayout.*;
import static org.testng.Assert.*;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class MemoryLayoutPrincipalTotalityTest {

Expand All @@ -43,7 +44,7 @@ public void testBasicTotality() {
int v0 = switch (memoryLayout) {
case MemoryLayout ml -> 1;
};
assertEquals(v0, 1);
assertEquals(1, v0);
}

@Test
Expand All @@ -55,7 +56,7 @@ public void testMLRemovedTotality() {
case SequenceLayout sl -> 0; // leaf
case ValueLayout vl -> 1;
};
assertEquals(v1, 1);
assertEquals(1, v1);
}

@Test
Expand All @@ -68,7 +69,7 @@ public void testMLGLRemovedTotality() {
case StructLayout sl -> 0; // leaf
case UnionLayout ul -> 0; // leaf
};
assertEquals(v2, 1);
assertEquals(1, v2);
}

@Test
Expand All @@ -89,7 +90,7 @@ public void testMLGLVLRemovedTotality() {
case OfLong ol -> 0; // leaf
case OfShort os -> 0; // leaf
};
assertEquals(v3, 1);
assertEquals(1, v3);
}

@Test
Expand All @@ -109,7 +110,7 @@ public void testMLVLRemovedTotality() {
case OfLong ol -> 0; // leaf
case OfShort os -> 0; // leaf
};
assertEquals(v4, 1);
assertEquals(1, v4);
}

private static MemoryLayout javaIntMemoryLayout() {
Expand Down
17 changes: 9 additions & 8 deletions test/jdk/java/foreign/MemoryLayoutTypeRetentionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@

/*
* @test
* @run testng/othervm MemoryLayoutTypeRetentionTest
* @run junit/othervm MemoryLayoutTypeRetentionTest
*/

import org.testng.annotations.*;

import java.lang.foreign.*;
import java.nio.ByteOrder;

import static java.lang.foreign.ValueLayout.*;
import static org.testng.Assert.*;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class MemoryLayoutTypeRetentionTest {

Expand Down Expand Up @@ -144,12 +145,12 @@ public void testAddressLayout() {
.withoutTargetLayout()
.withOrder(BYTE_ORDER);
check(v);
assertEquals(v.order(), BYTE_ORDER);
assertEquals(BYTE_ORDER, v.order());

assertFalse(v.targetLayout().isPresent());
AddressLayout v2 = v.withTargetLayout(JAVA_INT);
assertTrue(v2.targetLayout().isPresent());
assertEquals(v2.targetLayout().get(), JAVA_INT);
assertEquals(JAVA_INT, v2.targetLayout().get());
assertTrue(v2.withoutTargetLayout().targetLayout().isEmpty());
}

Expand Down Expand Up @@ -197,15 +198,15 @@ public void testUnionLayout() {

public void check(ValueLayout v) {
check((MemoryLayout) v);
assertEquals(v.order(), BYTE_ORDER);
assertEquals(BYTE_ORDER, v.order());
}

public void check(MemoryLayout v) {
// Check name properties
assertEquals(v.name().orElseThrow(), NAME);
assertEquals(NAME, v.name().orElseThrow());
assertTrue(v.withoutName().name().isEmpty());

assertEquals(v.byteAlignment(), BYTE_ALIGNMENT);
assertEquals(BYTE_ALIGNMENT, v.byteAlignment());
}

}
29 changes: 14 additions & 15 deletions test/jdk/java/foreign/SafeFunctionAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

/*
* @test id=specialized
* @run testng/othervm/native
* @run junit/othervm/native
* -Djdk.internal.foreign.DowncallLinker.USE_SPEC=true
* --enable-native-access=ALL-UNNAMED
* SafeFunctionAccessTest
*/

/*
* @test id=interpreted
* @run testng/othervm/native
* @run junit/othervm/native
* -Djdk.internal.foreign.DowncallLinker.USE_SPEC=false
* --enable-native-access=ALL-UNNAMED
* SafeFunctionAccessTest
Expand All @@ -49,9 +49,8 @@

import java.util.stream.Stream;

import org.testng.annotations.*;

import static org.testng.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class SafeFunctionAccessTest extends NativeTestHelper {
static {
Expand All @@ -62,18 +61,18 @@ public class SafeFunctionAccessTest extends NativeTestHelper {
C_INT, C_INT
);

@Test(expectedExceptions = IllegalStateException.class)
@Test
public void testClosedStruct() throws Throwable {
MemorySegment segment;
try (Arena arena = Arena.ofConfined()) {
segment = arena.allocate(POINT);
}
assertFalse(segment.scope().isAlive());
} assertFalse(segment.scope().isAlive());
MethodHandle handle = Linker.nativeLinker().downcallHandle(
findNativeOrThrow("struct_func"),
FunctionDescriptor.ofVoid(POINT));

handle.invokeExact(segment);
assertThrows(IllegalStateException.class, () -> {
handle.invokeExact(segment);
});
}

@Test
Expand Down Expand Up @@ -119,19 +118,19 @@ static Allocation of(MemoryLayout layout) {
}
}

@Test(expectedExceptions = IllegalStateException.class)
@Test
public void testClosedUpcall() throws Throwable {
MemorySegment upcall;
try (Arena arena = Arena.ofConfined()) {
MethodHandle dummy = MethodHandles.lookup().findStatic(SafeFunctionAccessTest.class, "dummy", MethodType.methodType(void.class));
upcall = Linker.nativeLinker().upcallStub(dummy, FunctionDescriptor.ofVoid(), arena);
}
assertFalse(upcall.scope().isAlive());
} assertFalse(upcall.scope().isAlive());
MethodHandle handle = Linker.nativeLinker().downcallHandle(
findNativeOrThrow("addr_func"),
FunctionDescriptor.ofVoid(C_POINTER));

handle.invokeExact(upcall);
assertThrows(IllegalStateException.class, () -> {
handle.invokeExact(upcall);
});
}

static void dummy() { }
Expand Down
Loading