1 #include <array_length.h>
4 #include <support/check.h>
9 static wchar_t buf
[100];
17 { array_length (buf
), "hello world", 11 },
18 { 0, "hello world", -1 },
19 { 1, "hello world", -1 },
20 { 2, "hello world", -1 },
21 { 11, "hello world", -1 },
22 { 12, "hello world", 11 },
24 { array_length (buf
), "", 0 }
32 TEST_COMPARE (swprintf (buf
, array_length (buf
), L
"Hello %s", "world"), 11);
33 TEST_COMPARE_STRING_WIDE (buf
, L
"Hello world");
35 TEST_COMPARE (swprintf (buf
, array_length (buf
), L
"Is this >%g< 3.1?", 3.1),
37 TEST_COMPARE_STRING_WIDE (buf
, L
"Is this >3.1< 3.1?");
39 for (n
= 0; n
< array_length (tests
); ++n
)
41 wmemset (buf
, 0xabcd, array_length (buf
));
43 ssize_t res
= swprintf (buf
, tests
[n
].n
, L
"%s", tests
[n
].str
);
45 if (tests
[n
].exp
< 0 && res
>= 0)
47 support_record_failure ();
48 printf ("swprintf (buf, %zu, L\"%%s\", \"%s\") expected to fail\n",
49 tests
[n
].n
, tests
[n
].str
);
51 else if (tests
[n
].exp
>= 0 && tests
[n
].exp
!= res
)
53 support_record_failure ();
55 swprintf (buf, %zu, L\"%%s\", \"%s\") expected to return %zd, but got %zd\n",
56 tests
[n
].n
, tests
[n
].str
, tests
[n
].exp
, res
);
60 && wcsnlen (buf
, array_length (buf
)) == array_length (buf
))
62 support_record_failure ();
64 error: swprintf (buf, %zu, L\"%%s\", \"%s\") missing null terminator\n",
65 tests
[n
].n
, tests
[n
].str
);
69 && wcsnlen (buf
, array_length (buf
)) < array_length (buf
)
70 && buf
[wcsnlen (buf
, array_length (buf
)) + 1] != 0xabcd)
72 support_record_failure ();
74 error: swprintf (buf, %zu, L\"%%s\", \"%s\") out of bounds write\n",
75 tests
[n
].n
, tests
[n
].str
);
78 if (res
< 0 && tests
[n
].n
< 0)
79 TEST_COMPARE (errno
, E2BIG
);
81 printf ("swprintf (buf, %zu, L\"%%s\", \"%s\") OK\n",
82 tests
[n
].n
, tests
[n
].str
);
85 TEST_COMPARE (swprintf (buf
, array_length (buf
), L
"%.0s", "foo"), 0);
86 TEST_COMPARE_STRING_WIDE (buf
, L
"");
88 TEST_COMPARE (swprintf (buf
, array_length (buf
), L
"%.0ls", L
"foo"), 0);
89 TEST_COMPARE_STRING_WIDE (buf
, L
"");
94 #include <support/test-driver.c>