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