-
Notifications
You must be signed in to change notification settings - Fork 0
/
testjsonutil.cc
405 lines (393 loc) · 10.6 KB
/
testjsonutil.cc
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
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <nlohmann/json.hpp>
#include "jsonutil.hh"
#include <array>
#include <vector>
struct Point{
double x,y,z;
int id;
JSON_MEMBER(x,y,z,id);
};
struct Triangle{
Point p1,p2,p3;
std::string name;
JSON_MEMBER(p1,p2,p3,name);
};
struct Points{
std::vector<Point> pts;
std::string name;
JSON_MEMBER(pts,name);
};
TEST_CASE("SimpleStruct"){
Point pt1{1.1, 2.2, 3.3, 4};
SECTION("member mapping in nlohmann::json"){
nlohmann::json j=pt1;
auto itr=j.begin();
CHECK(itr.key()=="id");
++itr;
CHECK(itr.key()=="x");
++itr;
CHECK(itr.key()=="y");
++itr;
CHECK(itr.key()=="z");
}
SECTION("member mapping in yos::map_json"){
yos::map_json j=pt1;
auto itr=j.begin();
CHECK(itr.key()=="id");
++itr;
CHECK(itr.key()=="x");
++itr;
CHECK(itr.key()=="y");
++itr;
CHECK(itr.key()=="z");
}
SECTION("native and nlohmann::json roundtrip"){
nlohmann::json j=pt1;
Point pt2=j;
CHECK(pt1.x==pt2.x);
CHECK(pt1.y==pt2.y);
CHECK(pt1.z==pt2.z);
CHECK(pt1.id==pt2.id);
}
SECTION("native and yos::array_json roundtrip"){
yos::array_json j=pt1;
CHECK(j.is_array());
Point pt2=j;
CHECK(pt1.x==pt2.x);
CHECK(pt1.y==pt2.y);
CHECK(pt1.z==pt2.z);
CHECK(pt1.id==pt2.id);
}
SECTION("native and yos::map_json roundtrip"){
yos::map_json j=pt1;
CHECK(!j.is_array());
Point pt2=j;
CHECK(pt1.x==pt2.x);
CHECK(pt1.y==pt2.y);
CHECK(pt1.z==pt2.z);
CHECK(pt1.id==pt2.id);
}
SECTION("native to yos::map_json to nlohmann:json to native"){
yos::map_json j=pt1;
CHECK(!j.is_array());
nlohmann::json nj=j;
Point pt2=nj;
CHECK(pt1.x==pt2.x);
CHECK(pt1.y==pt2.y);
CHECK(pt1.z==pt2.z);
CHECK(pt1.id==pt2.id);
}
SECTION("native to yos::array_json to nlohmann:json to native"){
yos::array_json j=pt1;
CHECK(j.is_array());
nlohmann::json nj=j;
Point pt2=nj;
CHECK(pt1.x==pt2.x);
CHECK(pt1.y==pt2.y);
CHECK(pt1.z==pt2.z);
CHECK(pt1.id==pt2.id);
}
}
TEST_CASE("Array of struct"){
std::array<Point,3> pts={{
{1.1,2.2,3.3,4},
{10.1,20.2,30.3,5},
{15.1,25.2,35.3,5}}};
SECTION("native and nlohmann::json roundtrip"){
nlohmann::json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
std::array<Point,3> pts2=j;
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native and yos::array_json roundtrip"){
yos::array_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(j[0].is_array());
std::array<Point,3> pts2=j;
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native and yos::map_json roundtrip"){
yos::map_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(!j[0].is_array());
CHECK(!j[1].is_array());
CHECK(!j[2].is_array());
std::array<Point,3> pts2=j;
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native to yos::map_json to nlohmann:json to native"){
yos::map_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(!j[0].is_array());
CHECK(!j[1].is_array());
CHECK(!j[2].is_array());
nlohmann::json nj=j;
std::array<Point,3> pts2=j;
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native to yos::array_json to nlohmann:json to native"){
yos::array_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(j[0].is_array());
nlohmann::json nj=j;
std::array<Point,3> pts2=j;
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
}
TEST_CASE("vector of struct"){
std::vector<Point> pts={{
{1.1,2.2,3.3,4},
{10.1,20.2,30.3,5},
{15.1,25.2,35.3,5}}};
SECTION("native and nlohmann::json roundtrip"){
nlohmann::json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native and yos::array_json roundtrip"){
yos::array_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(j[0].is_array());
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native and yos::map_json roundtrip"){
yos::map_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(!j[0].is_array());
CHECK(!j[1].is_array());
CHECK(!j[2].is_array());
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native to yos::map_json to nlohmann:json to native"){
yos::map_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(!j[0].is_array());
CHECK(!j[1].is_array());
CHECK(!j[2].is_array());
nlohmann::json nj=j;
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native to yos::array_json to nlohmann:json to native"){
yos::array_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(j[0].is_array());
nlohmann::json nj=j;
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
}
TEST_CASE("array of struct to vector of struct"){
std::array<Point,3> pts={{
{1.1,2.2,3.3,4},
{10.1,20.2,30.3,5},
{15.1,25.2,35.3,5}}};
SECTION("native and nlohmann::json roundtrip"){
nlohmann::json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native and yos::array_json roundtrip"){
yos::array_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(j[0].is_array());
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native and yos::map_json roundtrip"){
yos::map_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(!j[0].is_array());
CHECK(!j[1].is_array());
CHECK(!j[2].is_array());
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native to yos::map_json to nlohmann:json to native"){
yos::map_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(!j[0].is_array());
CHECK(!j[1].is_array());
CHECK(!j[2].is_array());
nlohmann::json nj=j;
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
SECTION("native to yos::array_json to nlohmann:json to native"){
yos::array_json j=pts;
CHECK(j.is_array());
CHECK(j.size()==3);
CHECK(j[0].is_array());
nlohmann::json nj=j;
std::vector<Point> pts2=j;
CHECK(pts2.size()==pts.size());
CHECK(pts[0].x ==pts2[0].x);
CHECK(pts[1].y ==pts2[1].y);
CHECK(pts[2].z ==pts2[2].z);
CHECK(pts[2].id==pts2[2].id);
}
}
TEST_CASE("Nested structure"){
Triangle tri={
{0,0,0,0},{1.1,2.2,3.3,1},{-3.3,-4.4,-5.5,2},"three points"
};
SECTION("member mapping"){
nlohmann::json j=tri;
CHECK(j.is_object());
auto itm=j.begin();
CHECK(itm.key()=="name");
itm++;
CHECK(itm.key()=="p1");
CHECK(itm.value().is_object());
itm++;
CHECK(itm.key()=="p2");
CHECK(itm.value().is_object());
itm++;
CHECK(itm.key()=="p3");
CHECK(itm.value().is_object());
CHECK(j["name"]=="three points");
CHECK(j["p1"]["x"]==0);
CHECK(j["p2"]["y"]==2.2);
CHECK(j["p3"]["z"]==-5.5);
}
SECTION("nlohmann::json roundtrip"){
nlohmann::json j=tri;
Triangle tri2=j;
CHECK(tri.name==tri2.name);
CHECK(tri.p1.x==tri2.p1.x);
CHECK(tri.p1.y==tri2.p1.y);
CHECK(tri.p1.z==tri2.p1.z);
CHECK(tri.p2.x==tri2.p2.x);
CHECK(tri.p2.y==tri2.p2.y);
CHECK(tri.p2.z==tri2.p2.z);
CHECK(tri.p3.x==tri2.p3.x);
CHECK(tri.p3.y==tri2.p3.y);
CHECK(tri.p3.z==tri2.p3.z);
}
SECTION("yos::map_json roundtrip"){
yos::map_json j=tri;
Triangle tri2=j;
CHECK(tri.name==tri2.name);
CHECK(tri.p1.x==tri2.p1.x);
CHECK(tri.p1.y==tri2.p1.y);
CHECK(tri.p1.z==tri2.p1.z);
CHECK(tri.p2.x==tri2.p2.x);
CHECK(tri.p2.y==tri2.p2.y);
CHECK(tri.p2.z==tri2.p2.z);
CHECK(tri.p3.x==tri2.p3.x);
CHECK(tri.p3.y==tri2.p3.y);
CHECK(tri.p3.z==tri2.p3.z);
}
}
TEST_CASE("Structure with std::vector"){
Points tri={
{{0,0,0,0},{1.1,2.2,3.3,1},{-3.3,-4.4,-5.5,2}},"three points"
};
SECTION("member mapping"){
nlohmann::json j=tri;
CHECK(j.is_object());
auto itm=j.begin();
CHECK(itm.key()=="name");
itm++;
CHECK(itm.key()=="pts");
CHECK(itm.value().is_array());
CHECK(j["pts"].size()==3);
CHECK(j["name"]=="three points");
CHECK(j["pts"][0]["x"]==0);
CHECK(j["pts"][1]["y"]==2.2);
CHECK(j["pts"][2]["z"]==-5.5);
}
SECTION("nlohmann::json roundtrip"){
nlohmann::json j=tri;
Points tri2=j;
CHECK(tri.name==tri2.name);
CHECK(tri2.pts.size()==3);
for(int i=0,ec=tri2.pts.size();i!=ec;++i){
CHECK(tri.pts[i].x==tri2.pts[i].x);
CHECK(tri.pts[i].y==tri2.pts[i].y);
CHECK(tri.pts[i].z==tri2.pts[i].z);
}
}
SECTION("yos::map_json roundtrip"){
yos::map_json j=tri;
Points tri2=j;
CHECK(tri.name==tri2.name);
CHECK(tri2.pts.size()==3);
for(int i=0,ec=tri2.pts.size();i!=ec;++i){
CHECK(tri.pts[i].x==tri2.pts[i].x);
CHECK(tri.pts[i].y==tri2.pts[i].y);
CHECK(tri.pts[i].z==tri2.pts[i].z);
}
}
}