* sysdeps/unix/sysv/linux/opensock.c: Include <string.h>.
[glibc.git] / libio / tst_swprintf.c
blobdc16db0a0c88765e65c2cd913c390dc20f8649dd
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 int 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: %d 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: %d 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 return result;