tests: implement get_test_name()
[platform/upstream/weston.git] / tests / weston-test-runner.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "config.h"
27
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/wait.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <signal.h>
36
37 #include "weston-test-runner.h"
38
39 #define SKIP 77
40
41 char __attribute__((weak)) *server_parameters="";
42
43 extern const struct weston_test __start_test_section, __stop_test_section;
44
45 static const char *test_name_;
46
47 const char *
48 get_test_name(void)
49 {
50         return test_name_;
51 }
52
53 static const struct weston_test *
54 find_test(const char *name)
55 {
56         const struct weston_test *t;
57
58         for (t = &__start_test_section; t < &__stop_test_section; t++)
59                 if (strcmp(t->name, name) == 0)
60                         return t;
61
62         return NULL;
63 }
64
65 static void
66 run_test(const struct weston_test *t, void *data, int iteration)
67 {
68         char str[512];
69
70         if (data) {
71                 snprintf(str, sizeof(str), "%s[%d]", t->name, iteration);
72                 test_name_ = str;
73         } else {
74                 test_name_ = t->name;
75         }
76
77         t->run(data);
78         exit(EXIT_SUCCESS);
79 }
80
81 static void
82 list_tests(void)
83 {
84         const struct weston_test *t;
85
86         fprintf(stderr, "Available test names:\n");
87         for (t = &__start_test_section; t < &__stop_test_section; t++)
88                 fprintf(stderr, "       %s\n", t->name);
89 }
90
91 static int
92 exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
93 {
94         int success = 0;
95         int skip = 0;
96         int hardfail = 0;
97         siginfo_t info;
98
99         pid_t pid = fork();
100         assert(pid >= 0);
101
102         if (pid == 0)
103                 run_test(t, test_data, iteration); /* never returns */
104
105         if (waitid(P_ALL, 0, &info, WEXITED)) {
106                 fprintf(stderr, "waitid failed: %m\n");
107                 abort();
108         }
109
110         if (test_data)
111                 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
112         else
113                 fprintf(stderr, "test \"%s\":\t", t->name);
114
115         switch (info.si_code) {
116         case CLD_EXITED:
117                 fprintf(stderr, "exit status %d", info.si_status);
118                 if (info.si_status == EXIT_SUCCESS)
119                         success = 1;
120                 else if (info.si_status == SKIP)
121                         skip = 1;
122                 break;
123         case CLD_KILLED:
124         case CLD_DUMPED:
125                 fprintf(stderr, "signal %d", info.si_status);
126                 if (info.si_status != SIGABRT)
127                         hardfail = 1;
128                 break;
129         }
130
131         if (t->must_fail)
132                 success = !success;
133
134         if (success && !hardfail) {
135                 fprintf(stderr, ", pass.\n");
136                 return 1;
137         } else if (skip) {
138                 fprintf(stderr, ", skip.\n");
139                 return SKIP;
140         } else {
141                 fprintf(stderr, ", fail.\n");
142                 return 0;
143         }
144 }
145
146 /* Returns number of tests and number of pass / fail in param args */
147 static int
148 iterate_test(const struct weston_test *t, int *passed, int *skipped)
149 {
150         int ret, i;
151         void *current_test_data = (void *) t->table_data;
152         for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
153         {
154                 ret = exec_and_report_test(t, current_test_data, i);
155                 if (ret == SKIP)
156                         ++(*skipped);
157                 else if (ret)
158                         ++(*passed);
159         }
160
161         return t->n_elements;
162 }
163
164 int main(int argc, char *argv[])
165 {
166         const struct weston_test *t;
167         int total = 0;
168         int pass = 0;
169         int skip = 0;
170
171         if (argc == 2) {
172                 const char *testname = argv[1];
173                 if (strcmp(testname, "--help") == 0 ||
174                     strcmp(testname, "-h") == 0) {
175                         fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
176                         list_tests();
177                         exit(EXIT_SUCCESS);
178                 }
179
180                 if (strcmp(testname, "--params") == 0 ||
181                     strcmp(testname, "-p") == 0) {
182                         printf("%s", server_parameters);
183                         exit(EXIT_SUCCESS);
184                 }
185
186                 t = find_test(argv[1]);
187                 if (t == NULL) {
188                         fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
189                         list_tests();
190                         exit(EXIT_FAILURE);
191                 }
192
193                 int number_passed_in_test = 0, number_skipped_in_test = 0;
194                 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
195                 pass += number_passed_in_test;
196                 skip += number_skipped_in_test;
197         } else {
198                 for (t = &__start_test_section; t < &__stop_test_section; t++) {
199                         int number_passed_in_test = 0, number_skipped_in_test = 0;
200                         total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
201                         pass += number_passed_in_test;
202                         skip += number_skipped_in_test;
203                 }
204         }
205
206         fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
207                 total, pass, skip, total - pass - skip);
208
209         if (skip == total)
210                 return SKIP;
211         else if (pass + skip == total)
212                 return EXIT_SUCCESS;
213
214         return EXIT_FAILURE;
215 }