-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
285 lines (251 loc) · 8.61 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
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
#include <stdio.h>
#include <stdlib.h>
#include "c_doc/doc.h"
// #define LOG_DISABLE
#ifndef LOG_DISABLE
#define log(const_format_str, ...) printf("[%s:%i] " const_format_str, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define log(const_format_str, ...)
#endif
int main(int argc, char **argv){
// new dynamic object
doc *obj = doc_new(
"velocidades", dt_obj,
"max", dt_double, 56.0,
"media", dt_double, 22.5,
"pontos", dt_array,
"p1", dt_double, 23.0,
"p2", dt_double, 70.0,
"p3", dt_double, 104.3,
"p4", dt_double, 22.0,
";",
"integer", dt_uint8, 255,
"packets", dt_const_bindata, "void_ptr", 9ULL,
"future_value", dt_null,
"some_object", dt_obj,
";",
"matrix", dt_array,
dt_array,
"1", dt_int, 11,
"2", dt_int, 22,
"3", dt_int, 33,
";",
";",
"some_values", dt_array,
dt_uint8, 251,
dt_uint8, 252,
dt_uint8, 253,
";",
"string", dt_const_string, "hello_world", 12ULL,
"1", dt_int64, -256,
"objs", dt_array,
dt_obj,
"atb", dt_bool, true,
";",
dt_obj,
"atb", dt_bool, false,
";",
";",
";"
);
// error handling
if(doc_error_code < 0){
log("Error: %s",doc_get_error_msg());
return -1;
}
double value = doc_get(obj, "pontos.p1", double);
// CHECKS
// check array get() on anonymous members
int member_value = doc_get(obj, "matrix[0][1]", int);
if(doc_error_code)
log("[ERROR] Value: [%u]. Error_check: %s\n", member_value, doc_get_error_msg());
else
log("[OK]\n");
// check [] notation on objs
double point = doc_get(obj, "pontos[1]", double);
if(doc_error_code)
log("[ERROR] Value: [%f]. Error_check: %s\n", point, doc_get_error_msg());
else
log("[OK]\n");
// check set_value on a string
doc_set_string(obj, "string", "new_string", 11ULL);
if(doc_get_size(obj, "string") != 11ULL){
log("[ERROR]\n");
}
else{
log("[OK]\n");
}
// check set_value on non value obj
doc_set(obj, "future_value", double, 255.8);
if (doc_error_code == errno_doc_trying_to_set_value_of_non_value_type_data_type)
log("[OK]\n");
else
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
// check add of a single value
doc_add(obj, "pontos", "p5", dt_double, 50.0);
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else
log("[OK]\n");
// check add of a non value
doc_add(obj, ".", "p_null", dt_null);
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else
log("[OK]\n");
// check add on non addable objects
doc_add(obj, "pontos.p5", "extra", dt_bool, true);
if(doc_error_code == errno_doc_trying_to_add_new_data_to_non_object_or_non_array)
log("[OK]\n");
else
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
// check add value on array, with different type
doc_add(obj, "pontos", "p6", dt_int, 64);
if(doc_error_code == errno_doc_value_not_same_type_as_array)
log("[OK]\n");
else
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
// check get childs amount
doc_size_t obj_size = doc_get_size(obj, ".");
if(doc_error_code)
log("[ERROR] Error_check: %s\n", doc_get_error_msg());
else{
log("[OK] Obj size: %u\n", obj_size);
log("[OK]\n");
}
// get string and bindata
uint8_t *bindata_test = doc_get(obj, "packets", uint8_t*);
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else
log("[OK]\n");
doc_size_t bindata_size = doc_get_size(obj, "packets");
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
log("[OK] Packets size: %u\n", bindata_size);
log("[OK]\n");
}
char *string_test = doc_get(obj, "string", char*);
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else
log("[OK]\n");
doc_size_t string_size = doc_get_size(obj, "string");
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
log("[OK] string: \"%s\" - size: %u\n", string_test, string_size);
log("[OK]\n");
}
// copy value variables
doc *copy = doc_copy(obj, "pontos.p3");
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
double copy_value = doc_get(copy, ".", double);
log("[OK] Error_check: %s\n",doc_get_error_msg());
log("[OK] Copy value: %f\n", copy_value);
log("[OK]\n");
}
// append value variable
doc_set(copy, ".", double, 999.0);
// rename check
doc_rename(copy, ".", "p6");
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
log("[OK]\n");
}
doc_append(obj, "pontos", copy);
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
double append_value = doc_get(obj, "pontos.p6", double);
log("[OK] Error_check: %s\n",doc_get_error_msg());
log("[OK] Append value: %f\n", append_value);
log("[OK]\n");
}
// copy obj variables
doc *copy_obj = doc_copy(obj, "pontos");
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
double copy_member = doc_get(copy_obj, "p2", double);
log("[OK] Error_check: %s\n",doc_get_error_msg());
log("[OK] Copied member: %f\n", copy_member);
log("[OK]\n");
}
// append obj variable
doc_rename(copy_obj, ".", "pontos_new");
doc_set(copy_obj, "p2", double, 999.0);
doc_append(obj, ".", copy_obj);
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
double copy_append_value = doc_get(obj, "pontos_new.p2", double);
log("[OK] Error_check: %s\n",doc_get_error_msg());
log("[OK] Copy Append value: %f\n", copy_append_value);
log("[OK]\n");
}
// check doc_get on numeric names
int64_t one = doc_get(obj, "1", int64_t);
if(one){
log("[OK] Value 1 : %i\n", one);
log("[OK]\n");
}
else{
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
log("[ERROR] Value 1 : %i\n", one);
}
// check get member on array index
bool check_bool = doc_get(obj, "objs[1].atb", bool);
if(!check_bool){
log("[OK] Error_check: %s\n",doc_get_error_msg());
log("[OK] Bool: %s\n", check_bool ? "true" : "false");
}
else{
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
}
// check iterator functionality
for(doc_loop(cursor, obj)){
log("[LOOP] Member loop: %s\n", cursor->name);
}
// interfaces with structs
// struct_ex_t custom_struct = {.value1 = 20, .value2 = 44, .value3 = 69.0};
// doc *struct_doc = doc_struct_new_struct_ex_t(custom_struct);
// if(doc_error_code)
// log("[ERROR] Error_check: %s\n",doc_get_error_msg());
// else{
// double struct_value = doc_get(struct_doc, "value3", double);
// log("[OK] Error_check: %s\n",doc_get_error_msg());
// log("[OK] struct value3: %f\n", struct_value);
// log("[OK]\n");
// }
// custom_struct.value3 = 75.0;
// doc_struct_set_struct_ex_t(custom_struct, struct_doc);
// if(doc_error_code)
// log("[ERROR] Error_check: %s\n",doc_get_error_msg());
// else{
// double struct_value = doc_get(struct_doc, "value3", double);
// log("[OK] Error_check: %s\n",doc_get_error_msg());
// log("[OK] struct value3: %f\n", struct_value);
// log("[OK]\n");
// }
// doc_set(struct_doc, "value2", int , 100);
// doc_struct_get_struct_ex_t(&custom_struct, struct_doc);
// if(doc_error_code)
// log("[ERROR] Error_check: %s\n",doc_get_error_msg());
// else{
// log("[OK] struct value2: %i\n", custom_struct.value2);
// log("[OK]\n");
// }
// delete all, but can be any instance
doc_delete(obj,".");
if(doc_error_code)
log("[ERROR] Error_check: %s\n",doc_get_error_msg());
else{
log("[OK]\n");
}
return 0;
}