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 17, 2024
1 parent fe73bf0 commit da9accb
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions test/jdk/java/foreign/StdLibTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ 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) {
// Allocate a memory segment using the native encoded bytes
byte[] nativeBytes = str.getBytes(nativeCharset);
return arena.allocateFrom(ValueLayout.JAVA_BYTE, Arrays.copyOf(nativeBytes, nativeBytes.length + 1));
}
}

private StdLibHelper stdLibHelper = new StdLibHelper();

@Test(dataProvider = "stringPairs")
Expand Down Expand Up @@ -315,7 +325,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 = allocateSegmentForString(arena, format);
return (int)specializedPrintf(args).invokeExact(formatStr,
args.stream().map(a -> a.nativeValue(arena)).toArray());
}
Expand Down Expand Up @@ -396,7 +406,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 -> allocateSegmentForString(arena, "str"), "str");

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

0 comments on commit da9accb

Please sign in to comment.