OLD | NEW |
(Empty) | |
| 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 |
| 28 #ifndef CCTEST_H_ |
| 29 #define CCTEST_H_ |
| 30 |
| 31 #include <stdio.h> |
| 32 #include <string.h> |
| 33 |
| 34 #include "utils.h" |
| 35 |
| 36 #ifndef TEST |
| 37 #define TEST(Name) \ |
| 38 static void Test##Name(); \ |
| 39 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true); \ |
| 40 static void Test##Name() |
| 41 #endif |
| 42 |
| 43 #ifndef DEPENDENT_TEST |
| 44 #define DEPENDENT_TEST(Name, Dep) \ |
| 45 static void Test##Name(); \ |
| 46 CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true); \ |
| 47 static void Test##Name() |
| 48 #endif |
| 49 |
| 50 #ifndef DISABLED_TEST |
| 51 #define DISABLED_TEST(Name) \ |
| 52 static void Test##Name(); \ |
| 53 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false); \ |
| 54 static void Test##Name() |
| 55 #endif |
| 56 |
| 57 #define CHECK(condition) CheckHelper(__FILE__, __LINE__, #condition, condition) |
| 58 #define CHECK_GE(a, b) CHECK((a) >= (b)) |
| 59 |
| 60 static inline void CheckHelper(const char* file, |
| 61 int line, |
| 62 const char* source, |
| 63 bool condition) { |
| 64 if (!condition) { |
| 65 printf("%s:%d:\n CHECK(%s) failed\n", file, line, source); |
| 66 abort(); |
| 67 } |
| 68 } |
| 69 |
| 70 #define CHECK_EQ(a, b) CheckEqualsHelper(__FILE__, __LINE__, #a, a, #b, b) |
| 71 |
| 72 static inline void CheckEqualsHelper(const char* file, int line, |
| 73 const char* expected_source, |
| 74 const char* expected, |
| 75 const char* value_source, |
| 76 const char* value) { |
| 77 if ((expected == NULL && value != NULL) || |
| 78 (expected != NULL && value == NULL) || |
| 79 (expected != NULL && value != NULL && strcmp(expected, value) != 0)) { |
| 80 printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n" |
| 81 "# Expected: %s\n" |
| 82 "# Found: %s\n", |
| 83 file, line, expected_source, value_source, expected, value); |
| 84 abort(); |
| 85 } |
| 86 } |
| 87 |
| 88 static inline void CheckEqualsHelper(const char* file, int line, |
| 89 const char* expected_source, |
| 90 int expected, |
| 91 const char* value_source, |
| 92 int value) { |
| 93 if (expected != value) { |
| 94 printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n" |
| 95 "# Expected: %d\n" |
| 96 "# Found: %d\n", |
| 97 file, line, expected_source, value_source, expected, value); |
| 98 abort(); |
| 99 } |
| 100 } |
| 101 |
| 102 static inline void CheckEqualsHelper(const char* file, int line, |
| 103 const char* expected_source, |
| 104 double expected, |
| 105 const char* value_source, |
| 106 double value) { |
| 107 // If expected and value are NaNs then expected != value. |
| 108 if (expected != value && (expected == expected || value == value)) { |
| 109 printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n" |
| 110 "# Expected: %.30e\n" |
| 111 "# Found: %.30e\n", |
| 112 file, line, expected_source, value_source, expected, value); |
| 113 abort(); |
| 114 } |
| 115 } |
| 116 |
| 117 |
| 118 class CcTest { |
| 119 public: |
| 120 typedef void (TestFunction)(); |
| 121 CcTest(TestFunction* callback, const char* file, const char* name, |
| 122 const char* dependency, bool enabled); |
| 123 void Run() { callback_(); } |
| 124 static int test_count(); |
| 125 static CcTest* last() { return last_; } |
| 126 CcTest* prev() { return prev_; } |
| 127 const char* file() { return file_; } |
| 128 const char* name() { return name_; } |
| 129 const char* dependency() { return dependency_; } |
| 130 bool enabled() { return enabled_; } |
| 131 private: |
| 132 TestFunction* callback_; |
| 133 const char* file_; |
| 134 const char* name_; |
| 135 const char* dependency_; |
| 136 bool enabled_; |
| 137 static CcTest* last_; |
| 138 CcTest* prev_; |
| 139 }; |
| 140 |
| 141 #endif // ifndef CCTEST_H_ |
OLD | NEW |