forked from cesanta/frozen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.c
1093 lines (981 loc) · 33.7 KB
/
unit_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
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
* Copyright (c) 2013 Cesanta Software Limited
* All rights reserved
*
* This library is dual-licensed: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. For the terms of this
* license, see <http: *www.gnu.org/licenses/>.
*
* You are free to use this library under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* Alternatively, you can license this library under a commercial
* license, as set out in <http://cesanta.com/products.html>.
*/
/*
* To unit test on Mac system, do
*
* g++ unit_test.c -o unit_test -W -Wall -g -O0 -fprofile-arcs -ftest-coverage
* clang unit_test.c -o unit_test -W -Wall -g -O0 -fprofile-arcs -ftest-coverage
* ./unit_test
* gcov -a unit_test.c
*/
#include "frozen.c"
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *tok_type_names[] = {
"INVALID", "STRING", "NUMBER", "TRUE", "FALSE",
"NULL", "OBJECT_START", "OBJECT_END", "ARRAY_START", "ARRAY_END",
};
#define FAIL(str, line) \
do { \
fprintf(stderr, "Fail on line %d: [%s]\n", line, str); \
return str; \
} while (0)
#define ASSERT(expr) \
do { \
static_num_tests++; \
if (!(expr)) FAIL(#expr, __LINE__); \
} while (0)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define RUN_TEST(test) \
do { \
const char *msg = test(); \
if (msg) return msg; \
} while (0)
static int static_num_tests = 0;
static const char *test_errors(void) {
/* clang-format off */
static const char *invalid_tests[] = {
"p", "a:3", "\x01", "{:",
" { 1", "{a:\"\n\"}", "{a:1x}", "{a:1e}",
"{a:.1}", "{a:0.}", "{a:0.e}", "{a:0.e1}",
"{a:0.1e}", "{a:\"\\u\" } ", "{a:\"\\yx\"}", "{a:\"\\u111r\"}",
NULL};
static const char *incomplete_tests[] = {"",
" \r\n\t",
"{",
" { a",
"{a:",
"{a:\"",
" { a : \"xx",
"{a:12",
"{a:\"\\uf",
"{a:\"\\uff",
"{a:\"\\ufff",
"{a:\"\\uffff",
"{a:\"\\uffff\"",
"{a:\"\\uffff\" ,",
"{a:n",
"{a:nu",
"{a:nul",
"{a:null",
NULL};
/* clang-format on */
static const struct {
const char *str;
int expected_len;
} success_tests[] = {{"{}", 2},
/* 2, 3, 4 byte utf-8 chars */
{"{a:\"\xd0\xb1\xe3\x81\xaf\xf0\xa2\xb3\x82\"}", 15},
{"{a:\"\\u0006\"}", 12},
{" { } ", 4},
{"{a:1}", 5},
{"{a:1.23}", 8},
{"{a:1e23}", 8},
{"{a:1.23e2}", 10},
{"{a:-123}", 8},
{"{a:-1.3}", 8},
{"{a:-1.3e-2}", 11},
{"{a:\"\"}", 6},
{"{a:\" \\n\\t\\r\"}", 13},
{" {a:[1]} 123456", 8},
{" {a:[]} 123456", 7},
{" {a:[1,2]} 123456", 10},
{"{a:1,b:2} xxxx", 9},
{"{a:1,b:{},c:[{}]} xxxx", 17},
{"{a:true,b:[false,null]} xxxx", 23},
{"[1.23, 3, 5]", 12},
{"[13, {\"a\":\"hi there\"}, 5]", 25},
{NULL, 0}};
const char *s1 =
" { a: 1, b: \"hi there\", c: true, d: false, "
" e : null, f: [ 1, -2, 3], g: { \"1\": [], h: [ 7 ] } } ";
int i;
ASSERT(json_walk(NULL, 0, NULL, 0) == JSON_STRING_INVALID);
for (i = 0; invalid_tests[i] != NULL; i++) {
ASSERT(json_walk(invalid_tests[i], strlen(invalid_tests[i]), NULL, NULL) ==
JSON_STRING_INVALID);
}
for (i = 0; incomplete_tests[i] != NULL; i++) {
ASSERT(json_walk(incomplete_tests[i], strlen(incomplete_tests[i]), NULL,
NULL) == JSON_STRING_INCOMPLETE);
}
for (i = 0; success_tests[i].str != NULL; i++) {
ASSERT(json_walk(success_tests[i].str, strlen(success_tests[i].str), NULL,
NULL) == success_tests[i].expected_len);
}
ASSERT(json_walk("{}", 2, NULL, NULL) == 2);
ASSERT(json_walk(s1, strlen(s1), NULL, 0) > 0);
return NULL;
}
struct my_struct {
int a, b;
};
static int print_my_struct(struct json_out *out, va_list *ap) {
struct my_struct *p = va_arg(*ap, struct my_struct *);
return json_printf(out, "{a: %d, b: %d}", p->a, p->b);
}
static const char *test_json_printf(void) {
char buf[200] = "";
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "42 42";
json_printf(&out, "%ld %d", 42, 42);
ASSERT(strcmp(buf, result) == 0);
}
/* platform specific compatibility where it matters */
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "16045690985373621933 42";
json_printf(&out, "%" UINT64_FMT " %d", 0xdeadbeeffee1deadUL, 42);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "12 42";
size_t foo = 12;
json_printf(&out, "%lu %d", foo, 42);
ASSERT(strcmp(buf, result) == 0);
}
/* people live in the future today, %llu works even on recent windozes */
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "16045690985373621933 42";
json_printf(&out, "%llu %d", 0xdeadbeeffee1deadUL, 42);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "12 42";
size_t foo = 12;
json_printf(&out, "%zu %d", foo, 42);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "{\"foo\": 123, \"x\": [false, true], \"y\": \"hi\"}";
json_printf(&out, "{%Q: %d, x: [%B, %B], y: %Q}", "foo", 123, 0, -1, "hi");
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
int arr[] = {-2387, 943478};
json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
"%d");
ASSERT(strcmp(buf, "[-2387, 943478]") == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
double arr[] = {9.32156, 3.1415926};
json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
"%.2lf");
ASSERT(strcmp(buf, "[9.32, 3.14]") == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
unsigned short arr[] = {65535, 777};
const char *result = "{\"a\": [-1, 777], \"b\": 37}";
json_printf(&out, "{a: %M, b: %d}", json_printf_array, arr, sizeof(arr),
sizeof(arr[0]), "%hd", 37);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *arr[] = {"hi", "there", NULL};
const char *result = "[\"hi\", \"there\", null]";
json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
"%Q");
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "{\"a\": \"\\\"\\\\\\r\\nя\\t\\u0002\"}";
json_printf(&out, "{a: %Q}", "\"\\\r\nя\t\x02");
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
struct my_struct mys = {1, 2};
const char *result = "{\"foo\": {\"a\": 1, \"b\": 2}, \"bar\": 3}";
json_printf(&out, "{foo: %M, bar: %d}", print_my_struct, &mys, 3);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
out.u.buf.size = 3;
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "{%d}", 123) == 5);
ASSERT(memcmp(buf, "{1\x00\x00\x00", 5) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "\"foo\"";
out.u.buf.size = 6;
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "%.*Q", 3, "foobar") == 5);
ASSERT(memcmp(buf, result, 5) == 0);
}
{
/*
* Check long string (which forces frozen to allocate a temporary buffer
* from heap)
*/
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result =
"{\"foo\": "
"\"12345678901234567890123456789012345678901234567890123456789012345678"
"90123456789012345678901234567890\"}";
const char *s =
"\"123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890\"";
json_printf(&out, "{foo: %s}", s);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *fmt = "{a: \"%s\"}", *result = "{\"a\": \"b\"}";
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, fmt, "b") > 0);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "%.*s %d", 2, "abc", 5) > 0);
ASSERT(strcmp(buf, "ab 5") == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "\"a_b0\": 1";
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "a_b0: %d", 1) > 0);
ASSERT(strcmp(buf, result) == 0);
}
#if JSON_ENABLE_BASE64
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "\"YTI=\"";
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "%V", "a2", 2) > 0);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "\"ACABIAIgYWJj\"";
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "%V", "\x00 \x01 \x02 abc", 9) > 0);
ASSERT(strcmp(buf, result) == 0);
}
#endif /* JSON_ENABLE_BASE64 */
#if JSON_ENABLE_HEX
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "\"002001200220616263\"";
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "%H", 9, "\x00 \x01 \x02 abc") > 0);
ASSERT(strcmp(buf, result) == 0);
}
#endif /* JSON_ENABLE_HEX */
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "%c", 0x53) > 0);
ASSERT(strcmp(buf, "S") == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *result = "<\"array\">0f";
memset(buf, 0, sizeof(buf));
ASSERT(json_printf(&out, "<array>%02x", 15) > 0);
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
double arr[] = {9.32156, 3.1415926};
#if !defined(_MSC_VER) || _MSC_VER >= 1700
const char *result = "[9.32e+00, 3.14e+00]"; // Modern compilers
#else
const char *result = "[9.32e+000, 3.14e+000]"; // Old VC98 compiler
#endif
json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
"%.2e");
ASSERT(strcmp(buf, result) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
double arr[] = {9.32156, 3.1415926};
json_printf(&out, "%M", json_printf_array, arr, sizeof(arr), sizeof(arr[0]),
"%.4g");
ASSERT(strcmp(buf, "[9.322, 3.142]") == 0);
}
return NULL;
}
static const char *test_system(void) {
char buf[2020];
uint64_t u = (uint64_t) 0xdeadbeeffee1dead;
int64_t d = (int64_t) u;
int res;
snprintf(buf, sizeof(buf), "%" UINT64_FMT, u);
ASSERT(strcmp(buf, "16045690985373621933") == 0);
snprintf(buf, sizeof(buf), "%" INT64_FMT, d);
ASSERT(strcmp(buf, "-2401053088335929683") == 0);
res = snprintf(buf, 3, "foo");
ASSERT(res == 3);
ASSERT(buf[0] == 'f');
ASSERT(buf[1] == 'o');
ASSERT(buf[2] == '\0');
return NULL;
}
static void cb(void *data, const char *name, size_t name_len, const char *path,
const struct json_token *token) {
char *buf = (char *) data;
const char *snull = "<null>";
sprintf(buf + strlen(buf), "name:'%.*s', path:'%s', type:%s, val:'%.*s'\n",
(int) (name != NULL ? name_len : strlen(snull)),
name != NULL ? name : snull, path, tok_type_names[token->type],
(int) (token->ptr != NULL ? token->len : (int) strlen(snull)),
token->ptr != NULL ? token->ptr : snull);
}
static const char *test_callback_api(void) {
const char *s =
"{\"c\":[\"foo\", \"bar\", {\"a\":9, \"b\": \"x\"}], "
"\"mynull\": null, \"mytrue\": true, \"myfalse\": false}";
const char *result =
"name:'<null>', path:'', type:OBJECT_START, val:'<null>'\n"
"name:'c', path:'.c', type:ARRAY_START, val:'<null>'\n"
"name:'0', path:'.c[0]', type:STRING, val:'foo'\n"
"name:'1', path:'.c[1]', type:STRING, val:'bar'\n"
"name:'2', path:'.c[2]', type:OBJECT_START, val:'<null>'\n"
"name:'a', path:'.c[2].a', type:NUMBER, val:'9'\n"
"name:'b', path:'.c[2].b', type:STRING, val:'x'\n"
"name:'<null>', path:'.c[2]', type:OBJECT_END, val:'{\"a\":9, \"b\": "
"\"x\"}'\n"
"name:'<null>', path:'.c', type:ARRAY_END, val:'[\"foo\", \"bar\", "
"{\"a\":9, \"b\": \"x\"}]'\n"
"name:'mynull', path:'.mynull', type:NULL, val:'null'\n"
"name:'mytrue', path:'.mytrue', type:TRUE, val:'true'\n"
"name:'myfalse', path:'.myfalse', type:FALSE, val:'false'\n"
"name:'<null>', path:'', type:OBJECT_END, val:'{\"c\":[\"foo\", \"bar\", "
"{\"a\":9, \"b\": \"x\"}], \"mynull\": null, \"mytrue\": true, "
"\"myfalse\": false}'\n";
char buf[4096] = "";
ASSERT(json_walk(s, strlen(s), cb, buf) == (int) strlen(s));
ASSERT(strcmp(buf, result) == 0);
return NULL;
}
/*
* Tests with the path which is longer than JSON_MAX_PATH_LEN (at the moment,
* 60)
*/
static const char *test_callback_api_long_path(void) {
const char *s =
"{\"MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZvf\": {}, \"jYP-27917287424p\": {}}";
const char *result =
"name:'<null>', path:'', type:OBJECT_START, val:'<null>'\n"
"name:'"
"MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZvf', "
"path:'."
"MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', "
"type:OBJECT_START, val:'<null>'\n"
"name:'<null>', "
"path:'."
"MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', "
"type:OBJECT_END, val:'{}'\n"
"name:'jYP-27917287424p', path:'.jYP-27917287424p', type:OBJECT_START, "
"val:'<null>'\n"
"name:'<null>', path:'.jYP-27917287424p', type:OBJECT_END, val:'{}'\n"
"name:'<null>', path:'', type:OBJECT_END, "
"val:'{"
"\"MyWZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZvf\": {}, \"jYP-27917287424p\": {}}'\n";
char buf[4096] = "";
ASSERT(json_walk(s, strlen(s), cb, buf) == (int) strlen(s));
ASSERT(strcmp(buf, result) == 0);
return NULL;
}
static void scan_array(const char *str, int len, void *user_data) {
struct json_token t;
int i;
char *buf = (char *) user_data;
for (i = 0; json_scanf_array_elem(str, len, ".x", i, &t) > 0; i++) {
sprintf(buf + strlen(buf), "%d[%.*s] ", i, t.len, t.ptr);
}
}
static const char *test_scanf(void) {
char buf[100] = "";
int a = 0, b = 0;
char *d = NULL;
const char *str =
"{ a: 1234, b : true, \"c\": {x: [17, 78, -20]}, d: \"hi%20there\" }";
ASSERT(json_scanf(str, strlen(str), "{a: %d, b: %B, c: [%M], d: %Q}", &a, &b,
&scan_array, buf, &d) == 4);
ASSERT(a == 1234);
ASSERT(b == 1);
ASSERT(strcmp(buf, "0[17] 1[78] 2[-20] ") == 0);
ASSERT(d != NULL);
ASSERT(strcmp(d, "hi%20there") == 0);
free(d);
{
const char* str = "{a:{b:4},c:5}";
int b = 0, c=0;
ASSERT(json_scanf(str, strlen(str), "{a:{b:%d},c:%d}", &b, &c) == 2);
ASSERT(b == 4);
ASSERT(c == 5);
}
{
const char* str = "{a:{b:4},c:\"abc\"}";
int b = 0;
char* c = NULL;
ASSERT(json_scanf(str, strlen(str), "{a:{b:%d},c:%Q}", &b, &c) == 2);
ASSERT(b == 4);
ASSERT(strcmp(c, "abc") == 0);
free(c);
}
{
const char* str = "{a:{b:{c:4}},d:\"abc\"}";
int c = 0;
char* d = NULL;
ASSERT(json_scanf(str, strlen(str), "{a:{b:{c:%d}},d:%Q}", &c, &d) == 2);
ASSERT(c == 4);
ASSERT(strcmp(d, "abc") == 0);
free(d);
}
{
const char* str = "{a:{b:{c:4},d:5},e:\"abc\"}";
int c=0,d = 0;
char* e = NULL;
ASSERT(json_scanf(str, strlen(str), "{a:{b:{c:%d},d:%d},e:%Q}", &c, &d, &e) == 3);
ASSERT(c == 4);
ASSERT(d == 5);
ASSERT(strcmp(e, "abc") == 0);
free(e);
}
{
/* Test errors */
const char *str = "{foo:1, bar:[2,3,4]}";
size_t i;
ASSERT(json_walk(str, strlen(str), NULL, NULL) == (int) strlen(str));
for (i = 1; i < strlen(str); i++) {
ASSERT(json_walk(str, i, NULL, NULL) == JSON_STRING_INCOMPLETE);
}
}
{
/* Test that paths are utf8 */
const char *str = "{\"ы\": 123}";
int x = 0;
ASSERT(json_scanf(str, strlen(str), "{ы: %d}", &x) == 1);
ASSERT(x == 123);
}
{
/* Test that paths are utf8 */
const char *str = "{a: 123, b: [1,2,3]}";
struct json_token t;
memset(&t, 0, sizeof(t));
ASSERT(json_scanf(str, strlen(str), "{b: %T}", &t) == 1);
ASSERT(t.type == JSON_TYPE_ARRAY_END);
ASSERT(t.len == 7);
ASSERT(strncmp(t.ptr, "[1,2,3]", t.len) == 0);
}
{
/* Test zero termination */
char *s = NULL;
const char *str = "{a: \"foo\", b:123}";
ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &s) == 1);
ASSERT(s != NULL);
ASSERT(s[3] == '\0');
free(s);
}
{
/* Test for scalar value being a root element */
int n = 0;
const char *str = " true ";
ASSERT(json_scanf(str, strlen(str), " %B ", &n) == 1);
ASSERT(n == 1);
}
{
/* Test array of objects */
const char *str = " { \"a\": [ {\"b\": 123}, {\"b\": 345} ]} ";
int i, value, len = strlen(str), values[] = {123, 345};
struct json_token t;
/* Scan each array element into a token */
for (i = 0; json_scanf_array_elem(str, len, ".a", i, &t) > 0; i++) {
/* Now scan each token */
ASSERT(t.type == JSON_TYPE_OBJECT_END);
ASSERT(json_scanf(t.ptr, t.len, "{b: %d}", &value) == 1);
ASSERT((size_t) i < sizeof(values) / sizeof(values[0]));
ASSERT(values[i] == value);
}
ASSERT(i == 2);
}
{
const char *str = "{a : [\"foo\", \"\", \"a\"] }";
struct json_token t;
ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 0, &t) == 3);
ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 1, &t) == 0);
ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 2, &t) == 1);
ASSERT(json_scanf_array_elem(str, strlen(str), ".a", 3, &t) == -1);
}
{
const char *str = "{a : \"foo\\b\\f\\n\\r\\t\\\\\" }";
char *result;
ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 1);
ASSERT(strcmp(result, "foo\b\f\n\r\t\\") == 0);
free(result);
ASSERT(json_scanf(str, 9, "{a: %Q}", &result) == 0);
}
{
const char *str = "{a : \"привет\" }";
char *result;
ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 1);
ASSERT(strcmp(result, "привет") == 0);
free(result);
}
#if JSON_ENABLE_BASE64
{
const char *str = "{a : \"YTI=\" }";
int len;
char *result;
ASSERT(json_scanf(str, strlen(str), "{a: %V}", &result, &len) == 1);
ASSERT(len == 2);
ASSERT(strcmp(result, "a2") == 0);
free(result);
}
{
const char *str = "{a : \"0L/RgNC40LLQtdGC0Ys=\" }";
int len;
char *result;
ASSERT(json_scanf(str, strlen(str), "{a: %V}", &result, &len) == 1);
ASSERT(len == 14);
ASSERT(strcmp(result, "приветы") == 0);
free(result);
}
#endif /* JSON_ENABLE_BASE64 */
#if JSON_ENABLE_HEX
{
const char *str = "{a : \"61626320\" }";
int len = 0;
char *result = NULL;
ASSERT(json_scanf(str, strlen(str), "{a: %H}", &len, &result) == 1);
ASSERT(len == 4);
ASSERT(strcmp(result, "abc ") == 0);
free(result);
}
#endif /* JSON_ENABLE_HEX */
{
const char *str = "{a : null }";
char *result = (char *) 123;
ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 0);
ASSERT(result == NULL);
free(result);
}
{
int a = 0;
bool b = false;
int c = 0xFFFFFFFF;
const char *str = "{\"b\":true,\"c\":false,\"a\":2}";
ASSERT(json_scanf(str, strlen(str), "{a:%d, b:%B, c:%B}", &a, &b, &c) == 3);
ASSERT(a == 2);
ASSERT(b == true);
if (sizeof(bool) == 1) {
ASSERT((char) c == false);
} else {
ASSERT(c == false);
}
}
{
const char *str = "{ fa: 1, fb: 2.34, fc: 5.67 }";
const char *fmt = "{fa: %f, fb: %f, fc: %lf}";
float fa = 0.0, fb = 0.0;
double fc = 0.0;
#if !JSON_MINIMAL
float a = 1.0f, b = 2.34f;
double c = 5.67;
ASSERT(json_scanf(str, strlen(str), fmt, &fa, &fb, &fc) == 3);
ASSERT(fa == a);
ASSERT(fb == b);
ASSERT(fabs(fc - c) < FLT_EPSILON);
#else
ASSERT(json_scanf(str, strlen(str), fmt, &fa, &fb, &fc) == 0);
#endif
}
{
int v = -1;
long lv = -1;
const char *s = "{\"v\": 0x12, \"lv\": 0x34}";
ASSERT(json_scanf("0x", 2, "%i", &v) == 0); // Incomplete string
ASSERT(json_scanf("0xe", 3, "%i", &v) == 1);
ASSERT(v == 0xe);
ASSERT(json_scanf("12", 2, "%i", &v) == 1);
ASSERT(v == 12);
// %d and %ld accept hex
ASSERT(json_scanf(s, strlen(s), "{lv:%ld, v:%d}", &lv, &v) == 2);
ASSERT(v == 0x12);
ASSERT(lv == 0x34);
}
{
unsigned int v = 0;
unsigned long lv = 0;
const char *s = "{\"v\": 0x12, \"lv\": 0x34}";
// %u and %lu accept hex
ASSERT(json_scanf(s, strlen(s), "{lv:%lu, v:%u}", &lv, &v) == 2);
ASSERT(v == 0x12);
ASSERT(lv == 0x34);
}
return NULL;
}
static const char *test_json_unescape(void) {
char buf[1];
ASSERT(json_unescape("foo", 3, NULL, 0) == 3);
ASSERT(json_unescape("foo\\", 4, NULL, 0) == JSON_STRING_INCOMPLETE);
ASSERT(json_unescape("foo\\x", 5, NULL, 0) == JSON_STRING_INVALID);
ASSERT(json_unescape("\\ueeee", 5, NULL, 0) == JSON_STRING_INCOMPLETE);
ASSERT(json_unescape("\\ueeee", 6, NULL, 0) == JSON_STRING_INVALID);
// Simple one-byte escapes should work
ASSERT(json_unescape("\\u0026", 2, NULL, 0) == JSON_STRING_INCOMPLETE);
ASSERT(json_unescape("\\u0026", 3, NULL, 0) == JSON_STRING_INCOMPLETE);
ASSERT(json_unescape("\\u0026", 4, NULL, 0) == JSON_STRING_INCOMPLETE);
ASSERT(json_unescape("\\u0026", 5, NULL, 0) == JSON_STRING_INCOMPLETE);
ASSERT(json_unescape("\\u0026", 6, buf, sizeof(buf)) == 1);
ASSERT(buf[0] == '&');
return NULL;
}
static void cb2(void *data, const char *name, size_t name_len, const char *path,
const struct json_token *token) {
struct json_token *pt = (struct json_token *) data;
pt->ptr = token->ptr;
pt->len = token->len;
(void) path;
(void) name_len;
(void) name;
}
static const char *test_parse_string(void) {
const char *str = " \" foo\\bar\"";
const int str_len = strlen(str);
struct json_token t;
struct frozen f;
memset(&f, 0, sizeof(f));
f.end = str + str_len;
f.cur = str;
f.callback_data = (void *) &t;
f.callback = cb2;
ASSERT(json_parse_string(&f) == 0);
ASSERT(strncmp(t.ptr, " foo\\bar", t.len) == 0);
return NULL;
}
static const char *test_eos(void) {
const char *s = "{\"a\": 12345}";
size_t n = 999;
char *buf = (char *) malloc(n);
int s_len = strlen(s), a = 0;
ASSERT(buf != NULL);
memset(buf, 'x', n);
memcpy(buf, s, s_len);
ASSERT(json_scanf(buf, n, "{a:%d}", &a) == 1);
ASSERT(a == 12345);
free(buf);
return NULL;
}
static int compare_file(const char *file_name, const char *s) {
int res = -1;
char *p = json_fread(file_name);
if (p == NULL) return res;
res = strcmp(p, s);
free(p);
return res == 0;
}
static const char *test_fprintf(void) {
const char *fname = "a.json";
const char *result = "{\"a\":123}\n";
char *p;
ASSERT(json_fprintf(fname, "{a:%d}", 123) > 0);
p = json_fread(fname);
ASSERT(p != NULL);
ASSERT(strcmp(p, result) == 0);
free(p);
remove(fname);
ASSERT(json_fread(fname) == NULL);
return NULL;
}
static const char *test_json_setf(void) {
char buf[200];
const char *s1 = "{ \"a\": 123, \"b\": [ 1 ], \"c\": true }";
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"a\": 7, \"b\": [ 1 ], \"c\": true }";
int res = json_setf(s1, strlen(s1), &out, ".a", "%d", 7);
ASSERT(res == 1);
ASSERT(strcmp(buf, s2) == 0);
}
{
/* Add Key with length > 1 */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 =
"{ \"a\": 123, \"b\": [ 1 ], \"c\": true,\"foo\":{\"bar\":42} }";
int res = json_setf(s1, strlen(s1), &out, ".foo.bar", "%d", 42);
ASSERT(res == 0);
ASSERT(strcmp(buf, s2) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"a\": 123, \"b\": false, \"c\": true }";
int res = json_setf(s1, strlen(s1), &out, ".b", "%B", 0);
ASSERT(res == 1);
ASSERT(strcmp(buf, s2) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"a\": 123, \"b\": [ 2 ], \"c\": true }";
int res = json_setf(s1, strlen(s1), &out, ".b[0]", "%d", 2);
ASSERT(res == 1);
ASSERT(strcmp(buf, s2) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"b\": [ 1 ], \"c\": true }";
int res = json_setf(s1, strlen(s1), &out, ".a", NULL);
ASSERT(res == 1);
ASSERT(strcmp(buf, s2) == 0);
}
{
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"a\": 123, \"b\": [ 1 ] }";
int res = json_setf(s1, strlen(s1), &out, ".c", NULL);
ASSERT(res == 1);
ASSERT(strcmp(buf, s2) == 0);
}
{
/* Delete non-existent key */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s1 = "{\"a\":1}";
int res = json_setf(s1, strlen(s1), &out, ".d", NULL);
ASSERT(res == 0);
ASSERT(strcmp(buf, s1) == 0);
}
{
/* Delete non-existent key, spaces in obj */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
int res = json_setf(s1, strlen(s1), &out, ".d", NULL);
ASSERT(res == 0);
ASSERT(strcmp(buf, s1) == 0);
}
{
/* Change the whole JSON object */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "123";
int res = json_setf(s1, strlen(s1), &out, "", "%d", 123);
ASSERT(res == 1);
ASSERT(strcmp(buf, s2) == 0);
}
{
/* Add missing keys */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 =
"{ \"a\": 123, \"b\": [ 1 ], \"c\": true,\"d\":{\"e\":8} }";
int res = json_setf(s1, strlen(s1), &out, ".d.e", "%d", 8);
ASSERT(res == 0);
ASSERT(strcmp(buf, s2) == 0);
}
{
/* Append to arrays */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"a\": 123, \"b\": [ 1,2 ], \"c\": true }";
int res = json_setf(s1, strlen(s1), &out, ".b[]", "%d", 2);
ASSERT(res == 0);
ASSERT(strcmp(buf, s2) == 0);
}
{
/* Delete from array */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"a\": 123, \"b\": [ ], \"c\": true }";
int res = json_setf(s1, strlen(s1), &out, ".b[0]", NULL);
ASSERT(res == 1);
ASSERT(strcmp(buf, s2) == 0);
}
{
/* Create array and push value */
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 = "{ \"a\": 123, \"b\": [ 1 ], \"c\": true,\"d\":[3] }";
int res = json_setf(s1, strlen(s1), &out, ".d[]", "%d", 3);
// printf("[%s]\n[%s]\n", buf, s2);
ASSERT(res == 0);
ASSERT(strcmp(buf, s2) == 0);
}
return NULL;
}
static const char *test_prettify(void) {
const char *fname = "a.json";
char buf[200];
{
const char *s1 = "{ \"a\": 1, \"b\":2,\"c\":[null,\"aa\",{},true]}";
struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
const char *s2 =
"{\n \"a\": 1,\n \"b\": 2,\n \"c\": [\n null,\n \"aa\",\n "
"{},\n true\n ]\n}";
ASSERT(json_prettify(s1, strlen(s1), &out) > 0);
ASSERT(strcmp(buf, s2) == 0);
}
{
remove(fname);
ASSERT(json_prettify_file(fname) == -1);
}
{
ASSERT(compare_file(fname, "") == -1);
json_fprintf(fname, "::");
ASSERT(json_prettify_file(fname) == JSON_STRING_INVALID);
ASSERT(compare_file(fname, "::\n"));
remove(fname);
}
{
ASSERT(compare_file(fname, "") == -1);
json_fprintf(fname, "{");
ASSERT(json_prettify_file(fname) == JSON_STRING_INCOMPLETE);
ASSERT(compare_file(fname, "{\n"));
remove(fname);
}
{
ASSERT(compare_file(fname, "") == -1);
json_fprintf(fname, "%d", 123);
ASSERT(compare_file(fname, "123\n"));
ASSERT(json_prettify_file(fname) > 0);
ASSERT(compare_file(fname, "123\n"));
remove(fname);
}
{
const char *s = "{\n \"a\": 123\n}\n";
ASSERT(compare_file(fname, "") == -1);
json_fprintf(fname, "{a:%d}", 123);
ASSERT(json_prettify_file(fname) > 0);
ASSERT(compare_file(fname, s));
(void) remove(fname);
}
return NULL;
}
static const char *test_json_next(void) {
struct json_token key, val;
char buf[100];
{
/* Traverse an object */
void *h = NULL;
int i = 0;
const char *s = "{ \"a\": [], \"b\": [ 1, {} ], \"c\": true }";
int len = strlen(s);