Blame src/stdio/snprintf.c

nsz 1be84a
#define _XOPEN_SOURCE 700
nsz 1be84a
#include <stdio.h>
nsz 1be84a
#include <string.h>
nsz 1be84a
#include <errno.h>
nsz 1be84a
#include <limits.h>
nsz 1be84a
#include <math.h>
nsz 1be84a
#include "test.h"
nsz 1be84a
nsz 1be84a
#define DISABLE_SLOW_TESTS
nsz 1be84a
nsz 1be84a
#define TEST(r, f, x, m) ( \
nsz 1be84a
	((r) = (f)) == (x) || \
nsz 1be84a
	(error("%s failed (" m ")\n", #f, r, x), 0) )
nsz 1be84a
nsz 1be84a
#define TEST_S(s, x, m) ( \
nsz 1be84a
	!strcmp((s),(x)) || \
nsz 1be84a
	(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
nsz 1be84a
nsz 1be84a
static const struct {
nsz 1be84a
	const char *fmt;
nsz 1be84a
	int i;
nsz 1be84a
	const char *expect;
nsz 1be84a
} int_tests[] = {
nsz 1be84a
	/* width, precision, alignment */
nsz 1be84a
	{ "%04d", 12, "0012" },
nsz 1be84a
	{ "%.3d", 12, "012" },
nsz 1be84a
	{ "%3d", 12, " 12" },
nsz 1be84a
	{ "%-3d", 12, "12 " },
nsz 1be84a
	{ "%+3d", 12, "+12" },
nsz 1be84a
	{ "%+-5d", 12, "+12  " },
nsz 1be84a
	{ "%+- 5d", 12, "+12  " },
nsz 1be84a
	{ "%- 5d", 12, " 12  " },
nsz 1be84a
	{ "% d", 12, " 12" },
nsz 1be84a
	{ "%0-5d", 12, "12   " },
nsz 1be84a
	{ "%-05d", 12, "12   " },
nsz 1be84a
nsz 1be84a
	/* ...explicit precision of 0 shall be no characters. */
nsz 1be84a
	{ "%.0d", 0, "" },
nsz 1be84a
	{ "%.0o", 0, "" },
nsz 1be84a
	{ "%#.0d", 0, "" },
nsz 1be84a
	{ "%#.0o", 0, "" },
nsz 1be84a
	{ "%#.0x", 0, "" },
nsz 1be84a
nsz 1be84a
	/* ...but it still has to honor width and flags. */
nsz 1be84a
	{ "%2.0u", 0, "  " },
nsz 1be84a
	{ "%02.0u", 0, "  " },
nsz 1be84a
	{ "%2.0d", 0, "  " },
nsz 1be84a
	{ "%02.0d", 0, "  " },
nsz 1be84a
	{ "% .0d", 0, " " },
nsz 1be84a
	{ "%+.0d", 0, "+" },
nsz 1be84a
nsz 1be84a
	/* hex: test alt form and case */
nsz 1be84a
	{ "%x", 63, "3f" },
nsz 1be84a
	{ "%#x", 63, "0x3f" },
nsz 1be84a
	{ "%X", 63, "3F" },
nsz 1be84a
nsz 1be84a
	/* octal: test alt form */
nsz 1be84a
	{ "%o", 15, "17" },
nsz 1be84a
	{ "%#o", 15, "017" },
nsz 1be84a
nsz 1be84a
	{ NULL, 0.0, NULL }
nsz 1be84a
};
nsz 1be84a
nsz 1be84a
static const struct {
nsz 1be84a
	const char *fmt;
nsz 1be84a
	double f;
nsz 1be84a
	const char *expect;
nsz 1be84a
} fp_tests[] = {
nsz 1be84a
	/* basic form, handling of exponent/precision for 0 */
nsz 1be84a
	{ "%e", 0.0, "0.000000e+00" },
nsz 1be84a
	{ "%f", 0.0, "0.000000" },
nsz 1be84a
	{ "%g", 0.0, "0" },
nsz 1be84a
	{ "%#g", 0.0, "0.00000" },
nsz 1be84a
nsz 1be84a
	/* rounding */
nsz 1be84a
	{ "%f", 1.1, "1.100000" },
nsz 1be84a
	{ "%f", 1.2, "1.200000" },
nsz 1be84a
	{ "%f", 1.3, "1.300000" },
nsz 1be84a
	{ "%f", 1.4, "1.400000" },
nsz 1be84a
	{ "%f", 1.5, "1.500000" },
nsz 1be84a
	{ "%.4f", 1.06125, "1.0612" },
nsz 1be84a
	{ "%.2f", 1.375, "1.38" },
nsz 1be84a
	{ "%.1f", 1.375, "1.4" },
nsz 1be84a
	{ "%.15f", 1.1, "1.100000000000000" },
nsz 1be84a
	{ "%.16f", 1.1, "1.1000000000000001" },
nsz 1be84a
	{ "%.17f", 1.1, "1.10000000000000009" },
nsz 1be84a
	{ "%.2e", 1500001.0, "1.50e+06" },
nsz 1be84a
	{ "%.2e", 1505000.0, "1.50e+06" },
nsz 1be84a
	{ "%.2e", 1505000.00000095367431640625, "1.51e+06" },
nsz 1be84a
	{ "%.2e", 1505001.0, "1.51e+06" },
nsz 1be84a
	{ "%.2e", 1506000.0, "1.51e+06" },
nsz 1be84a
	
nsz 1be84a
	/* correctness in DBL_DIG places */
nsz 1be84a
	{ "%.15g", 1.23456789012345, "1.23456789012345" },
nsz 1be84a
nsz 1be84a
	/* correct choice of notation for %g */
nsz 1be84a
	{ "%g", 0.0001, "0.0001" },
nsz 1be84a
	{ "%g", 0.00001, "1e-05" },
nsz 1be84a
	{ "%g", 123456, "123456" },
nsz 1be84a
	{ "%g", 1234567, "1.23457e+06" },
nsz 1be84a
	{ "%.7g", 1234567, "1234567" },
nsz 1be84a
	{ "%.7g", 12345678, "1.234568e+07" },
nsz 1be84a
	{ "%.8g", 0.1, "0.1" },
nsz 1be84a
	{ "%.9g", 0.1, "0.1" },
nsz 1be84a
	{ "%.10g", 0.1, "0.1" },
nsz 1be84a
	{ "%.11g", 0.1, "0.1" },
nsz 1be84a
nsz 1be84a
	/* pi in double precision, printed to a few extra places */
nsz 1be84a
	{ "%.15f", M_PI, "3.141592653589793" },
nsz 1be84a
	{ "%.18f", M_PI, "3.141592653589793116" },
nsz 1be84a
nsz 1be84a
	/* exact conversion of large integers */
nsz 1be84a
	{ "%.0f", 340282366920938463463374607431768211456.0,
nsz 1be84a
	         "340282366920938463463374607431768211456" },
nsz 1be84a
nsz 1be84a
	{ NULL, 0.0, NULL }
nsz 1be84a
};
nsz 1be84a
nsz 1be84a
void test_snprintf(void)
nsz 1be84a
{
nsz 1be84a
	int i, j, k;
nsz 1be84a
	char b[2000], *s;
nsz 1be84a
nsz 1be84a
	TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d");
nsz 1be84a
	TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d");
nsz 1be84a
	TEST(i, snprintf(b, 0, "%.0s", "goodbye"), 0, "length returned %d != %d");
nsz 1be84a
nsz 1be84a
	strcpy(b, "xxxxxxxx");
nsz 1be84a
	TEST(i, snprintf(b, 4, "%d", 123456), 6, "length returned %d != %d");
nsz 1be84a
	TEST_S(b, "123", "incorrect output");
nsz 1be84a
	TEST(i, b[5], 'x', "buffer overrun");
nsz 1be84a
nsz 1be84a
	/* Perform ascii arithmetic to test printing tiny doubles */
nsz 1be84a
	TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d");
nsz 1be84a
	b[1] = '0';
nsz 1be84a
	for (i=0; i<1021; i++) {
nsz 1be84a
		for (k=0, j=1023; j>0; j--) {
nsz 1be84a
			if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0;
nsz 1be84a
			else b[j]+=b[j]-'0'-10+k, k=1;
nsz 1be84a
		}
nsz 1be84a
	}
nsz 1be84a
	TEST(i, b[1], '1', "'%c' != '%c'");
nsz 1be84a
	for (j=2; b[j]=='0'; j++);
nsz 1be84a
	TEST(i, j, 1024, "%d != %d");
nsz 1be84a
nsz 1be84a
nsz 1be84a
#ifndef DISABLE_SLOW_TESTS
nsz 1be84a
	errno = 0;
nsz 1be84a
	TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d");
nsz 1be84a
	TEST(i, snprintf(NULL, 0, "%.*u ", 2147483647, 0), -1, "integer overflow %d");
nsz 1be84a
	TEST(i, errno, EOVERFLOW, "after overflow: %d != %d");
nsz 1be84a
#endif
nsz 1be84a
	for (j=0; int_tests[j].fmt; j++) {
nsz 1be84a
		TEST(i, snprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i), strlen(b), "%d != %d");
nsz 1be84a
		TEST_S(b, int_tests[j].expect, "bad integer conversion");
nsz 1be84a
	}
nsz 1be84a
nsz 1be84a
	for (j=0; fp_tests[j].fmt; j++) {
nsz 1be84a
		TEST(i, snprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f), strlen(b), "%d != %d");
nsz 1be84a
		TEST_S(b, fp_tests[j].expect, "bad floating point conversion");
nsz 1be84a
	}
nsz 1be84a
nsz 1be84a
	TEST(i, snprintf(0, 0, "%.4a", 1.0), 11, "%d != %d");
nsz 1be84a
}