headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / system / libroot / posix / tst-swprintf.c
blob70c846130ba35aea194dbdb278c1b7ceaef30858
1 #include <stdio.h>
2 #include <wchar.h>
3 #include <sys/types.h>
5 static wchar_t buf[100];
6 #define nbuf (sizeof (buf) / sizeof (buf[0]))
7 static const struct {
8 size_t n;
9 const char *str;
10 ssize_t exp;
11 } tests[] = {
12 { nbuf, "hello world", 11 },
13 { 0, "hello world", -1 },
14 { 0, "", -1 },
15 { nbuf, "", 0 }
19 int
20 main(int argc, char *argv[])
22 size_t n;
23 int result = 0;
25 puts("test 1");
26 n = swprintf(buf, nbuf,L"Hello %s" , "world");
27 if (n != 11)
29 printf ("incorrect return value: %zd instead of 11\n", n);
30 result = 1;
32 else if (wcscmp(buf, L"Hello world") != 0)
34 printf("incorrect string: L\"%ls\" instead of L\"Hello world\"\n", buf);
35 result = 1;
37 puts ("test 2");
38 n = swprintf(buf, nbuf, L"Is this >%g< 3.1?", 3.1);
39 if (n != 18) {
40 printf("incorrect return value: %zd instead of 18\n", n);
41 result = 1;
42 } else if (wcscmp (buf, L"Is this >3.1< 3.1?") != 0) {
43 printf("incorrect string: L\"%ls\" instead of L\"Is this >3.1< 3.1?\"\n",
44 buf);
45 result = 1;
48 for (n = 0; n < sizeof(tests) / sizeof(tests[0]); ++n) {
49 ssize_t res = swprintf(buf, tests[n].n, L"%s", tests[n].str);
51 if (tests[n].exp < 0 && res >= 0) {
52 printf("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
53 tests[n].n, tests[n].str);
54 result = 1;
55 } else if (tests[n].exp >= 0 && tests[n].exp != res) {
56 printf("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
57 tests[n].n, tests[n].str, tests[n].exp, res);
58 result = 1;
59 } else {
60 printf("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n", tests[n].n,
61 tests[n].str);
65 if (swprintf(buf, nbuf, L"%.0s", "foo") != 0 || wcslen(buf) != 0) {
66 printf("swprintf (buf, %Zu, L\"%%.0s\", \"foo\") create some output\n",
67 nbuf);
68 result = 1;
71 if (swprintf(buf, nbuf, L"%.0ls", L"foo") != 0 || wcslen(buf) != 0) {
72 printf("swprintf (buf, %Zu, L\"%%.0ls\", L\"foo\") create some output\n",
73 nbuf);
74 result = 1;
77 return result;