Skip to content

Commit

Permalink
Merge pull request #62 from pshipton/stdlibtest
Browse files Browse the repository at this point in the history
(0.48) Fix StdLibTest for platforms with non standard native charset
  • Loading branch information
JasonFengJ9 authored Oct 18, 2024
2 parents b468dc5 + 4d53794 commit 2b0e819
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions test/jdk/java/foreign/StdLibTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public class StdLibTest extends NativeTestHelper {

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

static MemorySegment allocateSegmentForString(Arena arena, String str) {
// 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 +321,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 +402,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 2b0e819

Please sign in to comment.