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
25 #ifndef H_54E531F7_9154_454B_BEB9_257408429470
26 #define H_54E531F7_9154_454B_BEB9_257408429470
37 struct AssertFailedError: std::exception {
38 ~AssertFailedError() throw() {}
40 AssertFailedError(const char* filename, int _line, const char* _errmsg):
41 basename(_basename(filename)), line(_line), errmsg(_errmsg) {}
43 const char* what() const throw() {
44 if (not _what.size()) {
45 std::ostringstream ss;
46 ss << "assertion failed (" << basename << ":" << line;
57 mutable std::string _what;
59 static const char* _basename(const char* filename) {
60 const char* basename = filename + strlen(filename);
61 while (basename != filename && *basename != '/') {
72 ASSERT_FAIL = FAILED | 1<<1,
73 EXCEPTION_UNCAUGHT = FAILED | 2<<1,
74 SIGNAL_UNCAUGHT = FAILED | 3<<1,
75 SIGNAL_SEGFAULT = SIGNAL_UNCAUGHT | 1<<3,
76 SIGNAL_ABORT = SIGNAL_UNCAUGHT | 2<<3,
77 SIGNAL_DIVZERO = SIGNAL_UNCAUGHT | 2<<3,
84 TestStatus expected_status;
86 virtual ~TestBase() {}
87 TestBase(const char*, TestStatus);
88 virtual void do_test() = 0;
94 } catch(const AssertFailedError& e) {
95 printf("!! %s\n", e.what());
97 } catch(const std::exception& e) {
98 printf("!! exception: %s\n", e.what());
99 return EXCEPTION_UNCAUGHT;
101 printf("!! unknown exception\n");
102 return EXCEPTION_UNCAUGHT;
107 typedef std::vector<TestBase*> test_registry_t;
108 extern test_registry_t test_registry;
110 TestBase::TestBase(const char* n, TestStatus s): name(n), expected_status(s) {
111 test_registry.push_back(this);
116 #define _TEST_STATUS(name, status) \
117 struct TEST_##name: ::test::TestBase { \
118 TEST_##name(): TestBase(#name, status) {} \
121 void TEST_##name::do_test()
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)
132 #define ASSERT(expr) \
133 (expr) ? static_cast<void>(0) \
134 : throw ::test::AssertFailedError( \
135 __FILE__, __LINE__, #expr)
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)
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, <)
149 #define ASSERT_THROW(expr, e_type) \
151 catch (const e_type&) { break; } \
152 throw ::test::AssertFailedError( \
153 __FILE__, __LINE__, "expected exception " #e_type); \
156 #define ASSERT_ANY_THROW(expr) \
158 catch (...) { break; } \
159 throw ::test::AssertFailedError( \
160 __FILE__, __LINE__, "expected any exception"); \
163 #define ASSERT_NO_THROW(expr) \
166 throw ::test::AssertFailedError( \
167 __FILE__, __LINE__, "no exception expected"); \