tests: Cleanup superfluous headers
[platform/upstream/weston.git] / tests / weston-test-runner.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <errno.h>
32 #include <signal.h>
33
34 #include "weston-test-runner.h"
35
36 #define SKIP 77
37
38 char __attribute__((weak)) *server_parameters="";
39
40 extern const struct weston_test __start_test_section, __stop_test_section;
41
42 static const struct weston_test *
43 find_test(const char *name)
44 {
45         const struct weston_test *t;
46
47         for (t = &__start_test_section; t < &__stop_test_section; t++)
48                 if (strcmp(t->name, name) == 0)
49                         return t;
50
51         return NULL;
52 }
53
54 static void
55 run_test(const struct weston_test *t, void *data)
56 {
57         t->run(data);
58         exit(EXIT_SUCCESS);
59 }
60
61 static void
62 list_tests(void)
63 {
64         const struct weston_test *t;
65
66         fprintf(stderr, "Available test names:\n");
67         for (t = &__start_test_section; t < &__stop_test_section; t++)
68                 fprintf(stderr, "       %s\n", t->name);
69 }
70
71 static int
72 exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
73 {
74         int success = 0;
75         int skip = 0;
76         int hardfail = 0;
77         siginfo_t info;
78
79         pid_t pid = fork();
80         assert(pid >= 0);
81
82         if (pid == 0)
83                 run_test(t, test_data); /* never returns */
84
85         if (waitid(P_ALL, 0, &info, WEXITED)) {
86                 fprintf(stderr, "waitid failed: %m\n");
87                 abort();
88         }
89
90         if (test_data)
91                 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
92         else
93                 fprintf(stderr, "test \"%s\":\t", t->name);
94
95         switch (info.si_code) {
96         case CLD_EXITED:
97                 fprintf(stderr, "exit status %d", info.si_status);
98                 if (info.si_status == EXIT_SUCCESS)
99                         success = 1;
100                 else if (info.si_status == SKIP)
101                         skip = 1;
102                 break;
103         case CLD_KILLED:
104         case CLD_DUMPED:
105                 fprintf(stderr, "signal %d", info.si_status);
106                 if (info.si_status != SIGABRT)
107                         hardfail = 1;
108                 break;
109         }
110
111         if (t->must_fail)
112                 success = !success;
113
114         if (success && !hardfail) {
115                 fprintf(stderr, ", pass.\n");
116                 return 1;
117         } else if (skip) {
118                 fprintf(stderr, ", skip.\n");
119                 return SKIP;
120         } else {
121                 fprintf(stderr, ", fail.\n");
122                 return 0;
123         }
124 }
125
126 /* Returns number of tests and number of pass / fail in param args */
127 static int
128 iterate_test(const struct weston_test *t, int *passed, int *skipped)
129 {
130         int ret, i;
131         void *current_test_data = (void *) t->table_data;
132         for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
133         {
134                 ret = exec_and_report_test(t, current_test_data, i);
135                 if (ret == SKIP)
136                         ++(*skipped);
137                 else if (ret)
138                         ++(*passed);
139         }
140
141         return t->n_elements;
142 }
143
144 int main(int argc, char *argv[])
145 {
146         const struct weston_test *t;
147         int total = 0;
148         int pass = 0;
149         int skip = 0;
150
151         if (argc == 2) {
152                 const char *testname = argv[1];
153                 if (strcmp(testname, "--help") == 0 ||
154                     strcmp(testname, "-h") == 0) {
155                         fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
156                         list_tests();
157                         exit(EXIT_SUCCESS);
158                 }
159
160                 if (strcmp(testname, "--params") == 0 ||
161                     strcmp(testname, "-p") == 0) {
162                         printf("%s", server_parameters);
163                         exit(EXIT_SUCCESS);
164                 }
165
166                 t = find_test(argv[1]);
167                 if (t == NULL) {
168                         fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
169                         list_tests();
170                         exit(EXIT_FAILURE);
171                 }
172
173                 int number_passed_in_test = 0, number_skipped_in_test = 0;
174                 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
175                 pass += number_passed_in_test;
176                 skip += number_skipped_in_test;
177         } else {
178                 for (t = &__start_test_section; t < &__stop_test_section; t++) {
179                         int number_passed_in_test = 0, number_skipped_in_test = 0;
180                         total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
181                         pass += number_passed_in_test;
182                         skip += number_skipped_in_test;
183                 }
184         }
185
186         fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
187                 total, pass, skip, total - pass - skip);
188
189         if (skip == total)
190                 return SKIP;
191         else if (pass + skip == total)
192                 return EXIT_SUCCESS;
193
194         return EXIT_FAILURE;
195 }