Fix endless loop with invalid /etc/shells file.
[glibc.git] / libio / tst_swprintf.c
blobe4bd7f022a360658565dcfcf3b6cf9c21baeddcd
1 #include <stdio.h>
2 #include <wchar.h>
3 #include <sys/types.h>
6 static wchar_t buf[100];
7 #define nbuf (sizeof (buf) / sizeof (buf[0]))
8 static const struct
10 size_t n;
11 const char *str;
12 ssize_t exp;
13 } tests[] =
15 { nbuf, "hello world", 11 },
16 { 0, "hello world", -1 },
17 { 0, "", -1 },
18 { nbuf, "", 0 }
21 int
22 main (int argc, char *argv[])
24 size_t n;
25 int result = 0;
27 puts ("test 1");
28 n = swprintf (buf, nbuf, L"Hello %s", "world");
29 if (n != 11)
31 printf ("incorrect return value: %zd instead of 11\n", n);
32 result = 1;
34 else if (wcscmp (buf, L"Hello world") != 0)
36 printf ("incorrect string: L\"%ls\" instead of L\"Hello world\"\n", buf);
37 result = 1;
40 puts ("test 2");
41 n = swprintf (buf, nbuf, L"Is this >%g< 3.1?", 3.1);
42 if (n != 18)
44 printf ("incorrect return value: %zd instead of 18\n", n);
45 result = 1;
47 else if (wcscmp (buf, L"Is this >3.1< 3.1?") != 0)
49 printf ("incorrect string: L\"%ls\" instead of L\"Is this >3.1< 3.1?\"\n",
50 buf);
51 result = 1;
54 for (n = 0; n < sizeof (tests) / sizeof (tests[0]); ++n)
56 ssize_t res = swprintf (buf, tests[n].n, L"%s", tests[n].str);
58 if (tests[n].exp < 0 && res >= 0)
60 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
61 tests[n].n, tests[n].str);
62 result = 1;
64 else if (tests[n].exp >= 0 && tests[n].exp != res)
66 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
67 tests[n].n, tests[n].str, tests[n].exp, res);
68 result = 1;
70 else
71 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n",
72 tests[n].n, tests[n].str);
75 if (swprintf (buf, nbuf, L"%.0s", "foo") != 0
76 || wcslen (buf) != 0)
78 printf ("swprintf (buf, %Zu, L\"%%.0s\", \"foo\") create some output\n",
79 nbuf);
80 result = 1;
83 if (swprintf (buf, nbuf, L"%.0ls", L"foo") != 0
84 || wcslen (buf) != 0)
86 printf ("swprintf (buf, %Zu, L\"%%.0ls\", L\"foo\") create some output\n",
87 nbuf);
88 result = 1;
91 return result;