aboutsummaryrefslogtreecommitdiff
path: root/libio/tst_swprintf.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2001-08-09 19:49:54 +0000
committerUlrich Drepper <drepper@redhat.com>2001-08-09 19:49:54 +0000
commit5569e0a6fb5c99cfb1b6b9ac020edfa95f710603 (patch)
tree1a56cb1137e45f94340256ce1e0fb4a746e48dbc /libio/tst_swprintf.c
parentcc7f258f32352916d508a6f034f51f14abfc7efe (diff)
downloadglibc-5569e0a6fb5c99cfb1b6b9ac020edfa95f710603.tar
glibc-5569e0a6fb5c99cfb1b6b9ac020edfa95f710603.tar.gz
glibc-5569e0a6fb5c99cfb1b6b9ac020edfa95f710603.tar.bz2
glibc-5569e0a6fb5c99cfb1b6b9ac020edfa95f710603.zip
Update.
* libio/vswprintf.c (_IO_vswprintf): Fix return value handling which is different from snprintf. * libio/tst_swprintf.c: Add tests for too small output buffer.
Diffstat (limited to 'libio/tst_swprintf.c')
-rw-r--r--libio/tst_swprintf.c52
1 files changed, 43 insertions, 9 deletions
diff --git a/libio/tst_swprintf.c b/libio/tst_swprintf.c
index 67d10a006d..dc16db0a0c 100644
--- a/libio/tst_swprintf.c
+++ b/libio/tst_swprintf.c
@@ -1,42 +1,76 @@
#include <stdio.h>
#include <wchar.h>
+#include <sys/types.h>
+
+
+static wchar_t buf[100];
+#define nbuf (sizeof (buf) / sizeof (buf[0]))
+static const struct
+{
+ size_t n;
+ const char *str;
+ ssize_t exp;
+} tests[] =
+ {
+ { nbuf, "hello world", 11 },
+ { 0, "hello world", -1 },
+ { 0, "", -1 },
+ { nbuf, "", 0 }
+ };
int
main (int argc, char *argv[])
{
- wchar_t buf[100];
int n;
int result = 0;
puts ("test 1");
- n = swprintf (buf, sizeof (buf) / sizeof (buf[0]), L"Hello %s", "world");
+ n = swprintf (buf, nbuf, L"Hello %s", "world");
if (n != 11)
{
printf ("incorrect return value: %d instead of 11\n", n);
result = 1;
}
-
- if (wcscmp (buf, L"Hello world") != 0)
+ else if (wcscmp (buf, L"Hello world") != 0)
{
printf ("incorrect string: L\"%ls\" instead of L\"Hello world\"\n", buf);
result = 1;
}
puts ("test 2");
- n = swprintf (buf, sizeof (buf) / sizeof (buf[0]), L"Is this >%g< 3.1?",
- 3.1);
+ n = swprintf (buf, nbuf, L"Is this >%g< 3.1?", 3.1);
if (n != 18)
-{
+ {
printf ("incorrect return value: %d instead of 18\n", n);
result = 1;
}
-
- if (wcscmp (buf, L"Is this >3.1< 3.1?") != 0)
+ else if (wcscmp (buf, L"Is this >3.1< 3.1?") != 0)
{
printf ("incorrect string: L\"%ls\" instead of L\"Is this >3.1< 3.1?\"\n",
buf);
result = 1;
}
+ for (n = 0; n < sizeof (tests) / sizeof (tests[0]); ++n)
+ {
+ ssize_t res = swprintf (buf, tests[n].n, L"%s", tests[n].str);
+
+ if (tests[n].exp < 0 && res >= 0)
+ {
+ printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
+ tests[n].n, tests[n].str);
+ result = 1;
+ }
+ else if (tests[n].exp >= 0 && tests[n].exp != res)
+ {
+ printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
+ tests[n].n, tests[n].str, tests[n].exp, res);
+ result = 1;
+ }
+ else
+ printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n",
+ tests[n].n, tests[n].str);
+ }
+
return result;
}