-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.h
195 lines (146 loc) · 5.2 KB
/
tests.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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <Windows.h>
#include "include/softjson.h"
void run_sample_test(char* filepath) {
JsonHandler handler = soft_create_handler();
// Read File
char* source;
if (!file_read_all_text(filepath, &source)) {
return;
}
// Parse
JsonValue value = soft_load_string(&handler, source);
/*
File starts with:
'y' - Parser must accept input
'n' - Parser must reject input
'i' - Parser can either accept or reject input
*/
if (!handler.error.exists) {
if (filepath[14] == 'y' || filepath[14] == 'i') {
// Parser passed a test it was supposed to pass (result: SUCCESS)
printf("<passed: YES, result: PARSE_SUCCESS, file: %s>\n", filepath);
}
else {
// Parser passed a test it was supposed to fail (result: FAILURE)
printf("<passed: NO, result: PARSE_FAILURE, file: %s>\n", filepath);
}
json_value_free(&value, TRUE);
}
else {
if (filepath[14] == 'n' || filepath[14] == 'i') {
// Parser failed a test it was supposed to fail (result: SUCCESS)
printf("<passed: NO, result: PARSE_SUCCESS, file: %s, log: %s>\n", filepath, handler.error.log);
}
else {
// Parser failed a test it was supposed to pass (result: FAILURE)
printf("<passed: YES, result: PARSE_FAILURE, file: %s, log: %s>\n", filepath, handler.error.log);
}
}
}
void run_all_sample_tests() {
WIN32_FIND_DATA fdata;
HANDLE hFind;
hFind = FindFirstFile(L"tests/samples/*", &fdata);
while (FindNextFile(hFind, &fdata) != 0)
{
static char filepath[256];
snprintf(filepath, 256, "tests/samples/%ws", fdata.cFileName);
run_sample_test(filepath);
}
FindClose(hFind);
}
void run_load_string_test() {
char* string = "{ \
\"name\": \"John\", \
\"age\": 20, \
\"gender\": \"male\", \
\"occupation\": null, \
\"hobbies\": [ \
\"Cycling\", \
\"Swimming\" \
] \
}";
JsonHandler handler = soft_create_handler();
clock_t begin = clock();
JsonValue value = soft_load_string(&handler, string);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
if (handler.error.exists) {
printf("[TEST_ERROR] Time: %g ms, At: run_load_string_test, Log: %s\n", time_spent, handler.error.log);
}
else {
printf("[TEST_SUCCESS] Time: %g ms, At: run_load_string_test\n", time_spent);
json_value_free(&value, TRUE);
}
}
void run_load_file_test() {
JsonHandler handler = soft_create_handler();
clock_t begin = clock();
JsonValue value = soft_load_file(&handler, "tests/run_load_file_test.json");
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
if (handler.error.exists) {
printf("[TEST_ERROR] Time: %g ms, At: run_load_string_test, Log: %s\n", time_spent, handler.error.log);
}
else {
printf("[TEST_SUCCESS] Time: %g ms, At: run_load_string_test\n", time_spent);
json_value_free(&value, TRUE);
}
}
void run_dump_string_test() {
// Build Data
JsonList hobbies = json_create_list();
json_list_add(&hobbies, json_create_string_value("Cycling"));
json_list_add(&hobbies, json_create_string_value("Swimming"));
JsonObject person = json_create_object();
json_object_add(&person, "name", json_create_string_value("John"));
json_object_add(&person, "age", json_create_int_value(20));
json_object_add(&person, "gender", json_create_string_value("male"));
json_object_add(&person, "occupation", json_create_null_value());
json_object_add(&person, "hobbies", json_create_list_value(hobbies));
// Serialise
JsonHandler handler = soft_create_handler();
clock_t begin = clock();
soft_dump_string(&handler, json_create_object_value(person));
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
if (handler.error.exists) {
printf("[TEST_ERROR] Time: %g ms, At: run_dump_string_test, Log: %s\n", time_spent, handler.error.log);
}
else {
printf("[TEST_SUCCESS] Time: %g ms, At: run_dump_string_test\n", time_spent);
json_value_free(&person, FALSE);
}
}
void run_dump_file_test() {
// Build Data
JsonList hobbies = json_create_list();
json_list_add(&hobbies, json_create_string_value("Cycling"));
json_list_add(&hobbies, json_create_string_value("Swimming"));
JsonObject person = json_create_object();
json_object_add(&person, "name", json_create_string_value("John"));
json_object_add(&person, "age", json_create_int_value(20));
json_object_add(&person, "gender", json_create_string_value("male"));
json_object_add(&person, "occupation", json_create_null_value());
json_object_add(&person, "hobbies", json_create_list_value(hobbies));
// Write to File
JsonHandler handler = soft_create_handler();
soft_dump_file(&handler, json_create_object_value(person), "tests/run_dump_file_test.json");
if (handler.error.exists) {
printf("[TEST_ERROR] At: run_dump_file_test, Log: %s\n", handler.error.log);
}
else {
printf("[TEST_SUCCESS] At: run_dump_file_test\n");
json_object_free(&person, FALSE);
}
}
void run_all_tests() {
run_load_string_test();
run_load_file_test();
run_dump_string_test();
run_dump_file_test();
run_all_sample_tests();
}