Skip to content

Commit

Permalink
Fix StdLibTest for platforms with non standard native charset
Browse files Browse the repository at this point in the history
If native encoding charset is not one of the standard charset, allocate segment
using default charset for printf down call in StdLibTest.

Fixes: eclipse-openj9/openj9#20337

Signed-off-by: Rahil Shah <[email protected]>
  • Loading branch information
r30shah committed Oct 16, 2024
1 parent fe73bf0 commit ad70d34
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions test/jdk/java/foreign/StdLibTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public class StdLibTest extends NativeTestHelper {

final static Charset nativeCharset = Charset.forName(System.getProperty("native.encoding"));

static MemorySegment allocateSegmentForString(Arena arena, String str) {
try {
return arena.allocateFrom(str, nativeCharset);
} catch (IllegalArgumentException ex) {
return arena.allocateFrom(str);
}
}

private StdLibHelper stdLibHelper = new StdLibHelper();

@Test(dataProvider = "stringPairs")
Expand Down Expand Up @@ -315,7 +323,7 @@ int rand() throws Throwable {

int printf(String format, List<PrintfArg> args) throws Throwable {
try (var arena = Arena.ofConfined()) {
MemorySegment formatStr = arena.allocateFrom(format, nativeCharset);
MemorySegment formatStr = allocateSegmentFromString(arena, format);
return (int)specializedPrintf(args).invokeExact(formatStr,
args.stream().map(a -> a.nativeValue(arena)).toArray());
}
Expand Down Expand Up @@ -396,7 +404,7 @@ enum PrintfArg {
INT(int.class, C_INT, "%d", "%d", arena -> 42, 42),
LONG(long.class, C_LONG_LONG, "%lld", "%d", arena -> 84L, 84L),
DOUBLE(double.class, C_DOUBLE, "%.4f", "%.4f", arena -> 1.2345d, 1.2345d),
STRING(MemorySegment.class, C_POINTER, "%s", "%s", arena -> arena.allocateFrom("str", nativeCharset), "str");
STRING(MemorySegment.class, C_POINTER, "%s", "%s", arena -> allocateSegmentFromString(arena, "str"), "str");

final Class<?> carrier;
final ValueLayout layout;
Expand Down

0 comments on commit ad70d34

Please sign in to comment.