]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/tst_swprintf.c
8a2ffe60ecf208b41d1b6ad4cf7c0593b976a8bc
[thirdparty/glibc.git] / libio / tst_swprintf.c
1 #include <array_length.h>
2 #include <stdio.h>
3 #include <support/check.h>
4 #include <sys/types.h>
5 #include <wchar.h>
6
7
8 static wchar_t buf[100];
9 static const struct
10 {
11 size_t n;
12 const char *str;
13 ssize_t exp;
14 } tests[] =
15 {
16 { array_length (buf), "hello world", 11 },
17 { 0, "hello world", -1 },
18 { 1, "hello world", -1 },
19 { 2, "hello world", -1 },
20 { 11, "hello world", -1 },
21 { 12, "hello world", 11 },
22 { 0, "", -1 },
23 { array_length (buf), "", 0 }
24 };
25
26 static int
27 do_test (void)
28 {
29 size_t n;
30
31 TEST_COMPARE (swprintf (buf, array_length (buf), L"Hello %s", "world"), 11);
32 TEST_COMPARE_STRING_WIDE (buf, L"Hello world");
33
34 TEST_COMPARE (swprintf (buf, array_length (buf), L"Is this >%g< 3.1?", 3.1),
35 18);
36 TEST_COMPARE_STRING_WIDE (buf, L"Is this >3.1< 3.1?");
37
38 for (n = 0; n < array_length (tests); ++n)
39 {
40 ssize_t res = swprintf (buf, tests[n].n, L"%s", tests[n].str);
41
42 if (tests[n].exp < 0 && res >= 0)
43 {
44 support_record_failure ();
45 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
46 tests[n].n, tests[n].str);
47 }
48 else if (tests[n].exp >= 0 && tests[n].exp != res)
49 {
50 support_record_failure ();
51 printf ("\
52 swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
53 tests[n].n, tests[n].str, tests[n].exp, res);
54 }
55 else
56 printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n",
57 tests[n].n, tests[n].str);
58 }
59
60 TEST_COMPARE (swprintf (buf, array_length (buf), L"%.0s", "foo"), 0);
61 TEST_COMPARE_STRING_WIDE (buf, L"");
62
63 TEST_COMPARE (swprintf (buf, array_length (buf), L"%.0ls", L"foo"), 0);
64 TEST_COMPARE_STRING_WIDE (buf, L"");
65
66 return 0;
67 }
68
69 #include <support/test-driver.c>