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
|
#include "test.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static bool any_failure = false;
void test_fail(const char *fmt, ...) {
any_failure = true;
fprintf(stderr, "\x1b[1m\x1b[91mFailure:\x1b[0m ");
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
static const char *test_dir;
#include <dirent.h>
char **list_dir(const char *dir_name, const char *suffix) {
char *dir_path = malloc(strlen(test_dir) + strlen(dir_name) + 8);
sprintf(dir_path, "%s/%s", test_dir, dir_name);
DIR *dir = opendir(dir_path);
if (!dir) {
test_fail("Couldn't open test directory %s", dir_path);
return NULL;
}
struct dirent *ent;
size_t entries = 0;
while ((ent = readdir(dir))) entries++;
rewinddir(dir);
char **listing = calloc(entries + 1, sizeof (char *));
size_t i = 0;
while ((ent = readdir(dir))) {
const char *name = ent->d_name;
if (strlen(name) < strlen(suffix) || strcmp(name+strlen(name)-strlen(suffix), suffix) != 0
|| i >= entries) {
continue;
}
char *path = malloc(strlen(dir_path) + strlen(name) + 8);
sprintf(path, "%s/%s", dir_path, name);
listing[i++] = path;
}
closedir(dir);
free(dir_path);
return listing;
}
void free_listing(char **listing) {
for (size_t i = 0; listing[i]; i++) {
free(listing[i]);
}
free(listing);
}
int main(int argc, char **argv) {
if (argc > 2 || (argc == 2 && strcmp(argv[1], "--help") == 0)) {
printf("usage: tests [TEST DIRECTORY]\n");
return EXIT_FAILURE;
}
test_dir = argc == 2 ? argv[1] : "../tests";
test_parsing();
test_errors();
test_location();
if (any_failure) {
fprintf(stderr, "\x1b[1m\x1b[91mSome tests failed.\x1b[0m\n");
return EXIT_FAILURE;
} else {
printf("\x1b[1m\x1b[92mAll tests OK\x1b[0m\n");
return 0;
}
}
|