OLD | NEW |
1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #ifndef SRC_SHARED_ASSERT_H_ | 5 #ifndef SRC_SHARED_ASSERT_H_ |
6 #define SRC_SHARED_ASSERT_H_ | 6 #define SRC_SHARED_ASSERT_H_ |
7 | 7 |
8 #ifdef TESTING | 8 #ifdef TESTING |
9 #include <sstream> | 9 #include <sstream> |
10 #include <string> | 10 #include <string> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 #ifdef static_assert | 14 #ifdef static_assert |
15 #undef static_assert | 15 #undef static_assert |
16 #endif | 16 #endif |
17 | 17 |
18 #include "src/shared/globals.h" | 18 #include "src/shared/globals.h" |
19 | 19 |
20 namespace fletch { | 20 namespace dartino { |
21 namespace DynamicAssertionHelper { | 21 namespace DynamicAssertionHelper { |
22 | 22 |
23 template <typename K> | 23 template <typename K> |
24 void Fail(const char* file, int line, const char* format, ...); | 24 void Fail(const char* file, int line, const char* format, ...); |
25 | 25 |
26 // two specializations of the above helper routine | 26 // two specializations of the above helper routine |
27 class ASSERT; | 27 class ASSERT; |
28 class EXPECT; | 28 class EXPECT; |
29 | 29 |
30 template <> | 30 template <> |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 if (left >= right) return; | 92 if (left >= right) return; |
93 std::stringstream ess, ass; | 93 std::stringstream ess, ass; |
94 ess << left; | 94 ess << left; |
95 ass << right; | 95 ass << right; |
96 std::string es = ess.str(), as = ass.str(); | 96 std::string es = ess.str(), as = ass.str(); |
97 Fail<K>(file, line, "expected: %s >= %s", es.c_str(), as.c_str()); | 97 Fail<K>(file, line, "expected: %s >= %s", es.c_str(), as.c_str()); |
98 } | 98 } |
99 #endif // ifdef TESTING | 99 #endif // ifdef TESTING |
100 | 100 |
101 } // namespace DynamicAssertionHelper | 101 } // namespace DynamicAssertionHelper |
102 } // namespace fletch | 102 } // namespace dartino |
103 | 103 |
104 #define FATAL(error) \ | 104 #define FATAL(error) \ |
105 fletch::DynamicAssertionHelper::Fail< \ | 105 dartino::DynamicAssertionHelper::Fail< \ |
106 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, "%s", error) | 106 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, "%s", error) |
107 | 107 |
108 #define FATAL1(format, p1) \ | 108 #define FATAL1(format, p1) \ |
109 fletch::DynamicAssertionHelper::Fail< \ | 109 dartino::DynamicAssertionHelper::Fail< \ |
110 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, format, \ | 110 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, format, \ |
111 (p1)) | 111 (p1)) |
112 | 112 |
113 #define FATAL2(format, p1, p2) \ | 113 #define FATAL2(format, p1, p2) \ |
114 fletch::Assert(__FILE__, __LINE__).Fail(format, (p1), (p2)) | 114 dartino::Assert(__FILE__, __LINE__).Fail(format, (p1), (p2)) |
115 | 115 |
116 #define UNIMPLEMENTED() FATAL("unimplemented code") | 116 #define UNIMPLEMENTED() FATAL("unimplemented code") |
117 | 117 |
118 #define UNREACHABLE() FATAL("unreachable code") | 118 #define UNREACHABLE() FATAL("unreachable code") |
119 | 119 |
120 #if !defined(TESTING) | 120 #if !defined(TESTING) |
121 // Only define the minimal set of assertions when not building the test | 121 // Only define the minimal set of assertions when not building the test |
122 // binaries. | 122 // binaries. |
123 | 123 |
124 // If the system already has an assert of its own, undefine it here so that | 124 // If the system already has an assert of its own, undefine it here so that |
125 // ours gets used. | 125 // ours gets used. |
126 #ifdef ASSERT | 126 #ifdef ASSERT |
127 #undef ASSERT | 127 #undef ASSERT |
128 #endif | 128 #endif |
129 | 129 |
130 #if defined(DEBUG) | 130 #if defined(DEBUG) |
131 // DEBUG binaries use assertions in the code. Due to concerns about the code | 131 // DEBUG binaries use assertions in the code. Due to concerns about the code |
132 // size we do not use the Equals templates for the ASSERT_EQ at the moment. | 132 // size we do not use the Equals templates for the ASSERT_EQ at the moment. |
133 | 133 |
134 #define ASSERT(condition) \ | 134 #define ASSERT(condition) \ |
135 if (!(condition)) { \ | 135 if (!(condition)) { \ |
136 fletch::DynamicAssertionHelper::Fail< \ | 136 dartino::DynamicAssertionHelper::Fail< \ |
137 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, \ | 137 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, \ |
138 "expected: %s", #condition); \ | 138 "expected: %s", #condition); \ |
139 } | 139 } |
140 | 140 |
141 #else // if defined(DEBUG) | 141 #else // if defined(DEBUG) |
142 | 142 |
143 // In order to avoid variable unused warnings for code that only uses | 143 // In order to avoid variable unused warnings for code that only uses |
144 // a variable in an ASSERT or EXPECT, we make sure to use the macro | 144 // a variable in an ASSERT or EXPECT, we make sure to use the macro |
145 // argument. | 145 // argument. |
146 #define ASSERT(condition) \ | 146 #define ASSERT(condition) \ |
147 while (false && (condition)) { \ | 147 while (false && (condition)) { \ |
148 } | 148 } |
149 | 149 |
150 #endif // if defined(DEBUG) | 150 #endif // if defined(DEBUG) |
151 | 151 |
152 #else // if !defined(TESTING) | 152 #else // if !defined(TESTING) |
153 // TESTING is defined when building the test files. They do have a much wider | 153 // TESTING is defined when building the test files. They do have a much wider |
154 // variety of checks and error reporting available when compared to the normal | 154 // variety of checks and error reporting available when compared to the normal |
155 // runtime code. Also all of the checks are enabled for the tests themselves. | 155 // runtime code. Also all of the checks are enabled for the tests themselves. |
156 // The runtime code only has assertions enabled when running the debug test | 156 // The runtime code only has assertions enabled when running the debug test |
157 // binaries. | 157 // binaries. |
158 | 158 |
159 #define ASSERT(condition) \ | 159 #define ASSERT(condition) \ |
160 if (!(condition)) { \ | 160 if (!(condition)) { \ |
161 fletch::DynamicAssertionHelper::Fail< \ | 161 dartino::DynamicAssertionHelper::Fail< \ |
162 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, \ | 162 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, \ |
163 "expected: %s", #condition); \ | 163 "expected: %s", #condition); \ |
164 } | 164 } |
165 | 165 |
166 #define ASSERT_EQ(expected, actual) \ | 166 #define ASSERT_EQ(expected, actual) \ |
167 fletch::DynamicAssertionHelper::Equals< \ | 167 dartino::DynamicAssertionHelper::Equals< \ |
168 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (expected), \ | 168 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (expected), \ |
169 (actual)) | 169 (actual)) |
170 | 170 |
171 #define ASSERT_STREQ(expected, actual) \ | 171 #define ASSERT_STREQ(expected, actual) \ |
172 fletch::DynamicAssertionHelper::StringEquals< \ | 172 dartino::DynamicAssertionHelper::StringEquals< \ |
173 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (expected), \ | 173 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (expected), \ |
174 (actual)) | 174 (actual)) |
175 | 175 |
176 #define ASSERT_LT(left, right) \ | 176 #define ASSERT_LT(left, right) \ |
177 fletch::DynamicAssertionHelper::LessThan< \ | 177 dartino::DynamicAssertionHelper::LessThan< \ |
178 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ | 178 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ |
179 (right)) | 179 (right)) |
180 | 180 |
181 #define ASSERT_LE(left, right) \ | 181 #define ASSERT_LE(left, right) \ |
182 fletch::DynamicAssertionHelper::LessEqual< \ | 182 dartino::DynamicAssertionHelper::LessEqual< \ |
183 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ | 183 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ |
184 (right)) | 184 (right)) |
185 | 185 |
186 #define ASSERT_GT(left, right) \ | 186 #define ASSERT_GT(left, right) \ |
187 fletch::DynamicAssertionHelper::GreaterThan< \ | 187 dartino::DynamicAssertionHelper::GreaterThan< \ |
188 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ | 188 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ |
189 (right)) | 189 (right)) |
190 | 190 |
191 #define ASSERT_GE(left, right) \ | 191 #define ASSERT_GE(left, right) \ |
192 fletch::DynamicAssertionHelper::GreaterEqual< \ | 192 dartino::DynamicAssertionHelper::GreaterEqual< \ |
193 fletch::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ | 193 dartino::DynamicAssertionHelper::ASSERT>(__FILE__, __LINE__, (left), \ |
194 (right)) | 194 (right)) |
195 | 195 |
196 #define EXPECT(condition) \ | 196 #define EXPECT(condition) \ |
197 if (!(condition)) { \ | 197 if (!(condition)) { \ |
198 fletch::DynamicAssertionHelper::Fail< \ | 198 dartino::DynamicAssertionHelper::Fail< \ |
199 fletch::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, \ | 199 dartino::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, \ |
200 "expected: %s", #condition); \ | 200 "expected: %s", #condition); \ |
201 } | 201 } |
202 | 202 |
203 #define EXPECT_EQ(expected, actual) \ | 203 #define EXPECT_EQ(expected, actual) \ |
204 fletch::DynamicAssertionHelper::Equals< \ | 204 dartino::DynamicAssertionHelper::Equals< \ |
205 fletch::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (expected), \ | 205 dartino::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (expected), \ |
206 (actual)) | 206 (actual)) |
207 | 207 |
208 #define EXPECT_STREQ(expected, actual) \ | 208 #define EXPECT_STREQ(expected, actual) \ |
209 fletch::DynamicAssertionHelper::StringEquals< \ | 209 dartino::DynamicAssertionHelper::StringEquals< \ |
210 fletch::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (expected), \ | 210 dartino::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (expected), \ |
211 (actual)) | 211 (actual)) |
212 | 212 |
213 #define EXPECT_LT(left, right) \ | 213 #define EXPECT_LT(left, right) \ |
214 fletch::DynamicAssertionHelper::LessThan< \ | 214 dartino::DynamicAssertionHelper::LessThan< \ |
215 fletch::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ | 215 dartino::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ |
216 (right)) | 216 (right)) |
217 | 217 |
218 #define EXPECT_LE(left, right) \ | 218 #define EXPECT_LE(left, right) \ |
219 fletch::DynamicAssertionHelper::LessEqual< \ | 219 dartino::DynamicAssertionHelper::LessEqual< \ |
220 fletch::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ | 220 dartino::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ |
221 (right)) | 221 (right)) |
222 | 222 |
223 #define EXPECT_GT(left, right) \ | 223 #define EXPECT_GT(left, right) \ |
224 fletch::DynamicAssertionHelper::GreaterThan< \ | 224 dartino::DynamicAssertionHelper::GreaterThan< \ |
225 fletch::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ | 225 dartino::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ |
226 (right)) | 226 (right)) |
227 | 227 |
228 #define EXPECT_GE(left, right) \ | 228 #define EXPECT_GE(left, right) \ |
229 fletch::DynamicAssertionHelper::GreaterEqual< \ | 229 dartino::DynamicAssertionHelper::GreaterEqual< \ |
230 fletch::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ | 230 dartino::DynamicAssertionHelper::EXPECT>(__FILE__, __LINE__, (left), \ |
231 (right)) | 231 (right)) |
232 | 232 |
233 #endif // if !defined(TESTING) | 233 #endif // if !defined(TESTING) |
234 | 234 |
235 #endif // SRC_SHARED_ASSERT_H_ | 235 #endif // SRC_SHARED_ASSERT_H_ |
OLD | NEW |