OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef PLATFORM_ASSERT_H_ | 5 #ifndef PLATFORM_ASSERT_H_ |
6 #define PLATFORM_ASSERT_H_ | 6 #define PLATFORM_ASSERT_H_ |
7 | 7 |
8 // TODO(5411406): include sstream for now, once we have a Utils::toString() | 8 // TODO(5411406): include sstream for now, once we have a Utils::toString() |
9 // implemented for all the primitive types we can replace the usage of | 9 // implemented for all the primitive types we can replace the usage of |
10 // sstream by Utils::toString() | 10 // sstream by Utils::toString() |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 void LessThan(const E& left, const A& right); | 58 void LessThan(const E& left, const A& right); |
59 | 59 |
60 template<typename E, typename A> | 60 template<typename E, typename A> |
61 void LessEqual(const E& left, const A& right); | 61 void LessEqual(const E& left, const A& right); |
62 | 62 |
63 template<typename E, typename A> | 63 template<typename E, typename A> |
64 void GreaterThan(const E& left, const A& right); | 64 void GreaterThan(const E& left, const A& right); |
65 | 65 |
66 template<typename E, typename A> | 66 template<typename E, typename A> |
67 void GreaterEqual(const E& left, const A& right); | 67 void GreaterEqual(const E& left, const A& right); |
| 68 #endif |
68 | 69 |
69 template<typename T> | 70 template<typename T> |
70 void NotNull(const T p); | 71 T NotNull(const T p); |
71 #endif | |
72 | 72 |
73 private: | 73 private: |
74 const char* const file_; | 74 const char* const file_; |
75 const int line_; | 75 const int line_; |
76 const Kind kind_; | 76 const Kind kind_; |
77 | 77 |
78 DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicAssertionHelper); | 78 DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicAssertionHelper); |
79 }; | 79 }; |
80 | 80 |
81 | 81 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 208 |
209 template<typename E, typename A> | 209 template<typename E, typename A> |
210 void DynamicAssertionHelper::GreaterEqual(const E& left, const A& right) { | 210 void DynamicAssertionHelper::GreaterEqual(const E& left, const A& right) { |
211 if (left >= right) return; | 211 if (left >= right) return; |
212 std::ostringstream ess, ass; | 212 std::ostringstream ess, ass; |
213 ess << left; | 213 ess << left; |
214 ass << right; | 214 ass << right; |
215 std::string es = ess.str(), as = ass.str(); | 215 std::string es = ess.str(), as = ass.str(); |
216 Fail("expected: %s >= %s", es.c_str(), as.c_str()); | 216 Fail("expected: %s >= %s", es.c_str(), as.c_str()); |
217 } | 217 } |
| 218 #endif |
218 | 219 |
219 | 220 |
220 template<typename T> | 221 template<typename T> |
221 void DynamicAssertionHelper::NotNull(const T p) { | 222 T DynamicAssertionHelper::NotNull(const T p) { |
222 if (p != NULL) return; | 223 if (p != NULL) return p; |
223 Fail("expected: not NULL, found NULL"); | 224 Fail("expected: not NULL, found NULL"); |
| 225 return NULL; |
224 } | 226 } |
225 #endif | |
226 | 227 |
227 } // namespace dart | 228 } // namespace dart |
228 | 229 |
229 | 230 |
230 #define FATAL(error) \ | 231 #define FATAL(error) \ |
231 dart::Assert(__FILE__, __LINE__).Fail("%s", error) | 232 dart::Assert(__FILE__, __LINE__).Fail("%s", error) |
232 | 233 |
233 #define FATAL1(format, p1) \ | 234 #define FATAL1(format, p1) \ |
234 dart::Assert(__FILE__, __LINE__).Fail(format, (p1)) | 235 dart::Assert(__FILE__, __LINE__).Fail(format, (p1)) |
235 | 236 |
(...skipping 15 matching lines...) Expand all Loading... |
251 | 252 |
252 #define ASSERT(cond) \ | 253 #define ASSERT(cond) \ |
253 do { \ | 254 do { \ |
254 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \ | 255 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \ |
255 } while (false) | 256 } while (false) |
256 | 257 |
257 // DEBUG_ASSERT allows identifiers in condition to be undeclared in release | 258 // DEBUG_ASSERT allows identifiers in condition to be undeclared in release |
258 // mode. | 259 // mode. |
259 #define DEBUG_ASSERT(cond) ASSERT(cond) | 260 #define DEBUG_ASSERT(cond) ASSERT(cond) |
260 | 261 |
| 262 // Returns 'ptr'; useful for initializer lists: |
| 263 // class Foo { Foo(int* ptr) : ptr_(ASSERT_NOTNULL(ptr)) ... |
| 264 #define ASSERT_NOTNULL(ptr) \ |
| 265 dart::Assert(__FILE__, __LINE__).NotNull((ptr)) |
| 266 |
261 #else // if defined(DEBUG) | 267 #else // if defined(DEBUG) |
262 | 268 |
263 // In order to avoid variable unused warnings for code that only uses | 269 // In order to avoid variable unused warnings for code that only uses |
264 // a variable in an ASSERT or EXPECT, we make sure to use the macro | 270 // a variable in an ASSERT or EXPECT, we make sure to use the macro |
265 // argument. | 271 // argument. |
266 #define ASSERT(condition) do {} while (false && (condition)) | 272 #define ASSERT(condition) do {} while (false && (condition)) |
267 | 273 |
268 #define DEBUG_ASSERT(cond) | 274 #define DEBUG_ASSERT(cond) |
269 | 275 |
| 276 #define ASSERT_NOTNULL(ptr) (ptr) |
| 277 |
270 #endif // if defined(DEBUG) | 278 #endif // if defined(DEBUG) |
271 | 279 |
272 | 280 |
273 // The COMPILE_ASSERT macro can be used to verify that a compile time | 281 // The COMPILE_ASSERT macro can be used to verify that a compile time |
274 // expression is true. For example, you could use it to verify the | 282 // expression is true. For example, you could use it to verify the |
275 // size of a static array: | 283 // size of a static array: |
276 // | 284 // |
277 // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES); | 285 // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES); |
278 // | 286 // |
279 // or to make sure a struct is smaller than a certain size: | 287 // or to make sure a struct is smaller than a certain size: |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 | 351 |
344 #define FAIL1(format, p1) \ | 352 #define FAIL1(format, p1) \ |
345 dart::Expect(__FILE__, __LINE__).Fail(format, (p1)) | 353 dart::Expect(__FILE__, __LINE__).Fail(format, (p1)) |
346 | 354 |
347 #define FAIL2(format, p1, p2) \ | 355 #define FAIL2(format, p1, p2) \ |
348 dart::Expect(__FILE__, __LINE__).Fail(format, (p1), (p2)) | 356 dart::Expect(__FILE__, __LINE__).Fail(format, (p1), (p2)) |
349 | 357 |
350 #endif // defined(TESTING) | 358 #endif // defined(TESTING) |
351 | 359 |
352 #endif // PLATFORM_ASSERT_H_ | 360 #endif // PLATFORM_ASSERT_H_ |
OLD | NEW |