-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
240 lines (227 loc) · 5.26 KB
/
test.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "test.h"
#include "jstream.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <algorithm>
#ifdef _WIN32
#include <io.h>
#include <windows.h>
#else
#include <dirent.h>
#include <unistd.h>
#define O_BINARY 0
#endif
std::string trim(std::string const &s)
{
char const *begin = s.c_str();
char const *end = begin + s.size();
while (begin < end && isspace((unsigned char)*begin)) begin++;
while (begin < end && isspace((unsigned char)end[-1])) end--;
std::vector<char> vec;
vec.reserve(end - begin);
char const *src = begin;
while (src < end) {
if (*src == '\r') {
src++;
} else {
vec.push_back(*src);
src++;
}
}
if (vec.empty()) return {};
begin = vec.data();
end = begin + vec.size();
return std::string(begin, end);
}
bool read_file(char const *path, std::vector<char> *out)
{
bool ok = false;
out->clear();
int fd = open(path, O_RDONLY | O_BINARY);
if (fd != -1) {
struct stat st;
if (fstat(fd, &st) == 0) {
ok = true;
if (st.st_size > 0) {
out->resize(st.st_size);
if (read(fd, out->data(), out->size()) != st.st_size) {
ok = false;
}
}
}
close(fd);
}
return ok;
}
void parse_test_case(char const *begin, char const *end, std::vector<std::string> *strings)
{
strings->clear();
char const *head = begin;
char const *ptr = begin;
bool newline = true;
while (1) {
int c = 0;
if (ptr < end) {
c = (unsigned char)*ptr;
}
if (c == 0) {
strings->push_back(std::string(head, ptr));
break;
}
if (c == '\r' || c == '\n') {
newline = true;
ptr++;
} else {
if (c == '-' && newline) {
if (ptr + 3 < end && ptr[1] == '-' && ptr[2] == '-') {
c = ptr[3];
if (c == '\r' || c == '\n') {
strings->push_back(std::string(head, ptr));
ptr += 4;
head = ptr;
}
}
}
newline = false;
ptr++;
}
}
}
void parse(char const *source, std::string *result1)
{
jstream::Reader json(source, source + strlen(source));
while (json.next()) {
char tmp[1000];
tmp[0] = 0;
switch (json.state()) {
case jstream::StartObject:
sprintf(tmp, "StartObject: \"%s\"\n", json.key().c_str());
break;
case jstream::EndObject:
sprintf(tmp, "EndObject: \"%s\"\n", json.key().c_str());
break;
case jstream::StartArray:
sprintf(tmp, "StartArray: \"%s\"\n", json.key().c_str());
break;
case jstream::EndArray:
sprintf(tmp, "EndArray: \"%s\"\n", json.key().c_str());
break;
case jstream::String:
sprintf(tmp, "StringValue: \"%s\" = \"%s\"\n", json.key().c_str(), json.string().c_str());
break;
case jstream::Number:
sprintf(tmp, "NumberValue: \"%s\" = %f\n", json.key().c_str(), json.number());
break;
case jstream::Null:
case jstream::False:
case jstream::True:
sprintf(tmp, "SymbolValue: \"%s\" = %s\n", json.key().c_str(), json.string().c_str());
break;
}
*result1 += tmp;
if (json.isvalue()) {
std::string s = json.path() + '=';
if (json.state() == jstream::String) {
s = s + '\"' + json.string() + '\"';
} else {
s = s + json.string();
}
s += '\n';
*result1 += s;
}
}
if (json.state() == jstream::Error) {
*result1 += "Error: " + json.string();
}
}
#ifdef _WIN32
void get_tests(const std::string &loc, std::vector<std::string> *out)
{
out->clear();
std::string filter = loc + "*.*";
WIN32_FIND_DATAA fd;
HANDLE h = FindFirstFileA(filter.c_str(), &fd);
if (h != INVALID_HANDLE_VALUE) {
do {
std::string name;
name = fd.cFileName;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
continue;
}
out->push_back(name);
} while (FindNextFileA(h, &fd));
FindClose(h);
}
}
#else
void get_tests(std::string const &loc, std::vector<std::string> *out)
{
out->clear();
DIR *dir = opendir(loc.c_str());
if (dir) {
while (dirent *d = readdir(dir)) {
std::string name;
name = d->d_name;
if (d->d_type & DT_DIR) {
continue;
}
out->push_back(name);
}
closedir(dir);
}
}
#endif
void test_all(bool print_result)
{
int pass = 0;
int fail = 0;
int total = 0;
std::string dir = "test/";
std::vector<std::string> tests;
get_tests(dir.c_str(), &tests);
std::sort(tests.begin(), tests.end());
for (std::string const &name : tests) {
std::string path = dir + name;
std::vector<char> vec;
if (read_file(path.c_str(), &vec) && !vec.empty()) {
total++;
std::vector<std::string> strings;
char const *begin = vec.data();
char const *end = begin + vec.size();
parse_test_case(begin, end, &strings);
std::string input;
std::string result;
if (strings.size() == 2) {
input = strings[0];
result = strings[1];
} else {
fail++;
printf("#%d TEST CASE SYNTAX ERROR %s\n", total, name.c_str());
continue;
}
std::string result1;
parse(input.c_str(), &result1);
if (trim(result1) == trim(result)) {
pass++;
if (print_result) {
printf("#%d PASS %s\n", total, name.c_str());
}
} else {
fail++;
if (print_result) {
printf("#%d FAIL %s\n", total, name.c_str());
puts("--- INPUT ---------");
puts(trim(input).c_str());
puts("--- EXPECTED EVENTS ----------");
puts(trim(result).c_str());
puts("--- ACTUAL RESULT ----------");
puts(trim(result1).c_str());
puts("---");
}
}
}
}
if (print_result) {
printf("---\n" "TOTAL: %d\n" " PASS: %d\n" " FAIL: %d\n", total, pass, fail);
}
}