-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_value.cpp
More file actions
1679 lines (1528 loc) · 42.1 KB
/
test_value.cpp
File metadata and controls
1679 lines (1528 loc) · 42.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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 <hjson.h>
#include <cmath>
#include <cstring>
#include <sstream>
#include <cstdio>
#include "hjson_test.h"
static std::string _test_string_param(std::string param) {
return param;
}
void test_value() {
{
Hjson::Value valVec(Hjson::Type::Vector);
assert(valVec.type() == Hjson::Type::Vector);
Hjson::Value valMap(Hjson::Type::Map);
assert(valMap.type() == Hjson::Type::Map);
}
{
Hjson::Value val(true);
assert(val.type() == Hjson::Type::Bool);
assert(val);
assert(val == true);
assert(val != false);
assert(true == (bool) val);
assert(false != (bool) val);
assert(val && val);
// The assignment statement must not be performed (short-circuit of ||).
assert(val || (val = false));
assert(!!val);
{
std::stringstream ss;
ss << val;
assert(ss.str() == "true");
}
val = false;
assert(!val);
assert(!val.empty());
// size() is the number of child elements, can only be > 0 for Vector or Map.
assert(val.size() == 0);
assert(val.to_string() == "false");
assert(val.to_double() == 0);
assert(val.to_int64() == 0);
val = true;
assert(val.to_double() == 1);
assert(val.to_int64() == 1);
assert(val.to_string() == "true");
// The result of the comparison is undefined in C++11.
// assert(val.begin() == val.end());
}
{
Hjson::Value val(Hjson::Type::Null);
assert(val.type() == Hjson::Type::Null);
assert(!val);
assert(val.empty());
assert(val.size() == 0);
Hjson::Value val2(Hjson::Type::Null);
assert(val == val2);
Hjson::Value val3;
assert(val != val3);
assert(val.to_double() == 0);
assert(val.to_int64() == 0);
assert(val.to_string() == "null");
{
std::stringstream ss;
ss << val;
assert(ss.str() == "null");
}
assert(val3.to_double() == 0);
assert(val3.to_int64() == 0);
assert(val3.to_string() == "");
// The result of the comparison is undefined in C++11.
// assert(val.begin() == val.end());
// assert(val3.begin() == val3.end());
}
{
const Hjson::Value val = 3.0;
assert(val == 3.0);
assert(val != 4.0);
assert(3.0 == val);
assert(4.0 != val);
double third = val;
int fourth = val;
assert(fourth == 3);
third = static_cast<int>(val);
assert(third == 3.0);
assert(val == 3);
assert(val != 2);
assert(3 == val);
assert(2 != val);
assert(val < 4);
assert(4 > val);
assert(val < 4.0);
assert(4.0 > val);
assert(val * 3 == 9);
third = val * 3;
assert(third == 9);
third = 3 * val;
assert(third == 9);
assert(3 * val == 9);
assert(val * 3.0 == 9.0);
assert(3.0 * val == 9.0);
assert(val / 3 == 1);
assert(3 / val == 1);
assert(val / 3.0 == 1.0);
assert(3.0 / val == 1.0);
assert(val + 1 == 4);
assert(1 + val == 4);
assert(val + 1.0 == 4.0);
assert(1.0 + val == 4.0);
assert(val - 1 == 2);
assert(1 - val == -2);
assert(val - 1.0 == 2.0);
assert(1.0 - val == -2.0);
assert(val.to_double() == 3);
assert(val.to_int64() == 3);
assert(val.to_string() == "3.0");
{
std::stringstream ss;
ss << val;
assert(ss.str() == "3.0");
}
assert(val.type() != Hjson::Type::Int64);
// The result of the comparison is undefined in C++11.
// assert(val.begin() == val.end());
}
{
Hjson::Value val = 3.0;
Hjson::Value val2 = 3.0;
assert(val == val2);
assert(val != val2 + 1);
assert(val2 + 1 != val);
double third = val + val2;
assert(third == 6.0);
val2 = 6;
assert(val * val2 == 18);
assert(val2 / val == 2.0);
assert(val + val2 == 9);
assert(val - val2 == -3.0);
}
{
Hjson::Value val = 1;
assert(val == 1);
assert(val != 2);
assert(val != 2.0);
assert(val.to_double() == 1.0);
assert(val.to_int64() == 1);
assert(val.to_string() == "1");
{
std::stringstream ss;
ss << val;
assert(ss.str() == "1");
}
assert(val.type() == Hjson::Type::Int64);
int i = 2;
Hjson::Value val2(i);
assert(val2 != val);
assert(val2 > val);
assert(val < val2);
assert(val2 > 1);
assert(val2 < 3);
assert(1 < val2);
assert(3 > val2);
assert(3 > val2.to_int64());
assert(val2 == i);
assert((val2 + 1) == static_cast<double>(i + 1));
assert((val2 - 1) == static_cast<double>(i - 1));
char i3 = 4;
Hjson::Value val3(i3);
assert(val3 == 4);
assert(4 == val3);
assert(val3 == i3);
assert(i3 == val3);
assert(!(i3 > val3));
assert(!(val3 > i3));
assert(!(i3 < val3));
assert(!(val3 < i3));
i3 = val3;
assert(i3 == 4);
Hjson::Value val4("-1");
assert(val4.to_double() == -1);
assert(val4.to_int64() == -1);
assert(val4.to_string() == "-1");
{
std::stringstream ss;
ss << val4;
assert(ss.str() == "\"-1\"");
}
Hjson::Value val5(-1);
assert(val5 == -1);
assert(val5 < val);
assert(val5 < 1.0);
short i6 = -28;
Hjson::Value val6(i6);
assert(val6 == -28);
assert(-28 == val6);
assert(val6 == i6);
assert(i6 == val6);
assert(!(val6 > i6));
assert(!(i6 > val6));
assert(!(val6 < i6));
assert(!(i6 < val6));
i6 = val6;
assert(i6 == -28);
i6 = -val6;
assert(i6 == 28);
i6 = +val6;
assert(i6 == -28);
val6 += i6;
assert(val6 == -56);
val6 -= i6;
assert(val6 == -28);
unsigned short i7 = 29;
Hjson::Value val7(i7);
assert(val7 == 29);
assert(29 == val7);
assert(val7 == i7);
assert(i7 == val7);
assert(!(val7 > i7));
assert(!(i7 > val7));
assert(!(val7 < i7));
assert(!(i7 < val7));
val7 -= i7;
assert(val7 == 0);
unsigned char i8 = 4;
Hjson::Value val8(i8);
assert(val8 == 4);
assert(4 == val8);
assert(val8 == i8);
assert(i8 == val8);
assert(!(i8 > val8));
assert(!(val8 > i8));
assert(!(i8 < val8));
assert(!(val8 < i8));
unsigned int i9 = 4;
Hjson::Value val9(i9);
assert(val9 == 4);
assert(4 == val9);
assert(val9 == i9);
assert(i9 == val9);
assert(!(i9 > val9));
assert(!(val9 > i9));
assert(!(i9 < val9));
assert(!(val9 < i9));
i9 = val9;
assert(i9 == 4);
}
{
unsigned char i1 = 250;
char i2 = 100;
Hjson::Value val1(i1);
Hjson::Value val2 = i2;
assert(val1 + val2 == 350);
assert(350 == val2 + val1);
assert(i1 + val1 == 500);
assert(val1 * val2 == 25000);
assert(val1 / val2 == (250 / 100));
}
{
Hjson::Value val(144115188075855873);
assert(val.type() == Hjson::Type::Int64);
assert(val == Hjson::Value(144115188075855873));
assert(val != Hjson::Value(144115188075855874));
assert(val.to_int64() == 144115188075855873);
assert(static_cast<std::int64_t>(val) == 144115188075855873);
val = Hjson::Value(9223372036854775807);
assert(val.to_string() == "9223372036854775807");
assert(val == Hjson::Value(9223372036854775807));
assert(val != Hjson::Value(9223372036854775806));
assert(val.to_int64() == 9223372036854775807);
assert(val > Hjson::Value(9223372036854775806));
std::int64_t i = 9223372036854775806;
std::int64_t i2 = 9223372036854775806;
Hjson::Value val2(i);
assert(val2 == Hjson::Value(i));
assert(val2 != val);
assert(val2 < val);
assert(val > val2);
assert(val2 < Hjson::Value(9223372036854775807));
assert(9223372036854775807 > val2);
assert(9223372036854775807 > val2.to_int64());
// These two assertions fail in GCC 5.4, which is ok because doubles can
// only represent integers up to 9007199254000000 (2^53) without precision
// loss.
//assert((val2 + 1) == static_cast<double>(i + 1));
//assert((val2 - 1) == static_cast<double>(i - 1));
Hjson::Value val6 = 9223372036854775807;
assert(val6 == 9223372036854775807);
Hjson::Value val7("-9223372036854775806");
assert(val7.to_int64() == -9223372036854775806);
assert(val7.to_string() == "-9223372036854775806");
Hjson::Value val8(-9223372036854775806);
assert(val8 == Hjson::Value(-9223372036854775806));
assert(val8.to_int64() == -9223372036854775806);
assert(val8 < val);
assert(val8 < 1.0);
std::int64_t i3 = 144115188075855873;
Hjson::Value val9 = i3;
assert(val9 == i3);
assert(i3 == val9);
assert(!(val9 > i3));
assert(!(val9 < i3));
assert(!(i3 > val9));
assert(!(i3 > val9));
assert(val9 >= i3);
assert(val9 <= i3);
assert(i3 >= val9);
assert(i3 >= val9);
i3 = val9;
assert(i3 == 144115188075855873);
std::int64_t i4 = 1;
assert(i4 != val9);
assert(val9 != i4);
assert(val9 + i4 == 144115188075855874);
assert(i4 + val9 == 144115188075855874);
val9 += i4;
assert(val9 == 144115188075855874);
assert(val9 - i4 == 144115188075855873);
assert(i4 - val9 == -144115188075855873);
val9 -= i4;
assert(val9 == 144115188075855873);
assert(val9 / i4 == val9);
assert(i4 / val9 == 0);
val9 /= i4;
assert(val9 == 144115188075855873);
assert(val9 % i4 == 0);
assert(i4 % val9 == 1);
val9 %= i4;
assert(val9 == 0);
}
{
Hjson::Value val1("92233720368547758073829419051489548484843823585675828488686");
Hjson::Value val2("92233720368547758073829419051489548484843823585675828488686.0");
Hjson::Value val3(92233720368547758073829419051489548484843823585675828488686.0);
assert(val1.to_double() == val2.to_double());
assert(val1.to_double() == val3.to_double());
}
{
Hjson::Value val1 = 3;
val1 += 1;
assert(val1 == 4);
assert(++val1 == 5);
assert(val1 == 5);
assert(val1++ == 5);
assert(val1 == 6);
val1 += 1.0;
assert(val1 == 7);
val1 -= 1;
assert(val1 == 6);
val1 -= 1.0;
assert(val1 == 5);
assert(--val1 == 4);
assert(val1 == 4);
assert(val1-- == 4);
assert(val1 == 3);
}
{
Hjson::Value val("alpha");
Hjson::Value val2 = "alpha";
assert(val == val2);
assert(val != "beta");
assert("beta" != val);
assert(_test_string_param(val) == "alpha");
val = std::string("alpha");
std::string st = val;
assert(st == val);
assert(val == st);
assert(val == val2);
assert(val2 == std::string("alpha"));
assert(val2 != std::string("beta"));
assert(std::string("alpha") == val2.operator std::string());
assert(std::string("beta") != val2.operator std::string());
assert(val.to_double() == 0);
assert(val.to_int64() == 0);
assert(val.to_string() == "alpha");
st = val + "beta";
assert(st == "alphabeta");
val2 = val + "beta";
assert(val2 == "alphabeta");
val2 = val + std::string("beta");
assert(val2 == "alphabeta");
val2 = "beta" + val;
assert(val2 == "betaalpha");
val2 = std::string("beta") + val;
assert(val2 == "betaalpha");
val += "beta";
assert(val == "alphabeta");
val += st;
assert(val == "alphabetaalphabeta");
val = 3;
assert("a" + val == "a3");
val = 3.0;
assert("a" + val == "a3.0");
// The result of the comparison is undefined in C++11.
// assert(val.begin() == val.end());
}
{
Hjson::Value val("alpha");
Hjson::Value val2 = "beta";
assert(val < val2);
assert(val2 > val);
assert(val + val2 == "alphabeta");
assert(val < "beta");
assert("beta" > val.to_string());
assert(val + "beta" == "alphabeta");
assert("alpha" + val2.to_string() == "alphabeta");
// The result of the comparison is undefined in C++11.
// assert(val.begin() == val.end());
}
{
Hjson::Value val("3.0");
assert(val.to_double() == 3);
assert(val.to_int64() == 3);
Hjson::Value val2(3.0);
assert(val != val2);
assert(!(val == val2));
try {
bool a = val > val2;
assert(!"Did not throw error when using < operator on values of different types.");
} catch(const Hjson::type_mismatch& e) {}
try {
bool a = val < val2;
assert(!"Did not throw error when using < operator on values of different types.");
} catch(const Hjson::type_mismatch& e) {}
try {
std::string a = val + val2;
assert(!"Did not throw error when using + operator on values of different types.");
} catch(const Hjson::type_mismatch& e) {}
try {
double a = val2 - val;
assert(!"Did not throw error when using - operator on values of different types.");
} catch(const Hjson::type_mismatch& e) {}
try {
Hjson::Value val3 = val - Hjson::Value("0");
assert(!"Did not throw error when using - operator on value of type STRING.");
} catch(const Hjson::type_mismatch& e) {}
}
{
Hjson::Value val;
val["first"] = "leaf1";
Hjson::Value val2 = val["first"];
val["second"] = val["first"];
val["fourth"] = 4.0;
const Hjson::Value& valC = val;
double fourth = valC.operator[]("fourth");
assert(fourth == valC["fourth"]);
assert(fourth == valC.at("fourth"));
assert(!valC["fifth"].defined());
try {
double fifth = valC.at("fifth");
assert(!"Did not throw error when calling at() with invalid key.");
} catch(const Hjson::index_out_of_bounds& e) {}
fourth = val["fourth"];
assert(fourth == val["fourth"]);
assert(fourth == val.at("fourth"));
try {
double fifth = val.at("fifth");
assert(!"Did not throw error when calling at() with invalid key.");
} catch(const Hjson::index_out_of_bounds& e) {}
try {
std::string fourthString = val["fourth"];
assert(!"Did not throw error when assigning double to string.");
} catch(const Hjson::type_mismatch& e) {}
std::string leaft1 = val["first"];
assert(leaft1 == "leaf1");
assert(val[std::string("first")] == "leaf1");
assert(val.at(std::string("first")) == "leaf1");
assert(val["first"] == "leaf1");
assert(!strcmp("leaf1", val["first"]));
auto it = val.begin();
assert(it->first == "first");
assert(it->second == "leaf1");
++it;
assert(it->first == "fourth");
assert(it->second == 4);
++it;
assert(it->first == "second");
assert(it->second == "leaf1");
++it;
assert(it == val.end());
const Hjson::Value valConst = val;
std::map<std::string, Hjson::Value>::const_iterator itConst = valConst.begin();
assert(itConst->first == "first");
assert(itConst->second == "leaf1");
++itConst;
assert(itConst->first == "fourth");
assert(itConst->second == 4);
++itConst;
assert(itConst->first == "second");
assert(itConst->second == "leaf1");
++itConst;
assert(itConst == valConst.end());
}
{
Hjson::Value val;
val["first"] = "leaf1";
char szKey[20];
sprintf(szKey, "first");
assert(val[szKey] == "leaf1");
char *szKey2 = szKey;
assert(val[szKey2] == "leaf1");
}
{
Hjson::Value val;
val["one"] = "uno";
val["two"] = "due";
assert(val["one"] == "uno");
val["one"].clear();
// clear() does nothing for a string, only affects vector and map.
assert(!val.at("one").empty());
assert(val["two"] == "due");
auto ptr = &val.at("two");
assert(*ptr == "due");
val["two"] = 2;
assert(*ptr == 2);
val.clear();
assert(val.empty());
}
{
Hjson::Value val;
val.push_back(3);
val.push_back(4);
assert(val.size() == 2);
auto ptr = &val[0];
assert(*ptr == 3);
val[0] = 5;
assert(*ptr == 5);
val.clear();
assert(val.empty());
}
try {
Hjson::Value val;
val["first"] = "leaf1";
Hjson::Value undefined = val["first"]["down1"]["down2"];
assert(!"Did not throw error when using brackets on string Value.");
} catch (const Hjson::type_mismatch& e) {}
{
const Hjson::Value val;
Hjson::Value undefined = val["down1"]["down2"]["down3"];
assert(undefined.type() == Hjson::Type::Undefined);
assert(!val.defined());
}
{
Hjson::Value val;
Hjson::Value undefined = val["down1"]["down2"]["down3"];
assert(undefined.type() == Hjson::Type::Undefined);
// The type of val is set to Map because a MapProxy is created, no easy way
// to avoid that.
//assert(!val.defined());
}
{
Hjson::Value val;
val["down1"]["down2"]["down3"] = "three levels deep!";
std::string tld = val["down1"]["down2"]["down3"];
assert(tld == "three levels deep!");
assert(val["down1"]["down2"]["down3"] == "three levels deep!");
}
{
Hjson::Value root;
root["one"] = 1;
{
Hjson::Value test1 = root["one"];
root.erase(0);
}
assert(root.empty());
}
{
Hjson::Value val1, val2;
val2 = val1;
val2["test1"] = "t1";
std::string t1 = val1["test1"];
// Assert that the assignment "val2 = val1" was by reference, not by value.
assert(t1 == "t1");
assert(val1["test1"] == "t1");
assert(val1["test1"] == std::string("t1"));
assert(val2["test1"] == "t1");
assert(val2["test1"] == std::string("t1"));
}
{
Hjson::Value root;
root["key1"]["key2"]["key3"]["A"] = 4;
Hjson::Value val2 = root["key1"]["key2"]["key3"];
val2["B"] = 5;
assert(root["key1"]["key2"]["key3"]["B"] == 5);
}
{
int a = 0;
char *szBrackets = new char[19];
for (; a < 10; a++) {
szBrackets[a] = '[';
szBrackets[++a] = '\n';
}
--a;
for (; a < 18; a++) {
szBrackets[a] = ']';
szBrackets[++a] = '\n';
}
szBrackets[18] = 0;
auto root = Hjson::Unmarshal(szBrackets);
Hjson::EncoderOptions opt;
opt.indentBy = "";
auto res = Hjson::Marshal(root, opt);
assert(!std::strcmp(res.c_str(), szBrackets));
delete[] szBrackets;
}
{
std::ostringstream oss;
for (int a = 0; a < 10; ++a) {
oss << "a: {\n";
}
oss << "a: {}\n";
for (int a = 0; a < 10; ++a) {
oss << "}\n";
}
const std::string in = oss.str();
Hjson::DecoderOptions decOpt;
decOpt.comments = true;
decOpt.whitespaceAsComments = true;
Hjson::Value root = Hjson::Unmarshal(in);
Hjson::EncoderOptions opt;
opt.comments = true;
opt.omitRootBraces = true;
opt.indentBy = "";
const std::string out = Hjson::Marshal(root, opt) + "\n";
assert(out == in);
}
{
Hjson::Value node;
node["a"] = 1;
{
Hjson::Value root;
root["n"] = node;
}
assert(node.size() == 1);
}
{
Hjson::Value node;
node["a"] = 1;
node["a2"] = 2;
{
Hjson::Value node2;
node2["b"] = node;
node2["c"] = "alfa";
node2["d"] = Hjson::Value(Hjson::Type::Undefined);
{
Hjson::Value root;
root["n"] = node2;
}
assert(node2.size() == 3);
}
assert(node.size() == 2);
}
{
Hjson::Value val;
try {
val[0] = 0;
assert(!"Did not throw error when trying to assign Value to Type::Vector index that is out of bounds.");
} catch(const Hjson::index_out_of_bounds& e) {}
try {
const Hjson::Value val2;
const auto val3 = val2[0];
assert(!"Did not throw error when trying to access Type::Vector index that is out of bounds.");
} catch(const Hjson::index_out_of_bounds& e) {}
try {
if (val[0].empty()) {
assert(!"Did not throw error when trying to access Type::Vector index that is out of bounds.");
}
assert(!"Did not throw error when trying to access Type::Vector index that is out of bounds.");
} catch(const Hjson::index_out_of_bounds& e) {}
val.push_back("first");
val.push_back(2);
std::string f = val[0];
Hjson::Value val2 = val[0];
assert(val2 == std::string("first"));
assert(val2 == "first");
try {
val2.push_back(0);
assert(!"Did not throw error when trying to push_back() on Value that is not a Type::Vector.");
} catch(const Hjson::type_mismatch& e) {}
assert(val[1] == 2);
assert(val[1].type() == Hjson::Type::Int64);
val[0] = 3;
assert(val[0] == 3);
assert(val.size() == 2);
try {
val[2] = 2;
assert(!"Did not throw error when trying to assign Value to Type::Vector index that is out of bounds.");
} catch(const Hjson::index_out_of_bounds& e) {}
try {
if (val[2].empty()) {
assert(!"Did not throw error when trying to access Type::Vector index that is out of bounds.");
}
assert(!"Did not throw error when trying to access Type::Vector index that is out of bounds.");
} catch(const Hjson::index_out_of_bounds& e) {}
}
{
Hjson::Value val;
{
Hjson::Value val2;
val2.push_back("first");
val = val2[0];
}
assert(val == "first");
}
{
Hjson::Value val;
Hjson::Value val2 = val["åäö"];
assert(!val2.defined());
assert(val["åäö"].type() == Hjson::Type::Undefined);
// Assert that the comparison didn't create an element.
assert(val.size() == 0);
Hjson::Value sub1, sub2;
val["abc"] = sub1;
val["åäö"] = sub2;
assert(val["åäö"].type() == Hjson::Type::Undefined);
assert(!val["åäö"].defined());
// Assert that explicit assignment creates an element.
assert(val.size() == 2);
std::string generatedHjson = Hjson::Marshal(val);
assert(generatedHjson == "{}");
Hjson::EncoderOptions options;
options.preserveInsertionOrder = false;
generatedHjson = Hjson::Marshal(val, options);
assert(generatedHjson == "{}");
sub1["sub1"] = "abc";
sub2["sub2"] = "åäö";
generatedHjson = Hjson::Marshal(val);
assert(generatedHjson == "{\n abc: {\n sub1: abc\n }\n åäö: {\n sub2: åäö\n }\n}");
{
std::stringstream ss;
ss << val;
assert(ss.str() == "{\n abc: {\n sub1: abc\n }\n åäö: {\n sub2: åäö\n }\n}");
}
Hjson::Value val3 = Hjson::Unmarshal(generatedHjson.c_str(), generatedHjson.size());
assert(val3["abc"].defined());
assert(val3["åäö"]["sub2"] == val["åäö"]["sub2"]);
assert(val3.deep_equal(val));
sub2["sub3"] = "sub3";
assert(!val3.deep_equal(val));
}
{
Hjson::Value val;
if (val) {
val.push_back(0);
}
assert(val.empty());
if (!val) {
val.push_back(0);
}
assert(!val.empty());
assert(val.size() == 1);
assert(val[0] == 0);
if (val[0]) {
assert(!"A 0 Value should be treated as false in boolean expressions.");
}
assert(!val[0].empty());
assert(!val[0]);
int val0 = val[0];
assert(val0 == 0);
double valD = val[0];
assert(valD == 0);
// The result of the comparison is undefined in C++11.
// assert(val.begin() == val.end());
}
{
Hjson::Value val;
if (val.erase("key1")) {
assert(!"Returned non-zero number when trying to do Type::Map erase on Type::Undefined Value.");
}
val["key1"] = "first";
val["key2"] = "second";
val.erase("key1");
assert(val.size() == 1);
assert(val["key1"].empty());
if (val.erase("key1")) {
assert(!"Returned non-zero number when trying to do Type::Map erase with a non-existing key.");
}
val.erase(std::string("key2"));
assert(val.empty());
if (val.erase("key1")) {
assert(!"Returned non-zero number when trying to do Type::Map erase on an empty Type::Map.");
}
Hjson::Value val2("secondVal");
try {
val2.erase("key1");
assert(!"Did not throw error when trying to do Type::Map erase on a STRING Value.");
} catch(const Hjson::type_mismatch& e) {}
}
{
Hjson::Value val;
try {
val.erase(1);
assert(!"Did not throw error when trying to do Type::Vector erase on Type::Undefined Value.");
} catch(const Hjson::index_out_of_bounds& e) {}
val.push_back("first");
val.push_back("second");
Hjson::Value val2;
val2["down1"] = "third";
val.push_back(val2);
assert(val[2]["down1"] == "third");
val.erase(2);
val.erase(0);
assert(val.size() == 1);
try {
val.erase(1);
assert(!"Did not throw error when trying to do Type::Vector erase with an out-of-bounds index.");
} catch(const Hjson::index_out_of_bounds& e) {}
val.erase(0);
assert(val.empty());
try {
val.erase(0);
assert(!"Did not throw error when trying to do Type::Vector erase on an empty Type::Vector.");
} catch(const Hjson::index_out_of_bounds& e) {}
Hjson::Value val3(3);
try {
val3.erase(0);
assert(!"Did not throw error when trying to do Type::Vector erase on a Type::Double Value.");
} catch(const Hjson::type_mismatch& e) {}
}
{
Hjson::Value root = Hjson::Unmarshal("[3,4,5]", 8);
assert(root[0] == 3);
assert(root[1] == 4);
assert(root[2] == 5);
assert(root.size() == 3);
std::string generatedHjson = Hjson::Marshal(root);
assert(generatedHjson == "[\n 3\n 4\n 5\n]");
Hjson::Value root2;
root2.push_back(3);
root2.push_back(4);
root2.push_back(5);
assert(root2.deep_equal(root));
}
{
Hjson::Value val1;
Hjson::Value val2;
assert(val1 == val2);
val1 = 3;
val2 = 3;
assert(val1 == val2);
val1 = "alpha";
val2 = "alpha";
assert(val1 == val2);
}
{
Hjson::Value root, val(0);
root.push_back(1.0/val);
root.push_back(std::sqrt(-1));
std::string generatedHjson = Hjson::Marshal(root);
assert(generatedHjson == "[\n null\n null\n]");
}
{
Hjson::Value val1, val2;
assert(val1.deep_equal(val2));
val1 = 1;
assert(!val1.deep_equal(val2));
val2 = 1;
assert(val1.deep_equal(val2));
val1 = Hjson::Value();
val1.push_back(2);
assert(!val1.deep_equal(val2));
val2 = Hjson::Value();
val2["2"] = 2;
assert(!val1.deep_equal(val2));
val1 = Hjson::Value(Hjson::Type::Vector);
val2 = Hjson::Value(Hjson::Type::Vector);
assert(val1.deep_equal(val2));
val1 = Hjson::Value(Hjson::Type::Map);
assert(!val1.deep_equal(val2));
val2 = Hjson::Value(Hjson::Type::Map);
assert(val1.deep_equal(val2));
}
{
Hjson::Value val1;
val1["first"] = 1;
Hjson::Value val2 = val1.clone();
val1["second"] = 2;
assert(val2.size() == 1);
val1["third"]["first"] = 3;
val2 = val1.clone();
val2["third"]["second"] = 4;
// size() is the number of child elements, can only be > 0 for Vector or Map.
assert(val1["first"].size() == 0);
}
{
Hjson::Value val1;
val1["zeta"] = 1;
val1["y"] = 2;
val1["xerxes"]["first"] = 3;
assert(val1[0] == 1);
val1[0] = 99;
assert(val1["zeta"] == 99);
assert(val1.key(2) == "xerxes");
val1.move(0, 3);
assert(val1.key(0) == "y");
assert(val1[2] == 99);
val1.move(1, 0);
auto str = Hjson::Marshal(val1);
assert(str == "{\n xerxes: {\n first: 3\n }\n y: 2\n zeta: 99\n}");
assert(val1[0]["first"] == 3);
assert(val1.key(1) == "y");
}
{
Hjson::Value val1;
val1.push_back(1);
Hjson::Value val2 = val1.clone();
val1.push_back(2);
assert(val2.size() == 1);
val1.push_back(val2);
val2 = val1.clone();
val2[2].push_back(3);
assert(val1[2].size() == 1);
}
{
std::string baseStr = R"({
debug: false
rect: {
x: 0
y: 0
width: 800
height: 600
}
path: C:/temp
seq: [
0
1
2
]
scale: 3
window: {
x: 13
y: 37
width: 200
height: 200
}
})";
Hjson::Value base = Hjson::Unmarshal(baseStr);
Hjson::Value ext = Hjson::Unmarshal(R"(
{
debug: true
rect: {
x: 0
y: 0
height: 480