-
Notifications
You must be signed in to change notification settings - Fork 262
/
test.h
209 lines (175 loc) · 4.69 KB
/
test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once
#include "config.h"
#include "memorystat.h"
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#ifdef __CYGWIN__
#include <cstdio>
#include <cstdarg>
// Cygwin does not define std::snprintf, std::to_string(...), etc.
namespace std {
inline int snprintf(char * s, size_t n, const char * format, ... ) {
va_list args;
va_start (args, format);
int ret = vsnprintf (s, n, format, args);
va_end (args);
return ret;
}
inline std::string to_string(double value) {
char buf[256];
sprintf(buf, "%f", value);
return std::string(buf);
}
inline long stol(const std::string& str, std::size_t* pos = 0, int base = 10) {
const char* s = str.c_str();
char *ptr;
long ret = strtol(s, &ptr, base);
if (pos)
*pos = ptr - s;
return ret;
}
inline double stod(const std::string& str, std::size_t* pos = 0) {
const char* s = str.c_str();
char *ptr;
double ret = strtod(s, &ptr);
if (pos)
*pos = ptr - s;
return ret;
}
} // namespace std
#endif
class TestBase;
typedef std::vector<const TestBase *> TestList;
class TestManager {
public:
static TestManager& Instance() {
static TestManager* singleton = new TestManager;
return *singleton;
}
static void DestroyInstance() {
delete &Instance();
}
void AddTest(const TestBase* test) {
mTests.push_back(test);
}
const TestList& GetTests() const {
return mTests;
}
TestList& GetTests() {
return mTests;
}
private:
TestList mTests;
};
struct Stat {
size_t objectCount;
size_t arrayCount;
size_t numberCount;
size_t stringCount;
size_t trueCount;
size_t falseCount;
size_t nullCount;
size_t memberCount; // Number of members in all objects
size_t elementCount; // Number of elements in all arrays
size_t stringLength; // Number of code units in all strings
};
// Each test can customize what to be stored in parse result,
// which will be passed to Stringify()/Prettify()/Statistics()
class ParseResultBase {
public:
virtual ~ParseResultBase() {}
};
// Stringify()/Prettify() returns object derived from this class.
// So that it can prevents unncessary strdup().
class StringResultBase {
public:
virtual ~StringResultBase() {}
// The test framework call this function to get a null-terminated string.
virtual const char* c_str() const = 0;
};
class TestBase {
public:
TestBase() {
TestManager::Instance().AddTest(this);
}
bool operator<(const TestBase& rhs) const {
return strcmp(name_, rhs.name_) < 0;
}
// For each operation, call SetUp() before and TearDown() after.
// It is mainly for libraries require huge initialize time (e.g. Creating Isolate in V8).
virtual void SetUp() const {}
virtual void TearDown() const {}
#if TEST_INFO
virtual const char* GetName() const = 0;
virtual const char* GetFilename() const = 0;
#endif
#if TEST_PARSE
virtual ParseResultBase* Parse(const char* json, size_t length) const {
(void)json;
(void)length;
return 0;
}
#endif
#if TEST_STRINGIFY
virtual StringResultBase* Stringify(const ParseResultBase* parseResult) const {
(void)parseResult;
return 0;
}
#endif
#if TEST_PRETTIFY
virtual StringResultBase* Prettify(const ParseResultBase* parseResult) const {
(void)parseResult;
return 0;
}
#endif
#if TEST_STATISTICS
virtual bool Statistics(const ParseResultBase* parseResult, Stat* stat) const {
(void)parseResult;
(void)stat;
return false;
}
#endif
#if TEST_SAXROUNDTRIP
virtual StringResultBase* SaxRoundtrip(const char* json, size_t length) const {
(void)json;
(void)length;
return 0;
}
#endif
#if TEST_SAXSTATISTICS
virtual bool SaxStatistics(const char* json, size_t length, Stat* stat) const {
(void)json;
(void)length;
(void)stat;
return false;
}
#endif
#if TEST_SAXSTATISTICSUTF16
virtual bool SaxStatisticsUTF16(const char* json, size_t length, Stat* stat) const {
(void)json;
(void)length;
(void)stat;
return false;
}
#endif
#if TEST_CONFORMANCE
// Parse a JSON of an array containing one double
// Return false if it is failed to parse.
// E.g. json = "[1.0]" -> d = 1.0
virtual bool ParseDouble(const char* json, double* d) const {
(void)json;
(void)d;
return false;
}
virtual bool ParseString(const char* json, std::string& s) const {
(void)json;
(void)s;
return false;
}
#endif
protected:
const char* name_;
};
#define REGISTER_TEST(cls) static cls gRegister##cls