829bfcd82eb0c83c4aead65cbf93935790c9b4cc
[iec.git] / src / type3_AndroidCloud / anbox-master / external / backward-cpp / test / _test_main.cpp
1 /*
2  * _test_main.cpp
3  * Copyright 2013 Google Inc. All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23
24 #include "test.hpp"
25 #include <cstdio>
26 #include <cstdlib>
27 #ifndef __APPLE__
28 #include <error.h>
29 #endif
30 #include <unistd.h>
31 #include <sys/wait.h>
32
33 #ifdef __APPLE__
34 #include <stdarg.h>
35
36 void error(int status, int errnum, const char *format, ...) {
37         fflush(stdout);
38         fprintf(stderr, "%s: ", getprogname());
39
40         va_list args;
41         va_start(args, format);
42         vfprintf(stderr, format, args);
43         va_end(args);
44
45         if (errnum != 0) {
46                 fprintf(stderr, ": %s\n", strerror(errnum));
47         } else {
48                 fprintf(stderr, "\n");
49         }
50         if (status != 0) {
51                 exit(status);
52         }
53 }
54 #endif
55
56 test::test_registry_t test::test_registry;
57 using namespace test;
58
59 bool run_test(TestBase& test) {
60         printf("-- running test case: %s\n", test.name);
61
62         fflush(stdout);
63         pid_t child_pid = fork();
64         if (child_pid == 0) {
65                 exit(static_cast<int>(test.run()));
66         }
67         if (child_pid == -1) {
68                 error(EXIT_FAILURE, 0, "unable to fork");
69         }
70
71         int child_status = 0;
72         waitpid(child_pid, &child_status, 0);
73
74         test::TestStatus status;
75
76         if (WIFEXITED(child_status)) {
77                 int exit_status = WEXITSTATUS(child_status);
78                 if (exit_status & ~test::STATUS_MASK) {
79                         status = test::FAILED;
80                 } else {
81                         status = static_cast<test::TestStatus>(exit_status);
82                 }
83         } else if (WIFSIGNALED(child_status)) {
84                 const int signum = WTERMSIG(child_status);
85                 printf("!! signal (%d) %s\n", signum, strsignal(signum));
86                 switch (signum) {
87                         case SIGABRT:
88                                 status = test::SIGNAL_ABORT; break;
89                         case SIGSEGV:
90                         case SIGBUS:
91                                 status = test::SIGNAL_SEGFAULT; break;
92                         case SIGFPE:
93                                 status = test::SIGNAL_DIVZERO; break;
94                         default:
95                                 status = test::SIGNAL_UNCAUGHT;
96                 }
97         } else {
98                 status = test::SUCCESS;
99         }
100
101         if (test.expected_status == test::FAILED) {
102                 return (status & test::FAILED);
103         }
104
105         if (test.expected_status == test::SIGNAL_UNCAUGHT) {
106                 return (status & test::SIGNAL_UNCAUGHT);
107         }
108
109         return status == test.expected_status;
110 }
111
112 int main(int argc, const char* const argv[]) {
113
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;
119
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;
124                                 break;
125                         }
126                 }
127                 if (not consider_test) {
128                         continue;
129                 }
130
131                 total_cnt += 1;
132                 if (run_test(test)) {
133                         printf("-- test case success: %s\n", test.name);
134                         success_cnt += 1;
135                 } else {
136                         printf("** test case FAILED : %s\n", test.name);
137                 }
138         }
139         printf("-- tests passing: %lu/%lu", success_cnt, total_cnt);
140         if (total_cnt) {
141                 printf(" (%lu%%)\n", success_cnt * 100 / total_cnt);
142         } else {
143                 printf("\n");
144         }
145         return (success_cnt == total_cnt) ? EXIT_SUCCESS : EXIT_FAILURE;
146 }