summaryrefslogtreecommitdiff
path: root/tests/test-snprintf-posix.h
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2023-03-22 21:43:23 +0100
committerBruno Haible <bruno@clisp.org>2023-03-22 21:43:23 +0100
commitf61570c0ef38744c93865172dcbac80e7d4e7b23 (patch)
tree969f281aa83d72113adc702bcba758e3f155a6c3 /tests/test-snprintf-posix.h
parent806d6f857009f0920b74405004a8ea42b4adb23d (diff)
downloadgnulib-f61570c0ef38744c93865172dcbac80e7d4e7b23.tar.gz
*printf-posix: Fix implementation of %b directive.
* lib/vasnprintf.c (VASNPRINTF): In the %b directive implementation, fix the precision handling, and ignore the '0' flag when a width and a precision are both present. * tests/test-snprintf-posix.h (test_function): Add test cases for the %x directive and more test cases for 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. * modules/vasnwprintf-posix-tests (Files): Add m4/musl.m4. (configure.ac): Invoke gl_MUSL_LIBC.
Diffstat (limited to 'tests/test-snprintf-posix.h')
-rw-r--r--tests/test-snprintf-posix.h270
1 files changed, 270 insertions, 0 deletions
diff --git a/tests/test-snprintf-posix.h b/tests/test-snprintf-posix.h
index 05c7be8059..a8993f947a 100644
--- a/tests/test-snprintf-posix.h
+++ b/tests/test-snprintf-posix.h
@@ -3091,6 +3091,190 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...))
}
#endif
+ /* Test the support of the 'x' conversion specifier for hexadecimal output of
+ integers. */
+
+ { /* Zero. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%x %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), "%x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* A large positive number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%x %d", 0xFFFFFFFEU, 33, 44, 55);
+ ASSERT (strcmp (result, "fffffffe 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Width. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, " 303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Width given as argument. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%*x %d", 10, 12348, 33, 44, 55);
+ ASSERT (strcmp (result, " 303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Negative width given as argument (cf. FLAG_LEFT below). */
+ int retval =
+ my_snprintf (result, sizeof (result), "%*x %d", -10, 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%.10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "000000303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Zero precision and a positive number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%.0x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Zero precision and a zero number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%.0x %d", 0, 33, 44, 55);
+ /* ISO C and POSIX specify that "The result of converting a zero value
+ with a precision of zero is no characters." */
+ ASSERT (strcmp (result, " 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Width and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%15.10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, " 000000303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Padding and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%015.10x %d", 12348, 33, 44, 55);
+ /* Neither ISO C nor POSIX specify that the '0' flag is ignored when a width
+ and a precision are both present. But most implementations do so. */
+ #ifdef __MINGW32__
+ ASSERT (strcmp (result, "00000000000303c 33") == 0);
+ #else
+ ASSERT (strcmp (result, " 000000303c 33") == 0);
+ #endif
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_LEFT. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%-10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with zero. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#x %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), "%#x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "0x303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and width. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, " 0x303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and padding. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%0#10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "0x0000303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%0#.10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "0x000000303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and width and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#15.10x %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, " 0x000000303c 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and padding and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%0#15.10x %d", 12348, 33, 44, 55);
+ /* Neither ISO C nor POSIX specify that the '0' flag is ignored when a width
+ and a precision are both present. But most implementations do so. */
+ #ifdef __MINGW32__
+ ASSERT (strcmp (result, "0x000000000303c 33") == 0);
+ #else
+ ASSERT (strcmp (result, " 0x000000303c 33") == 0);
+ #endif
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a zero precision and a zero number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#.0x %d", 0, 33, 44, 55);
+ /* ISO C and POSIX specify that "The result of converting a zero value
+ with a precision of zero is no characters.", and the prefix is added
+ only for non-zero values. */
+ ASSERT (strcmp (result, " 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Uppercase 'X'. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%X %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "303C 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Uppercase 'X' with FLAG_ALT. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#X %d", 12348, 33, 44, 55);
+ ASSERT (strcmp (result, "0X303C 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Uppercase 'X' with FLAG_ALT and zero precision and a zero number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#.0X %d", 0, 33, 44, 55);
+ /* ISO C and POSIX specify that "The result of converting a zero value
+ with a precision of zero is no characters.", and the prefix is added
+ only for non-zero values. */
+ ASSERT (strcmp (result, " 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
/* Test the support of the 'b' conversion specifier for binary output of
integers. */
@@ -3136,6 +3320,45 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...))
ASSERT (retval == strlen (result));
}
+ { /* Precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%.20b %d", 12345, 33, 44, 55);
+ ASSERT (strcmp (result, "00000011000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Zero precision and a positive number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%.0b %d", 12345, 33, 44, 55);
+ ASSERT (strcmp (result, "11000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Zero precision and a zero number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%.0b %d", 0, 33, 44, 55);
+ /* ISO C and POSIX specify that "The result of converting a zero value
+ with a precision of zero is no characters." */
+ ASSERT (strcmp (result, " 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Width and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%25.20b %d", 12345, 33, 44, 55);
+ ASSERT (strcmp (result, " 00000011000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* Padding and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%025.20b %d", 12345, 33, 44, 55);
+ /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
+ a width and a precision are both present. But implementations do so. */
+ ASSERT (strcmp (result, " 00000011000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
{ /* FLAG_LEFT. */
int retval =
my_snprintf (result, sizeof (result), "%-20b %d", 12345, 33, 44, 55);
@@ -3156,4 +3379,51 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...))
ASSERT (strcmp (result, "0b11000000111001 33") == 0);
ASSERT (retval == strlen (result));
}
+
+ { /* FLAG_ALT with a positive number and width. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#20b %d", 12345, 33, 44, 55);
+ ASSERT (strcmp (result, " 0b11000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and padding. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%0#20b %d", 12345, 33, 44, 55);
+ ASSERT (strcmp (result, "0b000011000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%0#.20b %d", 12345, 33, 44, 55);
+ ASSERT (strcmp (result, "0b00000011000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and width and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#25.20b %d", 12345, 33, 44, 55);
+ ASSERT (strcmp (result, " 0b00000011000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a positive number and padding and precision. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%0#25.20b %d", 12345, 33, 44, 55);
+ /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
+ a width and a precision are both present. But implementations do so. */
+ ASSERT (strcmp (result, " 0b00000011000000111001 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
+
+ { /* FLAG_ALT with a zero precision and a zero number. */
+ int retval =
+ my_snprintf (result, sizeof (result), "%#.0b %d", 0, 33, 44, 55);
+ /* ISO C and POSIX specify that "The result of converting a zero value
+ with a precision of zero is no characters.", and the prefix is added
+ only for non-zero values. */
+ ASSERT (strcmp (result, " 33") == 0);
+ ASSERT (retval == strlen (result));
+ }
}