|
nsz |
462b4f |
#ifndef _XOPEN_SOURCE
|
|
nsz |
1be84a |
#define _XOPEN_SOURCE 700
|
|
nsz |
462b4f |
#endif
|
|
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) || \
|
|
Szabolcs Nagy |
cfa23c |
(t_error("%s failed (" m ")\n", #f, r, x), 0) )
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
#define TEST_S(s, x, m) ( \
|
|
nsz |
1be84a |
!wcscmp((s),(x)) || \
|
|
Szabolcs Nagy |
cfa23c |
(t_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 |
462b4f |
int main(void)
|
|
nsz |
462b4f |
{
|
|
nsz |
1be84a |
int i, j;
|
|
nsz |
a9d0a0 |
wchar_t b[500];
|
|
nsz |
1be84a |
|
|
nsz |
462b4f |
(void)(
|
|
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 |
462b4f |
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++) {
|
|
Szabolcs Nagy |
f10a03 |
i = swprintf(b, sizeof b, int_tests[j].fmt, int_tests[j].i);
|
|
Szabolcs Nagy |
f10a03 |
if (i != wcslen(int_tests[j].expect)) {
|
|
Szabolcs Nagy |
cfa23c |
t_error("swprintf(b, sizeof b, \"%ls\", %d) returned %d wanted %d\n",
|
|
Szabolcs Nagy |
f10a03 |
int_tests[j].fmt, int_tests[j].i, i, wcslen(int_tests[j].expect));
|
|
Szabolcs Nagy |
f10a03 |
}
|
|
Szabolcs Nagy |
f10a03 |
if (wcscmp(b, int_tests[j].expect) != 0)
|
|
Szabolcs Nagy |
cfa23c |
t_error("bad integer conversion: got \"%ls\", want \"%ls\"\n", b, int_tests[j].expect);
|
|
nsz |
1be84a |
}
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
for (j=0; fp_tests[j].fmt; j++) {
|
|
Szabolcs Nagy |
f10a03 |
i = swprintf(b, sizeof b, fp_tests[j].fmt, fp_tests[j].f);
|
|
Szabolcs Nagy |
f10a03 |
if (i != wcslen(fp_tests[j].expect)) {
|
|
Szabolcs Nagy |
cfa23c |
t_error("swprintf(b, sizeof b, \"%ls\", %f) returned %d wanted %d\n",
|
|
Szabolcs Nagy |
f10a03 |
fp_tests[j].fmt, fp_tests[j].f, i, wcslen(fp_tests[j].expect));
|
|
Szabolcs Nagy |
f10a03 |
}
|
|
Szabolcs Nagy |
f10a03 |
if (wcscmp(b, fp_tests[j].expect) != 0)
|
|
Szabolcs Nagy |
cfa23c |
t_error("bad floating-point conversion: got \"%ls\", want \"%ls\"\n", b, fp_tests[j].expect);
|
|
nsz |
1be84a |
}
|
|
Szabolcs Nagy |
cfa23c |
return t_status;
|
|
nsz |
1be84a |
}
|