| 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 RUNTIME_PLATFORM_ASSERT_H_ | 5 #ifndef RUNTIME_PLATFORM_ASSERT_H_ |
| 6 #define RUNTIME_PLATFORM_ASSERT_H_ | 6 #define RUNTIME_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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 private: | 73 private: |
| 74 static bool failed_; | 74 static bool failed_; |
| 75 | 75 |
| 76 const char* const file_; | 76 const char* const file_; |
| 77 const int line_; | 77 const int line_; |
| 78 const Kind kind_; | 78 const Kind kind_; |
| 79 | 79 |
| 80 DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicAssertionHelper); | 80 DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicAssertionHelper); |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 | |
| 84 class Assert : public DynamicAssertionHelper { | 83 class Assert : public DynamicAssertionHelper { |
| 85 public: | 84 public: |
| 86 Assert(const char* file, int line) | 85 Assert(const char* file, int line) |
| 87 : DynamicAssertionHelper(file, line, ASSERT) {} | 86 : DynamicAssertionHelper(file, line, ASSERT) {} |
| 88 }; | 87 }; |
| 89 | 88 |
| 90 | |
| 91 class Expect : public DynamicAssertionHelper { | 89 class Expect : public DynamicAssertionHelper { |
| 92 public: | 90 public: |
| 93 Expect(const char* file, int line) | 91 Expect(const char* file, int line) |
| 94 : DynamicAssertionHelper(file, line, EXPECT) {} | 92 : DynamicAssertionHelper(file, line, EXPECT) {} |
| 95 }; | 93 }; |
| 96 | 94 |
| 97 | |
| 98 #if defined(TESTING) | 95 #if defined(TESTING) |
| 99 // Only allow the expensive (with respect to code size) assertions | 96 // Only allow the expensive (with respect to code size) assertions |
| 100 // in testing code. | 97 // in testing code. |
| 101 template <typename E, typename A> | 98 template <typename E, typename A> |
| 102 void DynamicAssertionHelper::Equals(const E& expected, const A& actual) { | 99 void DynamicAssertionHelper::Equals(const E& expected, const A& actual) { |
| 103 if (actual == expected) return; | 100 if (actual == expected) return; |
| 104 std::ostringstream ess, ass; | 101 std::ostringstream ess, ass; |
| 105 ess << expected; | 102 ess << expected; |
| 106 ass << actual; | 103 ass << actual; |
| 107 std::string es = ess.str(), as = ass.str(); | 104 std::string es = ess.str(), as = ass.str(); |
| 108 Fail("expected: <%s> but was: <%s>", es.c_str(), as.c_str()); | 105 Fail("expected: <%s> but was: <%s>", es.c_str(), as.c_str()); |
| 109 } | 106 } |
| 110 | 107 |
| 111 | |
| 112 template <typename E, typename A> | 108 template <typename E, typename A> |
| 113 void DynamicAssertionHelper::NotEquals(const E& not_expected, const A& actual) { | 109 void DynamicAssertionHelper::NotEquals(const E& not_expected, const A& actual) { |
| 114 if (actual != not_expected) return; | 110 if (actual != not_expected) return; |
| 115 std::ostringstream ness; | 111 std::ostringstream ness; |
| 116 ness << not_expected; | 112 ness << not_expected; |
| 117 std::string nes = ness.str(); | 113 std::string nes = ness.str(); |
| 118 Fail("did not expect: <%s>", nes.c_str()); | 114 Fail("did not expect: <%s>", nes.c_str()); |
| 119 } | 115 } |
| 120 | 116 |
| 121 | |
| 122 template <typename E, typename A, typename T> | 117 template <typename E, typename A, typename T> |
| 123 void DynamicAssertionHelper::FloatEquals(const E& expected, | 118 void DynamicAssertionHelper::FloatEquals(const E& expected, |
| 124 const A& actual, | 119 const A& actual, |
| 125 const T& tol) { | 120 const T& tol) { |
| 126 if (((expected - tol) <= actual) && (actual <= (expected + tol))) { | 121 if (((expected - tol) <= actual) && (actual <= (expected + tol))) { |
| 127 return; | 122 return; |
| 128 } | 123 } |
| 129 std::ostringstream ess, ass, tolss; | 124 std::ostringstream ess, ass, tolss; |
| 130 ess << expected; | 125 ess << expected; |
| 131 ass << actual; | 126 ass << actual; |
| 132 tolss << tol; | 127 tolss << tol; |
| 133 std::string es = ess.str(), as = ass.str(), tols = tolss.str(); | 128 std::string es = ess.str(), as = ass.str(), tols = tolss.str(); |
| 134 Fail("expected: <%s> but was: <%s> (tolerance: <%s>)", es.c_str(), as.c_str(), | 129 Fail("expected: <%s> but was: <%s> (tolerance: <%s>)", es.c_str(), as.c_str(), |
| 135 tols.c_str()); | 130 tols.c_str()); |
| 136 } | 131 } |
| 137 | 132 |
| 138 | |
| 139 template <typename E, typename A> | 133 template <typename E, typename A> |
| 140 NO_SANITIZE_MEMORY void DynamicAssertionHelper::StringEquals(const E& expected, | 134 NO_SANITIZE_MEMORY void DynamicAssertionHelper::StringEquals(const E& expected, |
| 141 const A& actual) { | 135 const A& actual) { |
| 142 std::ostringstream ess, ass; | 136 std::ostringstream ess, ass; |
| 143 ess << expected; | 137 ess << expected; |
| 144 ass << actual; | 138 ass << actual; |
| 145 std::string es = ess.str(), as = ass.str(); | 139 std::string es = ess.str(), as = ass.str(); |
| 146 if (as == es) return; | 140 if (as == es) return; |
| 147 Fail("expected:\n<\"%s\">\nbut was:\n<\"%s\">", es.c_str(), as.c_str()); | 141 Fail("expected:\n<\"%s\">\nbut was:\n<\"%s\">", es.c_str(), as.c_str()); |
| 148 } | 142 } |
| 149 | 143 |
| 150 | |
| 151 template <typename E, typename A> | 144 template <typename E, typename A> |
| 152 NO_SANITIZE_MEMORY void DynamicAssertionHelper::IsSubstring(const E& needle, | 145 NO_SANITIZE_MEMORY void DynamicAssertionHelper::IsSubstring(const E& needle, |
| 153 const A& haystack) { | 146 const A& haystack) { |
| 154 std::ostringstream ess, ass; | 147 std::ostringstream ess, ass; |
| 155 ess << needle; | 148 ess << needle; |
| 156 ass << haystack; | 149 ass << haystack; |
| 157 std::string es = ess.str(), as = ass.str(); | 150 std::string es = ess.str(), as = ass.str(); |
| 158 if (as.find(es) != std::string::npos) return; | 151 if (as.find(es) != std::string::npos) return; |
| 159 Fail("expected <\"%s\"> to be a substring of <\"%s\">", es.c_str(), | 152 Fail("expected <\"%s\"> to be a substring of <\"%s\">", es.c_str(), |
| 160 as.c_str()); | 153 as.c_str()); |
| 161 } | 154 } |
| 162 | 155 |
| 163 | |
| 164 template <typename E, typename A> | 156 template <typename E, typename A> |
| 165 NO_SANITIZE_MEMORY void DynamicAssertionHelper::IsNotSubstring( | 157 NO_SANITIZE_MEMORY void DynamicAssertionHelper::IsNotSubstring( |
| 166 const E& needle, | 158 const E& needle, |
| 167 const A& haystack) { | 159 const A& haystack) { |
| 168 std::ostringstream ess, ass; | 160 std::ostringstream ess, ass; |
| 169 ess << needle; | 161 ess << needle; |
| 170 ass << haystack; | 162 ass << haystack; |
| 171 std::string es = ess.str(), as = ass.str(); | 163 std::string es = ess.str(), as = ass.str(); |
| 172 if (as.find(es) == std::string::npos) return; | 164 if (as.find(es) == std::string::npos) return; |
| 173 Fail("expected <\"%s\"> to not be a substring of <\"%s\">", es.c_str(), | 165 Fail("expected <\"%s\"> to not be a substring of <\"%s\">", es.c_str(), |
| 174 as.c_str()); | 166 as.c_str()); |
| 175 } | 167 } |
| 176 | 168 |
| 177 | |
| 178 template <typename E, typename A> | 169 template <typename E, typename A> |
| 179 void DynamicAssertionHelper::LessThan(const E& left, const A& right) { | 170 void DynamicAssertionHelper::LessThan(const E& left, const A& right) { |
| 180 if (left < right) return; | 171 if (left < right) return; |
| 181 std::ostringstream ess, ass; | 172 std::ostringstream ess, ass; |
| 182 ess << left; | 173 ess << left; |
| 183 ass << right; | 174 ass << right; |
| 184 std::string es = ess.str(), as = ass.str(); | 175 std::string es = ess.str(), as = ass.str(); |
| 185 Fail("expected: %s < %s", es.c_str(), as.c_str()); | 176 Fail("expected: %s < %s", es.c_str(), as.c_str()); |
| 186 } | 177 } |
| 187 | 178 |
| 188 | |
| 189 template <typename E, typename A> | 179 template <typename E, typename A> |
| 190 void DynamicAssertionHelper::LessEqual(const E& left, const A& right) { | 180 void DynamicAssertionHelper::LessEqual(const E& left, const A& right) { |
| 191 if (left <= right) return; | 181 if (left <= right) return; |
| 192 std::ostringstream ess, ass; | 182 std::ostringstream ess, ass; |
| 193 ess << left; | 183 ess << left; |
| 194 ass << right; | 184 ass << right; |
| 195 std::string es = ess.str(), as = ass.str(); | 185 std::string es = ess.str(), as = ass.str(); |
| 196 Fail("expected: %s <= %s", es.c_str(), as.c_str()); | 186 Fail("expected: %s <= %s", es.c_str(), as.c_str()); |
| 197 } | 187 } |
| 198 | 188 |
| 199 | |
| 200 template <typename E, typename A> | 189 template <typename E, typename A> |
| 201 void DynamicAssertionHelper::GreaterThan(const E& left, const A& right) { | 190 void DynamicAssertionHelper::GreaterThan(const E& left, const A& right) { |
| 202 if (left > right) return; | 191 if (left > right) return; |
| 203 std::ostringstream ess, ass; | 192 std::ostringstream ess, ass; |
| 204 ess << left; | 193 ess << left; |
| 205 ass << right; | 194 ass << right; |
| 206 std::string es = ess.str(), as = ass.str(); | 195 std::string es = ess.str(), as = ass.str(); |
| 207 Fail("expected: %s > %s", es.c_str(), as.c_str()); | 196 Fail("expected: %s > %s", es.c_str(), as.c_str()); |
| 208 } | 197 } |
| 209 | 198 |
| 210 | |
| 211 template <typename E, typename A> | 199 template <typename E, typename A> |
| 212 void DynamicAssertionHelper::GreaterEqual(const E& left, const A& right) { | 200 void DynamicAssertionHelper::GreaterEqual(const E& left, const A& right) { |
| 213 if (left >= right) return; | 201 if (left >= right) return; |
| 214 std::ostringstream ess, ass; | 202 std::ostringstream ess, ass; |
| 215 ess << left; | 203 ess << left; |
| 216 ass << right; | 204 ass << right; |
| 217 std::string es = ess.str(), as = ass.str(); | 205 std::string es = ess.str(), as = ass.str(); |
| 218 Fail("expected: %s >= %s", es.c_str(), as.c_str()); | 206 Fail("expected: %s >= %s", es.c_str(), as.c_str()); |
| 219 } | 207 } |
| 220 #endif | 208 #endif |
| 221 | 209 |
| 222 | |
| 223 template <typename T> | 210 template <typename T> |
| 224 T DynamicAssertionHelper::NotNull(const T p) { | 211 T DynamicAssertionHelper::NotNull(const T p) { |
| 225 if (p != NULL) return p; | 212 if (p != NULL) return p; |
| 226 Fail("expected: not NULL, found NULL"); | 213 Fail("expected: not NULL, found NULL"); |
| 227 return NULL; | 214 return NULL; |
| 228 } | 215 } |
| 229 | 216 |
| 230 } // namespace dart | 217 } // namespace dart |
| 231 | 218 |
| 232 | |
| 233 #define FATAL(error) dart::Assert(__FILE__, __LINE__).Fail("%s", error) | 219 #define FATAL(error) dart::Assert(__FILE__, __LINE__).Fail("%s", error) |
| 234 | 220 |
| 235 #define FATAL1(format, p1) dart::Assert(__FILE__, __LINE__).Fail(format, (p1)) | 221 #define FATAL1(format, p1) dart::Assert(__FILE__, __LINE__).Fail(format, (p1)) |
| 236 | 222 |
| 237 #define FATAL2(format, p1, p2) \ | 223 #define FATAL2(format, p1, p2) \ |
| 238 dart::Assert(__FILE__, __LINE__).Fail(format, (p1), (p2)) | 224 dart::Assert(__FILE__, __LINE__).Fail(format, (p1), (p2)) |
| 239 | 225 |
| 240 #define FATAL3(format, p1, p2, p3) \ | 226 #define FATAL3(format, p1, p2, p3) \ |
| 241 dart::Assert(__FILE__, __LINE__).Fail(format, (p1), (p2), (p3)) | 227 dart::Assert(__FILE__, __LINE__).Fail(format, (p1), (p2), (p3)) |
| 242 | 228 |
| 243 #define UNIMPLEMENTED() FATAL("unimplemented code") | 229 #define UNIMPLEMENTED() FATAL("unimplemented code") |
| 244 | 230 |
| 245 #define UNREACHABLE() FATAL("unreachable code") | 231 #define UNREACHABLE() FATAL("unreachable code") |
| 246 | 232 |
| 247 #define OUT_OF_MEMORY() FATAL("Out of memory.") | 233 #define OUT_OF_MEMORY() FATAL("Out of memory.") |
| 248 | 234 |
| 249 | |
| 250 #if defined(DEBUG) | 235 #if defined(DEBUG) |
| 251 // DEBUG binaries use assertions in the code. | 236 // DEBUG binaries use assertions in the code. |
| 252 // Note: We wrap the if statement in a do-while so that we get a compile | 237 // Note: We wrap the if statement in a do-while so that we get a compile |
| 253 // error if there is no semicolon after ASSERT(condition). This | 238 // error if there is no semicolon after ASSERT(condition). This |
| 254 // ensures that we get the same behavior on DEBUG and RELEASE builds. | 239 // ensures that we get the same behavior on DEBUG and RELEASE builds. |
| 255 | 240 |
| 256 #define ASSERT(cond) \ | 241 #define ASSERT(cond) \ |
| 257 do { \ | 242 do { \ |
| 258 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \ | 243 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \ |
| 259 } while (false) | 244 } while (false) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 274 #define ASSERT(condition) \ | 259 #define ASSERT(condition) \ |
| 275 do { \ | 260 do { \ |
| 276 } while (false && (condition)) | 261 } while (false && (condition)) |
| 277 | 262 |
| 278 #define DEBUG_ASSERT(cond) | 263 #define DEBUG_ASSERT(cond) |
| 279 | 264 |
| 280 #define ASSERT_NOTNULL(ptr) (ptr) | 265 #define ASSERT_NOTNULL(ptr) (ptr) |
| 281 | 266 |
| 282 #endif // if defined(DEBUG) | 267 #endif // if defined(DEBUG) |
| 283 | 268 |
| 284 | |
| 285 #define RELEASE_ASSERT(cond) \ | 269 #define RELEASE_ASSERT(cond) \ |
| 286 do { \ | 270 do { \ |
| 287 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \ | 271 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \ |
| 288 } while (false) | 272 } while (false) |
| 289 | 273 |
| 290 | |
| 291 // The COMPILE_ASSERT macro can be used to verify that a compile time | 274 // The COMPILE_ASSERT macro can be used to verify that a compile time |
| 292 // expression is true. For example, you could use it to verify the | 275 // expression is true. For example, you could use it to verify the |
| 293 // size of a static array: | 276 // size of a static array: |
| 294 // | 277 // |
| 295 // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES); | 278 // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES); |
| 296 // | 279 // |
| 297 // or to make sure a struct is smaller than a certain size: | 280 // or to make sure a struct is smaller than a certain size: |
| 298 // | 281 // |
| 299 // COMPILE_ASSERT(sizeof(foo) < 128); | 282 // COMPILE_ASSERT(sizeof(foo) < 128); |
| 300 // | 283 // |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 #define FAIL(error) dart::Expect(__FILE__, __LINE__).Fail("%s", error) | 342 #define FAIL(error) dart::Expect(__FILE__, __LINE__).Fail("%s", error) |
| 360 | 343 |
| 361 #define FAIL1(format, p1) dart::Expect(__FILE__, __LINE__).Fail(format, (p1)) | 344 #define FAIL1(format, p1) dart::Expect(__FILE__, __LINE__).Fail(format, (p1)) |
| 362 | 345 |
| 363 #define FAIL2(format, p1, p2) \ | 346 #define FAIL2(format, p1, p2) \ |
| 364 dart::Expect(__FILE__, __LINE__).Fail(format, (p1), (p2)) | 347 dart::Expect(__FILE__, __LINE__).Fail(format, (p1), (p2)) |
| 365 | 348 |
| 366 #endif // defined(TESTING) | 349 #endif // defined(TESTING) |
| 367 | 350 |
| 368 #endif // RUNTIME_PLATFORM_ASSERT_H_ | 351 #endif // RUNTIME_PLATFORM_ASSERT_H_ |
| OLD | NEW |