_test_main.cpp
Go to the documentation of this file.
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 #include <error.h>
28 #include <unistd.h>
29 #include <sys/wait.h>
30 
32 using namespace test;
33 
35  printf("-- running test case: %s\n", test.name);
36 
37  fflush(stdout);
38  pid_t child_pid = fork();
39  if (child_pid == 0) {
40  exit(static_cast<int>(test.run()));
41  }
42  if (child_pid == -1) {
43  error(EXIT_FAILURE, 0, "unable to fork");
44  }
45 
46  int child_status = 0;
47  waitpid(child_pid, &child_status, 0);
48 
49  test::TestStatus status;
50 
51  if (WIFEXITED(child_status)) {
52  int exit_status = WEXITSTATUS(child_status);
53  if (exit_status & ~test::STATUS_MASK) {
54  status = test::FAILED;
55  } else {
56  status = static_cast<test::TestStatus>(exit_status);
57  }
58  } else if (WIFSIGNALED(child_status)) {
59  const int signum = WTERMSIG(child_status);
60  printf("!! signal (%d) %s\n", signum, strsignal(signum));
61  switch (signum) {
62  case SIGABRT:
63  status = test::SIGNAL_ABORT; break;
64  case SIGSEGV:
65  case SIGBUS:
66  status = test::SIGNAL_SEGFAULT; break;
67  case SIGFPE:
68  status = test::SIGNAL_DIVZERO; break;
69  default:
70  status = test::SIGNAL_UNCAUGHT;
71  }
72  } else {
73  status = test::SUCCESS;
74  }
75 
76  if (test.expected_status == test::FAILED) {
77  return (status & test::FAILED);
78  }
79 
80  if (test.expected_status == test::SIGNAL_UNCAUGHT) {
81  return (status & test::SIGNAL_UNCAUGHT);
82  }
83 
84  return status == test.expected_status;
85 }
86 
87 int main(int argc, const char* const argv[]) {
88 
89  size_t success_cnt = 0;
90  size_t total_cnt = 0;
91  for (test_registry_t::iterator it = test_registry.begin();
92  it != test_registry.end(); ++it) {
93  TestBase& test = **it;
94 
95  bool consider_test = (argc <= 1);
96  for (int i = 1; i < argc; ++i) {
97  if (strcasecmp(argv[i], test.name) == 0) {
98  consider_test = true;
99  break;
100  }
101  }
102  if (not consider_test) {
103  continue;
104  }
105 
106  total_cnt += 1;
107  if (run_test(test)) {
108  printf("-- test case success: %s\n", test.name);
109  success_cnt += 1;
110  } else {
111  printf("** test case FAILED : %s\n", test.name);
112  }
113  }
114  printf("-- tests passing: %lu/%lu", success_cnt, total_cnt);
115  if (total_cnt) {
116  printf(" (%lu%%)\n", success_cnt * 100 / total_cnt);
117  } else {
118  printf("\n");
119  }
120  return (success_cnt == total_cnt) ? EXIT_SUCCESS : EXIT_FAILURE;
121 }
test::SUCCESS
@ SUCCESS
Definition: test.hpp:69
test::TestStatus
TestStatus
Definition: test.hpp:68
test::SIGNAL_UNCAUGHT
@ SIGNAL_UNCAUGHT
Definition: test.hpp:74
test::SIGNAL_SEGFAULT
@ SIGNAL_SEGFAULT
Definition: test.hpp:75
test
Definition: test.hpp:35
test::test_registry_t
std::vector< TestBase * > test_registry_t
Definition: test.hpp:107
test::STATUS_MASK
@ STATUS_MASK
Definition: test.hpp:79
test.hpp
test::SIGNAL_DIVZERO
@ SIGNAL_DIVZERO
Definition: test.hpp:77
test::TestBase
Definition: test.hpp:82
run_test
bool run_test(TestBase &test)
Definition: _test_main.cpp:34
test::FAILED
@ FAILED
Definition: test.hpp:70
test::test_registry
test_registry_t test_registry
Definition: _test_main.cpp:31
test::SIGNAL_ABORT
@ SIGNAL_ABORT
Definition: test.hpp:76
main
int main(int argc, const char *const argv[])
Definition: _test_main.cpp:87


backward_ros
Author(s):
autogenerated on Tue Mar 1 2022 23:50:48