OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_TESTS_TEST_CASE_H_ | 5 #ifndef PPAPI_TESTS_TEST_CASE_H_ |
6 #define PPAPI_TESTS_TEST_CASE_H_ | 6 #define PPAPI_TESTS_TEST_CASE_H_ |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
| 12 #include <sstream> |
12 #include <string> | 13 #include <string> |
13 | 14 |
14 #include "ppapi/c/pp_resource.h" | 15 #include "ppapi/c/pp_resource.h" |
15 #include "ppapi/c/pp_time.h" | 16 #include "ppapi/c/pp_time.h" |
16 #include "ppapi/c/private/ppb_testing_private.h" | 17 #include "ppapi/c/private/ppb_testing_private.h" |
17 #include "ppapi/cpp/dev/scrollbar_dev.h" | 18 #include "ppapi/cpp/dev/scrollbar_dev.h" |
18 #include "ppapi/cpp/message_loop.h" | 19 #include "ppapi/cpp/message_loop.h" |
19 #include "ppapi/cpp/view.h" | 20 #include "ppapi/cpp/view.h" |
20 #include "ppapi/tests/test_utils.h" | 21 #include "ppapi/tests/test_utils.h" |
21 #include "ppapi/tests/testing_instance.h" | 22 #include "ppapi/tests/testing_instance.h" |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 private: | 267 private: |
267 friend class TestingInstance; | 268 friend class TestingInstance; |
268 | 269 |
269 TestCaseFactory* next_; | 270 TestCaseFactory* next_; |
270 const char* name_; | 271 const char* name_; |
271 Method method_; | 272 Method method_; |
272 | 273 |
273 static TestCaseFactory* head_; | 274 static TestCaseFactory* head_; |
274 }; | 275 }; |
275 | 276 |
| 277 |
| 278 // This is an implementation detail, for use by ASSERT macros. |
| 279 template <class T> |
| 280 struct StringinatorBase { |
| 281 static std::string ToString(const T& value, const std::string& s) { |
| 282 std::stringstream stream; |
| 283 stream << value; |
| 284 return s + "(" + stream.str() + ")"; |
| 285 } |
| 286 private: |
| 287 // Not implemented, do not use. |
| 288 StringinatorBase(); |
| 289 ~StringinatorBase(); |
| 290 }; |
| 291 // The default version is for types that are not known to be stringifiable. |
| 292 // TODO(dmichael): Are there some useful traits for this? |
| 293 template <class T> |
| 294 struct Stringinator { |
| 295 static std::string ToString(const T& value, const std::string& s) { |
| 296 return s; |
| 297 } |
| 298 private: |
| 299 // Not implemented, do not use. |
| 300 Stringinator(); |
| 301 ~Stringinator(); |
| 302 }; |
| 303 #define STRINGINATOR_FOR_TYPE(type) \ |
| 304 template <> \ |
| 305 struct Stringinator<type> : public StringinatorBase<type> {}; |
| 306 STRINGINATOR_FOR_TYPE(int32_t); |
| 307 STRINGINATOR_FOR_TYPE(uint32_t); |
| 308 STRINGINATOR_FOR_TYPE(int64_t); |
| 309 STRINGINATOR_FOR_TYPE(uint64_t); |
| 310 STRINGINATOR_FOR_TYPE(float); |
| 311 STRINGINATOR_FOR_TYPE(double); |
| 312 STRINGINATOR_FOR_TYPE(bool); |
| 313 STRINGINATOR_FOR_TYPE(std::string); |
| 314 //TODO(dmichael): Put this somewhere else |
| 315 template <class T> |
| 316 std::string ToString(const T& param, const char* s) { |
| 317 return Stringinator<T>::ToString(param, std::string(s)); |
| 318 } |
| 319 // This overload is necessary to allow values from pp_errors.h such as PP_OK to |
| 320 // work. They won't automatically convert to an integral type to instantiate the |
| 321 // above template. |
| 322 inline std::string ToString(int32_t param, const char* s) { |
| 323 return Stringinator<int32_t>::ToString(param, std::string(s)); |
| 324 } |
| 325 #define TO_STRING(param) ToString(param, #param) |
| 326 |
276 // Use the REGISTER_TEST_CASE macro in your TestCase implementation file to | 327 // Use the REGISTER_TEST_CASE macro in your TestCase implementation file to |
277 // register your TestCase. If your test is named TestFoo, then add the | 328 // register your TestCase. If your test is named TestFoo, then add the |
278 // following to test_foo.cc: | 329 // following to test_foo.cc: |
279 // | 330 // |
280 // REGISTER_TEST_CASE(Foo); | 331 // REGISTER_TEST_CASE(Foo); |
281 // | 332 // |
282 // This will cause your test to be included in the set of known tests. | 333 // This will cause your test to be included in the set of known tests. |
283 // | 334 // |
284 #define REGISTER_TEST_CASE(name) \ | 335 #define REGISTER_TEST_CASE(name) \ |
285 static TestCase* Test##name##_FactoryMethod(TestingInstance* instance) { \ | 336 static TestCase* Test##name##_FactoryMethod(TestingInstance* instance) { \ |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 // cleanly. crbug.com/173503 | 417 // cleanly. crbug.com/173503 |
367 | 418 |
368 // Helper macros for checking values in tests, and returning a location | 419 // Helper macros for checking values in tests, and returning a location |
369 // description of the test fails. | 420 // description of the test fails. |
370 #define ASSERT_TRUE(cmd) \ | 421 #define ASSERT_TRUE(cmd) \ |
371 do { \ | 422 do { \ |
372 if (!(cmd)) \ | 423 if (!(cmd)) \ |
373 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ | 424 return MakeFailureMessage(__FILE__, __LINE__, #cmd); \ |
374 } while (false) | 425 } while (false) |
375 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) | 426 #define ASSERT_FALSE(cmd) ASSERT_TRUE(!(cmd)) |
376 #define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b)) | 427 #define ASSERT_BINARY_INTERNAL(a, op, b) \ |
| 428 do { \ |
| 429 if (!((a) op (b))) \ |
| 430 return MakeFailureMessage(__FILE__, __LINE__, \ |
| 431 (TO_STRING((a)) + " " #op " " + TO_STRING((b))).c_str()); \ |
| 432 } while (false) |
| 433 #define ASSERT_EQ(a, b) ASSERT_BINARY_INTERNAL((a), ==, (b)) |
377 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) | 434 #define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b)) |
378 #define ASSERT_LT(a, b) ASSERT_TRUE((a) < (b)) | 435 #define ASSERT_LT(a, b) ASSERT_TRUE((a) < (b)) |
379 #define ASSERT_LE(a, b) ASSERT_TRUE((a) <= (b)) | 436 #define ASSERT_LE(a, b) ASSERT_TRUE((a) <= (b)) |
380 #define ASSERT_GT(a, b) ASSERT_TRUE((a) > (b)) | 437 #define ASSERT_GT(a, b) ASSERT_TRUE((a) > (b)) |
381 #define ASSERT_GE(a, b) ASSERT_TRUE((a) >= (b)) | 438 #define ASSERT_GE(a, b) ASSERT_TRUE((a) >= (b)) |
382 | 439 |
383 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ | 440 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_TRUE( \ |
384 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) | 441 std::fabs((a)-(b)) <= std::numeric_limits<double>::epsilon()) |
385 | 442 |
386 // Runs |function| as a subtest and asserts that it has passed. | 443 // Runs |function| as a subtest and asserts that it has passed. |
387 #define ASSERT_SUBTEST_SUCCESS(function) \ | 444 #define ASSERT_SUBTEST_SUCCESS(function) \ |
388 do { \ | 445 do { \ |
389 std::string result = (function); \ | 446 std::string result = (function); \ |
390 if (!result.empty()) \ | 447 if (!result.empty()) \ |
391 return result; \ | 448 return result; \ |
392 } while (false) | 449 } while (false) |
393 | 450 |
394 #define PASS() return std::string() | 451 #define PASS() return std::string() |
395 | 452 |
396 #endif // PPAPI_TESTS_TEST_CASE_H_ | 453 #endif // PPAPI_TESTS_TEST_CASE_H_ |
OLD | NEW |