-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
334 lines (266 loc) · 8.71 KB
/
test.c
File metadata and controls
334 lines (266 loc) · 8.71 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "lib/json.h"
void test_construction()
{
printf("\nTESTING CONSTRUCTION\n");
// Initial structure:
// {
// "hello": 13,
// "hella": 14,
// "": 13.1,
// "good bye": false
// }
char buffer[256];
JsonObject* j = create_JsonObject();
float a1 = 13;
float a2 = 14;
float f1 = 13.1;
bool b1 = false;
set_value_float(j, "hello", a1);
set_value_float(j, "hella", a2);
set_value_float(j, "", f1);
set_value_null(j, "good bye");
JsonValue jd = get_value(j, "hello");
assert(jd.data.f == a1);
jd = get_value(j, "hella");
assert(jd.data.f == a2);
jd = get_value(j, "");
assert(jd.data.f == f1);
jd = get_value(j, "good bye");
assert(jd.data.n == NULL);
jd = get_value(j, "goo");
assert(jd.data.e == MISSING_KEY);
jd = get_value(j, "NOTAVALIDKEY");
assert(jd.data.e == MISSING_KEY);
set_value_bool(j, "good bye", b1);
jd = get_value(j, "good bye");
assert(jd.data.b == b1);
dump_JsonObject(j, buffer);
printf("%s\n", buffer);
// Structure:
// {
// "hello": 13,
// "hella": 14,
// "": 13.1,
// "good bye":
// {
// "good bye": true;
// }
// }
JsonObject* j2 = create_JsonObject();
b1 = true;
set_value_bool(j2, "good bye", b1);
set_value_object(j, "good bye", j2);
jd = get_value(j, "good bye");
assert(jd.data.o == j2);
dump_JsonObject(j, buffer);
printf("%s\n", buffer);
// Structure:
// {
// "hello": 13,
// "hella": 14,
// "": 13.1,
// "good bye":
// {
// "good bye": "string";
// }
// }
jd = get_value(jd.data.o, "good bye");
assert(jd.data.b == b1);
set_value_string(j, "good", "wurd");
jd = get_value(j, "good");
assert(strcmp(jd.data.s, "wurd") == 0);
dump_JsonObject(j, buffer);
printf("%s\n", buffer);
}
void test_arrays()
{
printf("\nTESTING ARRAYS\n");
// Start off with a simple multi element array:
// {"hi": [0, 332, true, null, "henlo world"]}
JsonObject * o = create_JsonObject();
JsonArray * array = create_JsonArray(5);
float i1 = 0, i2 = 332;
bool b1 = true;
set_element_float(array, 0, i1);
set_element_float(array, 1, i2);
set_element_bool(array, 2, b1);
set_element_null(array, 3);
set_element_string(array, 4, "henlo world");
set_value_array(o, "hi", array);
// test if array values are correct.
array = get_value(o, "hi").data.a;
JsonValue jd = get_element(array, 0);
assert(jd.data.f == 0);
jd = get_element(array, 1);
assert(jd.data.f == 332);
jd = get_element(array, 2);
assert(jd.data.b == true);
jd = get_element(array, 3);
assert(jd.data.n == NULL);
jd = get_element(array, 4);
assert(strcmp(jd.data.s, "henlo world") == 0);
char buffer[256];
dump_JsonObject(o, buffer);
printf("%s\n", buffer);
}
void test_nesting()
{
printf("\nTESTING NESTING\n");
// Nest that array into another array
// {
// "hi":
// [
// [0, -1, true, null, "henlo world"],
// {"pi": [3.14]}
// ]
// }
JsonArray * arrayInner = create_JsonArray(5);
float i1 = 0, i2 = 332;
bool b1 = true;
set_element_float(arrayInner, 0, i1);
set_element_float(arrayInner, 1, i2);
set_element_bool(arrayInner, 2, b1);
set_element_null(arrayInner, 3);
set_element_string(arrayInner, 4, "henlo world");
JsonArray * piArray = create_JsonArray(1);
float f1 = 3.14;
set_element_float(piArray, 0, f1);
JsonObject * piObj = create_JsonObject();
set_value_array(piObj, "pi", piArray);
JsonArray * arrayOuter = create_JsonArray(2);
set_element_array(arrayOuter, 0, arrayInner);
set_element_object(arrayOuter, 1, piObj);
JsonObject* o = create_JsonObject();
set_value_array(o, "hi", arrayOuter);
arrayOuter = get_value(o, "hi").data.a;
JsonArray * array = get_element(arrayOuter, 0).data.a;
// Test for first element in array
JsonValue jd = get_element(array, 0);
assert(jd.data.f == 0);
jd = get_element(array, 1);
assert(jd.data.f == 332);
jd = get_element(array, 2);
assert(jd.data.b == true);
jd = get_element(array, 3);
assert(jd.data.n == NULL);
jd = get_element(array, 4);
assert(strcmp(jd.data.s, "henlo world") == 0);
// Test for first element in array
piObj = get_element(arrayOuter, 1).data.o;
array = get_value(piObj, "pi").data.a;
jd = get_element(array, 1);
assert(fabs(jd.data.f - f1) > 0.0000000001);
char buffer[256];
dump_JsonObject(o, buffer);
printf("%s\n", buffer);
}
void test_printing()
{
printf("\nTESTING PRINTING\n");
// Create an object like:
// {"Hen":null,"Henlo":"Gudbye","10":,"":true,"inner":{"innerinner":{},"false":false}}
char buffer[256];
JsonObject* o = create_JsonObject();
dump_JsonObject(o, buffer);
printf("%s\n", buffer);
float i = 10;
bool b = true;
float f = 3.12;
float f4 = 5.44;
set_value_string(o, "Henlo", "Gudbye");
set_value_float(o, "10", i);
set_value_null(o, "Hen");
set_value_bool(o, "", b);
set_value_float(o, "f", f);
JsonObject* inner = create_JsonObject();
JsonObject* innerinner = create_JsonObject();
set_value_object(inner, "innerinner", innerinner);
set_value_bool(inner, "false", false);
set_value_float(o, "inz", f4);
set_value_object(o, "inner", inner);
dump_JsonObject(o, buffer);
printf("%s\n", buffer);
const char * expected = "{\"Hen\":null,\"Henlo\":\"Gudbye\",\"10\":10,\"\":true,\"f\":3.12,\"inz\":5.44,\"inner\":{\"innerinner\":{},\"false\":false}}";
printf("%s\n", expected);
assert(strcmp(buffer, expected) == 0);
JsonObject* iiii = create_JsonObject();
JsonObject* iii = create_JsonObject();
JsonObject* ii = create_JsonObject();
JsonObject* in = create_JsonObject();
set_value_object(iii, "iii", iiii);
set_value_object(ii, "ii", iii);
set_value_object(in, "i", ii);
dump_JsonObject(in, buffer);
printf("%s\n", buffer);
const char * expected2 = "{\"i\":{\"ii\":{\"iii\":{}}}}";
assert(strcmp(buffer, expected2) == 0);
}
void test_parsing()
{
char buffer[256];
Json_reset_mempool();
char* input1 = " { } ";
JsonObject* parsed1;
parse_JsonObject(input1, &parsed1);
dump_JsonObject(parsed1, buffer);
printf("%s\n", buffer);
assert(strcmp(buffer, "{}") == 0);
Json_reset_mempool();
char* input2 = "{\"null\": null, \"true\": true, \"false\": false, \"inner\": {\"innerinner\": \"thisisalongstring\"}}";
JsonObject* parsed2;
parse_JsonObject(input2, &parsed2);
dump_JsonObject(parsed2, buffer);
printf("%s\n", buffer);
char* expected2 = "{\"null\":null,\"true\":true,\"false\":false,\"inner\":{\"innerinner\":\"thisisalongstring\"}}";
assert(strcmp(buffer, expected2) == 0);
Json_reset_mempool();
char* input3 = "{\"a\": 1, \"b\": 32.1e-2,\"c\": -3.012, \"d\": {\"33\": -33, \"\":{}}}";
JsonObject* parsed3;
parse_JsonObject(input3, &parsed3);
dump_JsonObject(parsed3, buffer);
printf("%s\n", buffer);
char* expected3 = "{\"a\":1,\"b\":0.321,\"c\":-3.012,\"d\":{\"33\":-33,\"\":{}}}";
assert(strcmp(buffer, expected3) == 0);
Json_reset_mempool();
char* input4 = "{\"a\": [], \"b\": [0, true, false, [], \"mystring1\", \"mystring2\", {\"yo\": [\"yothisisastring\", {\"yoinner\": [null]}]}]}";
JsonObject* parsed4;
parse_JsonObject(input4, &parsed4);
dump_JsonObject(parsed4, buffer);
printf("%s\n", buffer);
char* expected4 = "{\"a\":[],\"b\":[0,true,false,[],\"mystring1\",\"mystring2\",{\"yo\":[\"yothisisastring\",{\"yoinner\":[null]}]}]}";
assert(strcmp(buffer, expected4) == 0);
Json_reset_mempool();
char* input5 = "{\"\":[[[[], []], []], [], [], [[[], []]]]}";
JsonObject* parsed5;
parse_JsonObject(input5, &parsed5);
dump_JsonObject(parsed5, buffer);
printf("%s\n", buffer);
char* expected5 = "{\"\":[[[[],[]],[]],[],[],[[[],[]]]]}";
assert(strcmp(buffer, expected5) == 0);
}
int main()
{
time_t start = time(NULL);
// Statically allocate 1024kb.
#define MEMPOOL_SIZE 1024
char mempool[MEMPOOL_SIZE];
Json_set_mempool(mempool, MEMPOOL_SIZE);
test_construction();
Json_reset_mempool();
test_arrays();
Json_reset_mempool();
test_nesting();
Json_reset_mempool();
test_printing();
Json_reset_mempool();
test_parsing();
time_t end = time(NULL);
printf("Elapsed %f\n", (double)difftime(end, start));
return 0;
}