e3fb5446f5a9ce49f1b3ae8569ced986a4d8e30e
[iec.git] / src / type3_AndroidCloud / anbox-master / external / backward-cpp / test / test.hpp
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,
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,
78
79         STATUS_MASK = 0x1F
80 };
81
82 struct TestBase {
83         const char* name;
84         TestStatus expected_status;
85
86         virtual ~TestBase() {}
87         TestBase(const char*, TestStatus);
88         virtual void do_test() = 0;
89
90         TestStatus run() {
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;
108 extern test_registry_t test_registry;
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 */