1#include <stdio.h>
2#include <wchar.h>
3#include <sys/types.h>
4
5static wchar_t buf[100];
6#define nbuf (sizeof (buf) / sizeof (buf[0]))
7static 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 }
16};
17
18
19int
20main(int argc, char *argv[])
21{
22	size_t n;
23	int result = 0;
24
25	puts("test 1");
26	n = swprintf(buf, nbuf,L"Hello %s" , "world");
27	if (n != 11)
28	{
29		printf ("incorrect return value: %zd instead of 11\n", n);
30		result = 1;
31	}
32	else if (wcscmp(buf, L"Hello world") != 0)
33	{
34		printf("incorrect string: L\"%ls\" instead of L\"Hello world\"\n", buf);
35		result = 1;
36    }
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;
46    }
47
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);
50
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);
62		}
63	}
64
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;
69	}
70
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;
75	}
76
77	return result;
78}
79