-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dhdb_json.c
174 lines (147 loc) · 5.06 KB
/
test_dhdb_json.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
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
#include "dhdb_json.h"
#include "dhdb_dump.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
char *_progName;
static dhdb_t* _export_import(dhdb_t *s)
{
const char *str = dhdb_to_json(s);
dhdb_free(s);
//printf("..export: \033[36m%s\033[0m\n", str);
s = dhdb_create_from_json(str);
return s;
}
static dhdb_t* _test(const char *name, const char *json, bool want_export_import)
{
dhdb_t *s;
printf("\033[1m%s: %s\033[0m\n", _progName, name);
//printf("..import: \033[35m%s\033[0m\n", json);
s = dhdb_create_from_json(json);
if (want_export_import) s = _export_import(s);
return s;
}
static void _test_parse(bool want_export_import)
{
dhdb_t *s;
const char *str;
// String
s = _test("String", "\"hello world\"", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_STRING);
assert(!strcmp(dhdb_str(s), "hello world"));
dhdb_free(s);
// Positive number
s = _test("Positive number", "3", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_NUMBER);
assert(dhdb_num(s) == 3.0);
dhdb_free(s);
// Negative number
s = _test("Negative number", "-3", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_NUMBER);
assert(dhdb_num(s) == -3);
dhdb_free(s);
// Positive float
s = _test("Positive float", "3.5", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_NUMBER);
assert(dhdb_num(s) == 3.5);
dhdb_free(s);
// Negative float
s = _test("Negative float", "-3.5", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_NUMBER);
assert(dhdb_num(s) == -3.5);
dhdb_free(s);
// True
s = _test("Boolean true", "true", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_BOOL);
assert(dhdb_num(s) == 1);
dhdb_free(s);
// False
s = _test("Boolean false", "false", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_BOOL);
assert(dhdb_num(s) == 0);
dhdb_free(s);
// Null
s = _test("Literal null", "null", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_NULL);
assert(dhdb_num(s) == 0);
dhdb_free(s);
// Mixed array
s = _test("Mixed array", "[ 1, -5.1, \"hello world\", false, true, null ]", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_ARRAY);
assert(dhdb_num_at(s, 0) == 1);
assert(dhdb_num_at(s, 1) == -5.1);
assert(!strcmp(dhdb_str_at(s, 2), "hello world"));
assert(dhdb_num_at(s, 3) == 0);
assert(dhdb_num_at(s, 4) == 1);
assert(dhdb_num_at(s, 5) == 0);
dhdb_free(s);
// Array within array
s = _test("Array within array", "[ 1, -5.1, \"hello world\", [ 2, -3 ] ]", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_ARRAY);
assert(dhdb_num_at(s, 0) == 1);
assert(dhdb_num_at(s, 1) == -5.1);
assert(!strcmp(dhdb_str_at(s, 2), "hello world"));
assert(dhdb_type(dhdb_at(s, 3)) == DHDB_VALUE_ARRAY);
assert(dhdb_num_at(dhdb_at(s, 3), 0) == 2);
assert(dhdb_num_at(dhdb_at(s, 3), 1) == -3);
dhdb_free(s);
// Mixed object
s = _test("Mixed object", "{ \"f1\" : \"val1\", \"f2\" : 3, \"f3\" : -3.5 }", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_OBJECT);
assert(!strcmp(dhdb_str_by(s, "f1"), "val1"));
assert(dhdb_num_by(s, "f2") == 3);
assert(dhdb_num_by(s, "f3") == -3.5);
dhdb_free(s);
// Object within object
s = _test("Object within object", "{ \"f1\" : \"val1\", \"f2\" : { \"foo\" : false, \"bar\" : 3 }, \"f3\" : -3 }", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_OBJECT);
assert(!strcmp(dhdb_str_by(s, "f1"), "val1"));
assert(dhdb_type(dhdb_by(s, "f2")) == DHDB_VALUE_OBJECT);
assert(dhdb_num_by(dhdb_by(s, "f2"), "foo") == 0);
assert(dhdb_num_by(dhdb_by(s, "f2"), "bar") == 3);
assert(dhdb_num_by(s, "f3") == -3);
dhdb_free(s);
// Object within array
s = _test("Object within array", "[ 1, { \"f1\" : true, \"f2\" : -3.5 }, 2 ]", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_ARRAY);
assert(dhdb_type(dhdb_at(s, 1)) == DHDB_VALUE_OBJECT);
assert(dhdb_num_at(s, 0) == 1);
assert(dhdb_num_by(dhdb_at(s, 1), "f1") == 1);
assert(dhdb_num_by(dhdb_at(s, 1), "f2") == -3.5);
assert(dhdb_num_at(s, 2) == 2);
dhdb_free(s);
// Array within object
s = _test("Array within object", "{ \"f1\" : [ 2, 1, 3 ], \"f2\" : 3, \"f3\" : -3.5 }", want_export_import);
assert(dhdb_type(s) == DHDB_VALUE_OBJECT);
assert(dhdb_type(dhdb_by(s, "f1")) == DHDB_VALUE_ARRAY);
assert(dhdb_num_at(dhdb_by(s, "f1"), 0) == 2);
assert(dhdb_num_at(dhdb_by(s, "f1"), 1) == 1);
assert(dhdb_num_at(dhdb_by(s, "f1"), 2) == 3);
assert(dhdb_num_by(s, "f2") == 3);
assert(dhdb_num_by(s, "f3") == -3.5);
dhdb_free(s);
// Error
s = _test("Error handling 1", "{ \"f1\" : [ af, foop ] }", false);
assert(s == NULL);
s = _test("Error handling 2", "{ \"f1\" : [ 1.5, 1.a, 1 ] }", false);
assert(s == NULL);
s = _test("Error handling 3", "{ \"f1\" : [ 1, 2 }", false);
assert(s == NULL);
s = _test("Error handling 4", "{ \"f1 : [ 1, 2 ]", false);
assert(s == NULL);
s = _test("Error handling 5", "{ f1 : [ 1, 2 ] }", false);
assert(s == NULL);
s = _test("Error handling 6", "{ \"f1\" : { 1, 2 ] }", false);
assert(s == NULL);
// Test file
s = dhdb_create_from_json_file("test.json");
dhdb_dump(s);
assert(s);
dhdb_free(s);
}
int main(int argc, char **argv)
{
_progName = argv[0];
_test_parse(true);
return 0;
}