test.hpp
Go to the documentation of this file.
1 /*
2  * test/test.hpp
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 #pragma once
25 #ifndef H_54E531F7_9154_454B_BEB9_257408429470
26 #define H_54E531F7_9154_454B_BEB9_257408429470
27 
28 #include <exception>
29 #include <vector>
30 #include <cstring>
31 #include <cstdio>
32 #include <sstream>
33 #include <string>
34 
35 namespace test {
36 
37 struct AssertFailedError: std::exception {
38  ~AssertFailedError() throw() {}
39 
40  AssertFailedError(const char* filename, int _line, const char* _errmsg):
41  basename(_basename(filename)), line(_line), errmsg(_errmsg) {}
42 
43  const char* what() const throw() {
44  if (not _what.size()) {
45  std::ostringstream ss;
46  ss << "assertion failed (" << basename << ":" << line;
47  ss << ") " << errmsg;
48  _what = ss.str();
49  }
50  return _what.c_str();
51  }
52 
53  const char* basename;
54  int line;
55  const char* errmsg;
56 
57  mutable std::string _what;
58 
59  static const char* _basename(const char* filename) {
60  const char* basename = filename + strlen(filename);
61  while (basename != filename && *basename != '/') {
62  basename -= 1;
63  }
64  return basename + 1;
65  }
66 };
67 
68 enum TestStatus {
69  SUCCESS = 0<<0,
70  FAILED = 1<<0,
71 
72  ASSERT_FAIL = FAILED | 1<<1,
78 
79  STATUS_MASK = 0x1F
80 };
81 
82 struct TestBase {
83  const char* name;
85 
86  virtual ~TestBase() {}
87  TestBase(const char*, TestStatus);
88  virtual void do_test() = 0;
89 
91  try {
92  do_test();
93  return SUCCESS;
94  } catch(const AssertFailedError& e) {
95  printf("!! %s\n", e.what());
96  return ASSERT_FAIL;
97  } catch(const std::exception& e) {
98  printf("!! exception: %s\n", e.what());
99  return EXCEPTION_UNCAUGHT;
100  } catch(...) {
101  printf("!! unknown exception\n");
102  return EXCEPTION_UNCAUGHT;
103  }
104  }
105 };
106 
107 typedef std::vector<TestBase*> test_registry_t;
109 
110 TestBase::TestBase(const char* n, TestStatus s): name(n), expected_status(s) {
111  test_registry.push_back(this);
112 }
113 
114 } // namespace test
115 
116 #define _TEST_STATUS(name, status) \
117  struct TEST_##name: ::test::TestBase { \
118  TEST_##name(): TestBase(#name, status) {} \
119  void do_test(); \
120  } TEST_##name; \
121  void TEST_##name::do_test()
122 
123 #define TEST(name) _TEST_STATUS(name, ::test::SUCCESS)
124 #define TEST_FAIL(name) _TEST_STATUS(name, ::test::FAILED)
125 #define TEST_FAIL_ASSERT(name) _TEST_STATUS(name, ::test::ASSERT_FAIL)
126 #define TEST_UNCAUGHT_EXCEPTION(name) _TEST_STATUS(name, ::test::EXCEPTION_UNCAUGHT)
127 #define TEST_UNCAUGHT_SIGNAL(name) _TEST_STATUS(name, ::test::SIGNAL_UNCAUGHT)
128 #define TEST_SEGFAULT(name) _TEST_STATUS(name, ::test::SIGNAL_SEGFAULT)
129 #define TEST_ABORT(name) _TEST_STATUS(name, ::test::SIGNAL_ABORT)
130 #define TEST_DIVZERO(name) _TEST_STATUS(name, ::test::SIGNAL_DIVZERO)
131 
132 #define ASSERT(expr) \
133  (expr) ? static_cast<void>(0) \
134  : throw ::test::AssertFailedError( \
135  __FILE__, __LINE__, #expr)
136 
137 #define _ASSERT_BINOP(a, b, cmp) \
138  (not (a cmp b)) ? static_cast<void>(0) \
139  : throw ::test::AssertFailedError( \
140  __FILE__, __LINE__, "because " #a " " #cmp " " #b)
141 
142 #define ASSERT_EQ(a, b) _ASSERT_BINOP(a, b, !=)
143 #define ASSERT_NE(a, b) _ASSERT_BINOP(a, b, ==)
144 #define ASSERT_LT(a, b) _ASSERT_BINOP(a, b, >=)
145 #define ASSERT_LE(a, b) _ASSERT_BINOP(a, b, >)
146 #define ASSERT_GT(a, b) _ASSERT_BINOP(a, b, <=)
147 #define ASSERT_GE(a, b) _ASSERT_BINOP(a, b, <)
148 
149 #define ASSERT_THROW(expr, e_type) \
150  do { try { expr } \
151  catch (const e_type&) { break; } \
152  throw ::test::AssertFailedError( \
153  __FILE__, __LINE__, "expected exception " #e_type); \
154  } while(0)
155 
156 #define ASSERT_ANY_THROW(expr) \
157  do { try { expr } \
158  catch (...) { break; } \
159  throw ::test::AssertFailedError( \
160  __FILE__, __LINE__, "expected any exception"); \
161  } while(0)
162 
163 #define ASSERT_NO_THROW(expr) \
164  try { expr } \
165  catch (...) { \
166  throw ::test::AssertFailedError( \
167  __FILE__, __LINE__, "no exception expected"); \
168  }
169 
170 #endif /* H_GUARD */
test::AssertFailedError::_what
std::string _what
Definition: test.hpp:57
test::TestBase::name
const char * name
Definition: test.hpp:83
test::ASSERT_FAIL
@ ASSERT_FAIL
Definition: test.hpp:72
test::TestBase::expected_status
TestStatus expected_status
Definition: test.hpp:84
test::SUCCESS
@ SUCCESS
Definition: test.hpp:69
test::TestBase::TestBase
TestBase(const char *, TestStatus)
Definition: test.hpp:110
s
XmlRpcServer s
test::TestStatus
TestStatus
Definition: test.hpp:68
test::SIGNAL_UNCAUGHT
@ SIGNAL_UNCAUGHT
Definition: test.hpp:74
test::AssertFailedError::~AssertFailedError
~AssertFailedError()
Definition: test.hpp:38
test::AssertFailedError::basename
const char * basename
Definition: test.hpp:53
test::SIGNAL_SEGFAULT
@ SIGNAL_SEGFAULT
Definition: test.hpp:75
test
Definition: test.hpp:35
test::TestBase::do_test
virtual void do_test()=0
test::AssertFailedError::AssertFailedError
AssertFailedError(const char *filename, int _line, const char *_errmsg)
Definition: test.hpp:40
test::test_registry_t
std::vector< TestBase * > test_registry_t
Definition: test.hpp:107
test::AssertFailedError::errmsg
const char * errmsg
Definition: test.hpp:55
test::TestBase::run
TestStatus run()
Definition: test.hpp:90
test::AssertFailedError::what
const char * what() const
Definition: test.hpp:43
test::STATUS_MASK
@ STATUS_MASK
Definition: test.hpp:79
test::SIGNAL_DIVZERO
@ SIGNAL_DIVZERO
Definition: test.hpp:77
test::TestBase
Definition: test.hpp:82
test::AssertFailedError
Definition: test.hpp:37
test::FAILED
@ FAILED
Definition: test.hpp:70
test::test_registry
test_registry_t test_registry
Definition: _test_main.cpp:31
test::EXCEPTION_UNCAUGHT
@ EXCEPTION_UNCAUGHT
Definition: test.hpp:73
test::TestBase::~TestBase
virtual ~TestBase()
Definition: test.hpp:86
test::SIGNAL_ABORT
@ SIGNAL_ABORT
Definition: test.hpp:76
test::AssertFailedError::line
int line
Definition: test.hpp:54
test::AssertFailedError::_basename
static const char * _basename(const char *filename)
Definition: test.hpp:59


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