-
Notifications
You must be signed in to change notification settings - Fork 2
/
nanotest_getopt.c
99 lines (77 loc) · 2.64 KB
/
nanotest_getopt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <string.h>
#include <nanotest.h>
#include <autogen_tests.h>
/* Global pointers to test state. Neither are thread safe in this case
* and there is a race condition on callees corrupting them if they should
* crash during test.
*
* FIXME: Not thread safe.
*/
struct nanotest_fail g_nanotest_fail;
jmp_buf g_nanotest_jmpbuf;
/* Declared as global to prevent clobbering by setjmp/longjmp() */
static struct nanotest_unit** ptr = autogen_tests;
static struct nanotest_unit* test;
static int tests_run = 0;
static int tests_failed = 0;
struct test_args {
char *suite;
char *name;
};
static int parse_args(struct test_args* args, int argc, char* argv[])
{
int c;
while ((c = getopt (argc, argv, "s:n:")) != -1) {
switch (c)
{
case 's':
args->suite = optarg;
break;
case 'n':
args->name = optarg;
break;
default:
assert(0);
break;
}
}
return 0;
}
int main(int argc, char* argv[])
{
struct test_args args;
memset(&args, 0, sizeof(args));
parse_args(&args, argc, argv);
/* Iterate until the end of array NULL pointer is encountered */
while ((test = *ptr++)) {
/* Skip all but the specified tests */
if ((args.suite && strncmp(args.suite, test->suite, strlen(args.suite))) ||
(args.name && strncmp(args.name, test->name, strlen(args.name)))) {
printf("[Skipping ] %s::%s\n", test->suite, test->name);
continue;
}
printf("[Running ] %s::%s\n", test->suite, test->name);
/* Jump back to this point if a test fails */
int ret = setjmp(g_nanotest_jmpbuf);
if (!ret) {
test->func(test);
/* Only gets to this line if longjmp() isn't called first */
printf("[ Done] %s::%s\n", test->suite, test->name);
} else {
printf("[ FAILED] %s::%s %s:%u\n", test->suite, test->name,
g_nanotest_fail.file,
g_nanotest_fail.line);
printf("[ MSG] %s::%s assert( %s )\n", test->suite, test->name,
g_nanotest_fail.expr);
tests_failed += 1;
}
tests_run += 1;
}
printf("=========================================\n");
printf("Tests run: %d\n",tests_run);
printf("Tests failed: %d\n", tests_failed);
return (tests_failed ? 1 : 0);
}