return ret;
}
-#define EXPECT_VFPRINTF(c, expected, fmt, ...) \
- ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
+#define EXPECT_VFPRINTF(expected, fmt, ...) \
+ ret += expect_vfprintf(llen, expected, fmt, ##__VA_ARGS__)
-static int expect_vfprintf(int llen, int c, const char *expected, const char *fmt, ...)
+#define VFPRINTF_LEN 20
+static int expect_vfprintf(int llen, const char *expected, const char *fmt, ...)
{
- char buf[100];
+ char buf[VFPRINTF_LEN + 80];
+ unsigned int cmp_len;
va_list args;
- ssize_t w;
+ ssize_t w, expected_len;
va_start(args, fmt);
- /* Only allow writing 21 bytes, to test truncation */
- w = vsnprintf(buf, 21, fmt, args);
+ /* Limit buffer length to test truncation */
+ w = vsnprintf(buf, VFPRINTF_LEN + 1, fmt, args);
va_end(args);
llen += printf(" \"%s\"", buf);
- if (strncmp(expected, buf, c)) {
- llen += printf(" should be \"%s\"", expected);
+
+ expected_len = strlen(expected);
+ if (expected_len > VFPRINTF_LEN) {
+ /* Indicate truncated in test output */
+ llen += printf("+");
+ cmp_len = VFPRINTF_LEN;
+ } else {
+ cmp_len = expected_len;
+ }
+
+ if (memcmp(expected, buf, cmp_len) || buf[cmp_len]) {
+ /* Copy and truncate until "%.*s" supported */
+ memcpy(buf, expected, cmp_len);
+ buf[cmp_len] = 0;
+ llen += printf(" should be \"%s\"", buf);
result(llen, FAIL);
return 1;
}
- if (w != c) {
- llen += printf(" written(%d) != %d", (int)w, c);
+ if (w != expected_len) {
+ llen += printf(" written(%d) != %d", (int)w, (int)expected_len);
result(llen, FAIL);
return 1;
}
* test numbers.
*/
switch (test + __LINE__ + 1) {
- CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
- CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
- CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
- CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
- CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
- CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
- CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
- CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
- CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
- CASE_TEST(uintmax_t); EXPECT_VFPRINTF(20, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break;
- CASE_TEST(intmax_t); EXPECT_VFPRINTF(20, "-9223372036854775807", "%jd", 0x8000000000000001LL); break;
- CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break;
- CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1"); break;
- CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); break;
- CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
+ CASE_TEST(empty); EXPECT_VFPRINTF("", ""); break;
+ CASE_TEST(simple); EXPECT_VFPRINTF("foo", "foo"); break;
+ CASE_TEST(string); EXPECT_VFPRINTF("foo", "%s", "foo"); break;
+ CASE_TEST(number); EXPECT_VFPRINTF("1234", "%d", 1234); break;
+ CASE_TEST(negnumber); EXPECT_VFPRINTF("-1234", "%d", -1234); break;
+ CASE_TEST(unsigned); EXPECT_VFPRINTF("12345", "%u", 12345); break;
+ CASE_TEST(char); EXPECT_VFPRINTF("c", "%c", 'c'); break;
+ CASE_TEST(hex); EXPECT_VFPRINTF("f", "%x", 0xf); break;
+ CASE_TEST(pointer); EXPECT_VFPRINTF("0x1", "%p", (void *) 0x1); break;
+ CASE_TEST(uintmax_t); EXPECT_VFPRINTF("18446744073709551615", "%ju", 0xffffffffffffffffULL); break;
+ CASE_TEST(intmax_t); EXPECT_VFPRINTF("-9223372036854775807", "%jd", 0x8000000000000001LL); break;
+ CASE_TEST(truncation); EXPECT_VFPRINTF("0123456789012345678901234", "%s", "0123456789012345678901234"); break;
+ CASE_TEST(string_width); EXPECT_VFPRINTF(" 1", "%10s", "1"); break;
+ CASE_TEST(number_width); EXPECT_VFPRINTF(" 1", "%10d", 1); break;
+ CASE_TEST(width_trunc); EXPECT_VFPRINTF(" 1", "%25d", 1); break;
CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break;
CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;