xref: /aosp_15_r20/external/double-conversion/test/cctest/cctest.h (revision a6021da3bd53a1cb979b0905bbb837249345d1b1)
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 
CheckHelper(const char * file,int line,const char * source,bool condition)61 static 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 
73 template<typename T> inline void PrintfValue(T x);
PrintfValue(int x)74 template<> inline void PrintfValue(int x) { printf("%d", x); }
PrintfValue(unsigned int x)75 template<> inline void PrintfValue(unsigned int x) { printf("%u", x); }
PrintfValue(short x)76 template<> inline void PrintfValue(short x) { printf("%hd", x); }
PrintfValue(unsigned short x)77 template<> inline void PrintfValue(unsigned short x) { printf("%hu", x); }
PrintfValue(int64_t x)78 template<> inline void PrintfValue(int64_t x) { printf("%" PRId64, x); }
PrintfValue(uint64_t x)79 template<> inline void PrintfValue(uint64_t x) { printf("%" PRIu64, x); }
PrintfValue(float x)80 template<> inline void PrintfValue(float x) { printf("%.30e", static_cast<double>(x)); }
PrintfValue(double x)81 template<> inline void PrintfValue(double x) { printf("%.30e", x); }
PrintfValue(bool x)82 template<> inline void PrintfValue(bool x) { printf("%s", x ? "true" : "false"); }
83 
84 template<typename T1, typename T2>
CheckEqualsHelper(const char * file,int line,const char * expected_source,T1 expected,const char * value_source,T2 value)85 inline 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 
104 template<>
CheckEqualsHelper(const char * file,int line,const char * expected_source,const char * expected,const char * value_source,const char * value)105 inline 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 
124 template<>
CheckEqualsHelper(const char * file,int line,const char * expected_source,const char * expected,const char * value_source,char * value)125 inline 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 
133 class CcTest {
134  public:
135   typedef void (TestFunction)();
136   CcTest(TestFunction* callback, const char* file, const char* name,
137          const char* dependency, bool enabled);
Run()138   void Run() { callback_(); }
139   static int test_count();
last()140   static CcTest* last() { return last_; }
prev()141   CcTest* prev() { return prev_; }
file()142   const char* file() const { return file_; }
name()143   const char* name() const { return name_; }
dependency()144   const char* dependency() const { return dependency_; }
enabled()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