cctest.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2008 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #ifndef CCTEST_H_
  28. #define CCTEST_H_
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "utils.h"
  32. #ifndef TEST
  33. #define TEST(Name) \
  34. static void Test##Name(); \
  35. CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true); \
  36. static void Test##Name()
  37. #endif
  38. #ifndef DEPENDENT_TEST
  39. #define DEPENDENT_TEST(Name, Dep) \
  40. static void Test##Name(); \
  41. CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true); \
  42. static void Test##Name()
  43. #endif
  44. #ifndef DISABLED_TEST
  45. #define DISABLED_TEST(Name) \
  46. static void Test##Name(); \
  47. CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false); \
  48. static void Test##Name()
  49. #endif
  50. #define CHECK(condition) CheckHelper(__FILE__, __LINE__, #condition, condition)
  51. #define CHECK_GE(a, b) CHECK((a) >= (b))
  52. static inline void CheckHelper(const char* file,
  53. int line,
  54. const char* source,
  55. bool condition) {
  56. if (!condition) {
  57. printf("%s:%d:\n CHECK(%s) failed\n", file, line, source);
  58. abort();
  59. }
  60. }
  61. #define CHECK_EQ(a, b) CheckEqualsHelper(__FILE__, __LINE__, #a, a, #b, b)
  62. static inline void CheckEqualsHelper(const char* file, int line,
  63. const char* expected_source,
  64. const char* expected,
  65. const char* value_source,
  66. const char* value) {
  67. if ((expected == NULL && value != NULL) ||
  68. (expected != NULL && value == NULL) ||
  69. (expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
  70. printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
  71. "# Expected: %s\n"
  72. "# Found: %s\n",
  73. file, line, expected_source, value_source, expected, value);
  74. abort();
  75. }
  76. }
  77. static inline void CheckEqualsHelper(const char* file, int line,
  78. const char* expected_source,
  79. int expected,
  80. const char* value_source,
  81. int value) {
  82. if (expected != value) {
  83. printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
  84. "# Expected: %d\n"
  85. "# Found: %d\n",
  86. file, line, expected_source, value_source, expected, value);
  87. abort();
  88. }
  89. }
  90. static inline void CheckEqualsHelper(const char* file, int line,
  91. const char* expected_source,
  92. double expected,
  93. const char* value_source,
  94. double value) {
  95. // If expected and value are NaNs then expected != value.
  96. if (expected != value && (expected == expected || value == value)) {
  97. printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
  98. "# Expected: %.30e\n"
  99. "# Found: %.30e\n",
  100. file, line, expected_source, value_source, expected, value);
  101. abort();
  102. }
  103. }
  104. class CcTest {
  105. public:
  106. typedef void (TestFunction)();
  107. CcTest(TestFunction* callback, const char* file, const char* name,
  108. const char* dependency, bool enabled);
  109. void Run() { callback_(); }
  110. static int test_count();
  111. static CcTest* last() { return last_; }
  112. CcTest* prev() { return prev_; }
  113. const char* file() { return file_; }
  114. const char* name() { return name_; }
  115. const char* dependency() { return dependency_; }
  116. bool enabled() { return enabled_; }
  117. private:
  118. TestFunction* callback_;
  119. const char* file_;
  120. const char* name_;
  121. const char* dependency_;
  122. bool enabled_;
  123. static CcTest* last_;
  124. CcTest* prev_;
  125. };
  126. #endif // ifndef CCTEST_H_