Merge branch 'release/1.3'
[reservoir_sample] / test_suite.c
1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 #define TEST
8 #include "test_suite.h"
9
10 static struct options_ {
11 unsigned int verbosity;
12 } options_ = {
13 .verbosity = 0,
14 };
15
16 void test_verbose(const char *fmt, ...) {
17 va_list ap;
18
19 if (options_.verbosity < 1)
20 return;
21
22 va_start(ap, fmt);
23 vfprintf(stdout, fmt, ap);
24 va_end(ap);
25 }
26
27 void test_info(const char *fmt, ...) {
28 va_list ap;
29
30 va_start(ap, fmt);
31 vfprintf(stdout, fmt, ap);
32 va_end(ap);
33 }
34
35 void test_error(const char *fmt, ...) {
36 va_list ap;
37
38 va_start(ap, fmt);
39 vfprintf(stderr, fmt, ap);
40 va_end(ap);
41 }
42
43 static
44 int run_test_(test_t *t) {
45 int r = t->test_fn(t->test_data, test_suite_data);
46 test_info("-- test '%s': %s --\n", t->test_name, r ? "FAILED" : "OK");
47 return r;
48 }
49
50 int main(int argc, char **argv) {
51 test_t *t;
52 int c;
53
54 while ( (c = getopt(argc, argv, "vl")) != EOF ) {
55 switch (c) {
56 case 'v':
57 options_.verbosity++;
58 break;
59 case 'l':
60 test_info("available test suites:\n");
61 for (t = test_suite; t->test_name; t++) {
62 test_info("\t%s\n", t->test_name);
63 }
64 exit(EXIT_SUCCESS);
65 default:
66 exit(EXIT_FAILURE);
67 }
68 }
69
70 if (test_suite_pre(test_suite_data)) {
71 test_error("%s: %s", "test_suite_pre", "FAILED");
72 exit(EXIT_FAILURE);
73 }
74
75 if (argc - optind) {
76 while (argc - optind) {
77 for (t = test_suite; t->test_name && strcmp(argv[optind], t->test_name); t++) {}
78 if (t->test_name == NULL) {
79 test_error("unknown suite '%s'\n", argv[optind]);
80 exit(EXIT_FAILURE);
81 }
82 if (run_test_(t))
83 exit(EXIT_FAILURE);
84 optind++;
85 }
86 } else {
87 for (t = test_suite; t->test_name; t++) {
88 if (run_test_(t))
89 exit(EXIT_FAILURE);
90 }
91 }
92
93 if (test_suite_post(test_suite_data)) {
94 test_error("%s: %s\n", "test_suite_post", "FAILED");
95 exit(EXIT_FAILURE);
96 }
97
98 exit(EXIT_SUCCESS);
99 }