]> pd.if.org Git - lice/blob - test.c
autocommit for files dated 2014-11-17 20:15:26
[lice] / test.c
1 #include <dirent.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <ctype.h>
7 #include <sys/utsname.h>
8
9 #include "util.h"
10
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"
15
16 list_t *test_find(void) {
17     list_t        *list = list_create();
18     DIR           *dir;
19     struct dirent *ent;
20
21     if (!(dir = opendir(TEST_DIR)))
22         return NULL;
23
24     string_t *string;
25     string_t *name;
26     while ((ent = readdir(dir))) {
27         if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
28             continue;
29         string = string_create();
30         name   = string_create();
31
32         string_catf(string, TEST_DIR "/%s", ent->d_name);
33
34         // open file and read comment
35         FILE   *fp   = fopen(string_buffer(string), "r");
36         char   *line = NULL;
37         size_t  len  = 0;
38         getline(&line, &len, fp);
39         fclose(fp);
40
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");
44             exit(1);
45         }
46
47         // ignore whitespace
48         char *skip = &line[2];
49         while (isspace(*skip))
50             skip++;
51
52         // remove newline
53         *strchr(skip, '\n')='\0';
54
55         string_catf(name, skip);
56         free(line);
57
58         list_push(list, pair_create(string, name));
59     }
60
61     closedir(dir);
62     return list;
63 }
64
65 int test_compile(string_t *file, bool ld) {
66     string_t *command = string_create();
67     FILE     *find;
68
69     if (!(find = fopen("lice", "r"))) {
70         fprintf(stderr, "lice not found");
71         exit(1);
72     }
73     fclose(find);
74
75     string_catf(command, "{ cat testrun.c; %s %s; } | %s | %s - ", TEST_CPP, string_buffer(file), TEST_CC, TEST_AS);
76     if (ld)
77         string_catf(command, " -ldl");
78     string_catf(command, " && ./a.out");
79
80     return system(string_buffer(command));
81 }
82
83 int main() {
84     int     error = 0;
85     list_t *list  = test_find();
86
87
88     bool needld = false;
89     struct utsname u;
90     if (uname(&u) == -1) {
91         fprintf(stderr, "uname call failed, aborting tests");
92         return EXIT_FAILURE;
93     }
94
95     if (!strcmp(u.sysname, "Linux"))
96         needld = true;
97
98
99     if (!list) {
100         fprintf(stderr, "%s/ %s\n", TEST_DIR, strerror(errno));
101         return EXIT_FAILURE;
102     }
103
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);
108
109         printf("testing %s ...", string_buffer(name));
110         for (size_t i = 0; i < 40 - size; i++)
111             printf(" ");
112
113         if (test_compile(test->first, needld)) {
114             printf("\033[31mERROR\033[0m\n");
115             error++;
116         } else {
117             printf("\033[32mOK\033[0m\n");
118         }
119     }
120
121     // print the command used for the tests
122     printf("\nAll test were run with the following command:\n");
123     if (!needld)
124         printf("{ cat testrun.c; %s $SRC; } | %s | %s - && ./a.out\n", TEST_CPP, TEST_CC, TEST_AS);
125     else
126         printf("{ cat testrun.c; %s $SRC; } | %s | %s - -ldl && ./a.out\n", TEST_CPP, TEST_CC, TEST_AS);
127
128     return (error) ? EXIT_FAILURE
129                    : EXIT_SUCCESS;
130 }