test-runner.c: Consolidate test running code

This commit is contained in:
Kristian Høgsberg 2012-03-05 22:29:53 -05:00
parent 4bc5a0ab2c
commit 46df9232fa

View file

@ -31,21 +31,23 @@
extern const struct test __start_test_section, __stop_test_section; extern const struct test __start_test_section, __stop_test_section;
static int static const struct test *
run_one_test(const char *name) find_test(const char *name)
{ {
const struct test *t; const struct test *t;
for (t = &__start_test_section; t < &__stop_test_section; t++) { for (t = &__start_test_section; t < &__stop_test_section; t++)
if (strcmp(t->name, name) == 0) { if (strcmp(t->name, name) == 0)
t->run(); return t;
return EXIT_SUCCESS;
}
}
fprintf(stderr, "uknown test: \"%s\"\n", name); return NULL;
}
return EXIT_FAILURE; static void
run_test(const struct test *t)
{
t->run();
exit(EXIT_SUCCESS);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -55,17 +57,22 @@ int main(int argc, char *argv[])
int total, pass; int total, pass;
siginfo_t info; siginfo_t info;
if (argc == 2) if (argc == 2) {
return run_one_test(argv[1]); t = find_test(argv[1]);
if (t == NULL) {
fprintf(stderr, "uknown test: \"%s\"\n", argv[1]);
exit(EXIT_FAILURE);
}
run_test(t);
}
pass = 0; pass = 0;
for (t = &__start_test_section; t < &__stop_test_section; t++) { for (t = &__start_test_section; t < &__stop_test_section; t++) {
pid = fork(); pid = fork();
assert(pid >= 0); assert(pid >= 0);
if (pid == 0) { if (pid == 0)
t->run(); run_test(t);
exit(EXIT_SUCCESS);
}
if (waitid(P_ALL, 0, &info, WEXITED)) { if (waitid(P_ALL, 0, &info, WEXITED)) {
fprintf(stderr, "waitid failed: %m\n"); fprintf(stderr, "waitid failed: %m\n");
abort(); abort();