Skip to content
Closed
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
54 changes: 27 additions & 27 deletions test/jdk/java/foreign/StdLibTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,7 @@

/*
* @test
* @run testng/othervm --enable-native-access=ALL-UNNAMED StdLibTest
* @run testng/othervm/timeout=600 --enable-native-access=ALL-UNNAMED StdLibTest
*/

import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -51,33 +51,33 @@

public class StdLibTest extends NativeTestHelper {

final static Linker abi = Linker.nativeLinker();
static final Linker abi = Linker.nativeLinker();

private StdLibHelper stdLibHelper = new StdLibHelper();
private static final StdLibHelper STD_LIB_HELPER = new StdLibHelper();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The STD_LIB_HELPER is now a constant.


@Test(dataProvider = "stringPairs")
void test_strcat(String s1, String s2) throws Throwable {
assertEquals(stdLibHelper.strcat(s1, s2), s1 + s2);
assertEquals(STD_LIB_HELPER.strcat(s1, s2), s1 + s2);
}

@Test(dataProvider = "stringPairs")
void test_strcmp(String s1, String s2) throws Throwable {
assertEquals(Math.signum(stdLibHelper.strcmp(s1, s2)), Math.signum(s1.compareTo(s2)));
assertEquals(Math.signum(STD_LIB_HELPER.strcmp(s1, s2)), Math.signum(s1.compareTo(s2)));
}

@Test(dataProvider = "strings")
void test_puts(String s) throws Throwable {
assertTrue(stdLibHelper.puts(s) >= 0);
assertTrue(STD_LIB_HELPER.puts(s) >= 0);
}

@Test(dataProvider = "strings")
void test_strlen(String s) throws Throwable {
assertEquals(stdLibHelper.strlen(s), s.length());
assertEquals(STD_LIB_HELPER.strlen(s), s.length());
}

@Test(dataProvider = "instants")
void test_time(Instant instant) throws Throwable {
StdLibHelper.Tm tm = stdLibHelper.gmtime(instant.getEpochSecond());
StdLibHelper.Tm tm = STD_LIB_HELPER.gmtime(instant.getEpochSecond());
LocalDateTime localTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
assertEquals(tm.sec(), localTime.getSecond());
assertEquals(tm.min(), localTime.getMinute());
Expand All @@ -97,17 +97,17 @@ void test_time(Instant instant) throws Throwable {
void test_qsort(List<Integer> ints) throws Throwable {
if (ints.size() > 0) {
int[] input = ints.stream().mapToInt(i -> i).toArray();
int[] sorted = stdLibHelper.qsort(input);
int[] sorted = STD_LIB_HELPER.qsort(input);
Arrays.sort(input);
assertEquals(sorted, input);
}
}

@Test
void test_rand() throws Throwable {
int val = stdLibHelper.rand();
int val = STD_LIB_HELPER.rand();
for (int i = 0 ; i < 100 ; i++) {
int newVal = stdLibHelper.rand();
int newVal = STD_LIB_HELPER.rand();
if (newVal != val) {
return; //ok
}
Expand All @@ -131,7 +131,7 @@ void test_printf(List<PrintfArg> args) throws Throwable {
String expected = String.format(javaFormatString, args.stream()
.map(a -> a.javaValue).toArray());

int found = stdLibHelper.printf(nativeFormatString, args);
int found = STD_LIB_HELPER.printf(nativeFormatString, args);
assertEquals(found, expected.length());
}

Expand All @@ -140,41 +140,41 @@ void testSystemLibraryBadLookupName() {
assertTrue(LINKER.defaultLookup().find("strlen\u0000foobar").isEmpty());
}

static class StdLibHelper {
static final class StdLibHelper {

final static MethodHandle strcat = abi.downcallHandle(abi.defaultLookup().find("strcat").get(),
static final MethodHandle strcat = abi.downcallHandle(abi.defaultLookup().findOrThrow("strcat"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by cleanup to blessed order.

FunctionDescriptor.of(C_POINTER, C_POINTER, C_POINTER));

final static MethodHandle strcmp = abi.downcallHandle(abi.defaultLookup().find("strcmp").get(),
static final MethodHandle strcmp = abi.downcallHandle(abi.defaultLookup().findOrThrow("strcmp"),
FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER));

final static MethodHandle puts = abi.downcallHandle(abi.defaultLookup().find("puts").get(),
static final MethodHandle puts = abi.downcallHandle(abi.defaultLookup().findOrThrow("puts"),
FunctionDescriptor.of(C_INT, C_POINTER));

final static MethodHandle strlen = abi.downcallHandle(abi.defaultLookup().find("strlen").get(),
static final MethodHandle strlen = abi.downcallHandle(abi.defaultLookup().findOrThrow("strlen"),
FunctionDescriptor.of(C_INT, C_POINTER));

final static MethodHandle gmtime = abi.downcallHandle(abi.defaultLookup().find("gmtime").get(),
static final MethodHandle gmtime = abi.downcallHandle(abi.defaultLookup().findOrThrow("gmtime"),
FunctionDescriptor.of(C_POINTER.withTargetLayout(Tm.LAYOUT), C_POINTER));

// void qsort( void *ptr, size_t count, size_t size, int (*comp)(const void *, const void *) );
final static MethodHandle qsort = abi.downcallHandle(abi.defaultLookup().find("qsort").get(),
static final MethodHandle qsort = abi.downcallHandle(abi.defaultLookup().findOrThrow("qsort"),
FunctionDescriptor.ofVoid(C_POINTER, C_SIZE_T, C_SIZE_T, C_POINTER));

final static FunctionDescriptor qsortComparFunction = FunctionDescriptor.of(C_INT,
static final FunctionDescriptor qsortComparFunction = FunctionDescriptor.of(C_INT,
C_POINTER.withTargetLayout(C_INT), C_POINTER.withTargetLayout(C_INT));

final static MethodHandle qsortCompar;
static final MethodHandle qsortCompar;

final static MethodHandle rand = abi.downcallHandle(abi.defaultLookup().find("rand").get(),
static final MethodHandle rand = abi.downcallHandle(abi.defaultLookup().findOrThrow("rand"),
FunctionDescriptor.of(C_INT));

final static MethodHandle vprintf = abi.downcallHandle(abi.defaultLookup().find("vprintf").get(),
static final MethodHandle vprintf = abi.downcallHandle(abi.defaultLookup().findOrThrow("vprintf"),
FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER));

final static MemorySegment printfAddr = abi.defaultLookup().find("printf").get();
static final MemorySegment printfAddr = abi.defaultLookup().findOrThrow("printf");

final static FunctionDescriptor printfBase = FunctionDescriptor.of(C_INT, C_POINTER);
static final FunctionDescriptor printfBase = FunctionDescriptor.of(C_INT, C_POINTER);

static {
try {
Expand Down Expand Up @@ -225,7 +225,7 @@ Tm gmtime(long arg) throws Throwable {
}
}

static class Tm {
static final class Tm {

//Tm pointer should never be freed directly, as it points to shared memory
private final MemorySegment base;
Expand Down
10 changes: 5 additions & 5 deletions test/jdk/java/foreign/TestAccessModes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,10 +23,10 @@

/*
* @test
* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAccessModes
* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAccessModes
* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAccessModes
* @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAccessModes
* @run testng/othervm/timeout=600 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAccessModes
* @run testng/othervm/timeout=600 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAccessModes
* @run testng/othervm/timeout=600 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false -Xverify:all TestAccessModes
* @run testng/othervm/timeout=600 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true -Xverify:all TestAccessModes
*/

import java.lang.foreign.*;
Expand Down