[PATCH v2 2/2] selftests/nolibc: add tests for printf left alignment and zero padding

From: licheng.li

Date: Wed Jan 28 2026 - 04:45:38 EST


From: Cheng Li <im.lechain@xxxxxxxxx>

This patch adds validation for the recently added left-alignment ('-')
and zero-padding ('0') flags in printf().

It ensures that:
- Fields are correctly padded with spaces on the right when the '-'
flag is used.
- Integers are correctly padded with zeroes on the left when the '0'
flag is used.

Signed-off-by: Cheng Li <im.lechain@xxxxxxxxx>
---
v2 changes:
- Added test cases for zero padding (%08d)
---
tools/testing/selftests/nolibc/nolibc-test.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 3c5a226dad3a..47b20ecf5242 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1723,6 +1723,8 @@ 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(leading_zero); EXPECT_VFPRINTF(10, "|00000001|", "|%08d|", 1); break;
+ CASE_TEST(left_align); EXPECT_VFPRINTF(10, "|foo |", "|%-8s|", "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