-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutest.cpp
More file actions
726 lines (681 loc) · 30.4 KB
/
utest.cpp
File metadata and controls
726 lines (681 loc) · 30.4 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*!\file utest.cpp
* \brief Unit tests for ndict
*/
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <cstring>
#include <vector>
#include "ndict.h"
#include "njson.h"
int upassed=0;
int ufailed=0;
/*!\brief Simplistic unittest
* \param Name Name of the test
* \param Result True if the test passed
*/
void test(const std::string &Name,const bool &Result){
printf(" %-75s%s\n",Name.c_str(),Result?"PASS":"FAIL");
if(Result){
upassed++;
}
else{
ufailed++;
}
}
/*!\brief Simplisting clockstamp for performancetesting
* \return Time in seconds with usecond precision
*/
double getclock(){
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return 1e-6*tv.tv_usec+tv.tv_sec;
}
/*!\brief Test basic dictionary handling
*/
void test_dict(){
// Stage a dictionary object
printf("\nRunning dictionary test:\n");
ndict object;
test("Empty dictionary has 0 root items",object.size()==0);
object["string"]="string";
object["int"]=123;
object["double"]=123.456;
object["negative"]=-123456;
object["object"]["value1"]="value1";
object["object"]["value2"]="value2";
object["object"]["value3"]="value3";
object["outer"]["inner"]["value1"]="value1";
object["outer"]["inner"]["value2"]="value2";
object["outer"]["inner"]["value3"]="value3";
// Test dictionary
test("Dictionary has N root items",object.size()==6);
test("Dictionary string value",object["string"].getstring()=="string");
test("Dictionary char value",object["string"].getchar()==std::string("string"));
test("Dictionary int value",object["int"].getint()==123);
test("Dictionary double value",object["double"].getdouble()==123.456);
test("Dictionary negative value",object["negative"].getint()==-123456);
test("Dictionary object has N items",object["object"].size()==3);
test("Dictionary object first string value",object["object"]["value1"].getstring()=="value1");
test("Dictionary object second string value",object["object"]["value2"].getstring()=="value2");
test("Dictionary object third string value",object["object"]["value3"].getstring()=="value3");
test("Dictionary nested object has N items",object["outer"].size()==1);
test("Dictionary nested sub object has N items",object["outer"]["inner"].size()==3);
test("Dictionary nested sub object first value",object["outer"]["inner"]["value1"].getstring()=="value1");
test("Dictionary nested sub object second value",object["outer"]["inner"]["value2"].getstring()=="value2");
test("Dictionary nested sub object third value",object["outer"]["inner"]["value3"].getstring()=="value3");
// Copy dict and retest
ndict copy(object);
test("Dictionary copy has N root items",copy.size()==6);
test("Dictionary copy string value",copy["string"].getstring()=="string");
test("Dictionary copy char value",copy["string"].getchar()==std::string("string"));
test("Dictionary copy int value",copy["int"].getint()==123);
test("Dictionary copy double value",copy["double"].getdouble()==123.456);
test("Dictionary copy object has N items",copy["object"].size()==3);
test("Dictionary copy object first string value",copy["object"]["value1"].getstring()=="value1");
test("Dictionary copy object second string value",copy["object"]["value2"].getstring()=="value2");
test("Dictionary copy object third string value",copy["object"]["value3"].getstring()=="value3");
test("Dictionary copy nested object has N items",copy["outer"].size()==1);
test("Dictionary copy nested sub object has N items",copy["outer"]["inner"].size()==3);
test("Dictionary copy nested sub object first value",copy["outer"]["inner"]["value1"].getstring()=="value1");
test("Dictionary copy nested sub object second value",copy["outer"]["inner"]["value2"].getstring()=="value2");
test("Dictionary copy nested sub object third value",copy["outer"]["inner"]["value3"].getstring()=="value3");
copy["int"]=456;
test("Dictionary copy int value was changed",copy["int"].getint()==456);
test("Dictionary original retains int value",object["int"].getint()==123);
// Test removing of keys
object["object"].remove("value2");
test("Removing middle item contracts the object",object["object"].size()==2);
test("Preceeding value is intact",object["object"]["value1"].getstring()=="value1");
test("Next value is intact",object["object"]["value3"].getstring()=="value3");
object["object"].remove("value3");
test("Removing end item contracts the object",object["object"].size()==1);
test("Preceeding value is intact",object["object"]["value1"].getstring()=="value1");
object["object"].remove("value1");
test("Removing last item leaves an empty object",object["object"].size()==0);
}
/*!\brief Test basic array handling
*/
void test_array(){
// Stage a dictionary containing arrays
printf("\nRunning array handling test:\n");
ndict object;
std::vector<std::string> sv;
sv.push_back("this");
sv.push_back("is a");
sv.push_back("string");
sv.push_back("of");
sv.push_back("strings");
std::vector<int> iv;
iv.push_back(0);
iv.push_back(1);
iv.push_back(2);
iv.push_back(3);
iv.push_back(4);
std::vector<std::vector<int>> iiv;
iiv.push_back(iv);
iiv.push_back(iv);
// Test dictionary object
object["stringarray"]=sv;
object["intarray"]=iv;
object["intintarray"]=iiv;
test("Loaded vector of N string items",object["stringarray"].size()==5);
test("Loaded vector of N integer items",object["intarray"].size()==5);
test("Checking first string value",object["stringarray"][0].getstring()=="this");
test("Checking second string value",object["stringarray"][1].getstring()=="is a");
test("Checking third string value",object["stringarray"][2].getstring()=="string");
test("Checking fourth string value",object["stringarray"][3].getstring()=="of");
test("Checking fifth string value",object["stringarray"][4].getstring()=="strings");
test("Checking first integer value",object["intarray"][0].getint()==0);
test("Checking second integer value",object["intarray"][1].getint()==1);
test("Checking third integer value",object["intarray"][2].getint()==2);
test("Checking fourth integer value",object["intarray"][3].getint()==3);
test("Checking fifth integer value",object["intarray"][4].getint()==4);
// Try to remove items
object["intarray"].remove(2);
test("Removing middle item contracts the array",object["intarray"].size()==4);
test("Preceeding value is intact",object["intarray"][1].getint()==1);
test("Next value is relocated",object["intarray"][2].getint()==3);
object["intarray"].remove(0);
test("Removing first item contracts the array",object["intarray"].size()==3);
test("First value is now the second",object["intarray"][0].getint()==1);
object["intarray"].remove(3);
test("Removing out-of-bound item is ignored",object["intarray"].size()==3);
object["intarray"].remove(2);
test("Removing last item contracts the array",object["intarray"].size()==2);
object["intarray"].remove(0);
object["intarray"].remove(0);
test("Removing all items leaves an empty array",object["intarray"].size()==0);
object["intarray"].remove(0);
test("Removing items from empty array does nothing",object["intarray"].size()==0);
}
void test_types(){
// Stage a dictionary object
printf("\nRunning data type test:\n");
ndict object;
object["string"]="string";
object["int"]=123;
object["long"]=123L;
object["longlong"]=123LL;
object["nint"]=-123;
object["nlong"]=-123L;
object["nlonglong"]=-123LL;
object["uint"]=123U;
object["ulong"]=123UL;
object["ulonglong"]=123ULL;
test("Get int",object["int"].getint()==123);
test("Get long",object["long"].getlong()==123L);
test("Get long long",object["longlong"].getlonglong()==123LL);
test("Get negative int",object["nint"].getint()==-123);
test("Get negative long",object["nlong"].getlong()==-123L);
test("Get negative long long",object["nlonglong"].getlonglong()==-123LL);
test("Get unsigned int",object["uint"].getuint()==123U);
test("Get unsigned long",object["ulong"].getulong()==123UL);
test("Get unsigned long long",object["ulonglong"].getulonglong()==123ULL);
test("Compare int",object["int"]==123);
test("Compare long",object["long"]==123L);
test("Compare long long",object["longlong"]==123LL);
test("Compare negative int",object["nint"]==-123);
test("Compare negative long",object["nlong"]==-123L);
test("Compare negative long long",object["nlonglong"]==-123LL);
test("Compare unsigned int",object["uint"]==123U);
test("Compare unsigned long",object["ulong"]==123UL);
test("Compare unsigned long long",object["ulonglong"]==123ULL);
}
/*!\brief Test json-dictionary parsing
*/
void test_json_string(){
// Load a simple json string
printf("\nRunning json-string parser test:\n");
std::string text=""
"{\n"
" \"bool\" : true,\n"
" \"string\" : \"string\",\n"
" \"sstring\" : \"st ring\",\n"
" \"qstring\" : \"st\\\"ring\",\n"
" \"int\" : 123,\n"
" \"double\" : 123.456000,\n"
" \"intarray\" : [0,1,2,3,4],\n"
" \"strarray\" : [\"a\",\"b\",\"c\"],\n"
" \"outer\" : {\n"
" \"inner\" : {\n"
" \"value1\" : \"hello\",\n"
" \"value2\" : \"world\",\n"
" \"value3\" : \"!!!\"\n"
" }\n"
" }\n"
"}\n";
// Parse string to a dictionary object and test
ndict object=njson::decode(text);
//printf("in :%s\n",text.c_str());
//printf("out:%s\n",object.getjson().c_str());
test("Parsed N root items",object.size()==9);
test("Parsed bool item",object["bool"].getbool()==true);
test("Parsed vanilla string item",object["string"].getstring()=="string");
test("Parsed string item with space",object["sstring"].getstring()=="st ring");
test("Parsed string item with escape",object["qstring"].getstring()=="st\\\"ring");
test("Parsed integer item",object["int"].getint()==123);
test("Parsed double item",object["double"].getdouble()==123.456);
test("Parsed integer array of N items",object["intarray"].size()==5);
test("Parsed integer array first value",object["intarray"][0].getint()==0);
test("Parsed integer array second value",object["intarray"][1].getint()==1);
test("Parsed integer array third value",object["intarray"][2].getint()==2);
test("Parsed integer array fourth value",object["intarray"][3].getint()==3);
test("Parsed integer array fifth value",object["intarray"][4].getint()==4);
test("Parsed string array of N items",object["strarray"].size()==3);
test("Parsed string array first value",object["strarray"][0].getstring()=="a");
test("Parsed string array second value",object["strarray"][1].getstring()=="b");
test("Parsed string array third value",object["strarray"][2].getstring()=="c");
test("Parsed subobject of N objects",object["outer"].size()==1);
test("Parsed sub-subobject of N objects",object["outer"]["inner"].size()==3);
test("Parsed sub-subobject first value",object["outer"]["inner"]["value1"].getstring()=="hello");
test("Parsed sub-subobject second value",object["outer"]["inner"]["value2"].getstring()=="world");
test("Parsed sub-subobject third value",object["outer"]["inner"]["value3"].getstring()=="!!!");
}
/*!\brief Test json-dictionary parsing
*/
void test_json_file(){
// Stage a simple json string
printf("\nRunning json-file parser test:\n");
std::string text=""
"{\n"
" \"bool\" : true,\n"
" \"string\" : \"string\",\n"
" \"sstring\" : \"st ring\",\n"
" \"qstring\" : \"st\\\"ring\",\n"
" \"int\" : 123,\n"
" \"double\" : 123.456000,\n"
" \"intarray\" : [0,1,2,3,4],\n"
" \"strarray\" : [\"a\",\"b\",\"c\"],\n"
" \"outer\" : {\n"
" \"inner\" : {\n"
" \"value1\" : \"hello\",\n"
" \"value2\" : \"world\",\n"
" \"value3\" : \"!!!\"\n"
" }\n"
" }\n"
"}\n";
#if 0
// Write json to a temporary file (theoretically unsafe, but portable)
std::string tmpname=std::tmpnam(nullptr);
FILE *fd=fopen(tmpname.c_str(),"w");
if(fd){
fwrite(text.c_str(),text.size(),1,fd);
fclose(fd);
}
else{
printf("Failed to open %s for writing\n",tmpname.c_str());
ufailed++;
return;
}
#else
// Write json to a temporary file (safe, butlinux-only)
char fnbuffer[32];
strcpy(fnbuffer,"/tmp/ndict_utest_XXXXXX");
int fd=mkstemp(fnbuffer);
std::string tmpname=fnbuffer;
write(fd,text.c_str(),text.size());
close(fd);
#endif
// Parse read-back json to a dict and test
ndict object=njson::read(tmpname);
unlink(tmpname.c_str());
test("Parsed N root items",object.size()==9);
test("Parsed bool item",object["bool"].getbool()==true);
test("Parsed vanilla string item",object["string"].getstring()=="string");
test("Parsed string item with space",object["sstring"].getstring()=="st ring");
test("Parsed string item with escape",object["qstring"].getstring()=="st\\\"ring");
test("Parsed integer item",object["int"].getint()==123);
test("Parsed double item",object["double"].getdouble()==123.456);
test("Parsed integer array of N items",object["intarray"].size()==5);
test("Parsed integer array first value",object["intarray"][0].getint()==0);
test("Parsed integer array second value",object["intarray"][1].getint()==1);
test("Parsed integer array third value",object["intarray"][2].getint()==2);
test("Parsed integer array fourth value",object["intarray"][3].getint()==3);
test("Parsed integer array fifth value",object["intarray"][4].getint()==4);
test("Parsed string array of N items",object["strarray"].size()==3);
test("Parsed string array first value",object["strarray"][0].getstring()=="a");
test("Parsed string array second value",object["strarray"][1].getstring()=="b");
test("Parsed string array third value",object["strarray"][2].getstring()=="c");
test("Parsed subobject of N objects",object["outer"].size()==1);
test("Parsed sub-subobject of N objects",object["outer"]["inner"].size()==3);
test("Parsed sub-subobject first value",object["outer"]["inner"]["value1"].getstring()=="hello");
test("Parsed sub-subobject second value",object["outer"]["inner"]["value2"].getstring()=="world");
test("Parsed sub-subobject third value",object["outer"]["inner"]["value3"].getstring()=="!!!");
}
void test_compare(){
// Stage a simple dictionary object
printf("\nRunning value compare test:\n");
ndict object;
object["string"]="string";
object["stringint"]="123";
object["int"]=123;
object["long"]=123;
object["longlong"]=123;
object["double"]=123.456;
object["true"]=true;
object["false"]=false;
// Test original dictionary
test("Comparing string object to matching string",object["string"]==std::string("string"));
test("Comparing string object to mismatching string",object["string"]!=std::string("strinG"));
test("Comparing string object to matching cstring",object["string"]=="string");
test("Comparing string object to mismatching cstring",object["string"]!="strinG");
test("Comparing boolean object to matching value",object["true"]==true);
test("Comparing boolean object to mismatching value",object["false"]!=true);
test("Comparing boolean object to string",object["false"]!="true");
test("Comparing integer object to matching integer",object["int"]==123);
test("Comparing integer object to mismatching integer",object["int"]!=1234);
test("Comparing long object to matching long",object["long"]==123L);
test("Comparing long object to mismatching long",object["long"]!=1234L);
test("Comparing long long object to matching long long",object["longlong"]==123LL);
test("Comparing long long object to mismatching long long",object["longlong"]!=1234LL);
test("Comparing double object to matching double",object["double"]==123.456);
test("Comparing double object to mismatching double",object["double"]!=123.4567);
}
/*!\brief Test dictionary-json-dictionary encoding/decoding
*/
void test_encode_decode(){
// Stage a simple dictionary object
printf("\nRunning encode-decode test:\n");
ndict object;
object["string"]="string";
object["int"]=123;
object["double"]=123.456;
object["object"]["value1"]="value1";
object["object"]["value2"]="value2";
object["object"]["value3"]="value3";
object["outer"]["inner"]["value1"]="value1";
object["outer"]["inner"]["value2"]="value2";
object["outer"]["inner"]["value3"]="value3";
object["outer"]["inner"]["value4"];
// Test original dictionary
test("Original has N root items",object.size()==5);
test("Original string value",object["string"].getstring()=="string");
test("Original int value",object["int"].getint()==123);
test("Original double value",object["double"].getdouble()==123.456);
test("Original object has N items",object["object"].size()==3);
test("Original object first string value",object["object"]["value1"].getstring()=="value1");
test("Original object second string value",object["object"]["value2"].getstring()=="value2");
test("Original object third string value",object["object"]["value3"].getstring()=="value3");
test("Original nested object has N items",object["outer"].size()==1);
test("Original nested sub object has N items",object["outer"]["inner"].size()==4);
test("Original nested sub object first value",object["outer"]["inner"]["value1"].getstring()=="value1");
test("Original nested sub object second value",object["outer"]["inner"]["value2"].getstring()=="value2");
test("Original nested sub object third value",object["outer"]["inner"]["value3"].getstring()=="value3");
test("Original nested sub object has null-object",object["outer"]["inner"].size()==4);
// Parse to json, read back, and test new object
std::string text=njson::encode(object);
object=njson::decode(text);
test("Reencoded has N root items",object.size()==5);
test("Reencoded string value",object["string"].getstring()=="string");
test("Reencoded int value",object["int"].getint()==123);
test("Reencoded double value",object["double"].getdouble()==123.456);
test("Reencoded object has N items",object["object"].size()==3);
test("Reencoded object first string value",object["object"]["value1"].getstring()=="value1");
test("Reencoded object second string value",object["object"]["value2"].getstring()=="value2");
test("Reencoded object third string value",object["object"]["value3"].getstring()=="value3");
test("Reencoded nested object has N items",object["outer"].size()==1);
test("Reencoded nested sub object has N items",object["outer"]["inner"].size()==4);
test("Reencoded nested sub object first value",object["outer"]["inner"]["value1"].getstring()=="value1");
test("Reencoded nested sub object second value",object["outer"]["inner"]["value2"].getstring()=="value2");
test("Reencoded nested sub object third value",object["outer"]["inner"]["value3"].getstring()=="value3");
test("Reencoded nested sub object has null-object",object["outer"]["inner"].size()==4);
}
/*!\brief Test json-dictionary merging
*/
void test_json_merge(){
// Create a simple dictionary
printf("\nRunning json-dictionary merging test:\n");
ndict ogobject;
ogobject["bool"]=false;
ogobject["string2"]="test";
ogobject["outer"]["inner"]["value3"]="test";
ogobject["outer"]["inner"]["value5"];
// Load a simple json string and merge it into the dictionary
std::string text=""
"{\n"
" \"bool\" : true,\n"
" \"string\" : \"string\",\n"
" \"double\" : 123.456000,\n"
" \"intarray\" : [0,1,2,3,4],\n"
" \"outer\" : {\n"
" \"inner\" : {\n"
" \"value1\" : \"hello\",\n"
" \"value2\" : \"world\",\n"
" \"value3\" : \"test\",\n"
" \"value4\" : null,\n"
" }\n"
" }\n"
"}\n";
// Test merged dictionary
ndict object=njson::merge(text,ogobject);
test("Merged object has N root items",object.size()==6);
test("Destination retains unique values",object["string2"].getstring()=="test");
test("Destination got a copy of unique values",object["string"].getstring()=="string");
test("Destination had matching values overwritten",object["bool"].getbool()==true);
test("Destination retains nested unique values",object["outer"]["inner"]["value3"].getstring()=="test");
test("Destination successfully merged null values",object["outer"]["inner"].size()==5);
test("Destination got a copy of nested unique values",object["outer"]["inner"]["value1"].getstring()=="hello");
test("Destination got a copy of array",object["intarray"].size()==5);
test("Destination got a copy of array values",object["intarray"][3].getint()==3);
//printf("og:%s\n",ogobject.getjson().c_str());
//printf("src:%s\n",njson::decode(text).getjson().c_str());
//printf("merged:%s\n",object.getjson().c_str());
}
/*!\brief Test decoding of nested arrays/objects
* \note This didn't work for v1.0.0
*/
void test_nested_objects(){
// Load a simple json string and merge it into the dictionary
printf("\nTesting decoding of array of objects:\n");
std::string text=""
"{\n"
" \"array\" : [{\"a\":\"A\"},{\"b\":\"B\"}]\n"
"}\n";
ndict object=njson::decode(text);
test("Root object has 1 items",object.size()==1);
test("Array object has 2 items",object["array"].size()==2);
test("Inner object 1 has 1 items",object["array"][0].size()==1);
test("Inner object 2 has 1 items",object["array"][1].size()==1);
test("Inner object 1 has correctly decoded",object["array"][0]["a"].getstring()=="A");
test("Inner object 2 has correctly decoded",object["array"][1]["b"].getstring()=="B");
printf("\nTesting decoding of array of arrays:\n");
text=""
"{\n"
" \"array\" : [[1,2,3],[4,5,6],[7,8,9]]\n"
"}\n";
object=njson::decode(text);
test("Root object has 1 items",object.size()==1);
test("Root array has 3 items",object["array"].size()==3);
test("Array item has 3 items",object["array"][0].size()==3);
test("Array contents is as expected",object["array"][0][0].getint()==1);
test("Array contents is as expected",object["array"][0][1].getint()==2);
test("Array contents is as expected",object["array"][0][2].getint()==3);
test("Array contents is as expected",object["array"][1][0].getint()==4);
test("Array contents is as expected",object["array"][1][1].getint()==5);
test("Array contents is as expected",object["array"][1][2].getint()==6);
test("Array contents is as expected",object["array"][2][0].getint()==7);
test("Array contents is as expected",object["array"][2][1].getint()==8);
test("Array contents is as expected",object["array"][2][2].getint()==9);
printf("\nTesting decoding of array of null-objects:\n");
text=""
"{\n"
" \"array\" : [null,null]\n"
"}\n";
object=njson::decode(text);
test("Root object has 1 items",object.size()==1);
test("Root array has 2 items",object["array"].size()==2);
test("Array contents is as expected",object["array"][0].type==ndict::TNULL);
test("Array contents is as expected",object["array"][1].type==ndict::TNULL);
}
/*!\brief Test error handling
*/
void test_error(){
// Stage a dictionary object
printf("\nRunning error handling test:\n");
ndict object;
object["string"]="string";
object["int"]=123;
object["double"]=123.456;
// Test dictionary
#if !NDICT_CHECK_TYPE
test("Dictionary string value resolves to 0 as int",object["string"].getint()==0);
test("Dictionary string value resolves to 0 as double",object["string"].getdouble()==0);
test("Dictionary string value resolves to false as bool",object["string"].getbool()==false);
#else
{
bool result=false;
try{
std::string value=object["int"].getstring();
}
catch(ndict_exception &e){
result=true;
}
test("Dictionary throws exception when accessing invalid type",result);
}
#endif
#if !NDICT_CHECK_EXISTING
test("Invalid dictionary value resolves to empty string",object["invalid"].getstring()=="");
test("Invalid dictionary value resolves to 0 as int",object["invalid"].getint()==0);
test("Invalid dictionary value resolves to 0 as double",object["invalid"].getdouble()==0);
test("Invalid dictionary value resolves to false as bool",object["invalid"].getbool()==0);
#else
{
bool result=false;
try{
std::string value=object["dontexist"].getstring();
}
catch(ndict_exception &e){
result=true;
}
test("Dictionary throws exception when accessing non-existing value",result);
}
#endif
// Test encoding with trailing comma
{
std::string text=""
"{\n"
" \"bool\" : true,\n"
" \"string\" : \"string\",\n"
" \"int\" : 123,\n"
" \"double\" : 123.456000,\n"
" \"intarray\" : [0,1,2,3,4],\n"
"}\n";
object=njson::decode(text);
test("Can decode json with trailing comma",object.size()==5);
}
// Test encoding with trailing trailing junk
{
bool result=false;
std::string text=""
"{\n"
" \"bool\" : true,\n"
" \"string\" : \"string\",\n"
" \"int\" : 123,\n"
" \"double\" : 123.456000,\n"
" \"intarray\" : [0,1,2,3,4],\n"
"}xx\n";
try{
object=njson::decode(text);
}
catch(njson_exception &e){
result=true;
}
test("Decoding json with trailing junk throws exception",result);
}
// Test encoding with trailing trailing junk
{
bool result=false;
std::string text=""
"{\n"
" \"bool\" : rue,\n"
"}\n";
try{
object=njson::decode(text);
printf("%s\n",object.getjson().c_str());
}
catch(njson_exception &e){
result=true;
}
test("Decoding json with invalid keywords throws exception",result);
}
// Test encoding with trailing trailing junk
{
bool result=false;
std::string text=""
"{\n"
" \"bool\" : true,\n"
" \"double\" : 123,456000,\n"
" \"intarray\" : [0,1,2,3,4],\n"
"}\n";
try{
object=njson::decode(text);
}
catch(njson_exception &e){
result=true;
}
test("Decoding json with invalid decimal separator throws exception",result);
}
}
void test_special(){
printf("\nTesting special cases:\n");
ndict object;
object["string"]="string";
object["int"]=123;
object["double"]=123.456;
object["true"]=true;
object["false"]=false;
object["array"][0]=1;
object["array"][1]=2;
object["array"][2]=3;
// Test original dictionary
test("Size of string object",object["string"].size()==0);
test("Size of double object",object["double"].size()==0);
test("Size of int object",object["int"].size()==0);
test("Size of boolean object",object["true"].size()==0);
test("Size of boolean object",object["false"].size()==0);
test("Size of unknown object",object["blabla"].size()==0);
test("Size of array object",object["array"].size()==3);
// Test with quoted values
object=njson::decode("{\"k\\\"e\\\"y\":\"value\"}");
test("json with quoted key has right size",object.size()==1);
test("json with quoted key has quoted key",object.haskey("k\\\"e\\\"y"));
test("json with quoted key responds equal operator",object["k\\\"e\\\"y"]=="value");
object=njson::decode("{\"key\":\"val\\\"u\\\"e\"}");
test("json with quoted value has right size",object.size()==1);
test("json with quoted value has key",object.haskey("key"));
test("json with quoted value has quoted value",object["key"].getstring()=="val\\\"u\\\"e");
test("json with quoted value reponds to equal operator",object["key"]=="val\\\"u\\\"e");
// Test with empty array
object=njson::decode("{\"array\":[]}");
test("json with empty array decodes",object.size()==1);
test("json with empty array is empty",object["array"].size()==0);
object=njson::decode("{\"array\":[ \n \t ]}");
test("json with whitespace array decodes",object.size()==1);
test("json with whitespace array is empty",object["array"].size()==0);
}
void test_perf(){
double start,stop;
ndict object;
std::string text=""
"{\n"
" \"bool\" : true,\n"
" \"string\" : \"string\",\n"
" \"int\" : 123,\n"
" \"double\" : 123.456000,\n"
" \"intarray\" : [0,1,2,3,4,5,6,7,8,9],\n"
"}\n";
printf("\nRunning performance tests\n");
start=getclock();
object=njson::decode(text);
for(unsigned i=0;i<1000;i++){
std::string text=njson::encode(object);
object=njson::decode(text);
}
stop=getclock();
printf(" Encoded and decoded short json 1000 times in %.3f ms\n",(stop-start)*1000);
start=getclock();
object=njson::decode(text);
for(unsigned i=0;i<1000;i++){
object["longarray"][i]=i;
}
stop=getclock();
printf(" Added 1000 array values in %.3f ms\n",(stop-start)*1000);
start=getclock();
object=njson::decode(text);
for(unsigned i=0;i<1000;i++){
std::string text=njson::encode(object);
object=njson::decode(text);
}
stop=getclock();
printf(" Encoded and decoded long json 1000 times in %.3f ms\n",(stop-start)*1000);
}
/*!\brief Run baby! RUN!
*/
int main(){
printf("utest %s\n",NDICT_VERSION);
printf("ndict unittest system\n");
printf("Vegard Fiksdal (C) 2024\n");
test_dict();
test_array();
test_types();
test_compare();
test_special();
test_json_string();
test_json_file();
test_encode_decode();
test_json_merge();
test_nested_objects();
test_error();
test_perf();
printf("\nPassed %d/%d tests\n",upassed,upassed+ufailed);
if(ufailed==0){
printf("All clear!\n");
}
else{
printf("Failed %d tests!\n",ufailed);
}
return ufailed;
}