From: david.laight.linux@gmail.com
To: "Willy Tarreau" <w@1wt.eu>,
"Thomas Weißschuh" <linux@weissschuh.net>,
linux-kernel@vger.kernel.org, "Cheng Li" <lechain@gmail.com>
Cc: David Laight <david.laight.linux@gmail.com>
Subject: [PATCH v2 next 10/11] selftests/nolibc: Increase coverage of printf format tests
Date: Fri, 6 Feb 2026 19:11:20 +0000 [thread overview]
Message-ID: <20260206191121.3602-11-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20260206191121.3602-1-david.laight.linux@gmail.com>
From: David Laight <david.laight.linux@gmail.com>
Extra tests include:
- %%, including ignored modifiers.
- Invalid formats copied to output buffer (matches glibc).
- Left aligned output "%-..."
- Zero padding "%0...".
- "(nil)" for NULL pointers (matches glibc).
- Alternate form "%#x" (prepends 0x in non-zero).
- Field precision as well as width, printf("%.0d", 0) is "".
- Variable length width and precision "%*.*d".
- Length qualifiers L and ll.
- Conversion specifiers i and X.
- More 'corner' cases.
There are no explicit tests of long (l, t or z) because they would
have to differ between 32bit and 64bit.
Set the expected length to zero for all the non-truncating tests.
(Annoying when a test is changed.)
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
Changes for v2:
- Formally patch 11.
- Remove broken __attribute__ on expect_vfprintf().
- Add extra test for "#01x".
tools/testing/selftests/nolibc/nolibc-test.c | 49 +++++++++++++++-----
1 file changed, 37 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 9378a1f26c34..10ae6dcd71b8 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1727,20 +1727,45 @@ static int run_printf(int min, int max)
*/
switch (test + __LINE__ + 1) {
CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
- CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
- CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
- CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
- CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
- CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
- CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
- CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
- CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
- CASE_TEST(uintmax_t); EXPECT_VFPRINTF(20, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break;
- CASE_TEST(intmax_t); EXPECT_VFPRINTF(20, "-9223372036854775807", "%jd", 0x8000000000000001LL); break;
+ CASE_TEST(simple); EXPECT_VFPRINTF(0, "foo", "foo"); break;
+ CASE_TEST(string); EXPECT_VFPRINTF(0, "foo", "%s", "foo"); break;
+ CASE_TEST(number); EXPECT_VFPRINTF(0, "1234", "%d", 1234); break;
+ CASE_TEST(negnumber); EXPECT_VFPRINTF(0, "-1234", "%d", -1234); break;
+ CASE_TEST(unsigned); EXPECT_VFPRINTF(0, "12345", "%u", 12345); break;
+ CASE_TEST(signed_max); EXPECT_VFPRINTF(0, "2147483647", "%i", ~0u >> 1); break;
+ CASE_TEST(signed_min); EXPECT_VFPRINTF(0, "-2147483648", "%i", (~0u >> 1) + 1); break;
+ CASE_TEST(unsigned_max); EXPECT_VFPRINTF(0, "4294967295", "%u", ~0u); break;
+ CASE_TEST(char); EXPECT_VFPRINTF(0, "|c|d| e|", "|%c|%.0c|%4c|", 'c', 'd', 'e'); break;
+ CASE_TEST(hex); EXPECT_VFPRINTF(0, "|f|d|", "|%x|%X|", 0xf, 0xd); break;
+ CASE_TEST(pointer); EXPECT_VFPRINTF(0, "0x1", "%p", (void *) 0x1); break;
+ CASE_TEST(pointer_NULL); EXPECT_VFPRINTF(0, "|(nil)|(nil)|", "|%p|%.4p|", (void *)0, (void *)0); break;
+ CASE_TEST(string_NULL); EXPECT_VFPRINTF(0, "|(null)||(null)|", "|%s|%.5s|%.6s|", (void *)0, (void *)0, (void *)0); break;
+ CASE_TEST(percent); EXPECT_VFPRINTF(0, "a%d42%69%", "a%%d%d%%%d%%", 42, 69); break;
+ CASE_TEST(perc_qual); EXPECT_VFPRINTF(0, "a%d2", "a%-14l%d%d", 2); break;
+ CASE_TEST(invalid); EXPECT_VFPRINTF(0, "a%12yx3%y42%y", "a%12yx%d%y%d%y", 3, 42); break;
+ CASE_TEST(intmax_max); EXPECT_VFPRINTF(0, "9223372036854775807", "%lld", ~0ULL >> 1); break;
+ CASE_TEST(intmax_min); EXPECT_VFPRINTF(0, "-9223372036854775808", "%Li", (~0ULL >> 1) + 1); break;
+ CASE_TEST(uintmax_max); EXPECT_VFPRINTF(0, "18446744073709551615", "%ju", ~0ULL); break;
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(string_width); EXPECT_VFPRINTF(0, " 1", "%10s", "1"); break;
+ CASE_TEST(string_trunc); EXPECT_VFPRINTF(0, " 12345", "%10.5s", "1234567890"); break;
+ CASE_TEST(number_width); EXPECT_VFPRINTF(0, " 1", "%10d", 1); break;
+ CASE_TEST(number_left); EXPECT_VFPRINTF(0, "|-5 |", "|%-8d|", -5); break;
+ CASE_TEST(string_align); EXPECT_VFPRINTF(0, "|foo |", "|%-8s|", "foo"); break;
CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
+ CASE_TEST(width_tr_lft); EXPECT_VFPRINTF(25, "1 ", "%-25d", 1); break;
+ CASE_TEST(number_pad); EXPECT_VFPRINTF(0, "0000000005", "%010d", 5); break;
+ CASE_TEST(number_pad); EXPECT_VFPRINTF(0, "|0000000005|0x1234|", "|%010d|%#01x|", 5, 0x1234); break;
+ CASE_TEST(num_pad_neg); EXPECT_VFPRINTF(0, "-000000005", "%010d", -5); break;
+ CASE_TEST(num_pad_hex); EXPECT_VFPRINTF(0, "00fffffffb", "%010x", -5); break;
+ CASE_TEST(num_pad_trunc);EXPECT_VFPRINTF(40, " 0000000000", "%040d", 5); break; /* max 30 '0' can be added */
+ CASE_TEST(number_prec); EXPECT_VFPRINTF(0, " 00005", "%10.5d", 5); break;
+ CASE_TEST(num_prec_neg); EXPECT_VFPRINTF(0, " -00005", "%10.5d", -5); break;
+ CASE_TEST(num_prec_var); EXPECT_VFPRINTF(0, " -00005", "%*.*d", 10, 5, -5); break;
+ CASE_TEST(num_0_prec_0); EXPECT_VFPRINTF(0, "|| |+||||", "|%.0d|% .0d|%+.0d|%.0u|%.0x|%#.0x|", 0, 0, 0, 0, 0, 0); break;
+ CASE_TEST(hex_alt); EXPECT_VFPRINTF(0, "|0x1|0x01| 0x02|", "|%#x|%#04x|%#5.2x|", 1, 1, 2); break;
+ CASE_TEST(hex_0_alt); EXPECT_VFPRINTF(0, "|0|0000| 00|", "|%#x|%#04x|%#5.2x|", 0, 0, 0); break;
+ CASE_TEST(dec_alt); EXPECT_VFPRINTF(0, "|1|0001| 02|", "|%#d|%#04d|%#5.2d|", 1, 1, 2); break; /* '#' is ignored */
CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break;
CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
--
2.39.5
next prev parent reply other threads:[~2026-02-06 19:11 UTC|newest]
Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-06 19:11 [PATCH v2 next 00/11] tools/nolibc: Enhance printf() david.laight.linux
2026-02-06 19:11 ` [PATCH v2 next 01/11] tools/nolibc/printf: Change variable used for format chars from 'c' to 'ch' david.laight.linux
2026-02-07 18:51 ` Willy Tarreau
2026-02-16 18:52 ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 02/11] tools/nolibc/printf: Move snprintf length check to callback david.laight.linux
2026-02-07 19:12 ` Willy Tarreau
2026-02-07 23:28 ` David Laight
2026-02-08 15:12 ` Willy Tarreau
2026-02-08 22:49 ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 03/11] tools/nolibc/printf: Add buffering to vfprintf() callback david.laight.linux
2026-02-07 19:29 ` Willy Tarreau
2026-02-07 23:36 ` David Laight
2026-02-16 19:07 ` Thomas Weißschuh
2026-02-17 11:51 ` David Laight
2026-02-18 17:52 ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 04/11] tools/nolibc/printf: Output pad characters in 16 byte chunks david.laight.linux
2026-02-07 19:38 ` Willy Tarreau
2026-02-07 23:43 ` David Laight
2026-02-08 15:14 ` Willy Tarreau
2026-02-16 19:30 ` Thomas Weißschuh
2026-02-16 22:29 ` David Laight
2026-02-18 17:30 ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 05/11] tools/nolibc/printf: Simplify __nolibc_printf() david.laight.linux
2026-02-07 20:05 ` Willy Tarreau
2026-02-07 23:50 ` David Laight
2026-02-08 12:20 ` David Laight
2026-02-08 14:44 ` Willy Tarreau
2026-02-08 16:54 ` David Laight
2026-02-08 17:06 ` Willy Tarreau
2026-02-06 19:11 ` [PATCH v2 next 06/11] tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars david.laight.linux
2026-02-08 15:22 ` Willy Tarreau
2026-02-16 19:52 ` Thomas Weißschuh
2026-02-16 22:47 ` David Laight
2026-02-18 17:36 ` Thomas Weißschuh
2026-02-18 22:57 ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 07/11] tools/nolibc/printf: Add support for conversion flags "#- +" and format "%X" david.laight.linux
2026-02-08 15:47 ` Willy Tarreau
2026-02-08 17:14 ` David Laight
2026-02-08 16:06 ` Willy Tarreau
2026-02-16 19:57 ` Thomas Weißschuh
2026-02-16 22:50 ` David Laight
2026-02-18 17:39 ` Thomas Weißschuh
2026-02-16 20:11 ` Thomas Weißschuh
2026-02-16 22:52 ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 08/11] tools/nolibc/printf: Add support for zero padding and field precision david.laight.linux
2026-02-08 16:16 ` Willy Tarreau
2026-02-08 17:31 ` David Laight
2026-02-06 19:11 ` [PATCH v2 next 09/11] selftests/nolibc: Improve reporting of vfprintf() errors david.laight.linux
2026-02-16 20:05 ` Thomas Weißschuh
2026-02-17 10:48 ` David Laight
2026-02-18 17:48 ` Thomas Weißschuh
2026-02-06 19:11 ` david.laight.linux [this message]
2026-02-16 20:14 ` [PATCH v2 next 10/11] selftests/nolibc: Increase coverage of printf format tests Thomas Weißschuh
2026-02-16 20:23 ` Thomas Weißschuh
2026-02-16 22:54 ` David Laight
2026-02-18 17:41 ` Thomas Weißschuh
2026-02-06 19:11 ` [PATCH v2 next 11/11] selftests/nolibc: Use printf("%.*s", n, "") to align output david.laight.linux
2026-02-08 16:20 ` Willy Tarreau
2026-02-16 20:22 ` Thomas Weißschuh
2026-02-06 21:36 ` [PATCH v2 next 00/11] tools/nolibc: Enhance printf() David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260206191121.3602-11-david.laight.linux@gmail.com \
--to=david.laight.linux@gmail.com \
--cc=lechain@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=w@1wt.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.