-
Notifications
You must be signed in to change notification settings - Fork 141
/
json_test.cpp
10197 lines (8708 loc) · 285 KB
/
json_test.cpp
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
// Glaze Library
// For the license information refer to glaze.hpp
#include <any>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <forward_list>
#include <initializer_list>
#include <iostream>
#include <list>
#include <map>
#include <numbers>
#include <random>
#include <ranges>
#include <set>
#if defined(__STDCPP_FLOAT128_T__)
#include <stdfloat>
#endif
#include <tuple>
#include <unordered_map>
#include <variant>
#include "glaze/api/impl.hpp"
#include "glaze/containers/flat_map.hpp"
#include "glaze/file/hostname_include.hpp"
#include "glaze/file/raw_or_file.hpp"
#include "glaze/hardware/volatile_array.hpp"
#include "glaze/json.hpp"
#include "glaze/json/study.hpp"
#include "glaze/record/recorder.hpp"
#include "glaze/trace/trace.hpp"
#include "glaze/util/progress_bar.hpp"
#include "ut/ut.hpp"
using namespace ut;
glz::trace trace{};
suite start_trace = [] { trace.begin("json_test", "Full test suite duration."); };
struct my_struct
{
int i = 287;
double d = 3.14;
std::string hello = "Hello World";
std::array<uint64_t, 3> arr = {1, 2, 3};
};
template <>
struct glz::meta<my_struct>
{
static constexpr std::string_view name = "my_struct";
using T = my_struct;
static constexpr auto value = object(
"i", [](auto&& v) -> auto& { return v.i; }, //
"d", &T::d, //
"hello", &T::hello, //
"arr", &T::arr //
);
};
static_assert(glz::write_json_supported<my_struct>);
static_assert(glz::read_json_supported<my_struct>);
suite starter = [] {
"example"_test = [] {
my_struct s{};
std::string buffer{};
expect(not glz::write_json(s, buffer));
expect(buffer == R"({"i":287,"d":3.14,"hello":"Hello World","arr":[1,2,3]})");
auto pretty = glz::prettify_json(buffer);
expect(pretty == R"({
"i": 287,
"d": 3.14,
"hello": "Hello World",
"arr": [
1,
2,
3
]
})") << pretty;
pretty = glz::prettify_json<glz::opts{.new_lines_in_arrays = false}>(buffer);
expect(pretty == R"({
"i": 287,
"d": 3.14,
"hello": "Hello World",
"arr": [1, 2, 3]
})") << pretty;
};
};
struct sub_thing
{
double a{3.14};
std::string b{"stuff"};
};
template <>
struct glz::meta<sub_thing>
{
static constexpr std::string_view name = "sub_thing";
static constexpr auto value = object("a", &sub_thing::a, //
"b", [](auto&& v) -> auto& { return v.b; } //
);
};
struct sub_thing2
{
double a{3.14};
std::string b{"stuff"};
double c{999.342494903};
double d{0.000000000001};
double e{203082348402.1};
float f{89.089f};
double g{12380.00000013};
double h{1000000.000001};
};
template <>
struct glz::meta<sub_thing2>
{
using T = sub_thing2;
static constexpr std::string_view name = "sub_thing2";
static constexpr auto value = object("a", &T::a, //
"b", &T::b, //
"c", &T::c, //
"d", &T::d, //
"e", &T::e, //
"f", &T::f, //
"g", &T::g, //
"h", &T::h //
);
};
struct V3
{
double x{3.14};
double y{2.7};
double z{6.5};
bool operator==(const V3& rhs) const { return (x == rhs.x) && (y == rhs.y) && (z == rhs.z); }
};
template <>
struct glz::meta<V3>
{
static constexpr std::string_view name = "V3";
static constexpr auto value = array(&V3::x, &V3::y, &V3::z);
};
enum class Color { Red, Green, Blue };
template <>
struct glz::meta<Color>
{
static constexpr std::string_view name = "Color";
static constexpr auto value = enumerate("Red", Color::Red, //
"Green", Color::Green, //
"Blue", Color::Blue //
);
};
static_assert(glz::enum_name_v<Color::Red> == "Red");
static_assert(glz::detail::get_enum_name(Color::Green) == "Green");
suite get_enum_name_tests = [] {
"get_enum_name"_test = [] {
auto color = Color::Green;
const auto name = glz::detail::get_enum_name(color);
expect(name == "Green");
};
};
enum struct Vehicle : uint32_t { Car, Truck, Plane };
enum struct Shapes : uint32_t { circ, sq, triangle };
template <>
struct glz::meta<Vehicle>
{
using enum Vehicle;
static constexpr std::array keys{"Car", "Truck", "Plane"};
static constexpr std::array value{Car, Truck, Plane};
};
template <>
struct glz::meta<Shapes>
{
using enum Shapes;
static constexpr std::array keys{"Circle", "Square", "Triangle"};
static constexpr std::array value{circ, sq, triangle};
};
// static_assert(glz::reflect<Vehicle>::keys[uint32_t(Vehicle::Truck)] == "Truck");
suite glz_enum_test = [] {
"glz_enum"_test = [] {
auto name = glz::write_json(Vehicle::Plane).value();
expect(name == R"("Plane")") << name;
Vehicle vehicle{};
auto ec = glz::read_json(vehicle, name);
expect(not ec) << glz::format_error(ec, name);
expect(vehicle == Vehicle::Plane);
};
"glz_enum_map"_test = [] {
auto name = glz::write_json(Shapes::sq).value();
expect(name == R"("Square")") << name;
Shapes shape{};
expect(not glz::read_json(shape, name));
expect(shape == Shapes::sq);
};
};
enum class TestData : uint8_t { None, A, B, C, D, ERROR_E = 0xFF };
struct DummyData
{
uint32_t id{0};
int32_t a{0};
TestData b{TestData::None};
TestData c{TestData::None};
TestData d{TestData::None};
TestData e{TestData::None};
int64_t f{0};
};
template <>
struct glz::meta<TestData>
{
using enum TestData;
static constexpr auto value = enumerate(None, A, B, C, D, ERROR_E);
};
template <>
struct glz::meta<DummyData>
{
using T = DummyData;
static constexpr std::string_view name = "DummyData";
static constexpr auto value = object(&T::id, &T::a, &T::b, &T::c, &T::d, &T::e, &T::f);
};
suite test_data_struct_tests = [] {
"test_data_struct"_test = [] {
const std::vector<DummyData> test_data = {
DummyData{
.id = 0,
.a = 0,
.b = TestData::None,
.c = TestData::None,
.d = TestData::None,
.e = TestData::None,
.f = 0x00000000,
},
DummyData{
.id = 1,
.a = 1,
.b = TestData::A,
.c = TestData::B,
.d = TestData::A,
.e = TestData::B,
.f = 0xDDDDDDDD,
},
DummyData{
.id = 2,
.a = 6,
.b = TestData::A,
.c = TestData::B,
.d = TestData::C,
.e = TestData::D,
.f = 0xEEEEEEEE,
},
DummyData{
.id = 3,
.a = -1,
.b = TestData::ERROR_E,
.c = TestData::ERROR_E,
.d = TestData::ERROR_E,
.e = TestData::ERROR_E,
.f = 0xFFFFFFFF,
},
};
expect(glz::reflect<TestData>::keys.size() == 6);
std::string s = glz::write_json(test_data).value_or("error");
expect(s != "error") << s;
std::vector<DummyData> in_test_data;
expect(not glz::read_json(in_test_data, s));
};
};
struct var1_t
{
double x{};
};
template <>
struct glz::meta<var1_t>
{
using T = var1_t;
static constexpr std::string_view name = "var1_t";
static constexpr auto value = object("x", &T::x);
};
struct var2_t
{
double y{};
};
template <>
struct glz::meta<var2_t>
{
using T = var2_t;
static constexpr std::string_view name = "var2_t";
static constexpr auto value = object("y", &T::y);
};
struct Thing
{
sub_thing thing{};
std::array<sub_thing2, 1> thing2array{};
V3 vec3{};
std::list<int> list{6, 7, 8, 2};
std::array<std::string, 4> array = {"as\"df\\ghjkl", "pie", "42", "foo"};
std::vector<V3> vector = {{9.0, 6.7, 3.1}, {}};
int i{8};
double d{2};
bool b{};
char c{'W'};
std::variant<var1_t, var2_t> v{};
Color color{Color::Green};
std::vector<bool> vb = {true, false, false, true, true, true, true};
std::shared_ptr<sub_thing> sptr = std::make_shared<sub_thing>();
std::optional<V3> optional{};
std::deque<double> deque = {9.0, 6.7, 3.1};
std::map<std::string, int> map = {{"a", 4}, {"f", 7}, {"b", 12}};
std::map<int, double> mapi{{5, 3.14}, {7, 7.42}, {2, 9.63}};
sub_thing* thing_ptr{};
Thing() : thing_ptr(&thing){};
};
template <>
struct glz::meta<Thing>
{
using T = Thing;
static constexpr std::string_view name = "Thing";
static constexpr std::array<std::string_view, 2> required = {"thing", "i"};
static constexpr std::array<std::string_view, 1> examples = {R"({"thing":{},"i":42})"};
static constexpr auto value = object(
"thing", &T::thing, //
"thing2array", &T::thing2array, //
"vec3", &T::vec3, //
"list", &T::list, //
"deque", &T::deque, //
"vector", [](auto&& v) -> auto& { return v.vector; }, //
"i", [](auto&& v) -> auto& { return v.i; }, //
"d", &T::d, //
"b", &T::b, //
"c", &T::c, //
"v", &T::v, //
"color", &T::color, //
"vb", &T::vb, //
"sptr", &T::sptr, //
"optional", &T::optional, //
"array", &T::array, //
"map", &T::map, //
"mapi", &T::mapi, //
"thing_ptr", &T::thing_ptr //
);
};
struct Escaped
{
int escaped_key{};
std::string escaped_key2{"hi"};
std::string escape_chars{""};
};
template <>
struct glz::meta<Escaped>
{
static constexpr std::string_view name = "Escaped";
using T = Escaped;
static constexpr auto value = object(R"(escaped\"key)", &T::escaped_key, //
R"(escaped\"\"key2)", &T::escaped_key2, R"(escape_chars)", &T::escape_chars);
};
suite escaping_tests = [] {
"escaped_key"_test = [] {
std::string out;
Escaped obj{};
expect(not glz::write_json(obj, out));
expect(out == R"({"escaped\"key":0,"escaped\"\"key2":"hi","escape_chars":""})");
std::string in = R"({"escaped\"key":5,"escaped\"\"key2":"bye"})";
expect(!glz::read_json(obj, in));
expect(obj.escaped_key == 5);
expect(obj.escaped_key2 == "bye");
};
"ᇿ read"_test = [] {
std::string in = R"("\u11FF")";
std::string str{};
expect(!glz::read_json(str, in));
expect(str == "ᇿ") << str;
};
"escaped_characters read"_test = [] {
std::string in = R"({"escape_chars":"\b\f\n\r\t\u11FF"})";
Escaped obj{};
expect(glz::read_json(obj, in) == glz::error_code::none);
expect(obj.escape_chars == "\b\f\n\r\tᇿ") << obj.escape_chars;
};
"escaped_char read"_test = [] {
std::string in = R"("\b")";
char c{};
expect(glz::read_json(c, in) == glz::error_code::none);
expect(c == '\b');
in = R"("\f")";
expect(glz::read_json(c, in) == glz::error_code::none);
expect(c == '\f');
in = R"("\n")";
expect(glz::read_json(c, in) == glz::error_code::none);
expect(c == '\n');
in = R"("\r")";
expect(glz::read_json(c, in) == glz::error_code::none);
expect(c == '\r');
in = R"("\t")";
expect(glz::read_json(c, in) == glz::error_code::none);
expect(c == '\t');
};
"escaped_characters write"_test = [] {
std::string str = "\"\\\b\f\n\r\tᇿ";
std::string buffer{};
expect(not glz::write_json(str, buffer));
expect(buffer == R"("\"\\\b\f\n\r\tᇿ")") << buffer;
};
"escaped_char write"_test = [] {
std::string out{};
char c = '\b';
expect(not glz::write_json(c, out));
expect(out == R"("\b")");
c = '\f';
expect(not glz::write_json(c, out));
expect(out == R"("\f")");
c = '\n';
expect(not glz::write_json(c, out));
expect(out == R"("\n")");
c = '\r';
expect(not glz::write_json(c, out));
expect(out == R"("\r")");
c = '\t';
expect(not glz::write_json(c, out));
expect(out == R"("\t")");
};
};
template <std::floating_point T>
T generate_uniform()
{
static std::mt19937 gen(std::random_device{}());
static std::uniform_real_distribution<double> dis(0, 1);
int sign = (dis(gen) < 0.5) ? -1 : 1;
int min_exp = std::numeric_limits<T>::min_exponent;
int max_exp = std::numeric_limits<T>::max_exponent;
std::uniform_int_distribution<> exp_dis(min_exp, max_exp);
int exp = exp_dis(gen);
// Generate random significand
T sig = static_cast<T>(dis(gen));
// Combine to create the random float
return sign * std::ldexp(sig, exp);
}
template <std::floating_point T>
bool equal_within_ulps(T x, T y, size_t n)
{
if (std::abs(x) < (std::numeric_limits<T>::min)()) {
// a denormalized value - check that it is close enough to zero.
return std::abs(y) < (std::numeric_limits<T>::min)();
}
// Since `epsilon()` is the gap size (ULP, unit in the last place)
// of floating-point numbers in interval [1, 2), we can scale it to
// the gap size in interval [2^e, 2^{e+1}), where `e` is the exponent
// of `x` and `y`.
// If `x` and `y` have different gap sizes (which means they have
// different exponents), we take the smaller one. Taking the bigger
// one is also reasonable, I guess.
const T m = (std::min)(std::abs(x), std::abs(y));
// Subnormal numbers have fixed exponent, which is `min_exponent - 1`.
const int exp = m < (std::numeric_limits<T>::min)() ? std::numeric_limits<T>::min_exponent - 1 : std::ilogb(m);
// We consider `x` and `y` equal if the difference between them is
// within `n` ULPs.
return std::abs(x - y) <= n * std::ldexp(std::numeric_limits<T>::epsilon(), exp);
}
template <std::floating_point T>
bool equal(T x, T y)
{
if (std::abs(x) < (std::numeric_limits<T>::min)()) {
// a denormalized value - check that it is close enough to zero.
return std::abs(y) < (std::numeric_limits<T>::min)();
}
return x == y;
}
suite basic_types = [] {
using namespace ut;
"double write"_test = [] {
std::string buffer{};
expect(not glz::write_json(3.14, buffer));
expect(buffer == "3.14") << buffer;
expect(not glz::write_json(9.81, buffer));
expect(buffer == "9.81") << buffer;
expect(not glz::write_json(0.0, buffer));
expect(buffer == "0") << buffer;
expect(not glz::write_json(-0.0, buffer));
expect(buffer == "-0") << buffer;
expect(not glz::write_json(-8536070.0, buffer));
expect(buffer == "-8536070") << buffer;
expect(not glz::write_json(8536070.0, buffer));
expect(buffer == "8536070") << buffer;
expect(not glz::write_json(std::numeric_limits<double>::infinity(), buffer));
expect(buffer == "null") << buffer;
expect(not glz::write_json(1.0, buffer));
expect(buffer == "1") << buffer;
expect(not glz::write_json(10.0, buffer));
expect(buffer == "10") << buffer;
expect(not glz::write_json(100.0, buffer));
expect(buffer == "100") << buffer;
expect(not glz::write_json(0.1, buffer));
expect(buffer == "0.1") << buffer;
expect(not glz::write_json(0.01, buffer));
expect(buffer == "0.01") << buffer;
expect(not glz::write_json(0.001, buffer));
expect(buffer == "0.001") << buffer;
};
"double roundtrip"_test = [] {
for (const double expected : {-0x1.e42427b42cb42p+949, -0x1.3ffff0d0ddb37p+725, 0x1.73d40c08b20ffp-395}) {
double d{expected};
auto str = glz::write_json(d).value();
auto restored = glz::read_json<double>(str);
expect(restored.has_value());
expect(restored.value() == d);
}
};
"float write"_test = [] {
std::string buffer{};
expect(not glz::write_json(3.14f, buffer));
expect(buffer == "3.14") << buffer;
expect(not glz::write_json(9.81f, buffer));
expect(buffer == "9.81") << buffer;
expect(not glz::write_json(0.0f, buffer));
expect(buffer == "0") << buffer;
expect(not glz::write_json(-0.0f, buffer));
expect(buffer == "-0") << buffer;
expect(not glz::write_json(-8536070.f, buffer));
expect(buffer == "-8536070") << buffer;
expect(not glz::write_json(8536070.f, buffer));
expect(buffer == "8536070") << buffer;
expect(not glz::write_json(std::numeric_limits<float>::infinity(), buffer));
expect(buffer == "null") << buffer;
expect(not glz::write_json(1.0f, buffer));
expect(buffer == "1") << buffer;
expect(not glz::write_json(10.0f, buffer));
expect(buffer == "10") << buffer;
expect(not glz::write_json(100.0f, buffer));
expect(buffer == "100") << buffer;
expect(not glz::write_json(0.1f, buffer));
expect(buffer == "0.1") << buffer;
expect(not glz::write_json(0.01f, buffer));
expect(buffer == "0.01") << buffer;
expect(not glz::write_json(0.001f, buffer));
expect(buffer == "0.001") << buffer;
};
"double read valid"_test = [] {
double num{};
expect(glz::read_json(num, "3.14") == glz::error_code::none);
expect(num == 3.14);
expect(glz::read_json(num, "9.81") == glz::error_code::none);
expect(num == 9.81);
expect(glz::read_json(num, "0") == glz::error_code::none);
expect(num == 0);
expect(glz::read_json(num, "-0") == glz::error_code::none);
expect(num == -0);
};
"double write/read"_test = [] {
double x{};
auto buffer = glz::write_json(-1.40129846e-45).value();
auto ec = glz::read_json(x, buffer);
expect(not ec) << glz::format_error(ec, buffer);
};
"random double"_test = [] {
for (size_t i = 0; i < 10000; ++i) {
double x = generate_uniform<double>();
auto buffer = glz::write_json(x).value();
double y{};
auto ec = glz::read_json(y, buffer);
expect(not ec) << glz::format_error(ec, buffer);
expect(equal(x, y)) << x << ", " << y;
}
};
"float write/read"_test = [] {
float x{};
auto buffer = glz::write_json(-1.40129846e-45f).value();
auto ec = glz::read_json(x, buffer);
expect(not ec) << glz::format_error(ec, buffer);
};
"random float"_test = [] {
for (size_t i = 0; i < 10000; ++i) {
float x = generate_uniform<float>();
auto buffer = glz::write_json(x).value();
float y{};
auto ec = glz::read_json(y, buffer);
expect(not ec) << glz::format_error(ec, buffer);
expect(equal(x, y)) << x << ", " << y;
}
};
"int write"_test = [] {
std::string buffer{};
expect(not glz::write_json(0, buffer));
expect(buffer == "0");
buffer.clear();
expect(not glz::write_json(999, buffer));
expect(buffer == "999");
buffer.clear();
expect(not glz::write_json(-6, buffer));
expect(buffer == "-6");
buffer.clear();
expect(not glz::write_json(10000, buffer));
expect(buffer == "10000");
};
"int read valid"_test = [] {
int num{};
expect(glz::read_json(num, "-1") == glz::error_code::none);
expect(num == -1);
expect(glz::read_json(num, "0") == glz::error_code::none);
expect(num == 0);
expect(glz::read_json(num, "999") == glz::error_code::none);
expect(num == 999);
expect(glz::read_json(num, "1e4") == glz::error_code::none);
expect(num == 10000);
uint64_t num64{};
expect(glz::read_json(num64, "32948729483739289") == glz::error_code::none);
expect(num64 == 32948729483739289);
};
"int read invalid"_test = [] {
int num{33};
expect(glz::read_json(num, ";adsfa") == glz::error_code::parse_number_failure);
expect(glz::read_json(num, "{}") == glz::error_code::parse_number_failure);
expect(glz::read_json(num, "[]") == glz::error_code::parse_number_failure);
expect(glz::read_json(num, ".") == glz::error_code::parse_number_failure);
expect(glz::read_json(num, "0045") == glz::error_code::parse_number_failure);
};
"bool write"_test = [] {
std::string buffer{};
expect(not glz::write_json(true, buffer));
expect(buffer == "true");
buffer.clear();
expect(not glz::write_json(false, buffer));
expect(buffer == "false");
};
"bool read valid"_test = [] {
bool val{};
expect(glz::read_json(val, "true") == glz::error_code::none);
expect(val == true);
expect(glz::read_json(val, "false") == glz::error_code::none);
expect(val == false);
};
"bool read invalid"_test = [] {
bool val{};
expect(glz::read_json(val, "tru") != glz::error_code::none);
expect(glz::read_json(val, "alse") != glz::error_code::none);
};
"string write"_test = [] {
std::string buffer{};
expect(not glz::write_json("fish", buffer));
expect(buffer == "\"fish\"");
buffer.clear();
expect(not glz::write_json("as\"df\\ghjkl", buffer));
expect(buffer == "\"as\\\"df\\\\ghjkl\"");
"empty"_test = [] {
static constexpr std::string_view expected_empty{"\"\""};
static constexpr std::string_view expected_nothing{};
expect(glz::write_json(std::string_view{}) == expected_empty);
expect(glz::write_json(std::string{}) == expected_empty);
expect(glz::write_json("") == expected_empty);
auto write_raw = [](const auto& input) {
std::string result{};
expect(not glz::write<glz::opts{.raw = true}>(input, result));
return result;
};
expect(write_raw(std::string_view{}) == expected_nothing);
expect(write_raw(std::string{}) == expected_nothing);
expect(write_raw("") == expected_nothing);
auto write_raw_str = [](const auto& input) {
std::string result{};
expect(not glz::write<glz::opts{.raw_string = true}>(input, result));
return result;
};
expect(write_raw_str(std::string_view{}) == expected_empty);
expect(write_raw_str(std::string{}) == expected_empty);
expect(write_raw_str("") == expected_empty);
auto write_num = [](const auto& input) {
std::string result{};
expect(not glz::write<glz::opts{.number = true}>(input, result));
return result;
};
expect(write_num(std::string_view{}) == expected_nothing);
expect(write_num(std::string{}) == expected_nothing);
expect(write_num("") == expected_nothing);
};
};
"backslash testing"_test = [] {
std::string val{};
expect(glz::read_json(val, "\"fish\"") == glz::error_code::none);
expect(val == "fish");
expect(glz::read_json(val, "\"as\\\"df\\\\ghjkl\"") == glz::error_code::none);
expect(val == "as\"df\\ghjkl");
};
"string_view read"_test = [] {
std::string_view val{};
expect(glz::read_json(val, "\"fish\"") == glz::error_code::none);
expect(val == "fish");
expect(glz::read_json(val, "\"as\\\"df\\\\ghjkl\"") == glz::error_code::none);
expect(val == "as\\\"df\\\\ghjkl");
};
};
suite container_types = [] {
using namespace ut;
"vector int roundtrip"_test = [] {
std::vector<int> vec(100);
for (auto& item : vec) item = rand();
std::string buffer{};
std::vector<int> vec2{};
expect(not glz::write_json(vec, buffer));
expect(glz::read_json(vec2, buffer) == glz::error_code::none);
expect(vec == vec2);
};
"vector uint64_t roundtrip"_test = [] {
std::uniform_int_distribution<uint64_t> dist((std::numeric_limits<uint64_t>::min)(),
(std::numeric_limits<uint64_t>::max)());
std::mt19937 gen{};
std::vector<uint64_t> vec(100);
for (auto& item : vec) item = dist(gen);
std::string buffer{};
std::vector<uint64_t> vec2{};
expect(not glz::write_json(vec, buffer));
expect(glz::read_json(vec2, buffer) == glz::error_code::none);
expect(vec == vec2);
};
"vector double roundtrip"_test = [] {
std::vector<double> vec(100);
for (auto& item : vec) item = rand() / (1.0 + rand());
std::string buffer{};
std::vector<double> vec2{};
expect(not glz::write_json(vec, buffer));
expect(glz::read_json(vec2, buffer) == glz::error_code::none);
expect(vec == vec2);
};
"vector float roundtrip"_test = [] {
std::vector<float> vec(100);
for (auto& item : vec) item = rand() / (1.0 + rand());
std::string buffer{};
std::vector<float> vec2{};
expect(not glz::write_json(vec, buffer));
expect(not glz::read_json(vec2, buffer));
expect(vec == vec2);
};
"vector bool roundtrip"_test = [] {
std::vector<bool> vec(100);
for (auto&& item : vec) item = rand() / (1.0 + rand());
std::string buffer{};
std::vector<bool> vec2{};
expect(not glz::write_json(vec, buffer));
expect(glz::read_json(vec2, buffer) == glz::error_code::none);
expect(vec == vec2);
};
"vector pair"_test = [] {
std::vector<std::pair<int, int>> v;
expect(!glz::read<glz::opts{.concatenate = false}>(v, R"([{"1":2},{"3":4}])"));
const auto s = glz::write<glz::opts{.concatenate = false}>(v).value_or("error");
expect(s == R"([{"1":2},{"3":4}])") << s;
};
"vector pair"_test = [] {
std::vector<std::pair<int, int>> v;
expect(!glz::read<glz::opts{.concatenate = false}>(v, R"([{"1":2},{"3":4}])"));
const auto s = glz::write<glz::opts{.prettify = true, .concatenate = false}>(v).value_or("error");
expect(s == R"([
{
"1": 2
},
{
"3": 4
}
])") << s;
};
"vector pair roundtrip"_test = [] {
std::vector<std::pair<int, int>> v;
expect(!glz::read_json(v, R"({"1":2,"3":4})"));
const auto s = glz::write_json(v).value_or("error");
expect(s == R"({"1":2,"3":4})") << s;
};
"vector pair roundtrip"_test = [] {
std::vector<std::pair<int, int>> v;
expect(!glz::read_json(v, R"({"1":2,"3":4})"));
const auto s = glz::write<glz::opts{.prettify = true}>(v).value_or("error");
expect(s == R"({
"1": 2,
"3": 4
})") << s;
};
"deque roundtrip"_test = [] {
std::vector<int> deq(100);
for (auto& item : deq) item = rand();
std::string buffer{};
std::vector<int> deq2{};
expect(not glz::write_json(deq, buffer));
expect(glz::read_json(deq2, buffer) == glz::error_code::none);
expect(deq == deq2);
};
"list roundtrip"_test = [] {
std::list<int> lis(100);
for (auto& item : lis) item = rand();
std::string buffer{};
std::list<int> lis2{};
expect(not glz::write_json(lis, buffer));
expect(glz::read_json(lis2, buffer) == glz::error_code::none);
expect(lis == lis2);
};
"forward_list roundtrip"_test = [] {
std::forward_list<int> lis(100);
for (auto& item : lis) item = rand();
std::string buffer{};
std::forward_list<int> lis2{};
expect(not glz::write_json(lis, buffer));
expect(glz::read_json(lis2, buffer) == glz::error_code::none);
expect(lis == lis2);
};
"map string keys roundtrip"_test = [] {
std::map<std::string, int> map;
std::string str{"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
std::mt19937 g{};
for (auto i = 0; i < 20; ++i) {
std::shuffle(str.begin(), str.end(), g);
map[str] = rand();
}
std::string buffer{};
std::map<std::string, int> map2{};
static_assert(glz::detail::writable_map_t<decltype(map2)>);
expect(not glz::write_json(map, buffer));
expect(glz::read_json(map2, buffer) == glz::error_code::none);
// expect(map == map2);
for (auto& it : map) {
expect(map2[it.first] == it.second);
}
};
"map int keys roundtrip"_test = [] {
std::map<int, int> map;
for (auto i = 0; i < 20; ++i) {
map[rand()] = rand();
}
std::string buffer{};
std::map<int, int> map2{};
expect(not glz::write_json(map, buffer));
expect(glz::read_json(map2, buffer) == glz::error_code::none);
// expect(map == map2);
for (auto& it : map) {
expect(map2[it.first] == it.second);
}
};
"unordered_map int keys roundtrip"_test = [] {
std::unordered_map<int, int> map;
for (auto i = 0; i < 20; ++i) {
map[rand()] = rand();
}
std::string buffer{};
std::unordered_map<int, int> map2{};
expect(not glz::write_json(map, buffer));
expect(glz::read_json(map2, buffer) == glz::error_code::none);
// expect(map == map2);
for (auto& it : map) {
expect(map2[it.first] == it.second);
}
};
"unordered_map<int, std::string> roundtrip"_test = [] {
std::unordered_map<int, std::string> map;
for (auto i = 0; i < 5; ++i) {
map[rand()] = std::to_string(rand());
}
std::string buffer{};
std::unordered_map<int, std::string> map2{};
expect(not glz::write_json(map, buffer));
expect(!glz::read_json(map2, buffer));
for (auto& it : map) {
expect(map2[it.first] == it.second);
}
};
"tuple roundtrip"_test = [] {
auto tuple = std::make_tuple(3, 2.7, std::string("curry"));
decltype(tuple) tuple2{};
std::string buffer{};
expect(not glz::write_json(tuple, buffer));
expect(glz::read_json(tuple2, buffer) == glz::error_code::none);
expect(tuple == tuple2);
};
"pair roundtrip"_test = [] {
auto pair = std::make_pair(std::string("water"), 5.2);
decltype(pair) pair2{};
std::string buffer{};
expect(not glz::write_json(pair, buffer));
expect(glz::read_json(pair2, buffer) == glz::error_code::none);
expect(pair == pair2);
};
};
suite nullable_types = [] {
using namespace ut;
"optional"_test = [] {
std::optional<int> oint{};
std::string buffer{};
expect(not glz::write_json(oint, buffer));
expect(buffer == "null");
expect(glz::read_json(oint, "5") == glz::error_code::none);
expect(bool(oint) && *oint == 5);
buffer.clear();
expect(not glz::write_json(oint, buffer));
expect(buffer == "5");
expect(glz::read_json(oint, "null") == glz::error_code::none);
expect(!bool(oint));
buffer.clear();
expect(not glz::write_json(oint, buffer));
expect(buffer == "null");
};
"shared_ptr"_test = [] {
std::shared_ptr<int> ptr{};
std::string buffer{};
expect(not glz::write_json(ptr, buffer));
expect(buffer == "null");
expect(glz::read_json(ptr, "5") == glz::error_code::none);
expect(bool(ptr) && *ptr == 5);
buffer.clear();
expect(not glz::write_json(ptr, buffer));