glibc/libio/tst_swprintf.c

70 lines
1.7 KiB
C
Raw Normal View History

2022-11-09 16:27:14 +08:00
#include <array_length.h>
2022-05-13 23:30:52 +08:00
#include <stdio.h>
2022-11-09 16:27:14 +08:00
#include <support/check.h>
2022-05-13 23:30:52 +08:00
#include <sys/types.h>
2022-11-09 16:27:14 +08:00
#include <wchar.h>
2022-05-13 23:30:52 +08:00
static wchar_t buf[100];
static const struct
{
size_t n;
const char *str;
ssize_t exp;
} tests[] =
{
2022-11-09 16:27:14 +08:00
{ array_length (buf), "hello world", 11 },
2022-05-13 23:30:52 +08:00
{ 0, "hello world", -1 },
2022-11-09 16:27:14 +08:00
{ 1, "hello world", -1 },
{ 2, "hello world", -1 },
{ 11, "hello world", -1 },
{ 12, "hello world", 11 },
2022-05-13 23:30:52 +08:00
{ 0, "", -1 },
2022-11-09 16:27:14 +08:00
{ array_length (buf), "", 0 }
2022-05-13 23:30:52 +08:00
};
2022-11-09 16:27:14 +08:00
static int
do_test (void)
2022-05-13 23:30:52 +08:00
{
size_t n;
2022-11-09 16:27:14 +08:00
TEST_COMPARE (swprintf (buf, array_length (buf), L"Hello %s", "world"), 11);
TEST_COMPARE_STRING_WIDE (buf, L"Hello world");
2022-05-13 23:30:52 +08:00
2022-11-09 16:27:14 +08:00
TEST_COMPARE (swprintf (buf, array_length (buf), L"Is this >%g< 3.1?", 3.1),
18);
TEST_COMPARE_STRING_WIDE (buf, L"Is this >3.1< 3.1?");
2022-05-13 23:30:52 +08:00
2022-11-09 16:27:14 +08:00
for (n = 0; n < array_length (tests); ++n)
2022-05-13 23:30:52 +08:00
{
ssize_t res = swprintf (buf, tests[n].n, L"%s", tests[n].str);
if (tests[n].exp < 0 && res >= 0)
{
2022-11-09 16:27:14 +08:00
support_record_failure ();
2022-05-13 23:30:52 +08:00
printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
tests[n].n, tests[n].str);
}
else if (tests[n].exp >= 0 && tests[n].exp != res)
{
2022-11-09 16:27:14 +08:00
support_record_failure ();
printf ("\
swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
2022-05-13 23:30:52 +08:00
tests[n].n, tests[n].str, tests[n].exp, res);
}
else
printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n",
tests[n].n, tests[n].str);
}
2022-11-09 16:27:14 +08:00
TEST_COMPARE (swprintf (buf, array_length (buf), L"%.0s", "foo"), 0);
TEST_COMPARE_STRING_WIDE (buf, L"");
2022-05-13 23:30:52 +08:00
2022-11-09 16:27:14 +08:00
TEST_COMPARE (swprintf (buf, array_length (buf), L"%.0ls", L"foo"), 0);
TEST_COMPARE_STRING_WIDE (buf, L"");
2022-05-13 23:30:52 +08:00
2022-11-09 16:27:14 +08:00
return 0;
2022-05-13 23:30:52 +08:00
}
2022-11-09 16:27:14 +08:00
#include <support/test-driver.c>