Blame src/stdio/swprintf.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 <wchar.h>
nsz 1be84a
#include <locale.h>
nsz 1be84a
#include <langinfo.h>
nsz 1be84a
#include "test.h"
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
	!wcscmp((s),(x)) || \
nsz 1be84a
	(error("[%ls] != [%ls] (%s)\n", s, x, m), 0) )
nsz 1be84a
nsz 1be84a
static const struct {
nsz 1be84a
	const wchar_t *fmt;
nsz 1be84a
	int i;
nsz 1be84a
	const wchar_t *expect;
nsz 1be84a
} int_tests[] = {
nsz 1be84a
	/* width, precision, alignment */
nsz 1be84a
	{ L"%04d", 12, L"0012" },
nsz 1be84a
	{ L"%.3d", 12, L"012" },
nsz 1be84a
	{ L"%3d", 12, L" 12" },
nsz 1be84a
	{ L"%-3d", 12, L"12 " },
nsz 1be84a
	{ L"%+3d", 12, L"+12" },
nsz 1be84a
	{ L"%+-5d", 12, L"+12  " },
nsz 1be84a
	{ L"%+- 5d", 12, L"+12  " },
nsz 1be84a
	{ L"%- 5d", 12, L" 12  " },
nsz 1be84a
	{ L"% d", 12, L" 12" },
nsz 1be84a
	{ L"%0-5d", 12, L"12   " },
nsz 1be84a
	{ L"%-05d", 12, L"12   " },
nsz 1be84a
nsz 1be84a
	/* ...explicit precision of 0 shall be no characters. */
nsz 1be84a
	{ L"%.0d", 0, L"" },
nsz 1be84a
	{ L"%.0o", 0, L"" },
nsz 1be84a
	{ L"%#.0d", 0, L"" },
nsz 1be84a
	{ L"%#.0o", 0, L"" },
nsz 1be84a
	{ L"%#.0x", 0, L"" },
nsz 1be84a
nsz 1be84a
	/* hex: test alt form and case */
nsz 1be84a
	{ L"%x", 63, L"3f" },
nsz 1be84a
	{ L"%#x", 63, L"0x3f" },
nsz 1be84a
	{ L"%X", 63, L"3F" },
nsz 1be84a
nsz 1be84a
	/* octal: test alt form */
nsz 1be84a
	{ L"%o", 15, L"17" },
nsz 1be84a
	{ L"%#o", 15, L"017" },
nsz 1be84a
nsz 1be84a
	{ NULL, 0.0, NULL }
nsz 1be84a
};
nsz 1be84a
nsz 1be84a
static const struct {
nsz 1be84a
	const wchar_t *fmt;
nsz 1be84a
	double f;
nsz 1be84a
	const wchar_t *expect;
nsz 1be84a
} fp_tests[] = {
nsz 1be84a
	/* basic form, handling of exponent/precision for 0 */
nsz 1be84a
	{ L"%e", 0.0, L"0.000000e+00" },
nsz 1be84a
	{ L"%f", 0.0, L"0.000000" },
nsz 1be84a
	{ L"%g", 0.0, L"0" },
nsz 1be84a
	{ L"%#g", 0.0, L"0.00000" },
nsz 1be84a
nsz 1be84a
	/* rounding */
nsz 1be84a
	{ L"%f", 1.1, L"1.100000" },
nsz 1be84a
	{ L"%f", 1.2, L"1.200000" },
nsz 1be84a
	{ L"%f", 1.3, L"1.300000" },
nsz 1be84a
	{ L"%f", 1.4, L"1.400000" },
nsz 1be84a
	{ L"%f", 1.5, L"1.500000" },
nsz 1be84a
	
nsz 1be84a
	/* correctness in DBL_DIG places */
nsz 1be84a
	{ L"%.15g", 1.23456789012345, L"1.23456789012345" },
nsz 1be84a
nsz 1be84a
	/* correct choice of notation for %g */
nsz 1be84a
	{ L"%g", 0.0001, L"0.0001" },
nsz 1be84a
	{ L"%g", 0.00001, L"1e-05" },
nsz 1be84a
	{ L"%g", 123456, L"123456" },
nsz 1be84a
	{ L"%g", 1234567, L"1.23457e+06" },
nsz 1be84a
	{ L"%.7g", 1234567, L"1234567" },
nsz 1be84a
	{ L"%.7g", 12345678, L"1.234568e+07" },
nsz 1be84a
nsz 1be84a
	/* pi in double precision, printed to a few extra places */
nsz 1be84a
	{ L"%.15f", M_PI, L"3.141592653589793" },
nsz 1be84a
	{ L"%.18f", M_PI, L"3.141592653589793116" },
nsz 1be84a
nsz 1be84a
	/* exact conversion of large integers */
nsz 1be84a
	{ L"%.0f", 340282366920938463463374607431768211456.0,
nsz 1be84a
	         L"340282366920938463463374607431768211456" },
nsz 1be84a
nsz 1be84a
	{ NULL, 0.0, NULL }
nsz 1be84a
};
nsz 1be84a
nsz 1be84a
void test_swprintf(void) {
nsz 1be84a
	int i, j;
nsz 1be84a
	wchar_t b[500], *s;
nsz 1be84a
nsz 1be84a
	setlocale(LC_CTYPE, "en_US.UTF-8") ||
nsz 1be84a
	setlocale(LC_CTYPE, "en_GB.UTF-8") ||
nsz 1be84a
	setlocale(LC_CTYPE, "en.UTF-8") ||
nsz 1be84a
	setlocale(LC_CTYPE, "POSIX.UTF-8") ||
nsz 1be84a
	setlocale(LC_CTYPE, "C.UTF-8") ||
nsz 1be84a
	setlocale(LC_CTYPE, "UTF-8") ||
nsz 1be84a
	setlocale(LC_CTYPE, "");
nsz 1be84a
nsz 1be84a
	TEST(i, strcmp(nl_langinfo(CODESET), "UTF-8"), 0, "no UTF-8 locale; tests might fail");
nsz 1be84a
nsz 1be84a
	TEST(i, swprintf(0, 0, L"%d", 123456)<0, 1, "%d != %d");
nsz 1be84a
nsz 1be84a
	TEST(i, swprintf(b, 2, L"%lc", 0xc0), 1, "%d != %d");
nsz 1be84a
	TEST(i, b[0], 0xc0, "wrong character %x != %x");
nsz 1be84a
	TEST(i, swprintf(b, 2, L"%lc", 0x20ac), 1, "%d != %d");
nsz 1be84a
	TEST(i, b[0], 0x20ac, "wrong character %x != %x");
nsz 1be84a
	TEST(i, swprintf(b, 3, L"%s", "\xc3\x80!"), 2, "%d != %d");
nsz 1be84a
	TEST(i, b[0], 0xc0, "wrong character %x != %x");
nsz 1be84a
	TEST(i, swprintf(b, 2, L"%.1s", "\xc3\x80!"), 1, "%d != %d");
nsz 1be84a
	TEST(i, b[0], 0xc0, "wrong character %x != %x");
nsz 1be84a
nsz 1be84a
	wcscpy(b, L"xxxxxxxx");
nsz 1be84a
	TEST(i, swprintf(b, 4, L"%d", 123456)<0, 1, "%d != %d");
nsz 1be84a
	TEST_S(b, L"123", "incorrect output");
nsz 1be84a
	TEST(i, b[5], 'x', "buffer overrun");
nsz 1be84a
nsz 1be84a
	for (j=0; int_tests[j].fmt; j++) {
nsz 1be84a
		TEST(i, swprintf(b, sizeof b/sizeof *b, int_tests[j].fmt, int_tests[j].i), wcslen(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, swprintf(b, sizeof b/sizeof *b, fp_tests[j].fmt, fp_tests[j].f), wcslen(b), "%d != %d");
nsz 1be84a
		TEST_S(b, fp_tests[j].expect, "bad floating point conversion");
nsz 1be84a
	}
nsz 1be84a
}