[PATCH 2/2] selftests/nolibc: add tests for printf zero padding (0)
From: licheng.li
Date: Fri Jan 30 2026 - 03:38:10 EST
From: Cheng Li <im.lechain@xxxxxxxxx>
This patch adds validation for the '0' flag in printf() to ensure correct
behavior for pointers, positive integers, and negative integers.
The tests specifically verify that:
- Pointers include the '0x' prefix before padding.
- Negative numbers include the '-' sign before padding.
- Width and padding are correctly calculated.
Signed-off-by: Cheng Li <im.lechain@xxxxxxxxx>
---
tools/testing/selftests/nolibc/nolibc-test.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index c11fcf6c5075..77bb8796aa3d 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1723,6 +1723,9 @@ 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(zero_pad_ptr); EXPECT_VFPRINTF(5, "0x005", "%05p", (void *)5); break;
+ CASE_TEST(zero_pad_int); EXPECT_VFPRINTF(5, "00005", "%05d", 5); break;
+ CASE_TEST(zero_pad_neg); EXPECT_VFPRINTF(5, "-0005", "%05d", -5); break;
CASE_TEST(number_left); EXPECT_VFPRINTF(10, "|-5 |", "|%-8d|", -5); break;
CASE_TEST(string_align); EXPECT_VFPRINTF(10, "|foo |", "|%-8s|", "foo"); break;
CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
--
2.52.0