1 | #include <stdio.h> |
2 | #include <wchar.h> |
3 | #include <sys/types.h> |
4 | |
5 | |
6 | static wchar_t buf[100]; |
7 | #define nbuf (sizeof (buf) / sizeof (buf[0])) |
8 | static const struct |
9 | { |
10 | size_t n; |
11 | const char *str; |
12 | ssize_t exp; |
13 | } tests[] = |
14 | { |
15 | { nbuf, "hello world" , 11 }, |
16 | { 0, "hello world" , -1 }, |
17 | { 0, "" , -1 }, |
18 | { nbuf, "" , 0 } |
19 | }; |
20 | |
21 | int |
22 | main (int argc, char *argv[]) |
23 | { |
24 | size_t n; |
25 | int result = 0; |
26 | |
27 | puts (s: "test 1" ); |
28 | n = swprintf (s: buf, nbuf, format: L"Hello %s" , "world" ); |
29 | if (n != 11) |
30 | { |
31 | printf (format: "incorrect return value: %zd instead of 11\n" , n); |
32 | result = 1; |
33 | } |
34 | else if (wcscmp (s1: buf, s2: L"Hello world" ) != 0) |
35 | { |
36 | printf (format: "incorrect string: L\"%ls\" instead of L\"Hello world\"\n" , buf); |
37 | result = 1; |
38 | } |
39 | |
40 | puts (s: "test 2" ); |
41 | n = swprintf (s: buf, nbuf, format: L"Is this >%g< 3.1?" , 3.1); |
42 | if (n != 18) |
43 | { |
44 | printf (format: "incorrect return value: %zd instead of 18\n" , n); |
45 | result = 1; |
46 | } |
47 | else if (wcscmp (s1: buf, s2: L"Is this >3.1< 3.1?" ) != 0) |
48 | { |
49 | printf (format: "incorrect string: L\"%ls\" instead of L\"Is this >3.1< 3.1?\"\n" , |
50 | buf); |
51 | result = 1; |
52 | } |
53 | |
54 | for (n = 0; n < sizeof (tests) / sizeof (tests[0]); ++n) |
55 | { |
56 | ssize_t res = swprintf (s: buf, n: tests[n].n, format: L"%s" , tests[n].str); |
57 | |
58 | if (tests[n].exp < 0 && res >= 0) |
59 | { |
60 | printf (format: "swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n" , |
61 | tests[n].n, tests[n].str); |
62 | result = 1; |
63 | } |
64 | else if (tests[n].exp >= 0 && tests[n].exp != res) |
65 | { |
66 | printf (format: "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; |
69 | } |
70 | else |
71 | printf (format: "swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n" , |
72 | tests[n].n, tests[n].str); |
73 | } |
74 | |
75 | if (swprintf (s: buf, nbuf, format: L"%.0s" , "foo" ) != 0 |
76 | || wcslen (s: buf) != 0) |
77 | { |
78 | printf (format: "swprintf (buf, %Zu, L\"%%.0s\", \"foo\") create some output\n" , |
79 | nbuf); |
80 | result = 1; |
81 | } |
82 | |
83 | if (swprintf (s: buf, nbuf, format: L"%.0ls" , L"foo" ) != 0 |
84 | || wcslen (s: buf) != 0) |
85 | { |
86 | printf (format: "swprintf (buf, %Zu, L\"%%.0ls\", L\"foo\") create some output\n" , |
87 | nbuf); |
88 | result = 1; |
89 | } |
90 | |
91 | return result; |
92 | } |
93 | |