test-runner.c: Consolidate test running code
authorKristian Høgsberg <krh@bitplanet.net>
Tue, 6 Mar 2012 03:29:53 +0000 (22:29 -0500)
committerKristian Høgsberg <krh@bitplanet.net>
Tue, 6 Mar 2012 03:29:53 +0000 (22:29 -0500)
tests/test-runner.c

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