forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_serializer.cpp
More file actions
622 lines (524 loc) · 21.4 KB
/
Copy pathtest_serializer.cpp
File metadata and controls
622 lines (524 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest.h>
#include <taskflow/utility/serializer.hpp>
#include <random>
// ----------------------------------------------------------------------------
// Random generator utilities
// ----------------------------------------------------------------------------
// Function: random_engine
inline std::default_random_engine& random_engine() {
thread_local std::default_random_engine gen{0};
return gen;
}
// Function: random
// Randomly generate a floating value in the given range.
template <typename T>
std::enable_if_t<std::is_floating_point<T>::value, T> random(
const T from = -1.0,
const T to = 1.0
) {
return std::uniform_real_distribution<T>(from, to)(random_engine());
}
// Function: random
// Randomly generate an integer value.
template <typename T>
std::enable_if_t<std::is_integral<T>::value, T> random(
const T from = std::numeric_limits<T>::lowest(),
const T to = std::numeric_limits<T>::max()
) {
return std::uniform_int_distribution<T>(from, to)(random_engine());
}
// Function: random
// Randomly generate a string.
template <typename T>
std::enable_if_t<std::is_same<T, std::string>::value, T> random(
const std::string::value_type from = ' ',
const std::string::value_type to = '~',
const std::string::size_type len = 16
) {
std::string str(len, ' ');
for(auto& c : str) {
c = random<int>(from, to) % 128;
}
return str;
}
// ----------------------------------------------------------------------------
// Struct: PODs
struct PODs {
unsigned pod_uint32 = random<decltype(pod_uint32)>();
int pod_int32 = random<decltype(pod_int32)>();
unsigned long long pod_uint64 = random<decltype(pod_uint64)>();
long long pod_int64 = random<decltype(pod_int64)>();
float_t pod_float = random<decltype(pod_float)>();
double_t pod_double = random<decltype(pod_double)>();
template <typename ArchiverT>
auto save( ArchiverT& ar ) const {
return ar(
pod_uint32,
pod_int32,
pod_uint64,
pod_int64,
pod_float,
pod_double
);
}
template <typename ArchiverT>
auto load( ArchiverT& ar ) {
return ar(
pod_uint32,
pod_int32,
pod_uint64,
pod_int64,
pod_float,
pod_double
);
}
bool operator == (const PODs& rhs) const {
return pod_uint32 == rhs.pod_uint32 &&
pod_int32 == rhs.pod_int32 &&
pod_uint64 == rhs.pod_uint64 &&
pod_int64 == rhs.pod_int64 &&
pod_float == rhs.pod_float &&
pod_double == rhs.pod_double;
}
bool operator != (const PODs& rhs) const {
return !(*this == rhs);
}
};
// Procedure: test_pod
// The templated procedure for testing POD. Caller must specify the output
// and input archiver type.
void test_pod() {
// Output stream.
std::ostringstream os;
tf::Serializer oar(os);
const auto o_uint32 = random<unsigned>();
const auto o_int32 = random<int>();
const auto o_uint64 = random<unsigned long long>();
const auto o_int64 = random<long long>();
const auto o_float = random<float>();
const auto o_double = random<double>();
auto o_sz = oar(
o_uint32,
o_int32,
o_uint64,
o_int64,
o_float,
o_double
);
//REQUIRE(o_sz == os.out_avail());
// InputStreamBuffer
std::istringstream is(os.str());
tf::Deserializer iar(is);
auto i_uint32 = random<unsigned>();
auto i_int32 = random<int>();
auto i_uint64 = random<unsigned long long>();
auto i_int64 = random<long long>();
auto i_float = random<float>();
auto i_double = random<double>();
auto i_sz = iar(
i_uint32,
i_int32,
i_uint64,
i_int64,
i_float,
i_double
);
REQUIRE(is.rdbuf()->in_avail() == 0);
REQUIRE(i_sz == o_sz);
REQUIRE(o_uint32 == i_uint32);
REQUIRE(o_int32 == i_int32);
REQUIRE(o_uint64 == i_uint64);
REQUIRE(o_int64 == i_int64);
REQUIRE(o_float == i_float);
REQUIRE(o_double == i_double);
}
// Procedure: test_struct
// The templated procedure for testing POD. Caller must specify the output
// and input archiver type.
void test_struct() {
for(size_t i=0; i<64; ++i) {
// POD struct.
PODs o_pods;
PODs i_pods;
// Outputstream
std::ostringstream os;
tf::Serializer oar(os);
auto o_sz = oar(o_pods);
//REQUIRE(o_sz == os.out_avail());
// Inputstream
std::istringstream is(os.str());
tf::Deserializer iar(is);
auto i_sz = iar(i_pods);
REQUIRE(is.rdbuf()->in_avail() == 0);
REQUIRE(o_sz == i_sz);
REQUIRE(o_pods == i_pods);
}
}
// Procedure: test_string
// Template for testing basic strings. Caller must specify the output and input archiver type.
template <typename T>
void test_string() {
for(size_t i=0; i<4096; i++) {
// Outputstream
std::ostringstream os;
tf::Serializer oar(os);
T o_char_str = random<T>();
auto o_sz = oar(o_char_str);
//REQUIRE(o_sz == os.out_avail());
// Inputstream
std::istringstream is(os.str());
tf::Deserializer iar(is);
T i_char_str;
auto i_sz = iar(i_char_str);
REQUIRE(is.rdbuf()->in_avail() == 0);
REQUIRE(o_sz == i_sz);
REQUIRE(o_char_str == i_char_str);
}
}
#define TEST_SEQ_CONT_BODY(container) \
\
for(size_t i=0; i<64; i++) { \
const size_t num_data = random<size_t>(1, 1024); \
std::ostringstream os; \
tf::Serializer oar(os); \
\
std::container <int> o_int32s (num_data); \
std::container <long long> o_int64s (num_data); \
std::container <char> o_chars (num_data); \
std::container <float> o_floats (num_data); \
std::container <double> o_doubles (num_data); \
std::container <std::string> o_strings (num_data); \
std::container <PODs> o_podses (num_data); \
\
for(auto& v : o_int32s) v = random<int>(); \
for(auto& v : o_int64s) v = random<long long>(); \
for(auto& v : o_chars) v = random<int>(); \
for(auto& v : o_floats) v = random<float>(); \
for(auto& v : o_doubles) v = random<double>(); \
for(auto& v : o_strings) v = random<std::string>(); \
\
auto o_sz = oar(o_int32s, o_int64s, o_chars, o_floats, o_doubles, o_strings, o_podses);\
\
std::istringstream is(os.str()); \
tf::Deserializer iar(is); \
\
std::container <int> i_int32s; \
std::container <long long> i_int64s; \
std::container <char> i_chars; \
std::container <float> i_floats; \
std::container <double> i_doubles; \
std::container <std::string> i_strings; \
std::container <PODs> i_podses; \
\
auto i_sz = iar(i_int32s, i_int64s, i_chars, i_floats, i_doubles, i_strings, i_podses);\
\
REQUIRE(o_sz == i_sz); \
REQUIRE(o_int32s == i_int32s); \
REQUIRE(o_int64s == i_int64s); \
REQUIRE(o_chars == i_chars); \
REQUIRE(o_floats == i_floats); \
REQUIRE(o_doubles == i_doubles); \
REQUIRE(o_strings == i_strings); \
REQUIRE(o_podses == i_podses); \
}
#define TEST_MAP_CONT_BODY(container) \
\
for (size_t i = 0; i < 64; i++) { \
const size_t num_data = random<size_t>(1, 1024); \
std::ostringstream os; \
tf::Serializer oar(os); \
\
std::container<int, int> o_int32s; \
std::container<long long, long long> o_int64s; \
std::container<char, char> o_chars; \
std::container<float, float> o_floats; \
std::container<double, double> o_doubles; \
std::container<std::string, std::string> o_strings; \
\
for (size_t j = 0; j < num_data; j++) { \
o_int32s.emplace(random<int>(), random<int>()); \
o_int64s.emplace(random<long long>(), random<long long>()); \
o_chars.emplace(random<int>(), random<int>()); \
o_floats.emplace(random<float_t>(), random<float_t>()); \
o_doubles.emplace(random<double_t>(), random<double_t>()); \
o_strings.emplace(random<std::string>(), random<std::string>()); \
} \
\
auto o_sz = oar(o_int32s, o_int64s, o_chars, o_floats, o_doubles , o_strings); \
\
std::istringstream is(os.str()); \
tf::Deserializer iar(is); \
\
std::container<int, int> i_int32s; \
std::container<long long, long long> i_int64s; \
std::container<char, char> i_chars; \
std::container<float, float> i_floats; \
std::container<double, double> i_doubles; \
std::container<std::string, std::string> i_strings; \
\
auto i_sz = iar(i_int32s, i_int64s, i_chars, i_floats, i_doubles , i_strings); \
REQUIRE(0 == is.rdbuf()->in_avail()); \
\
REQUIRE(o_sz == i_sz); \
REQUIRE(o_int32s == i_int32s); \
REQUIRE(o_int64s == i_int64s); \
REQUIRE(o_chars == i_chars); \
REQUIRE(o_floats == i_floats); \
REQUIRE(o_doubles == i_doubles); \
REQUIRE(o_strings == i_strings); \
}
#define TEST_SET_CONT_BODY(container) \
\
for (size_t i = 0; i < 64; i++) { \
const size_t num_data = random<size_t>(1, 1024); \
std::ostringstream os; \
tf::Serializer oar(os); \
\
std::container<int> o_int32s; \
std::container<long long> o_int64s; \
std::container<char> o_chars; \
std::container<float> o_floats; \
std::container<double> o_doubles; \
std::container<std::string> o_strings; \
\
for (size_t j = 0; j < num_data; j++) { \
o_int32s.emplace(random<int>()); \
o_int64s.emplace(random<long long>()); \
o_chars.emplace(random<int>()); \
o_floats.emplace(random<float_t>()); \
o_doubles.emplace(random<double_t>()); \
o_strings.emplace(random<std::string>()); \
} \
auto o_sz = oar(o_int32s, o_int64s, o_chars, o_floats, o_doubles, o_strings); \
\
std::istringstream is(os.str()); \
tf::Deserializer iar(is); \
\
std::container<int> i_int32s; \
std::container<long long> i_int64s; \
std::container<char> i_chars; \
std::container<float> i_floats; \
std::container<double> i_doubles; \
std::container<std::string> i_strings; \
\
auto i_sz = iar(i_int32s, i_int64s, i_chars, i_floats, i_doubles, i_strings); \
REQUIRE(0 == is.rdbuf()->in_avail()); \
\
REQUIRE(o_sz == i_sz); \
REQUIRE(o_int32s == i_int32s); \
REQUIRE(o_int64s == i_int64s); \
REQUIRE(o_chars == i_chars); \
REQUIRE(o_floats == i_floats); \
REQUIRE(o_doubles == i_doubles); \
REQUIRE(o_strings == i_strings); \
}
// Procedure: test_array
// Template procedure for testing array container.
void test_array() {
for(size_t i=0; i<64; ++i) {
// Output
std::array<char, 1> ochar;
std::array<int, 512> oint;
std::array<double, 1024> odouble;
std::array<std::string, 2048> ostring;
for(auto &i : ochar) i = random<int>();
for(auto &i : oint) i = random<int>();
for(auto &i : odouble) i = random<double>();
for(auto &i : ostring) i = random<std::string>();
std::ostringstream os;
tf::Serializer oar(os);
auto osz = oar(ochar, oint, odouble, ostring);
// Input
std::array<char, 1> ichar;
std::array<int, 512> iint;
std::array<double, 1024> idouble;
std::array<std::string, 2048> istring;
std::istringstream is(os.str());
tf::Deserializer iar(is);
auto isz = iar(ichar, iint, idouble, istring);
REQUIRE(0 == is.rdbuf()->in_avail());
REQUIRE(osz == isz);
REQUIRE(ochar == ichar);
REQUIRE(oint == iint);
REQUIRE(odouble == idouble);
REQUIRE(ostring == istring);
}
}
// Procedure: test_variant
void test_variant() {
for (size_t i = 0; i < 64; i++) {
// Single POD variant.
std::variant<int> opod1 = random<int>();
std::variant<int> ipod1 = random<int>();
// Multiple POD variant
std::variant<int, double> opod2 = random<double>();
std::variant<int, double> ipod2 = random<int>();
// Multiple POD variant
std::variant<int, double, bool> opod3 = random<int>()%2;
std::variant<int, double, bool> ipod3 = random<double>();
// Mixing float and string
std::variant<float, std::string> omix2 = random<std::string>();
std::variant<float, std::string> imix2 = random<float>();
// Recursive variant
std::variant<int, decltype(omix2)> orec2 = omix2;
std::variant<int, decltype(omix2)> irec2 = random<int>();
// Output archiver
std::ostringstream os;
tf::Serializer oar(os);
auto osz = oar(opod1, opod2, opod3, omix2, orec2);
// Input archiver
std::istringstream is(os.str());
tf::Deserializer iar(is);
auto isz = iar(ipod1, ipod2, ipod3, imix2, irec2);
REQUIRE(0 == is.rdbuf()->in_avail());
REQUIRE(osz == isz);
REQUIRE(opod1 == ipod1);
REQUIRE(opod2 == ipod2);
REQUIRE(opod3 == ipod3);
REQUIRE(omix2 == imix2);
REQUIRE(orec2 == irec2);
}
}
// Procedure: test_time_point
void test_time_point() {
for(auto i=0; i<64; ++i) {
auto o_tpt1 = std::chrono::system_clock::now();
auto o_tpt2 = std::chrono::steady_clock::now();
auto o_tpt3 = std::chrono::high_resolution_clock::now();
auto o_dur1 = std::chrono::system_clock::now() - o_tpt1;
auto o_dur2 = std::chrono::steady_clock::now() - o_tpt2;
auto o_dur3 = std::chrono::high_resolution_clock::now() - o_tpt3;
// Output archiver
std::ostringstream os;
tf::Serializer oar(os);
auto osz = oar(o_tpt1, o_tpt2, o_tpt3, o_dur1, o_dur2, o_dur3);
decltype(o_tpt1) i_tpt1;
decltype(o_tpt2) i_tpt2;
decltype(o_tpt3) i_tpt3;
decltype(o_dur1) i_dur1;
decltype(o_dur2) i_dur2;
decltype(o_dur3) i_dur3;
// Input archiver
std::istringstream is(os.str());
tf::Deserializer iar(is);
auto isz = iar(i_tpt1, i_tpt2, i_tpt3, i_dur1, i_dur2, i_dur3);
REQUIRE(0 == is.rdbuf()->in_avail());
REQUIRE(osz == isz);
REQUIRE(o_tpt1 == i_tpt1);
REQUIRE(o_tpt2 == i_tpt2);
REQUIRE(o_tpt3 == i_tpt3);
REQUIRE(o_dur1 == i_dur1);
REQUIRE(o_dur2 == i_dur2);
REQUIRE(o_dur3 == i_dur3);
}
}
// Procedure: test_optional
void test_optional() {
for(auto i=0; i<64; ++i) {
std::optional<bool> o_nbool, i_nbool{true};
std::optional<bool> o_ybool{true}, i_ybool;
std::optional<std::string> o_nstr, i_nstr{random<std::string>()};
std::optional<std::string> o_ystr{random<std::string>()}, i_ystr;
// Output archiver
std::ostringstream os;
tf::Serializer oar(os);
auto osz = oar(o_nbool, o_ybool, o_nstr, o_ystr);
// Input archiver
std::istringstream is(os.str());
tf::Deserializer iar(is);
auto isz = iar(i_nbool, i_ybool, i_nstr, i_ystr);
REQUIRE(0 == is.rdbuf()->in_avail());
REQUIRE(osz == isz);
REQUIRE(o_nbool == i_nbool);
REQUIRE(o_ybool == i_ybool);
REQUIRE(o_nstr == i_nstr);
REQUIRE(o_ystr == i_ystr);
}
}
// Procedure: test_tuple
void test_tuple() {
for(auto i=0; i<64; ++i) {
std::tuple<> o0, i0;
std::tuple<char> o1 {'a'}, i1 {'b'};
std::tuple<int, double> o2 {1, 2.4}, i2 {3, 0.9};
std::tuple<std::string, std::vector<int>, float> o3{"123", {1, 2, 3}, 4.5f}, i3;
std::tuple<int, std::tuple<int, int>, int> o4 {1, {2, 3}, 4}, i4;
// Output archiver
std::ostringstream os;
tf::Serializer oar(os);
auto osz = oar(o0, o1, o2, o3, o4);
// Input archiver
std::istringstream is(os.str());
tf::Deserializer iar(is);
auto isz = iar(i0, i1, i2, i3, i4);
REQUIRE(0 == is.rdbuf()->in_avail());
REQUIRE(osz == isz);
REQUIRE(o0 == i0);
REQUIRE(o1 == i1);
REQUIRE(o2 == i2);
REQUIRE(o3 == i3);
REQUIRE(o4 == i4);
}
}
// ----------------------------------------------------------------------------
// POD
TEST_CASE("POD" * doctest::timeout(300)) {
test_pod();
}
// POD-struct
TEST_CASE("POD-Struct" * doctest::timeout(300)) {
test_struct();
}
// std::string
TEST_CASE("string" * doctest::timeout(300)) {
test_string<std::string>();
}
// std::vector
TEST_CASE("vector" * doctest::timeout(300)) {
TEST_SEQ_CONT_BODY(vector)
}
// std::deque
TEST_CASE("deque" * doctest::timeout(300)) {
TEST_SEQ_CONT_BODY(deque)
}
// std::list
TEST_CASE("list" * doctest::timeout(300)) {
TEST_SEQ_CONT_BODY(list)
}
// std::forward_list
TEST_CASE("forward_list" * doctest::timeout(300)) {
TEST_SEQ_CONT_BODY(forward_list)
}
// std::map
TEST_CASE("map" * doctest::timeout(300)) {
TEST_MAP_CONT_BODY(map);
}
// std::unordered_map
TEST_CASE("unordered_map" * doctest::timeout(300)) {
TEST_MAP_CONT_BODY(unordered_map);
}
// std::set
TEST_CASE("set" * doctest::timeout(300)) {
TEST_SET_CONT_BODY(set);
}
// std::unordered_set
TEST_CASE("unordered_set" * doctest::timeout(300)) {
TEST_SET_CONT_BODY(unordered_set);
}
// std::array
TEST_CASE("array" * doctest::timeout(300)) {
test_array();
}
// std::chrono::time_point
TEST_CASE("time_point" * doctest::timeout(300)) {
test_time_point();
}
// std::optional
TEST_CASE("optional" * doctest::timeout(300)) {
test_optional();
}
// std::tuple
TEST_CASE("tuple" * doctest::timeout(300)) {
test_tuple();
}