From 7cf875cd3f382fb478b7137f31ac0fff92ccaa1b Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Fri, 5 Jul 2013 13:30:19 +0000 Subject: [PATCH] fix warnings, no stdio buffering in test error messages --- src/functional/sscanf.c | 4 ++-- src/functional/test.h | 53 ++++++++++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/functional/sscanf.c b/src/functional/sscanf.c index a087c21..c109e5b 100644 --- a/src/functional/sscanf.c +++ b/src/functional/sscanf.c @@ -54,9 +54,9 @@ int main(void) TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d"); TEST(i, x, 20, "%d != %d"); - TEST(i, sscanf("xyz", "%d\n", &x, &y), 0, "got %d fields, expected no match (%d)"); + TEST(i, sscanf("xyz", "%d %d\n", &x, &y), 0, "got %d fields, expected no match (%d)"); - TEST(i, sscanf("", "%d\n", &x, &y), -1, "got %d fields, expected input failure (%d)"); + TEST(i, sscanf("", "%d %d\n", &x, &y), -1, "got %d fields, expected input failure (%d)"); TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d"); TEST(i, x, 12, "%d != %d"); diff --git a/src/functional/test.h b/src/functional/test.h index a17c185..4a7d3a5 100644 --- a/src/functional/test.h +++ b/src/functional/test.h @@ -1,21 +1,58 @@ #include #include -#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) +#include /* TODO: not thread-safe nor fork-safe */ -static int test_status; +static volatile int test_status; + +#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__) + +static int test_error(const char *fn, int l, const char *s, ...) +{ + va_list ap; + char buf[512]; + int n, k; + + test_status = 1; + n = snprintf(buf, sizeof buf, "ERROR %s:%d: ", fn, l); + if (n < 0) + n = 0; + else if (n >= sizeof buf) + n = sizeof buf; + va_start(ap, s); + k = vsnprintf(buf + n, sizeof buf - n, s, ap); + va_end(ap); + if (k < 0) + k = 0; + else if (k >= sizeof buf - n) { + k = sizeof buf - n; + buf[n + k - 1] = '\n'; + buf[n + k - 2] = '.'; + buf[n + k - 3] = '.'; + buf[n + k - 4] = '.'; + } + return write(1, buf, n + k); +} -static int test_error(const char *n, int l, const char *s, ...) +static int test_printf(const char *s, ...) { va_list ap; + char buf[512]; + int n; - if (test_status == 0) - printf("FAIL\n"); test_status = 1; - printf(" ERROR %s:%d: ", n, l); va_start(ap, s); - vprintf(s, ap); + n = vsnprintf(buf, sizeof buf, s, ap); va_end(ap); - return -1; + if (n < 0) + n = 0; + else if (n >= sizeof buf) { + n = sizeof buf; + buf[n - 1] = '\n'; + buf[n - 2] = '.'; + buf[n - 3] = '.'; + buf[n - 4] = '.'; + } + return write(1, buf, n); } -- 2.20.1