blob: 4274b393ac1a8d172633a5849a0cc364d25ffccd [file] [log] [blame]
U. Artie Eoff1ba9b382012-12-07 13:50:32 -08001/*
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
Peter Huttererd8ca8902013-09-11 16:08:04 +100023#include "config.h"
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080024#include <unistd.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <string.h>
30#include <assert.h>
31#include <errno.h>
Pekka Paalanen585c27c2012-12-18 17:30:16 +020032#include <signal.h>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080033#include "weston-test-runner.h"
34
35extern const struct weston_test __start_test_section, __stop_test_section;
36
37static const struct weston_test *
38find_test(const char *name)
39{
40 const struct weston_test *t;
41
42 for (t = &__start_test_section; t < &__stop_test_section; t++)
43 if (strcmp(t->name, name) == 0)
44 return t;
45
46 return NULL;
47}
48
49static void
Sam Spilsburyb5021262013-09-13 10:01:20 +080050run_test(const struct weston_test *t, void *data)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080051{
Sam Spilsburyb5021262013-09-13 10:01:20 +080052 t->run(data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080053 exit(EXIT_SUCCESS);
54}
55
Peter Hutterer534f0a42013-09-11 16:08:30 +100056static void
57list_tests(void)
58{
59 const struct weston_test *t;
60
61 fprintf(stderr, "Available test names:\n");
62 for (t = &__start_test_section; t < &__stop_test_section; t++)
63 fprintf(stderr, " %s\n", t->name);
64}
65
Sam Spilsburyb5021262013-09-13 10:01:20 +080066static int
67exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
68{
69 int success = 0;
70 int hardfail = 0;
71 siginfo_t info;
72
73 pid_t pid = fork();
74 assert(pid >= 0);
75
76 if (pid == 0)
77 run_test(t, test_data); /* never returns */
78
79 if (waitid(P_ALL, 0, &info, WEXITED)) {
80 fprintf(stderr, "waitid failed: %m\n");
81 abort();
82 }
83
84 if (test_data)
85 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
86 else
87 fprintf(stderr, "test \"%s\":\t", t->name);
88
89 switch (info.si_code) {
90 case CLD_EXITED:
91 fprintf(stderr, "exit status %d", info.si_status);
92 if (info.si_status == EXIT_SUCCESS)
93 success = 1;
94 break;
95 case CLD_KILLED:
96 case CLD_DUMPED:
97 fprintf(stderr, "signal %d", info.si_status);
98 if (info.si_status != SIGABRT)
99 hardfail = 1;
100 break;
101 }
102
103 if (t->must_fail)
104 success = !success;
105
106 if (success && !hardfail) {
107 fprintf(stderr, ", pass.\n");
108 return 1;
109 } else {
110 fprintf(stderr, ", fail.\n");
111 return 0;
112 }
113}
114
115/* Returns number of tests and number of pass / fail in param args */
116static int
117iterate_test(const struct weston_test *t, int *passed)
118{
119 int i;
120 void *current_test_data = (void *) t->table_data;
121 for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
122 {
123 if (exec_and_report_test(t, current_test_data, i))
124 ++(*passed);
125 }
126
127 return t->n_elements;
128}
129
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800130int main(int argc, char *argv[])
131{
132 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800133 int total = 0;
134 int pass = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800135
136 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000137 const char *testname = argv[1];
138 if (strcmp(testname, "--help") == 0 ||
139 strcmp(testname, "-h") == 0) {
140 fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
141 list_tests();
142 exit(EXIT_SUCCESS);
143 }
144
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800145 t = find_test(argv[1]);
146 if (t == NULL) {
147 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000148 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800149 exit(EXIT_FAILURE);
150 }
151
Sam Spilsburyb5021262013-09-13 10:01:20 +0800152 int number_passed_in_test = 0;
153 total += iterate_test(t, &number_passed_in_test);
154 pass += number_passed_in_test;
155 } else {
156 for (t = &__start_test_section; t < &__stop_test_section; t++) {
157 int number_passed_in_test = 0;
158 total += iterate_test(t, &number_passed_in_test);
159 pass += number_passed_in_test;
160 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800161 }
162
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800163 fprintf(stderr, "%d tests, %d pass, %d fail\n",
164 total, pass, total - pass);
165
166 return pass == total ? EXIT_SUCCESS : EXIT_FAILURE;
167}