fix warnings, no stdio buffering in test error messages
[libc-test.git] / src / functional / test.h
blob4a7d3a5e50e4573597c4113c7086c60a0b277f02
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <unistd.h>
5 /* TODO: not thread-safe nor fork-safe */
6 static volatile int test_status;
8 #define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
10 static int test_error(const char *fn, int l, const char *s, ...)
12 va_list ap;
13 char buf[512];
14 int n, k;
16 test_status = 1;
17 n = snprintf(buf, sizeof buf, "ERROR %s:%d: ", fn, l);
18 if (n < 0)
19 n = 0;
20 else if (n >= sizeof buf)
21 n = sizeof buf;
22 va_start(ap, s);
23 k = vsnprintf(buf + n, sizeof buf - n, s, ap);
24 va_end(ap);
25 if (k < 0)
26 k = 0;
27 else if (k >= sizeof buf - n) {
28 k = sizeof buf - n;
29 buf[n + k - 1] = '\n';
30 buf[n + k - 2] = '.';
31 buf[n + k - 3] = '.';
32 buf[n + k - 4] = '.';
34 return write(1, buf, n + k);
37 static int test_printf(const char *s, ...)
39 va_list ap;
40 char buf[512];
41 int n;
43 test_status = 1;
44 va_start(ap, s);
45 n = vsnprintf(buf, sizeof buf, s, ap);
46 va_end(ap);
47 if (n < 0)
48 n = 0;
49 else if (n >= sizeof buf) {
50 n = sizeof buf;
51 buf[n - 1] = '\n';
52 buf[n - 2] = '.';
53 buf[n - 3] = '.';
54 buf[n - 4] = '.';
56 return write(1, buf, n);