-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
125 lines (100 loc) · 3.05 KB
/
test.c
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
#include "include/ion.h"
#include "witc/foreach.h"
#include <stdio.h>
#include <stdbool.h>
bool test1() {
object_t obj = createEmptyObject();
obj = insertStringEntry(obj, string("hello"), string("world"));
obj = insertNumberEntry(obj, string("number"), makeNumber(2.275));
array_t arr = createEmptyArray();
arr = insertIntoArray(arr, objectValue(string("a")));
arr = insertIntoArray(arr, objectValue(string("b")));
arr = insertIntoArray(arr, objectValue(string("c")));
obj = insertArrayEntry(obj, string("array"), arr);
obj = insertBoolEntry(obj, string("val1"), true);
obj = insertBoolEntry(obj, string("val2"), false);
obj = insertNullEntry(obj, string("val3"));
string json = objectToJson(obj);
printf("%s\n", json);
printf("allocated size for json: %ld\n", stringbytesalloced(json));
destroyObject(obj);
destroyString(json);
return true;
}
#define literal(x...) literal_defered(x)
#define literal_defered(x...) #x
bool test2() {
string my_json_string = string(literal({"a" : "b", "c" : "d"}));
object_t testobj = jsonToObject(my_json_string);
printf("the original json string is %s\n", my_json_string);
string new_json_string = string("");
new_json_string = objectToJson(testobj);
printf("the new string is %s\n", new_json_string);
printf("inside of the object there is the key >%s<\n", testobj.key[0]);
destroyString(new_json_string);
destroyString(my_json_string);
destroyObject(testobj);
return true;
}
bool test3() {
string num = string("1234.5678");
size_t pos = 0;
obj_t_value_t result;
printf("parsing successful: %s\n", parseNumber(num, &pos, &result) ? "true" : "false");
printf("the result was %0.17f | %ld\n", result.num.as_double, result.num.as_int64_t);
destroyString(num);
return true;
}
bool test4() {
string str = string("\"test here\\\"\"");
size_t pos = 0;
obj_t_value_t result;
printf("parsing successful: %s\n", parseString(str, &pos, &result) ? "true" : "false");
printf("the result was %s\n", result.str);
destroyString(str);
return true;
}
bool test5() {
string n = string("null");
string t = string("true");
string f = string("false");
size_t pos = 0;
obj_t_value_t result;
parsePrimitive(n, &pos, &result);
pos = 0;
printf("the result discriminant is %d\n", result.discriminant);
parsePrimitive(t, &pos, &result);
pos = 0;
printf("the result discriminant is %d\n", result.discriminant);
parsePrimitive(f, &pos, &result);
pos = 0;
printf("the result discriminant is %d\n", result.discriminant);
destroyString(n);
destroyString(t);
destroyString(f);
return true;
}
bool test6() {
string json = string(literal({"a" : "b", "c" : [1, 2, 3, "xyz", true, false, null]}));
object_t obj = createEmptyObject();
obj = jsonToObject(json);
string output = objectToJson(obj);
printf("%s\n", output);
destroyString(output);
destroyObject(obj);
return true;
}
int main() {
typeof(bool (*)(void)) tests[] = {
test1,
test2,
test3,
test4,
test5,
test6,
};
foreach(typeof(bool (*)(void)) test of tests) {
assert(test());
}
return 0;
}