Skip to content

Commit 4f680fb

Browse files
committed
Fix StdLibTest for platforms with non standard native charset
Allocate segment using native encoded bytes for the string for printf down call in StdLibTest. Signed-off-by: Rahil Shah <[email protected]>
1 parent d0f338d commit 4f680fb

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

test/jdk/java/foreign/StdLibTest.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public class StdLibTest extends NativeTestHelper {
6262

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

65+
static MemorySegment allocateSegmentForString(Arena arena, String str) {
66+
// Allocate a memory segment using the native-encoded bytes.
67+
byte[] nativeBytes = str.getBytes(nativeCharset);
68+
return arena.allocateFrom(ValueLayout.JAVA_BYTE, Arrays.copyOf(nativeBytes, nativeBytes.length + 1));
69+
}
70+
6571
private StdLibHelper stdLibHelper = new StdLibHelper();
6672

6773
@Test(dataProvider = "stringPairs")
@@ -315,7 +321,7 @@ int rand() throws Throwable {
315321

316322
int printf(String format, List<PrintfArg> args) throws Throwable {
317323
try (var arena = Arena.ofConfined()) {
318-
MemorySegment formatStr = arena.allocateFrom(format, nativeCharset);
324+
MemorySegment formatStr = allocateSegmentForString(arena, format);
319325
return (int)specializedPrintf(args).invokeExact(formatStr,
320326
args.stream().map(a -> a.nativeValue(arena)).toArray());
321327
}
@@ -396,7 +402,7 @@ enum PrintfArg {
396402
INT(int.class, C_INT, "%d", "%d", arena -> 42, 42),
397403
LONG(long.class, C_LONG_LONG, "%lld", "%d", arena -> 84L, 84L),
398404
DOUBLE(double.class, C_DOUBLE, "%.4f", "%.4f", arena -> 1.2345d, 1.2345d),
399-
STRING(MemorySegment.class, C_POINTER, "%s", "%s", arena -> arena.allocateFrom("str", nativeCharset), "str");
405+
STRING(MemorySegment.class, C_POINTER, "%s", "%s", arena -> allocateSegmentForString(arena, "str"), "str");
400406

401407
final Class<?> carrier;
402408
final ValueLayout layout;

0 commit comments

Comments
 (0)