forked from kaidokert/minijson_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminijson_reader_tests.cpp
More file actions
1709 lines (1453 loc) · 63.7 KB
/
minijson_reader_tests.cpp
File metadata and controls
1709 lines (1453 loc) · 63.7 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
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
#include "minijson_reader.hpp"
#include <gtest/gtest.h>
#include <bitset>
template<typename T, size_t Size>
bool arrays_match(const T (&expected)[Size], const T (&actual)[Size])
{
return std::equal(expected, expected + Size, actual);
}
template<typename Context>
void test_context_helper(Context& context)
{
bool loop = true;
while (loop)
{
switch (context.read_offset())
{
case 0: ASSERT_EQ('h', context.read()); context.write('H'); break;
case 1: ASSERT_EQ('e', context.read()); context.write('e'); break;
case 2: ASSERT_EQ('l', context.read()); context.write('l'); break;
case 3: ASSERT_EQ('l', context.read()); context.write('l'); break;
case 4: ASSERT_EQ('o', context.read()); context.write('o'); break;
case 5: ASSERT_EQ(' ', context.read()); context.write(0); ASSERT_STREQ("Hello", context.write_buffer()); context.new_write_buffer(); break;
case 6: ASSERT_EQ('w', context.read()); context.write('W'); break;
case 7: ASSERT_EQ('o', context.read()); context.write('o'); break;
case 8: ASSERT_EQ('r', context.read()); context.write('r'); break;
case 9: ASSERT_EQ('l', context.read()); context.write('l'); break;
case 10: ASSERT_EQ('d', context.read()); context.write('d'); break;
case 11: ASSERT_EQ('.', context.read()); context.write(0); break;
case 12: ASSERT_EQ(0, context.read()); loop = false; break;
}
}
ASSERT_EQ(0, context.read());
ASSERT_EQ(12U, context.read_offset());
ASSERT_STREQ("World", context.write_buffer());
ASSERT_EQ(minijson::detail::context_base::NESTED_STATUS_NONE, context.nested_status());
context.begin_nested(minijson::detail::context_base::NESTED_STATUS_OBJECT);
ASSERT_EQ(minijson::detail::context_base::NESTED_STATUS_OBJECT, context.nested_status());
ASSERT_EQ(1U, context.nesting_level());
context.begin_nested(minijson::detail::context_base::NESTED_STATUS_ARRAY);
ASSERT_EQ(minijson::detail::context_base::NESTED_STATUS_ARRAY, context.nested_status());
ASSERT_EQ(2U, context.nesting_level());
context.end_nested();
ASSERT_EQ(1U, context.nesting_level());
context.end_nested();
ASSERT_EQ(0U, context.nesting_level());
context.reset_nested_status();
ASSERT_EQ(minijson::detail::context_base::NESTED_STATUS_NONE, context.nested_status());
}
TEST(minijson_reader, buffer_context)
{
char buffer[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '.' };
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
test_context_helper(buffer_context);
ASSERT_STREQ("Hello", buffer);
ASSERT_STREQ("World", buffer + 6);
ASSERT_THROW(buffer_context.write('x'), std::runtime_error);
buffer_context.new_write_buffer();
ASSERT_EQ(buffer + sizeof(buffer), buffer_context.write_buffer());
ASSERT_THROW(buffer_context.write('x'), std::runtime_error);
}
TEST(minijson_reader, const_buffer_context)
{
const char buffer[] = "hello world.";
minijson::const_buffer_context const_buffer_context(buffer, sizeof(buffer) - 1);
const char* const original_write_buffer = const_buffer_context.write_buffer();
test_context_helper(const_buffer_context);
ASSERT_STREQ("hello world.", buffer); // no side effects
ASSERT_THROW(const_buffer_context.write('x'), std::runtime_error);
const_buffer_context.new_write_buffer();
ASSERT_EQ(original_write_buffer + strlen(buffer), const_buffer_context.write_buffer());
ASSERT_THROW(const_buffer_context.write('x'), std::runtime_error);
}
TEST(minijson_reader, istream_context)
{
std::istringstream buffer("hello world.");
minijson::istream_context istream_context(buffer);
test_context_helper(istream_context);
}
template<typename Context>
void test_context_copy_construction_helper(const Context& original)
{
Context copy(original);
(void)copy;
}
TEST(minijson_reader, context_no_copy_construction)
{
std::istringstream ss;
// this test is compile-time only: uncommenting any of the following lines should cause a compile error
//test_context_copy_construction_helper(minijson::buffer_context(NULL, 0));
//test_context_copy_construction_helper(minijson::const_buffer_context(NULL, 0));
//test_context_copy_construction_helper(minijson::istream_context(ss));
(void)ss;
}
template<typename Context>
void test_context_copy_assignment_helper(const Context& original, Context& copy)
{
copy = original;
}
TEST(minijson_reader, context_no_copy_assignment)
{
std::istringstream ss;
minijson::buffer_context buffer_context(NULL, 0);
minijson::const_buffer_context const_buffer_context(NULL, 0);
minijson::istream_context istream_context(ss);
// this test is compile-time only: uncommenting any of the following lines should cause a compile error
//test_context_copy_assignment_helper(minijson::buffer_context(NULL, 0), buffer_context);
//test_context_copy_assignment_helper(minijson::const_buffer_context(NULL, 0), const_buffer_context);
//test_context_copy_assignment_helper(minijson::istream_context(ss), istream_context);
(void)ss;
(void)buffer_context;
(void)const_buffer_context;
(void)istream_context;
}
TEST(minijson_reader, parse_error)
{
{
minijson::buffer_context buffer_context(NULL, 0);
minijson::parse_error parse_error(buffer_context, minijson::parse_error::UNKNOWN);
ASSERT_EQ(0U, parse_error.offset());
ASSERT_EQ(minijson::parse_error::UNKNOWN, parse_error.reason());
ASSERT_STREQ("Unknown parse error", parse_error.what());
}
{
const char buffer[] = "hello world.";
minijson::const_buffer_context const_buffer_context(buffer, sizeof(buffer) - 1);
const_buffer_context.read();
const_buffer_context.read();
ASSERT_EQ(2U, const_buffer_context.read_offset());
minijson::parse_error parse_error(const_buffer_context, minijson::parse_error::UNKNOWN);
ASSERT_EQ(1U, parse_error.offset());
ASSERT_EQ(minijson::parse_error::UNKNOWN, parse_error.reason());
ASSERT_STREQ("Unknown parse error", parse_error.what());
}
}
TEST(minijson_reader_detail, utf8_quad)
{
minijson::detail::utf8_char utf8_quad;
ASSERT_EQ(0U, utf8_quad[0]);
ASSERT_EQ(0U, utf8_quad[1]);
ASSERT_EQ(0U, utf8_quad[2]);
ASSERT_EQ(0U, utf8_quad[3]);
const minijson::detail::utf8_char utf8_quad1(0, 1, 2, 3);
ASSERT_EQ(0U, utf8_quad1[0]);
ASSERT_EQ(1U, utf8_quad1[1]);
ASSERT_EQ(2U, utf8_quad1[2]);
ASSERT_EQ(3U, utf8_quad1[3]);
minijson::detail::utf8_char utf8_quad2;
ASSERT_TRUE(utf8_quad == utf8_quad2);
ASSERT_TRUE(utf8_quad != utf8_quad1);
ASSERT_FALSE(utf8_quad != utf8_quad2);
ASSERT_FALSE(utf8_quad == utf8_quad1);
}
TEST(minijson_reader_detail, utf16_to_utf32)
{
// code points 0000 to D7FF and E000 to FFFF
ASSERT_EQ(0x000000u, minijson::detail::utf16_to_utf32(0x0000, 0x0000));
ASSERT_EQ(0x000001u, minijson::detail::utf16_to_utf32(0x0001, 0x0000));
ASSERT_EQ(0x00D7FEu, minijson::detail::utf16_to_utf32(0xD7FE, 0x0000));
ASSERT_EQ(0x00D7FFu, minijson::detail::utf16_to_utf32(0xD7FF, 0x0000));
ASSERT_EQ(0x00E000u, minijson::detail::utf16_to_utf32(0xE000, 0x0000));
ASSERT_EQ(0x00FFFFu, minijson::detail::utf16_to_utf32(0xFFFF, 0x0000));
// code points 010000 to 10FFFF
ASSERT_EQ(0x010000u, minijson::detail::utf16_to_utf32(0xD800, 0xDC00));
ASSERT_EQ(0x010001u, minijson::detail::utf16_to_utf32(0xD800, 0xDC01));
ASSERT_EQ(0x10FFFEu, minijson::detail::utf16_to_utf32(0xDBFF, 0xDFFE));
ASSERT_EQ(0x10FFFFu, minijson::detail::utf16_to_utf32(0xDBFF, 0xDFFF));
}
TEST(minijson_reader_detail, utf16_to_utf32_invalid)
{
ASSERT_THROW(minijson::detail::utf16_to_utf32(0x0000, 0x0001), minijson::detail::encoding_error);
ASSERT_THROW(minijson::detail::utf16_to_utf32(0xD800, 0xDBFF), minijson::detail::encoding_error);
ASSERT_THROW(minijson::detail::utf16_to_utf32(0xD800, 0xE000), minijson::detail::encoding_error);
ASSERT_THROW(minijson::detail::utf16_to_utf32(0xDC00, 0xDC00), minijson::detail::encoding_error);
}
TEST(minijson_reader_detail, utf32_to_utf8)
{
// 1 byte
{
const uint8_t expected[] = { 0x00, 0x00, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x000000).bytes));
}
{
const uint8_t expected[] = { 0x01, 0x00, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x000001).bytes));
}
{
const uint8_t expected[] = { 0x7E, 0x00, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x00007E).bytes));
}
{
const uint8_t expected[] = { 0x7F, 0x00, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x00007F).bytes));
}
// 2 bytes
{
const uint8_t expected[] = { 0xC2, 0x80, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x000080).bytes));
}
{
const uint8_t expected[] = { 0xC2, 0x81, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x000081).bytes));
}
{
const uint8_t expected[] = { 0xDF, 0xBE, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x0007FE).bytes));
}
{
const uint8_t expected[] = { 0xDF, 0xBF, 0x00, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x0007FF).bytes));
}
// 3 bytes
{
const uint8_t expected[] = { 0xE0, 0xA0, 0x80, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x000800).bytes));
}
{
const uint8_t expected[] = { 0xE0, 0xA0, 0x81, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x000801).bytes));
}
{
const uint8_t expected[] = { 0xEF, 0xBF, 0xBE, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x00FFFE).bytes));
}
{
const uint8_t expected[] = { 0xEF, 0xBF, 0xBF, 0x00 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x00FFFF).bytes));
}
// 4 bytes
{
const uint8_t expected[] = { 0xF0, 0x90, 0x80, 0x80 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x010000).bytes));
}
{
const uint8_t expected[] = { 0xF0, 0x90, 0x80, 0x81 };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x010001).bytes));
}
{
const uint8_t expected[] = { 0xF7, 0xBF, 0xBF, 0xBE };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x1FFFFE).bytes));
}
{
const uint8_t expected[] = { 0xF7, 0xBF, 0xBF, 0xBF };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf32_to_utf8(0x1FFFFF).bytes));
}
}
TEST(minijson_reader_detail, utf32_to_utf8_invalid)
{
// invalid code unit
ASSERT_THROW(minijson::detail::utf32_to_utf8(0x200000), minijson::detail::encoding_error);
}
TEST(minijson_reader_detail, utf16_to_utf8)
{
// Just one test case, since utf16_to_utf8 calls utf16_to_utf32 and utf32_to_utf8,
// and other cases have been covered by previous tests
const uint8_t expected[] = { 0xF4, 0x8F, 0xBF, 0xBF };
ASSERT_TRUE(arrays_match(expected, minijson::detail::utf16_to_utf8(0xDBFF, 0xDFFF).bytes));
}
TEST(minijson_reader_detail, parse_long)
{
ASSERT_EQ(0, minijson::detail::parse_long("0"));
ASSERT_EQ(42, minijson::detail::parse_long("42"));
ASSERT_EQ(-42, minijson::detail::parse_long("-42"));
ASSERT_EQ(42, minijson::detail::parse_long("+42"));
ASSERT_EQ(42, minijson::detail::parse_long("042"));
ASSERT_EQ(255, minijson::detail::parse_long("ff", 16));
ASSERT_EQ(255, minijson::detail::parse_long("0xff", 16));
ASSERT_EQ(255, minijson::detail::parse_long("0ff", 16));
char buf[64];
sprintf(buf, "%ld", LONG_MAX);
ASSERT_EQ(LONG_MAX, minijson::detail::parse_long(buf));
sprintf(buf, "%ld", LONG_MIN);
ASSERT_EQ(LONG_MIN, minijson::detail::parse_long(buf));
}
TEST(minijson_reader_detail, parse_long_invalid)
{
ASSERT_THROW(minijson::detail::parse_long(""), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("+"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("-"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long(" 4"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("47f"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("_78945"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("78945_"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("78945 "), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("0x0"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("123456789012345678901234567890"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_long("-123456789012345678901234567890"), minijson::detail::number_parse_error);
}
TEST(minijson_reader_detail, parse_long_invalid_restore_errno)
{
errno = 42;
ASSERT_THROW(minijson::detail::parse_long("123456789012345678901234567890"), minijson::detail::number_parse_error);
ASSERT_EQ(42, errno);
}
TEST(minijson_reader_detail, parse_double)
{
ASSERT_DOUBLE_EQ(0, minijson::detail::parse_double("0"));
ASSERT_DOUBLE_EQ(42, minijson::detail::parse_double("42"));
ASSERT_DOUBLE_EQ(-42, minijson::detail::parse_double("-42"));
ASSERT_DOUBLE_EQ(42, minijson::detail::parse_double("+42"));
ASSERT_DOUBLE_EQ(42, minijson::detail::parse_double("042"));
ASSERT_DOUBLE_EQ(42.42, minijson::detail::parse_double("42.42"));
ASSERT_DOUBLE_EQ(42.42E+01, minijson::detail::parse_double("42.42E+01"));
ASSERT_DOUBLE_EQ(42.42E-01, minijson::detail::parse_double("42.42E-01"));
char buf[2048];
sprintf(buf, "%lf", std::numeric_limits<double>::max());
ASSERT_DOUBLE_EQ(std::numeric_limits<double>::max(), minijson::detail::parse_double(buf));
sprintf(buf, "%lf", -std::numeric_limits<double>::max());
ASSERT_DOUBLE_EQ(-std::numeric_limits<double>::max(), minijson::detail::parse_double(buf));
#if 0 // for some reason I have to determine, the following two tests fail
sprintf(buf, "%lf", std::numeric_limits<double>::min());
ASSERT_DOUBLE_EQ(std::numeric_limits<double>::min(), minijson::detail::parse_double(buf));
sprintf(buf, "%lf", -std::numeric_limits<double>::min());
ASSERT_DOUBLE_EQ(-std::numeric_limits<double>::min(), minijson::detail::parse_double(buf));
#endif
}
TEST(minijson_reader_detail, parse_double_invalid)
{
ASSERT_THROW(minijson::detail::parse_double(""), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("+"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("-"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double(" 4"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("47f"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("_78945"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("78945_"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("78945 "), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("0x0"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("42..42"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("42.42E+094 "), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("42.42E+09.4"), minijson::detail::number_parse_error);
ASSERT_THROW(minijson::detail::parse_double("1.0E+999"), minijson::detail::number_parse_error); // overflow
ASSERT_THROW(minijson::detail::parse_double("-1.0E+999"), minijson::detail::number_parse_error); // overflow
ASSERT_THROW(minijson::detail::parse_double("1.0E-999"), minijson::detail::number_parse_error); // underflow
ASSERT_THROW(minijson::detail::parse_double("-1.0E-999"), minijson::detail::number_parse_error); // underflow
}
TEST(minijson_reader_detail, parse_double_invalid_restore_errno)
{
errno = 42;
ASSERT_THROW(minijson::detail::parse_double("1.0E+999"), minijson::detail::number_parse_error);
ASSERT_EQ(42, errno);
}
TEST(minijson_reader_detail, parse_utf16_escape_sequence)
{
ASSERT_EQ(0x0000u, minijson::detail::parse_utf16_escape_sequence("0000"));
ASSERT_EQ(0x0001u, minijson::detail::parse_utf16_escape_sequence("0001"));
ASSERT_EQ(0xA6BCu, minijson::detail::parse_utf16_escape_sequence("A6BC"));
ASSERT_EQ(0xFFFEu, minijson::detail::parse_utf16_escape_sequence("FFFE"));
ASSERT_EQ(0xFFFFu, minijson::detail::parse_utf16_escape_sequence("FFFF"));
ASSERT_EQ(0xFFFEu, minijson::detail::parse_utf16_escape_sequence("fffe"));
ASSERT_EQ(0xFFFFu, minijson::detail::parse_utf16_escape_sequence("ffff"));
ASSERT_EQ(0xFFFFu, minijson::detail::parse_utf16_escape_sequence("ffFf"));
}
TEST(minijson_reader_detail, parse_utf16_escape_sequence_invalid)
{
ASSERT_THROW(minijson::detail::parse_utf16_escape_sequence("ffFp"), minijson::detail::encoding_error);
ASSERT_THROW(minijson::detail::parse_utf16_escape_sequence("-bcd"), minijson::detail::encoding_error);
ASSERT_THROW(minijson::detail::parse_utf16_escape_sequence(" abc"), minijson::detail::encoding_error);
ASSERT_THROW(minijson::detail::parse_utf16_escape_sequence("abc "), minijson::detail::encoding_error);
}
static void test_write_utf8_char(minijson::detail::utf8_char c, const char* expected_str)
{
char buf[] = "____";
minijson::buffer_context buffer_context(buf, sizeof(buf));
buffer_context.read();
buffer_context.read();
buffer_context.read();
buffer_context.read();
minijson::detail::write_utf8_char(buffer_context, c);
ASSERT_STREQ(expected_str, buf);
}
TEST(minijson_reader_detail, write_utf8_char)
{
test_write_utf8_char(minijson::detail::utf8_char(0x00, 0x00, 0x00, 0x00), "");
test_write_utf8_char(minijson::detail::utf8_char(0xFF, 0x00, 0x00, 0x00), "\xFF___");
test_write_utf8_char(minijson::detail::utf8_char(0xFF, 0xFE, 0x00, 0x00), "\xFF\xFE__");
test_write_utf8_char(minijson::detail::utf8_char(0xFF, 0xFE, 0xFD, 0x00), "\xFF\xFE\xFD_");
test_write_utf8_char(minijson::detail::utf8_char(0xFF, 0xFE, 0xFD, 0xFC), "\xFF\xFE\xFD\xFC");
}
TEST(minijson_reader_detail, read_quoted_string_empty)
{
char buffer[] = "\"\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context);
ASSERT_STREQ("", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_quoted_string_one_char)
{
char buffer[] = "\"a\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context);
ASSERT_STREQ("a", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_quoted_string_ascii)
{
char buffer[] = "\"foo\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context);
ASSERT_STREQ("foo", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_quoted_string_utf8)
{
char buffer[] = "\"你好\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context);
ASSERT_STREQ("你好", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_quoted_string_escape_sequences)
{
char buffer[] = "\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context);
ASSERT_STREQ("\"\\/\b\f\n\r\t", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_quoted_string_escape_sequences_utf16)
{
char buffer[] = "\"\\u0001\\u0002a\\ud7ff\\uE000\\uFffFb\\u4F60\\uD800\\uDC00\\uDBFF\\uDFFFà\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context);
ASSERT_STREQ("\x01\x02" "a" "\xED\x9F\xBF\xEE\x80\x80\xEF\xBF\xBF" "b" "你" "\xF0\x90\x80\x80" "\xF4\x8F\xBF\xBF" "à",
buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_quoted_string_escape_sequences_nullchar)
{
char buffer[] = "\"a\\u0000\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context);
ASSERT_STREQ("a", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_quoted_string_skip_opening_quote)
{
char buffer[] = "asd\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_quoted_string(buffer_context, true);
ASSERT_STREQ("asd", buffer_context.write_buffer());
}
template<size_t Length>
void read_quoted_string_invalid_helper(
const char (&buffer)[Length], minijson::parse_error::error_reason expected_reason, size_t expected_offset, const char* expected_what)
{
bool exception_thrown = false;
try
{
minijson::const_buffer_context context(buffer, Length - 1);
minijson::detail::read_quoted_string(context);
}
catch (const minijson::parse_error& parse_error)
{
exception_thrown = true;
ASSERT_EQ(expected_reason, parse_error.reason());
ASSERT_EQ(expected_offset, parse_error.offset());
ASSERT_STREQ(expected_what, parse_error.what());
}
ASSERT_TRUE(exception_thrown);
}
TEST(minijson_reader_detail, read_quoted_string_invalid)
{
read_quoted_string_invalid_helper("", minijson::parse_error::EXPECTED_OPENING_QUOTE, 0, "Expected opening quote");
read_quoted_string_invalid_helper("a", minijson::parse_error::EXPECTED_OPENING_QUOTE, 0, "Expected opening quote");
read_quoted_string_invalid_helper("\"", minijson::parse_error::EXPECTED_CLOSING_QUOTE, 0, "Expected closing quote");
read_quoted_string_invalid_helper("\"asd", minijson::parse_error::EXPECTED_CLOSING_QUOTE, 3, "Expected closing quote");
read_quoted_string_invalid_helper("\"\\h\"", minijson::parse_error::INVALID_ESCAPE_SEQUENCE, 2, "Invalid escape sequence");
read_quoted_string_invalid_helper("\"\\u0rff\"", minijson::parse_error::INVALID_UTF16_CHARACTER, 6, "Invalid UTF-16 character");
read_quoted_string_invalid_helper("\"\\uD800\\uD7FF\"", minijson::parse_error::INVALID_UTF16_CHARACTER, 12, "Invalid UTF-16 character");
read_quoted_string_invalid_helper("\"\\uDC00\"", minijson::parse_error::INVALID_UTF16_CHARACTER, 6, "Invalid UTF-16 character");
read_quoted_string_invalid_helper("\"\\uD800\"", minijson::parse_error::EXPECTED_UTF16_LOW_SURROGATE, 7, "Expected UTF-16 low surrogate");
read_quoted_string_invalid_helper("\"\\uD800a\"", minijson::parse_error::EXPECTED_UTF16_LOW_SURROGATE, 7, "Expected UTF-16 low surrogate");
}
template<size_t Length>
void read_unquoted_value_invalid_helper(
const char (&buffer)[Length], minijson::parse_error::error_reason expected_reason, size_t expected_offset, const char* expected_what)
{
bool exception_thrown = false;
try
{
minijson::const_buffer_context context(buffer, Length - 1);
minijson::detail::read_unquoted_value(context);
}
catch (const minijson::parse_error& parse_error)
{
exception_thrown = true;
ASSERT_EQ(expected_reason, parse_error.reason());
ASSERT_EQ(expected_offset, parse_error.offset());
ASSERT_STREQ(expected_what, parse_error.what());
}
ASSERT_TRUE(exception_thrown);
}
TEST(minijson_reader_detail, read_unquoted_value_empty)
{
read_unquoted_value_invalid_helper("", minijson::parse_error::UNTERMINATED_VALUE, 0, "Unterminated value");
}
TEST(minijson_reader_detail, read_unquoted_value_comma_terminated)
{
char buffer[] = "42.42,";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
ASSERT_EQ(',', minijson::detail::read_unquoted_value(buffer_context));
ASSERT_STREQ("42.42", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_unquoted_value_curly_bracket_terminated)
{
char buffer[] = "42.42E+104}";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
ASSERT_EQ('}', minijson::detail::read_unquoted_value(buffer_context));
ASSERT_STREQ("42.42E+104", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_unquoted_value_square_bracket_terminated)
{
char buffer[] = "42.42]";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
ASSERT_EQ(']', minijson::detail::read_unquoted_value(buffer_context));
ASSERT_STREQ("42.42", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_unquoted_value_whitespace_terminated)
{
char buffer[] = "true\t";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
ASSERT_EQ('\t', minijson::detail::read_unquoted_value(buffer_context));
ASSERT_STREQ("true", buffer_context.write_buffer());
}
TEST(minijson_reader_detail, read_unquoted_value_unterminated)
{
read_unquoted_value_invalid_helper("5", minijson::parse_error::UNTERMINATED_VALUE, 0, "Unterminated value");
}
TEST(minijson_reader_detail, read_unquoted_value_first_char)
{
char buffer[] = "true\t";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
buffer_context.read();
ASSERT_EQ('\t', minijson::detail::read_unquoted_value(buffer_context, 't'));
ASSERT_STREQ("true", buffer_context.write_buffer());
}
TEST(minijson_reader, value_default_constructed)
{
const minijson::value value;
ASSERT_EQ(minijson::Null, value.type());
ASSERT_STREQ("", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE(value.as_bool());
ASSERT_DOUBLE_EQ(0.0, value.as_double());
}
TEST(minijson_reader, value_example)
{
const minijson::value value(minijson::Number, "42.42", 42, 42.42);
ASSERT_EQ(minijson::Number, value.type());
ASSERT_STREQ("42.42", value.as_string());
ASSERT_EQ(42, value.as_long());
ASSERT_TRUE(value.as_bool());
ASSERT_DOUBLE_EQ(42.42, value.as_double());
}
template<typename Context>
void parse_unquoted_value_invalid_helper(Context& context, size_t expected_offset)
{
bool exception_thrown = false;
try
{
minijson::detail::parse_unquoted_value(context);
}
catch (const minijson::parse_error& parse_error)
{
exception_thrown = true;
ASSERT_EQ(minijson::parse_error::INVALID_VALUE, parse_error.reason());
ASSERT_EQ(expected_offset, parse_error.offset());
ASSERT_STREQ("Invalid value", parse_error.what());
}
ASSERT_TRUE(exception_thrown);
}
TEST(minijson_reader_detail, parse_unquoted_value_whitespace)
{
char buffer[] = " 42";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
read_unquoted_value(buffer_context);
parse_unquoted_value_invalid_helper(buffer_context, 0);
}
TEST(minijson_reader_detail, parse_unquoted_value_true)
{
char buffer[] = "true ";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::detail::read_unquoted_value(buffer_context);
const minijson::value value = minijson::detail::parse_unquoted_value(buffer_context);
ASSERT_EQ(minijson::Boolean, value.type());
ASSERT_STREQ("true", value.as_string());
ASSERT_EQ(1, value.as_long());
ASSERT_TRUE( value.as_bool());
ASSERT_DOUBLE_EQ(1.0, value.as_double());
}
TEST(minijson_reader_detail, parse_unquoted_value_false)
{
char buffer[] = "false}";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::detail::read_unquoted_value(buffer_context);
const minijson::value value = minijson::detail::parse_unquoted_value(buffer_context);
ASSERT_EQ(minijson::Boolean, value.type());
ASSERT_STREQ("false", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE( value.as_bool());
ASSERT_DOUBLE_EQ(0.0, value.as_double());
}
TEST(minijson_reader_detail, parse_unquoted_value_null)
{
char buffer[] = "null}";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::detail::read_unquoted_value(buffer_context);
const minijson::value value = minijson::detail::parse_unquoted_value(buffer_context);
ASSERT_EQ(minijson::Null, value.type());
ASSERT_STREQ("null", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE( value.as_bool());
ASSERT_DOUBLE_EQ(0.0, value.as_double());
}
TEST(minijson_reader_detail, parse_unquoted_value_integer)
{
char buffer[] = "42]";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::detail::read_unquoted_value(buffer_context);
const minijson::value value = minijson::detail::parse_unquoted_value(buffer_context);
ASSERT_EQ(minijson::Number, value.type());
ASSERT_STREQ("42", value.as_string());
ASSERT_EQ(42, value.as_long());
ASSERT_TRUE( value.as_bool());
ASSERT_DOUBLE_EQ(42.0, value.as_double());
}
TEST(minijson_reader_detail, parse_unquoted_value_double)
{
char buffer[] = "42.0e+76,";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::detail::read_unquoted_value(buffer_context);
const minijson::value value = minijson::detail::parse_unquoted_value(buffer_context);
ASSERT_EQ(minijson::Number, value.type());
ASSERT_STREQ("42.0e+76", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE( value.as_bool());
ASSERT_DOUBLE_EQ(42.0E+76, value.as_double());
}
TEST(minijson_reader_detail, parse_unquoted_value_invalid)
{
char buffer[] = "asd,";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
minijson::detail::read_unquoted_value(buffer_context);
parse_unquoted_value_invalid_helper(buffer_context, 3);
}
TEST(minijson_reader_detail, read_value_object)
{
char buffer[] = "{...";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
buffer_context.read();
const std::pair<minijson::value, char> result = minijson::detail::read_value(buffer_context, buffer[0]);
const minijson::value value = result.first;
const char ending_char = result.second;
ASSERT_EQ(minijson::Object, value.type());
ASSERT_STREQ("", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE( value.as_bool());
ASSERT_DOUBLE_EQ(0.0, value.as_double());
ASSERT_EQ(0, ending_char);
}
TEST(minijson_reader_detail, read_value_array)
{
char buffer[] = "[...";
minijson::buffer_context buffer_context(buffer, sizeof(buffer));
buffer_context.read();
const std::pair<minijson::value, char> result = minijson::detail::read_value(buffer_context, buffer[0]);
const minijson::value value = result.first;
const char ending_char = result.second;
ASSERT_EQ(minijson::Array, value.type());
ASSERT_STREQ("", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE( value.as_bool());
ASSERT_DOUBLE_EQ(0.0, value.as_double());
ASSERT_EQ(0, ending_char);
}
TEST(minijson_reader_detail, read_value_quoted_string)
{
char buffer[] = "\"Hello world\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
buffer_context.read();
const std::pair<minijson::value, char> result = minijson::detail::read_value(buffer_context, buffer[0]);
const minijson::value value = result.first;
const char ending_char = result.second;
ASSERT_EQ(minijson::String, value.type());
ASSERT_STREQ("Hello world", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE( value.as_bool());
ASSERT_DOUBLE_EQ(0.0, value.as_double());
ASSERT_EQ(0, ending_char);
}
TEST(minijson_reader_detail, read_value_quoted_string_empty)
{
char buffer[] = "\"\"";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
buffer_context.read();
const std::pair<minijson::value, char> result = minijson::detail::read_value(buffer_context, buffer[0]);
const minijson::value value = result.first;
const char ending_char = result.second;
ASSERT_EQ(minijson::String, value.type());
ASSERT_STREQ("", value.as_string());
ASSERT_EQ(0, value.as_long());
ASSERT_FALSE( value.as_bool());
ASSERT_DOUBLE_EQ(0.0, value.as_double());
ASSERT_EQ(0, ending_char);
}
TEST(minijson_reader_detail, read_value_unquoted)
{
char buffer[] = "true,";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
buffer_context.read();
const std::pair<minijson::value, char> result = minijson::detail::read_value(buffer_context, buffer[0]);
const minijson::value value = result.first;
const char ending_char = result.second;
ASSERT_EQ(minijson::Boolean, value.type());
ASSERT_STREQ("true", value.as_string());
ASSERT_EQ(1, value.as_long());
ASSERT_TRUE( value.as_bool());
ASSERT_DOUBLE_EQ(1.0, value.as_double());
ASSERT_EQ(',', ending_char);
// boolean false, null, integer and double cases have been already tested with parse_unquoted_value
}
TEST(minijson_reader_detail, read_value_unquoted_invalid)
{
char buffer[] = "xxx,";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
buffer_context.read();
bool exception_thrown = false;
try
{
minijson::detail::read_value(buffer_context, buffer[0]);
}
catch (const minijson::parse_error& parse_error)
{
exception_thrown = true;
ASSERT_EQ(minijson::parse_error::INVALID_VALUE, parse_error.reason());
ASSERT_EQ(3u, parse_error.offset());
ASSERT_STREQ("Invalid value", parse_error.what());
}
ASSERT_TRUE(exception_thrown);
}
void parse_object_empty_handler(const char*, minijson::value)
{
FAIL();
}
void parse_array_empty_handler(minijson::value)
{
FAIL();
}
TEST(minijson_reader, parse_object_empty)
{
char buffer[] = "{}";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::parse_object(buffer_context, parse_object_empty_handler);
}
struct check_on_destroy_handler
{
mutable bool check_on_destroy;
check_on_destroy_handler() :
check_on_destroy(true)
{
}
check_on_destroy_handler(const check_on_destroy_handler& other) :
check_on_destroy(true)
{
other.check_on_destroy = false;
}
};
struct parse_object_single_field_handler : check_on_destroy_handler
{
bool read_field;
explicit parse_object_single_field_handler() :
read_field(false)
{
}
~parse_object_single_field_handler()
{
if (check_on_destroy) EXPECT_TRUE(read_field);
}
void operator()(const char* field_name, const minijson::value& field_value)
{
read_field = true;
ASSERT_STREQ("field", field_name);
ASSERT_EQ(minijson::String, field_value.type());
ASSERT_STREQ("value", field_value.as_string());
}
};
TEST(minijson_reader, parse_object_single_field)
{
char buffer[] = " { \n\t\"field\" : \"value\"\t\n} ";
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::parse_object(buffer_context, parse_object_single_field_handler());
}
struct parse_object_multiple_fields_handler : check_on_destroy_handler
{
std::bitset<7> h;
~parse_object_multiple_fields_handler()
{
if (check_on_destroy) EXPECT_TRUE(h.all());
}
void operator()(const char* n, const minijson::value& v)
{
if (strcmp(n, "string") == 0)
{ h[0] = 1; ASSERT_EQ(minijson::String, v.type()); ASSERT_STREQ("value\"\\/\b\f\n\r\t", v.as_string()); }
else if (strcmp(n, "integer") == 0)
{ h[1] = 1; ASSERT_EQ(minijson::Number, v.type()); ASSERT_EQ(42, v.as_long()); }
else if (strcmp(n, "floating_point") == 0)
{ h[2] = 1; ASSERT_EQ(minijson::Number, v.type()); ASSERT_DOUBLE_EQ(4261826387162873618273687126387.0, v.as_double()); }
else if (strcmp(n, "boolean_true") == 0)
{ h[3] = 1; ASSERT_EQ(minijson::Boolean, v.type()); ASSERT_TRUE(v.as_bool()); }
else if (strcmp(n, "boolean_false") == 0)
{ h[4] = 1; ASSERT_EQ(minijson::Boolean, v.type()); ASSERT_FALSE(v.as_bool()); }
else if (strcmp(n, "") == 0)
{ h[5] = 1; ASSERT_EQ(minijson::Null, v.type()); }
else if (strcmp(n, "\xc3\xA0\x01\x02" "a" "\xED\x9F\xBF\xEE\x80\x80\xEF\xBF\xBF" "b" "你" "\xF0\x90\x80\x80" "\xF4\x8F\xBF\xBF" "à") == 0)
{ h[6] = 1; ASSERT_EQ(minijson::String, v.type()); ASSERT_STREQ("", v.as_string()); }
else
{ FAIL(); }
}
};
TEST(minijson_reader, parse_object_multiple_fields)
{
char buffer[] = "{\"string\":\"value\\\"\\\\\\/\\b\\f\\n\\r\\t\",\"integer\":42,\"floating_point\":4261826387162873618273687126387,"
"\"boolean_true\":true,\n\"boolean_false\":false,\"\":null,"
"\"\\u00e0\\u0001\\u0002a\\ud7ff\\uE000\\uFffFb\\u4F60\\uD800\\uDC00\\uDBFF\\uDFFFà\":\"\"}";
{
minijson::const_buffer_context const_buffer_context(buffer, sizeof(buffer) - 1);
minijson::parse_object(const_buffer_context, parse_object_multiple_fields_handler());
}
{
std::istringstream ss(buffer);
minijson::istream_context istream_context(ss);
minijson::parse_object(istream_context, parse_object_multiple_fields_handler());
}
{
buffer[sizeof(buffer) - 1] = 'x'; // damage null terminator to test robustness
minijson::buffer_context buffer_context(buffer, sizeof(buffer) - 1);
minijson::parse_object(buffer_context, parse_object_multiple_fields_handler());
}
}
template<typename Context>
struct parse_object_nested_handler : check_on_destroy_handler
{
std::bitset<2> h;
Context& context;
explicit parse_object_nested_handler(Context& context) :
context(context)
{
}
~parse_object_nested_handler()
{
if (check_on_destroy) EXPECT_TRUE(h.all());
}
void operator()(const char* n, const minijson::value& v)
{
if (strcmp(n, "") == 0)
{ h[0] = 1; ASSERT_EQ(minijson::Object, v.type()); minijson::parse_object(context, nested1_handler(context)); }