3 * Copyright 2013 Google Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 void error(int status, int errnum, const char *format, ...) {
38 fprintf(stderr, "%s: ", getprogname());
41 va_start(args, format);
42 vfprintf(stderr, format, args);
46 fprintf(stderr, ": %s\n", strerror(errnum));
48 fprintf(stderr, "\n");
56 test::test_registry_t test::test_registry;
59 bool run_test(TestBase& test) {
60 printf("-- running test case: %s\n", test.name);
63 pid_t child_pid = fork();
65 exit(static_cast<int>(test.run()));
67 if (child_pid == -1) {
68 error(EXIT_FAILURE, 0, "unable to fork");
72 waitpid(child_pid, &child_status, 0);
74 test::TestStatus status;
76 if (WIFEXITED(child_status)) {
77 int exit_status = WEXITSTATUS(child_status);
78 if (exit_status & ~test::STATUS_MASK) {
79 status = test::FAILED;
81 status = static_cast<test::TestStatus>(exit_status);
83 } else if (WIFSIGNALED(child_status)) {
84 const int signum = WTERMSIG(child_status);
85 printf("!! signal (%d) %s\n", signum, strsignal(signum));
88 status = test::SIGNAL_ABORT; break;
91 status = test::SIGNAL_SEGFAULT; break;
93 status = test::SIGNAL_DIVZERO; break;
95 status = test::SIGNAL_UNCAUGHT;
98 status = test::SUCCESS;
101 if (test.expected_status == test::FAILED) {
102 return (status & test::FAILED);
105 if (test.expected_status == test::SIGNAL_UNCAUGHT) {
106 return (status & test::SIGNAL_UNCAUGHT);
109 return status == test.expected_status;
112 int main(int argc, const char* const argv[]) {
114 size_t success_cnt = 0;
115 size_t total_cnt = 0;
116 for (test_registry_t::iterator it = test_registry.begin();
117 it != test_registry.end(); ++it) {
118 TestBase& test = **it;
120 bool consider_test = (argc <= 1);
121 for (int i = 1; i < argc; ++i) {
122 if (strcasecmp(argv[i], test.name) == 0) {
123 consider_test = true;
127 if (not consider_test) {
132 if (run_test(test)) {
133 printf("-- test case success: %s\n", test.name);
136 printf("** test case FAILED : %s\n", test.name);
139 printf("-- tests passing: %lu/%lu", success_cnt, total_cnt);
141 printf(" (%lu%%)\n", success_cnt * 100 / total_cnt);
145 return (success_cnt == total_cnt) ? EXIT_SUCCESS : EXIT_FAILURE;