[PATCH v3 next 17/17] selftests/nolibc: Use printf variable field widths and precisions

From: david . laight . linux

Date: Mon Feb 23 2026 - 05:18:39 EST


From: David Laight <david.laight.linux@xxxxxxxxx>

Now that printf supports '*' for field widths and precisions
then can be used to simplify the test output.
- aligning the "[OK]" strings.
- reporting the expected sprintf() output when there is a mismatch.

Acked-by: Willy Tarreau <w@xxxxxx>
Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
---

Changes for v3:
- Change code in expect_vfprintf() (code added in v3).

tools/testing/selftests/nolibc/nolibc-test.c | 26 ++++----------------
1 file changed, 5 insertions(+), 21 deletions(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index b1f27fb1dd74..6c043e5e1220 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -156,21 +156,6 @@ static const char *errorname(int err)
}
}

-static void align_result(size_t llen)
-{
- const size_t align = 64;
- char buf[align];
- size_t n;
-
- if (llen >= align)
- return;
-
- n = align - llen;
- memset(buf, ' ', n);
- buf[n] = '\0';
- fputs(buf, stdout);
-}
-
enum RESULT {
OK,
FAIL,
@@ -188,8 +173,10 @@ static void result(int llen, enum RESULT r)
else
msg = " [FAIL]";

- align_result(llen);
- puts(msg);
+ llen = 64 - llen;
+ if (llen < 0)
+ llen = 0;
+ printf("%*s%s\n", llen, "", msg);
}

/* The tests below are intended to be used by the macroes, which evaluate
@@ -1699,10 +1686,7 @@ static int expect_vfprintf(int llen, int cond, const char *expected, const char
}

if (memcmp(expected, buf, cmp_len) || buf[cmp_len]) {
- /* Copy and truncate until "%.*s" supported */
- memcpy(buf, expected, cmp_len);
- buf[cmp_len] = 0;
- llen += printf(" should be \"%s\"", buf);
+ llen += printf(" should be \"%.*s\"", VFPRINTF_LEN, expected);
result(llen, FAIL);
return 1;
}
--
2.39.5