[PATCH 2/2] selftests/nolibc: add tests for printf left alignment

From: licheng.li

Date: Sun Jan 25 2026 - 22:10:53 EST


From: Cheng Li <im.lechain@xxxxxxxxx>

This patch adds validation for the recently added '-' flag support in
printf(). It ensures that strings and numbers are correctly padded
with spaces on the right side when the left-alignment flag is used.

The test adds minimal overhead to the test binary size.
On x86_64:

text data bss dec hex filename
43677 248 112 44037 ac05 nolibc-test (before)
43761 248 112 44121 ac59 nolibc-test (after)

The test logic adds 84 bytes.

Signed-off-by: Cheng Li <im.lechain@xxxxxxxxx>
---
tools/testing/selftests/nolibc/nolibc-test.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 3c5a226dad3a..3d47711cad8f 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1723,6 +1723,7 @@ static int run_printf(int min, int max)
CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break;
CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1"); break;
CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); break;
+ CASE_TEST(left_align); EXPECT_VFPRINTF(10, "foo |", "%-9s|", "foo"); break;
CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break;
--
2.52.0