7 #include <sys/utsname.h>
11 #define TEST_DIR "tests"
12 #define TEST_AS "gcc -xassembler"
13 #define TEST_CPP "cpp -nostdinc -Iinclude/"
14 #define TEST_CC "./lice -std=licec"
16 list_t *test_find(void) {
17 list_t *list = list_create();
21 if (!(dir = opendir(TEST_DIR)))
26 while ((ent = readdir(dir))) {
27 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
29 string = string_create();
30 name = string_create();
32 string_catf(string, TEST_DIR "/%s", ent->d_name);
34 // open file and read comment
35 FILE *fp = fopen(string_buffer(string), "r");
38 getline(&line, &len, fp);
41 if (line[0] != '/' || line[1] != '/') {
42 fprintf(stderr, "unnamed test: %s\n", string_buffer(string));
43 fprintf(stderr, "please give it a name, aborting");
48 char *skip = &line[2];
49 while (isspace(*skip))
53 *strchr(skip, '\n')='\0';
55 string_catf(name, skip);
58 list_push(list, pair_create(string, name));
65 int test_compile(string_t *file, bool ld) {
66 string_t *command = string_create();
69 if (!(find = fopen("lice", "r"))) {
70 fprintf(stderr, "lice not found");
75 string_catf(command, "{ cat testrun.c; %s %s; } | %s | %s - ", TEST_CPP, string_buffer(file), TEST_CC, TEST_AS);
77 string_catf(command, " -ldl");
78 string_catf(command, " && ./a.out");
80 return system(string_buffer(command));
85 list_t *list = test_find();
90 if (uname(&u) == -1) {
91 fprintf(stderr, "uname call failed, aborting tests");
95 if (!strcmp(u.sysname, "Linux"))
100 fprintf(stderr, "%s/ %s\n", TEST_DIR, strerror(errno));
104 for (list_iterator_t *it = list_iterator(list); !list_iterator_end(it); ) {
105 pair_t *test = list_iterator_next(it);
106 string_t *name = test->second;
107 size_t size = string_length(name);
109 printf("testing %s ...", string_buffer(name));
110 for (size_t i = 0; i < 40 - size; i++)
113 if (test_compile(test->first, needld)) {
114 printf("\033[31mERROR\033[0m\n");
117 printf("\033[32mOK\033[0m\n");
121 // print the command used for the tests
122 printf("\nAll test were run with the following command:\n");
124 printf("{ cat testrun.c; %s $SRC; } | %s | %s - && ./a.out\n", TEST_CPP, TEST_CC, TEST_AS);
126 printf("{ cat testrun.c; %s $SRC; } | %s | %s - -ldl && ./a.out\n", TEST_CPP, TEST_CC, TEST_AS);
128 return (error) ? EXIT_FAILURE