• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "double-conversion/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 
CheckHelper(const char * file,int line,const char * source,bool condition)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 
CheckEqualsHelper(const char * file,int line,const char * expected_source,const char * expected,const char * value_source,const char * value)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     abort();
80   }
81 
82   if ((expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
83     printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
84            "#  Expected: %s\n"
85            "#  Found:    %s\n",
86            file, line, expected_source, value_source, expected, value);
87     abort();
88   }
89 }
90 
CheckEqualsHelper(const char * file,int line,const char * expected_source,int expected,const char * value_source,int value)91 static inline void CheckEqualsHelper(const char* file, int line,
92                                      const char* expected_source,
93                                      int expected,
94                                      const char* value_source,
95                                      int value) {
96   if (expected != value) {
97     printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
98            "#  Expected: %d\n"
99            "#  Found:    %d\n",
100            file, line, expected_source, value_source, expected, value);
101     abort();
102   }
103 }
104 
CheckEqualsHelper(const char * file,int line,const char * expected_source,double expected,const char * value_source,double value)105 static inline void CheckEqualsHelper(const char* file, int line,
106                                      const char* expected_source,
107                                      double expected,
108                                      const char* value_source,
109                                      double value) {
110   // If expected and value are NaNs then expected != value.
111   if (expected != value && (expected == expected || value == value)) {
112     printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
113            "#  Expected: %.30e\n"
114            "#  Found:    %.30e\n",
115            file, line, expected_source, value_source, expected, value);
116     abort();
117   }
118 }
119 
120 
121 class CcTest {
122  public:
123   typedef void (TestFunction)();
124   CcTest(TestFunction* callback, const char* file, const char* name,
125          const char* dependency, bool enabled);
Run()126   void Run() { callback_(); }
127   static int test_count();
last()128   static CcTest* last() { return last_; }
prev()129   CcTest* prev() { return prev_; }
file()130   const char* file() const { return file_; }
name()131   const char* name() const { return name_; }
dependency()132   const char* dependency() const { return dependency_; }
enabled()133   bool enabled() const { return enabled_; }
134  private:
135   TestFunction* callback_;
136   const char* file_;
137   const char* name_;
138   const char* dependency_;
139   bool enabled_;
140   static CcTest* last_;
141   CcTest* prev_;
142 };
143 
144 #endif  // ifndef CCTEST_H_
145