new test system
[libc-test] / src / common / test.h
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <unistd.h>
5
6 /* TODO: not thread-safe nor fork-safe */
7 static volatile int t_status;
8
9 #define T_LOC2(l) __FILE__ ":" #l
10 #define T_LOC1(l) T_LOC2(l)
11 #define t_error(...) t_printf("ERROR " T_LOC1(__LINE__) ": " __VA_ARGS__)
12
13 static int t_printf(const char *s, ...)
14 {
15         va_list ap;
16         char buf[512];
17         int n;
18
19         t_status = 1;
20         va_start(ap, s);
21         n = vsnprintf(buf, sizeof buf, s, ap);
22         va_end(ap);
23         if (n < 0)
24                 n = 0;
25         else if (n >= sizeof buf) {
26                 n = sizeof buf;
27                 buf[n - 1] = '\n';
28                 buf[n - 2] = '.';
29                 buf[n - 3] = '.';
30                 buf[n - 4] = '.';
31         }
32         return write(1, buf, n);
33 }
34
35 int t_vmfill(void **, size_t *, int);
36
37 void t_fdfill(void);
38
39 void t_randseed(uint64_t s);
40 uint64_t t_randn(uint64_t n);
41 uint64_t t_randint(uint64_t a, uint64_t b);
42 void t_shuffle(uint64_t *p, size_t n);
43 void t_randrange(uint64_t *p, size_t n);
44 int t_choose(uint64_t n, size_t k, uint64_t *p);
45
46 char *t_pathrel(char *buf, size_t n, char *argv0, char *p);
47