diff options
author | Bruno Haible <bruno@clisp.org> | 2023-03-17 22:45:56 +0100 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2023-03-17 22:45:56 +0100 |
commit | bb7cf2b64d2caf381249dda65ee0fe6766d9b3c6 (patch) | |
tree | 8f677d8de76d0bdbcd61ec347e926ad1bd325e3a /tests | |
parent | 21ed6e8365e371b9088df727d0449133df58e9bc (diff) | |
download | gnulib-bb7cf2b64d2caf381249dda65ee0fe6766d9b3c6.tar.gz |
*printf-posix: ISO C 23: Add %b directive for binary output of integers.
* lib/printf-parse.c (PRINTF_PARSE): Recognize the 'b' directive.
* lib/printf-parse.h: Update comment.
* lib/wprintf-parse.h: Likewise.
* lib/vasnprintf.c (MAX_ROOM_NEEDED, VASNPRINTF): Add support for the
'b' directive.
* m4/printf.m4 (gl_PRINTF_DIRECTIVE_B): New macro.
* m4/vasnprintf.m4 (gl_PREREQ_VASNPRINTF_DIRECTIVE_B): New macro.
(gl_PREREQ_VASNPRINTF_WITH_EXTRAS): Invoke it.
* m4/vasnwprintf-posix.m4 (gl_FUNC_VASNWPRINTF_POSIX): Invoke
gl_PREREQ_VASNPRINTF_DIRECTIVE_B.
* m4/dprintf-posix.m4 (gl_FUNC_DPRINTF_POSIX): Require
gl_PRINTF_DIRECTIVE_B and test its result. Invoke
gl_PREREQ_VASNPRINTF_DIRECTIVE_B.
* m4/fprintf-posix.m4 (gl_FUNC_FPRINTF_POSIX): Likewise.
* m4/obstack-printf-posix.m4 (gl_FUNC_OBSTACK_PRINTF_POSIX): Likewise.
* m4/snprintf-posix.m4 (gl_FUNC_SNPRINTF_POSIX): Likewise.
* m4/sprintf-posix.m4 (gl_FUNC_SPRINTF_POSIX): Likewise.
* m4/vasnprintf-posix.m4 (gl_FUNC_VASNPRINTF_POSIX): Likewise.
* m4/vasprintf-posix.m4 (gl_FUNC_VASPRINTF_POSIX): Likewise.
* m4/vdprintf-posix.m4 (gl_FUNC_VDPRINTF_POSIX): Likewise.
* m4/vfprintf-posix.m4 (gl_FUNC_VFPRINTF_POSIX): Likewise.
* m4/vsnprintf-posix.m4 (gl_FUNC_VSNPRINTF_POSIX): Likewise.
* m4/vsprintf-posix.m4 (gl_FUNC_VSPRINTF_POSIX): Likewise.
* tests/test-snprintf-posix.h (test_function): Add some tests of the %b
directive.
* tests/test-sprintf-posix.h (test_function): Likewise.
* tests/test-vasnprintf-posix.c (test_function): Likewise.
* tests/test-vasnwprintf-posix.c (test_function): Likewise.
* tests/test-vasprintf-posix.c (test_function): Likewise.
* doc/glibc-functions/asprintf.texi: Mention the 'b' directive.
* doc/glibc-functions/obstack_printf.texi: Likewise.
* doc/glibc-functions/obstack_vprintf.texi: Likewise.
* doc/glibc-functions/vasprintf.texi: Likewise.
* doc/posix-functions/dprintf.texi: Likewise.
* doc/posix-functions/fprintf.texi: Likewise.
* doc/posix-functions/fwprintf.texi: Likewise.
* doc/posix-functions/printf.texi: Likewise.
* doc/posix-functions/snprintf.texi: Likewise.
* doc/posix-functions/sprintf.texi: Likewise.
* doc/posix-functions/swprintf.texi: Likewise.
* doc/posix-functions/vdprintf.texi: Likewise.
* doc/posix-functions/vfprintf.texi: Likewise.
* doc/posix-functions/vfwprintf.texi: Likewise.
* doc/posix-functions/vprintf.texi: Likewise.
* doc/posix-functions/vsnprintf.texi: Likewise.
* doc/posix-functions/vsprintf.texi: Likewise.
* doc/posix-functions/vswprintf.texi: Likewise.
* doc/posix-functions/vwprintf.texi: Likewise.
* doc/posix-functions/wprintf.texi: Likewise.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-snprintf-posix.h | 68 | ||||
-rw-r--r-- | tests/test-sprintf-posix.h | 68 | ||||
-rw-r--r-- | tests/test-vasnprintf-posix.c | 93 | ||||
-rw-r--r-- | tests/test-vasnwprintf-posix.c | 93 | ||||
-rw-r--r-- | tests/test-vasprintf-posix.c | 93 |
5 files changed, 415 insertions, 0 deletions
diff --git a/tests/test-snprintf-posix.h b/tests/test-snprintf-posix.h index 4db76deb75..579d34583d 100644 --- a/tests/test-snprintf-posix.h +++ b/tests/test-snprintf-posix.h @@ -435,6 +435,8 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) { /* Positive infinity. */ int retval = my_snprintf (result, sizeof (result), "%La %d", Infinityl (), 33, 44, 55); + /* Note: This assertion fails under valgrind. + Reported at <https://bugs.kde.org/show_bug.cgi?id=424044>. */ ASSERT (strcmp (result, "inf 33") == 0); ASSERT (retval == strlen (result)); } @@ -3053,4 +3055,70 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (retval == strlen (result)); } #endif + + /* Test the support of the 'b' conversion specifier for binary output of + integers. */ + + { /* Zero. */ + int retval = + my_snprintf (result, sizeof (result), "%b %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* A positive number. */ + int retval = + my_snprintf (result, sizeof (result), "%b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* A large positive number. */ + int retval = + my_snprintf (result, sizeof (result), "%b %d", 0xFFFFFFFEU, 33, 44, 55); + ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Width. */ + int retval = + my_snprintf (result, sizeof (result), "%20b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Width given as argument. */ + int retval = + my_snprintf (result, sizeof (result), "%*b %d", 20, 12345, 33, 44, 55); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Negative width given as argument (cf. FLAG_LEFT below). */ + int retval = + my_snprintf (result, sizeof (result), "%*b %d", -20, 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_LEFT. */ + int retval = + my_snprintf (result, sizeof (result), "%-20b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with zero. */ + int retval = + my_snprintf (result, sizeof (result), "%#b %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number. */ + int retval = + my_snprintf (result, sizeof (result), "%#b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0b11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } } diff --git a/tests/test-sprintf-posix.h b/tests/test-sprintf-posix.h index 979254acc7..863d084b99 100644 --- a/tests/test-sprintf-posix.h +++ b/tests/test-sprintf-posix.h @@ -421,6 +421,8 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) { /* Positive infinity. */ int retval = my_sprintf (result, "%La %d", Infinityl (), 33, 44, 55); + /* Note: This assertion fails under valgrind. + Reported at <https://bugs.kde.org/show_bug.cgi?id=424044>. */ ASSERT (strcmp (result, "inf 33") == 0); ASSERT (retval == strlen (result)); } @@ -3035,4 +3037,70 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (retval == strlen (result)); } #endif + + /* Test the support of the 'b' conversion specifier for binary output of + integers. */ + + { /* Zero. */ + int retval = + my_sprintf (result, "%b %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* A positive number. */ + int retval = + my_sprintf (result, "%b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* A large positive number. */ + int retval = + my_sprintf (result, "%b %d", 0xFFFFFFFEU, 33, 44, 55); + ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Width. */ + int retval = + my_sprintf (result, "%20b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Width given as argument. */ + int retval = + my_sprintf (result, "%*b %d", 20, 12345, 33, 44, 55); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* Negative width given as argument (cf. FLAG_LEFT below). */ + int retval = + my_sprintf (result, "%*b %d", -20, 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_LEFT. */ + int retval = + my_sprintf (result, "%-20b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with zero. */ + int retval = + my_sprintf (result, "%#b %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + } + + { /* FLAG_ALT with a positive number. */ + int retval = + my_sprintf (result, "%#b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0b11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + } } diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c index 4a83f6d243..c6e1e2f650 100644 --- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -562,6 +562,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%La %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); + /* Note: This assertion fails under valgrind. + Reported at <https://bugs.kde.org/show_bug.cgi?id=424044>. */ ASSERT (strcmp (result, "inf 33") == 0); ASSERT (length == strlen (result)); free (result); @@ -3974,6 +3976,97 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) } #endif + /* Test the support of the 'b' conversion specifier for binary output of + integers. */ + + { /* Zero. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%b %d", 0, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* A positive number. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* A large positive number. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%b %d", 0xFFFFFFFEU, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Width. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%20b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Width given as argument. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%*b %d", 20, 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Negative width given as argument (cf. FLAG_LEFT below). */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%*b %d", -20, 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* FLAG_LEFT. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%-20b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* FLAG_ALT with zero. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%#b %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* FLAG_ALT with a positive number. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%#b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0b11000000111001 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) && !defined __UCLIBC__ /* Test that the 'I' flag is supported. */ { diff --git a/tests/test-vasnwprintf-posix.c b/tests/test-vasnwprintf-posix.c index f686bfb9fe..c609a104ff 100644 --- a/tests/test-vasnwprintf-posix.c +++ b/tests/test-vasnwprintf-posix.c @@ -562,6 +562,8 @@ test_function (wchar_t * (*my_asnwprintf) (wchar_t *, size_t *, const wchar_t *, wchar_t *result = my_asnwprintf (NULL, &length, L"%La %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); + /* Note: This assertion fails under valgrind. + Reported at <https://bugs.kde.org/show_bug.cgi?id=424044>. */ ASSERT (wcscmp (result, L"inf 33") == 0); ASSERT (length == wcslen (result)); free (result); @@ -3982,6 +3984,97 @@ test_function (wchar_t * (*my_asnwprintf) (wchar_t *, size_t *, const wchar_t *, } #endif + /* Test the support of the 'b' conversion specifier for binary output of + integers. */ + + { /* Zero. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%b %d", 0, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (wcscmp (result, L"0 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* A positive number. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (wcscmp (result, L"11000000111001 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* A large positive number. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%b %d", 0xFFFFFFFEU, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (wcscmp (result, L"11111111111111111111111111111110 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* Width. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%20b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (wcscmp (result, L" 11000000111001 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* Width given as argument. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%*b %d", 20, 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (wcscmp (result, L" 11000000111001 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* Negative width given as argument (cf. FLAG_LEFT below). */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%*b %d", -20, 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (wcscmp (result, L"11000000111001 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* FLAG_LEFT. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%-20b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (wcscmp (result, L"11000000111001 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* FLAG_ALT with zero. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%#b %d", 0, 33, 44, 55); + ASSERT (wcscmp (result, L"0 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + + { /* FLAG_ALT with a positive number. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%#b %d", 12345, 33, 44, 55); + ASSERT (wcscmp (result, L"0b11000000111001 33") == 0); + ASSERT (length == wcslen (result)); + free (result); + } + #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) && !defined __UCLIBC__ /* Test that the 'I' flag is supported. */ { diff --git a/tests/test-vasprintf-posix.c b/tests/test-vasprintf-posix.c index 8563502c47..13b3a1bca9 100644 --- a/tests/test-vasprintf-posix.c +++ b/tests/test-vasprintf-posix.c @@ -543,6 +543,8 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) int retval = my_asprintf (&result, "%La %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); + /* Note: This assertion fails under valgrind. + Reported at <https://bugs.kde.org/show_bug.cgi?id=424044>. */ ASSERT (strcmp (result, "inf 33") == 0); ASSERT (retval == strlen (result)); free (result); @@ -3914,6 +3916,97 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) free (result); } #endif + + /* Test the support of the 'b' conversion specifier for binary output of + integers. */ + + { /* Zero. */ + char *result; + int retval = + my_asprintf (&result, "%b %d", 0, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* A positive number. */ + char *result; + int retval = + my_asprintf (&result, "%b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* A large positive number. */ + char *result; + int retval = + my_asprintf (&result, "%b %d", 0xFFFFFFFEU, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Width. */ + char *result; + int retval = + my_asprintf (&result, "%20b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Width given as argument. */ + char *result; + int retval = + my_asprintf (&result, "%*b %d", 20, 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, " 11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* Negative width given as argument (cf. FLAG_LEFT below). */ + char *result; + int retval = + my_asprintf (&result, "%*b %d", -20, 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* FLAG_LEFT. */ + char *result; + int retval = + my_asprintf (&result, "%-20b %d", 12345, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* FLAG_ALT with zero. */ + char *result; + int retval = + my_asprintf (&result, "%#b %d", 0, 33, 44, 55); + ASSERT (strcmp (result, "0 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { /* FLAG_ALT with a positive number. */ + char *result; + int retval = + my_asprintf (&result, "%#b %d", 12345, 33, 44, 55); + ASSERT (strcmp (result, "0b11000000111001 33") == 0); + ASSERT (retval == strlen (result)); + free (result); + } } static int |