summaryrefslogtreecommitdiff
path: root/snprintf.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-03-19 22:26:06 -0400
committerRich Felker <dalias@aerifal.cx>2011-03-19 22:26:06 -0400
commita9baddd7d07b9fe15e212985a808a79773ec72e4 (patch)
treefce0a089eedeacef8b25b409c2ba50f5f30a8327 /snprintf.c
downloadlibc-testsuite-a9baddd7d07b9fe15e212985a808a79773ec72e4.tar.gz
initial check-in, taken from old libc svn repo with significant additions
Diffstat (limited to 'snprintf.c')
-rw-r--r--snprintf.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/snprintf.c b/snprintf.c
new file mode 100644
index 0000000..1e563e5
--- /dev/null
+++ b/snprintf.c
@@ -0,0 +1,120 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <math.h>
+
+#define TEST(r, f, x, m) ( \
+((r) = (f)) == (x) || \
+(printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x), err++, 0) )
+
+#define TEST_S(s, x, m) ( \
+!strcmp((s),(x)) || \
+(printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )
+
+static const struct {
+ const char *fmt;
+ int i;
+ const char *expect;
+} int_tests[] = {
+ /* width, precision, alignment */
+ { "%04d", 12, "0012" },
+ { "%.3d", 12, "012" },
+ { "%3d", 12, " 12" },
+ { "%-3d", 12, "12 " },
+ { "%+3d", 12, "+12" },
+ { "%+-5d", 12, "+12 " },
+ { "%+- 5d", 12, "+12 " },
+ { "%- 5d", 12, " 12 " },
+ { "% d", 12, " 12" },
+ { "%0-5d", 12, "12 " },
+ { "%-05d", 12, "12 " },
+
+ /* ...explicit precision of 0 shall be no characters. */
+ { "%.0d", 0, "" },
+ { "%.0o", 0, "" },
+ { "%#.0d", 0, "" },
+ { "%#.0o", 0, "" },
+ { "%#.0x", 0, "" },
+
+ /* hex: test alt form and case */
+ { "%x", 63, "3f" },
+ { "%#x", 63, "0x3f" },
+ { "%X", 63, "3F" },
+
+ /* octal: test alt form */
+ { "%o", 15, "17" },
+ { "%#o", 15, "017" },
+
+ { NULL, 0.0, NULL }
+};
+
+static const struct {
+ const char *fmt;
+ double f;
+ const char *expect;
+} fp_tests[] = {
+ /* basic form, handling of exponent/precision for 0 */
+ { "%e", 0.0, "0.000000e+00" },
+ { "%f", 0.0, "0.000000" },
+ { "%g", 0.0, "0" },
+ { "%#g", 0.0, "0.00000" },
+
+ /* rounding */
+ { "%f", 1.1, "1.100000" },
+ { "%f", 1.2, "1.200000" },
+ { "%f", 1.3, "1.300000" },
+ { "%f", 1.4, "1.400000" },
+ { "%f", 1.5, "1.500000" },
+
+ /* correctness in DBL_DIG places */
+ { "%.15g", 1.23456789012345, "1.23456789012345" },
+
+ /* correct choice of notation for %g */
+ { "%g", 0.0001, "0.0001" },
+ { "%g", 0.00001, "1e-05" },
+ { "%g", 123456, "123456" },
+ { "%g", 1234567, "1.23457e+06" },
+ { "%.7g", 1234567, "1234567" },
+ { "%.7g", 12345678, "1.234568e+07" },
+
+ /* pi in double precision, printed to a few extra places */
+ { "%.15f", M_PI, "3.141592653589793" },
+ { "%.18f", M_PI, "3.141592653589793116" },
+
+ /* exact conversion of large integers */
+ { "%.0f", 340282366920938463463374607431768211456.0,
+ "340282366920938463463374607431768211456" },
+
+ { NULL, 0.0, NULL }
+};
+
+int test_snprintf(void)
+{
+ int i, j;
+ int err=0;
+ char b[500], *s;
+
+ strcpy(b, "xxxxxxxx");
+ TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d");
+ TEST_S(b, "123", "incorrect output");
+ TEST(i, b[5], 'x', "buffer overrun");
+
+#ifndef DISABLE_SLOW_TESTS
+ errno = 0;
+ TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d");
+ TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d");
+ TEST(i, errno, EOVERFLOW, "after overflow: %d != %d");
+#endif
+ for (j=0; int_tests[j].fmt; j++) {
+ TEST(i, snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i), strlen(b), "%d != %d");
+ TEST_S(b, int_tests[j].expect, "bad integer conversion");
+ }
+
+ for (j=0; fp_tests[j].fmt; j++) {
+ TEST(i, snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f), strlen(b), "%d != %d");
+ TEST_S(b, fp_tests[j].expect, "bad floating point conversion");
+ }
+
+ return err;
+}