-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathswxJsonAdvancedTest_fetch_v2.cpp
More file actions
412 lines (330 loc) · 10.1 KB
/
swxJsonAdvancedTest_fetch_v2.cpp
File metadata and controls
412 lines (330 loc) · 10.1 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <iostream>
#include "swxJson.h"
using namespace std;
using namespace swxJson;
const std::string tupleDemo = R"(
{"aa":["aa", 123, 32.4, 100, "asas"], "bb":{}}
)";
const std::string arrayDemo = R"(
{"strArray":["aa", "bb", "cc"], "intArray":[1, 2, 3, 4, 5]}
)";
const std::string dictDemo = R"(
{"strDict":{"123":"aa", "456":"bb", "789":"cc"}, "intDict":{"aa":1, "bb":2, "cc":3, "dd":4, "ee":5}}
)";
void test_tuple(JsonPtr json, bool compatibleMode)
{
cout<<"[Test tuple] Pattern unmatched testing. Compatible mode: "<<(compatibleMode ? "True" : "False")<<endl;
std::tuple<std::string, int, double, uint64_t, std::string, int, std::string> more;
std::tuple<std::string, uint32_t, float, int> less;
cout<<"---- more elements ----------"<<endl;
try {
json->wantTuple("aa", more, compatibleMode);
cout<<"Element 1: "<<std::get<0>(more)<<endl;
cout<<"Element 2: "<<std::get<1>(more)<<endl;
cout<<"Element 3: "<<std::get<2>(more)<<endl;
cout<<"Element 4: "<<std::get<3>(more)<<endl;
cout<<"Element 5: "<<std::get<4>(more)<<endl;
cout<<"Element 6: "<<std::get<5>(more)<<endl;
cout<<"Element 7: "<<std::get<6>(more)<<endl;
}
catch (const JsonNodeTypeMissMatchError &e)
{
cout<<"[Expectant error]: Type miss match."<<endl;
}
cout<<"---- less elements ----------"<<endl;
try {
json->wantTuple("aa", less, compatibleMode);
cout<<"Element 1: "<<std::get<0>(less)<<endl;
cout<<"Element 2: "<<std::get<1>(less)<<endl;
cout<<"Element 3: "<<std::get<2>(less)<<endl;
cout<<"Element 4: "<<std::get<3>(less)<<endl;
}
catch (const JsonNodeTypeMissMatchError &e)
{
cout<<"[Expectant error]: Type miss match."<<endl;
}
}
void test_tuple()
{
cout<<endl<<"[Test tuple] Pattern matched testing ..."<<endl;
std::tuple<std::string, int, double, uint64_t, std::string> matchTuple;
try {
JsonPtr json = Json::parse(tupleDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
json->wantTuple("aa", matchTuple);
cout<<"Element 1: "<<std::get<0>(matchTuple)<<endl;
cout<<"Element 2: "<<std::get<1>(matchTuple)<<endl;
cout<<"Element 3: "<<std::get<2>(matchTuple)<<endl;
cout<<"Element 4: "<<std::get<3>(matchTuple)<<endl;
cout<<"Element 5: "<<std::get<4>(matchTuple)<<endl;
test_tuple(json, false);
test_tuple(json, true);
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K, size_t N>
void test_array(JsonPtr json, const char* key, const char* desc, bool compatibleMode)
{
cout<<endl<<desc<<endl;
std::array<K, N> elemArray = json->wantArray<K, N>(key, compatibleMode);
for (auto& element: elemArray)
cout<<"Element: "<<element<<endl;
}
void test_array()
{
cout<<endl<<"[Test array] ..."<<endl;
try {
JsonPtr json = Json::parse(arrayDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
cout<<endl<<"[Test array] Length matched testing ..."<<endl;
test_array<std::string, 3>(json, "strArray", "--- string array: length matched version.", false);
test_array<int, 5>(json, "intArray", "--- int array: length matched version.", false);
cout<<endl<<"[Test array] Length miss matched ** without ** compatible testing ..."<<endl;
try {
test_array<std::string, 5>(json, "strArray", "--- string array: more length version.", false);
}
catch (const JsonNodeTypeMissMatchError &e)
{
cout<<"[Expectant error]: Type miss match."<<endl;
}
try {
test_array<std::string, 2>(json, "strArray", "--- string array: less length version.", false);
}
catch (const JsonNodeTypeMissMatchError &e)
{
cout<<"[Expectant error]: Type miss match."<<endl;
}
try {
test_array<int, 7>(json, "intArray", "--- int array: more length version.", false);
}
catch (const JsonNodeTypeMissMatchError &e)
{
cout<<"[Expectant error]: Type miss match."<<endl;
}
try {
test_array<int, 3>(json, "intArray", "--- int array: less length version.", false);
}
catch (const JsonNodeTypeMissMatchError &e)
{
cout<<"[Expectant error]: Type miss match."<<endl;
}
cout<<endl<<"[Test array] Length miss matched ** with ** compatible testing ..."<<endl;
test_array<std::string, 5>(json, "strArray", "--- string array: more length version.", true);
test_array<std::string, 2>(json, "strArray", "--- string array: less length version.", true);
test_array<int, 7>(json, "intArray", "--- int array: more length version.", true);
test_array<int, 3>(json, "intArray", "--- int array: less length version.", true);
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_queue(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::queue<K> elemQueue = json->wantQueue<K>(key);
while (elemQueue.size())
{
cout<<"Element: "<<elemQueue.front()<<endl;
elemQueue.pop();
}
}
void test_queue()
{
cout<<endl<<"[Test queue] ..."<<endl;
try {
JsonPtr json = Json::parse(arrayDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_queue<std::string>(json, "strArray", "--- string queue:");
test_queue<int>(json, "intArray", "--- int queue:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_deque(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::deque<K> elemQueue = json->wantDeque<K>(key);
while (elemQueue.size())
{
cout<<"Element: "<<elemQueue.front()<<endl;
elemQueue.pop_front();
}
}
void test_deque()
{
cout<<endl<<"[Test deque] ..."<<endl;
try {
JsonPtr json = Json::parse(arrayDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_deque<std::string>(json, "strArray", "--- string deque:");
test_deque<int>(json, "intArray", "--- int deque:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_list(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::list<K> elemList = json->wantList<K>(key);
for (auto& element: elemList)
cout<<"Element: "<<element<<endl;
}
void test_list()
{
cout<<endl<<"[Test list] ..."<<endl;
try {
JsonPtr json = Json::parse(arrayDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_list<std::string>(json, "strArray", "--- string list:");
test_list<int>(json, "intArray", "--- int list:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_vector(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::vector<K> elemVector = json->wantVector<K>(key);
for (auto& element: elemVector)
cout<<"Element: "<<element<<endl;
}
void test_vector()
{
cout<<endl<<"[Test vector] ..."<<endl;
try {
JsonPtr json = Json::parse(arrayDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_vector<std::string>(json, "strArray", "--- string vector:");
test_vector<int>(json, "intArray", "--- int vector:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_set(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::set<K> elemSet = json->wantSet<K>(key);
for (auto& element: elemSet)
cout<<"Element: "<<element<<endl;
}
void test_set()
{
cout<<endl<<"[Test set] ..."<<endl;
try {
JsonPtr json = Json::parse(arrayDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_set<std::string>(json, "strArray", "--- string set:");
test_set<int>(json, "intArray", "--- int set:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_unordered_set(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::unordered_set<K> elemSet = json->wantUnorderedSet<K>(key);
for (auto& element: elemSet)
cout<<"Element: "<<element<<endl;
}
void test_unordered_set()
{
cout<<endl<<"[Test unordered set] ..."<<endl;
try {
JsonPtr json = Json::parse(arrayDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_unordered_set<std::string>(json, "strArray", "--- string unordered set:");
test_unordered_set<int>(json, "intArray", "--- int unordered set:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_dict(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::map<std::string, K> elemMap = json->wantDict<K>(key);
for (auto& pp: elemMap)
cout<<"Element: "<<pp.first<<":"<<pp.second<<endl;
}
void test_dict()
{
cout<<endl<<"[Test map] ..."<<endl;
try {
JsonPtr json = Json::parse(dictDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_dict<std::string>(json, "strDict", "--- string map:");
test_dict<int>(json, "intDict", "--- int map:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
template <typename K>
void test_unordered_dict(JsonPtr json, const char* key, const char* desc)
{
cout<<endl<<desc<<endl;
std::unordered_map<std::string, K> elemMap = json->wantUnorderedDict<K>(key);
for (auto& pp: elemMap)
cout<<"Element: "<<pp.first<<":"<<pp.second<<endl;
}
void test_unordered_dict()
{
cout<<endl<<"[Test unordered map] ..."<<endl;
try {
JsonPtr json = Json::parse(dictDemo.c_str());
cout<<"get json:"<<endl;
cout<<json<<endl<<endl;
test_unordered_dict<std::string>(json, "strDict", "--- string unordered map:");
test_unordered_dict<int>(json, "intDict", "--- int unordered map:");
}
catch (const JsonError &e)
{
cout<<"[Error]: "<<e.message()<<" line: "<<e.lineNumber()<<", fun: "<<e.function()<<endl;
}
}
int main()
{
test_tuple();
test_array();
test_queue();
test_deque();
test_list();
test_vector();
test_set();
test_unordered_set();
test_dict();
test_unordered_dict();
return 0;
}