/
test_packer.cpp
952 lines (869 loc) · 32.6 KB
/
test_packer.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
#include <filesystem>
#include <fstream>
#include <vector>
#include <binary_log/binary_log.hpp>
#include <doctest.hpp>
using doctest::test_suite;
static constexpr auto test_file = "test.log";
static constexpr auto test_file_index = "test.log.index";
static constexpr auto test_file_runlength = "test.log.runlength";
auto read_file(std::string_view path) -> std::vector<uint8_t>
{
std::ifstream infile(path.data(), std::ios::binary);
return std::vector<uint8_t>(std::istreambuf_iterator<char>(infile), {});
}
TEST_CASE("binary_log can pack format strings" * test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
BINARY_LOG(log, "Hello, world!");
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
// Runlength file should be empty.
REQUIRE(runlength_file.empty());
REQUIRE(index_file.size() == 15);
REQUIRE(index_file[0] == 0x0d); // format string length
REQUIRE(index_file[1] == 0x48); // 'H'
REQUIRE(index_file[2] == 0x65); // 'e'
REQUIRE(index_file[3] == 0x6c); // 'l'
REQUIRE(index_file[4] == 0x6c); // 'l'
REQUIRE(index_file[5] == 0x6f); // 'o'
REQUIRE(index_file[6] == 0x2c); // ','
REQUIRE(index_file[7] == 0x20); // ' '
REQUIRE(index_file[8] == 0x77); // 'w'
REQUIRE(index_file[9] == 0x6f); // 'o'
REQUIRE(index_file[10] == 0x72); // 'r'
REQUIRE(index_file[11] == 0x6c); // 'l'
REQUIRE(index_file[12] == 0x64); // 'd'
REQUIRE(index_file[13] == 0x21); // '!'
REQUIRE(index_file[14] == 0x00); // num_args == 0
REQUIRE(log_file.size() == 1);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE("binary_log can pack string arguments" * test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
BINARY_LOG(log, "Hello, {}!", "world");
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
// Runlength file should be empty.
REQUIRE(runlength_file.empty());
REQUIRE(index_file.size() == 14);
REQUIRE(index_file[0] == 0x0a); // format string length
REQUIRE(index_file[1] == 0x48); // 'H'
REQUIRE(index_file[2] == 0x65); // 'e'
REQUIRE(index_file[3] == 0x6c); // 'l'
REQUIRE(index_file[4] == 0x6c); // 'l'
REQUIRE(index_file[5] == 0x6f); // 'o'
REQUIRE(index_file[6] == 0x2c); // ','
REQUIRE(index_file[7] == 0x20); // ' '
REQUIRE(index_file[8] == 0x7b); // '{'
REQUIRE(index_file[9] == 0x7d); // '}'
REQUIRE(index_file[10] == 0x21); // '!'
REQUIRE(index_file[11] == 0x01); // num_args == 1
REQUIRE(index_file[12]
== static_cast<uint8_t>(
binary_log::fmt_arg_type::type_string)); // arg type is string
REQUIRE(index_file[13] == 0x00); // not constant
REQUIRE(log_file.size() == 7);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
REQUIRE(log_file[1] == 0x05); // length of "world"
REQUIRE(log_file[2] == 0x77); // 'w'
REQUIRE(log_file[3] == 0x6f); // 'o'
REQUIRE(log_file[4] == 0x72); // 'r'
REQUIRE(log_file[5] == 0x6c); // 'l'
REQUIRE(log_file[6] == 0x64); // 'd'
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE("binary_log can pack int32 arguments" * test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
BINARY_LOG(log, "Hello, {}!", 42);
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
// Runlength file should be empty.
REQUIRE(runlength_file.empty());
REQUIRE(index_file.size() == 14);
REQUIRE(index_file[0] == 0x0a); // format string length
REQUIRE(index_file[1] == 0x48); // 'H'
REQUIRE(index_file[2] == 0x65); // 'e'
REQUIRE(index_file[3] == 0x6c); // 'l'
REQUIRE(index_file[4] == 0x6c); // 'l'
REQUIRE(index_file[5] == 0x6f); // 'o'
REQUIRE(index_file[6] == 0x2c); // ','
REQUIRE(index_file[7] == 0x20); // ' '
REQUIRE(index_file[8] == 0x7b); // '{'
REQUIRE(index_file[9] == 0x7d); // '}'
REQUIRE(index_file[10] == 0x21); // '!'
REQUIRE(index_file[11] == 0x01); // num_args == 1
REQUIRE(index_file[12]
== static_cast<uint8_t>(
binary_log::fmt_arg_type::type_int32)); // arg type is int
REQUIRE(index_file[13] == 0x00); // not constant
REQUIRE(log_file.size() == 3);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
REQUIRE(log_file[1] == 0x01); // the integer is 1 byte long
REQUIRE(log_file[2] == 0x2a); // 42
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE("binary_log can pack int64 arguments" * test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
BINARY_LOG(log, "Hello, {}!", 5432123456789);
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
// Runlength file should be empty.
REQUIRE(runlength_file.empty());
REQUIRE(index_file.size() == 14);
REQUIRE(index_file[0] == 0x0a); // format string length
REQUIRE(index_file[1] == 0x48); // 'H'
REQUIRE(index_file[2] == 0x65); // 'e'
REQUIRE(index_file[3] == 0x6c); // 'l'
REQUIRE(index_file[4] == 0x6c); // 'l'
REQUIRE(index_file[5] == 0x6f); // 'o'
REQUIRE(index_file[6] == 0x2c); // ','
REQUIRE(index_file[7] == 0x20); // ' '
REQUIRE(index_file[8] == 0x7b); // '{'
REQUIRE(index_file[9] == 0x7d); // '}'
REQUIRE(index_file[10] == 0x21); // '!'
REQUIRE(index_file[11] == 0x01); // num_args == 1
REQUIRE(index_file[12]
== static_cast<uint8_t>(binary_log::fmt_arg_type::type_int64));
REQUIRE(index_file[13] == 0x00); // not constant
REQUIRE(log_file.size() == 10);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
REQUIRE(log_file[1] == 0x08); // the integer is 8 bytes long
REQUIRE(log_file[2] == 0x15); // 5432123456789
REQUIRE(log_file[3] == 0xfd);
REQUIRE(log_file[4] == 0xc9);
REQUIRE(log_file[5] == 0xc3);
REQUIRE(log_file[6] == 0xf0);
REQUIRE(log_file[7] == 0x04);
REQUIRE(log_file[8] == 0x00);
REQUIRE(log_file[9] == 0x00);
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE("binary_log can pack double arguments" * test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
BINARY_LOG(log, "Hello, {}!", 3.14159);
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
// Runlength file should be empty.
REQUIRE(runlength_file.empty());
REQUIRE(index_file.size() == 14);
REQUIRE(index_file[0] == 0x0a); // format string length
REQUIRE(index_file[1] == 0x48); // 'H'
REQUIRE(index_file[2] == 0x65); // 'e'
REQUIRE(index_file[3] == 0x6c); // 'l'
REQUIRE(index_file[4] == 0x6c); // 'l'
REQUIRE(index_file[5] == 0x6f); // 'o'
REQUIRE(index_file[6] == 0x2c); // ','
REQUIRE(index_file[7] == 0x20); // ' '
REQUIRE(index_file[8] == 0x7b); // '{'
REQUIRE(index_file[9] == 0x7d); // '}'
REQUIRE(index_file[10] == 0x21); // '!'
REQUIRE(index_file[11] == 0x01); // num_args == 1
REQUIRE(index_file[12]
== static_cast<uint8_t>(
binary_log::fmt_arg_type::type_double)); // arg type is double
REQUIRE(index_file[13] == 0x00); // not constant
REQUIRE(log_file.size() == 9);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
// The next bytes are 3.14159 as a double.
REQUIRE(log_file[1] == 0x6e);
REQUIRE(log_file[2] == 0x86);
REQUIRE(log_file[3] == 0x1b);
REQUIRE(log_file[4] == 0xf0);
REQUIRE(log_file[5] == 0xf9);
REQUIRE(log_file[6] == 0x21);
REQUIRE(log_file[7] == 0x09);
REQUIRE(log_file[8] == 0x40);
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE("binary_log can pack constants" * test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
BINARY_LOG(log,
"Str: {}, Int: {}, Float: {}, Bool: {}",
binary_log::constant("Hello"),
binary_log::constant(42),
binary_log::constant(3.14159f),
binary_log::constant(true));
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
// Runlength file should be empty.
REQUIRE(runlength_file.empty());
REQUIRE(index_file.size() == 62);
REQUIRE(index_file[0] == 0x25); // format string length
REQUIRE(index_file[1] == 0x53); // 'S'
REQUIRE(index_file[2] == 0x74); // 't'
REQUIRE(index_file[3] == 0x72); // 'r'
REQUIRE(index_file[4] == 0x3a); // ':'
REQUIRE(index_file[5] == 0x20); // ' '
REQUIRE(index_file[6] == 0x7b); // '{'
REQUIRE(index_file[7] == 0x7d); // '}'
REQUIRE(index_file[8] == 0x2c); // ','
REQUIRE(index_file[9] == 0x20); // ' '
REQUIRE(index_file[10] == 0x49); // 'I'
REQUIRE(index_file[11] == 0x6e); // 'n'
REQUIRE(index_file[12] == 0x74); // 't'
REQUIRE(index_file[13] == 0x3a); // ':'
REQUIRE(index_file[14] == 0x20); // ' '
REQUIRE(index_file[15] == 0x7b); // '{'
REQUIRE(index_file[16] == 0x7d); // '}'
REQUIRE(index_file[17] == 0x2c); // ','
REQUIRE(index_file[18] == 0x20); // ' '
REQUIRE(index_file[19] == 0x46); // 'F'
REQUIRE(index_file[20] == 0x6c); // 'l'
REQUIRE(index_file[21] == 0x6f); // 'o'
REQUIRE(index_file[22] == 0x61); // 'a'
REQUIRE(index_file[23] == 0x74); // 't'
REQUIRE(index_file[24] == 0x3a); // ':'
REQUIRE(index_file[25] == 0x20); // ' '
REQUIRE(index_file[26] == 0x7b); // '{'
REQUIRE(index_file[27] == 0x7d); // '}'
REQUIRE(index_file[28] == 0x2c); // ','
REQUIRE(index_file[29] == 0x20); // ' '
REQUIRE(index_file[30] == 0x42); // 'B'
REQUIRE(index_file[31] == 0x6f); // 'o'
REQUIRE(index_file[32] == 0x6f); // 'o'
REQUIRE(index_file[33] == 0x6c); // 'l'
REQUIRE(index_file[34] == 0x3a); // ':'
REQUIRE(index_file[35] == 0x20); // ' '
REQUIRE(index_file[36] == 0x7b); // '{'
REQUIRE(index_file[37] == 0x7d); // '}'
REQUIRE(index_file[38] == 0x04); // num_args == 4
REQUIRE(index_file[39]
== static_cast<uint8_t>(
binary_log::fmt_arg_type::type_string)); // arg type is string
REQUIRE(index_file[40]
== static_cast<uint8_t>(
binary_log::fmt_arg_type::type_int32)); // arg type is int
REQUIRE(index_file[41]
== static_cast<uint8_t>(
binary_log::fmt_arg_type::type_float)); // arg type is float
REQUIRE(index_file[42]
== static_cast<uint8_t>(
binary_log::fmt_arg_type::type_bool)); // arg type is bool
REQUIRE(index_file[43] == 0x01); // arg1 is a constant
REQUIRE(index_file[44] == 0x05); // arg1 string length
REQUIRE(index_file[45] == 0x48); // 'H'
REQUIRE(index_file[46] == 0x65); // 'e'
REQUIRE(index_file[47] == 0x6c); // 'l'
REQUIRE(index_file[48] == 0x6c); // 'l'
REQUIRE(index_file[49] == 0x6f); // 'o'
REQUIRE(index_file[50] == 0x01); // arg2 is a constant
REQUIRE(index_file[51] == 0x2a); // arg2 is 42
REQUIRE(index_file[52] == 0x00);
REQUIRE(index_file[53] == 0x00);
REQUIRE(index_file[54] == 0x00);
REQUIRE(index_file[55] == 0x01); // arg3 is a constant
REQUIRE(index_file[56] == 0xd0); // arg3 is 3.14159f
REQUIRE(index_file[57] == 0x0f);
REQUIRE(index_file[58] == 0x49);
REQUIRE(index_file[59] == 0x40);
REQUIRE(index_file[60] == 0x01); // arg4 is a constant
REQUIRE(index_file[61] == 0x01); // arg4 is true
REQUIRE(log_file.size() == 1);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE("binary_log can run-length encode repeated log calls without args"
* test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
for (std::size_t i = 0; i < 3; ++i) {
BINARY_LOG(log, "Thread started");
}
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
REQUIRE(std::filesystem::file_size(test_file_runlength) == 3);
// 1 byte for the index
// 2 bytes for the runlength
REQUIRE(runlength_file[0]
== 0x00); // index of 0, referring to row in index file
REQUIRE(runlength_file[1] == 0x01); // it's a uint8_t
REQUIRE(runlength_file[2] == 0x03); // the runlength is 3
REQUIRE(log_file.size() == 1);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
REQUIRE(index_file.size() == 16);
REQUIRE(index_file[0] == 0x0e); // Length of the format string
REQUIRE(index_file[1] == 0x54); // 'T'
REQUIRE(index_file[2] == 0x68); // 'h'
REQUIRE(index_file[3] == 0x72); // 'r'
REQUIRE(index_file[4] == 0x65); // 'e'
REQUIRE(index_file[5] == 0x61); // 'a'
REQUIRE(index_file[6] == 0x64); // 'd'
REQUIRE(index_file[7] == 0x20); // ' '
REQUIRE(index_file[8] == 0x73); // 's'
REQUIRE(index_file[9] == 0x74); // 't'
REQUIRE(index_file[10] == 0x61); // 'a'
REQUIRE(index_file[11] == 0x72); // 'r'
REQUIRE(index_file[12] == 0x74); // 't'
REQUIRE(index_file[13] == 0x65); // 'e'
REQUIRE(index_file[14] == 0x64); // 'd'
REQUIRE(index_file[15] == 0x00); // num_args is 0
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE("binary_log can run-length encode repeated log calls with args"
* test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
for (std::size_t i = 0; i < 3; ++i) {
BINARY_LOG(log, "Thread {} started", i);
}
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
REQUIRE(std::filesystem::file_size(test_file_runlength) == 3);
// 1 byte for the index
// 2 bytes for the runlength
REQUIRE(runlength_file[0]
== 0x00); // index of 0, referring to row in index file
REQUIRE(runlength_file[1] == 0x01); // it's a uint8_t
REQUIRE(runlength_file[2] == 0x03); // the runlength is 3
REQUIRE(log_file.size() == 7);
REQUIRE(log_file[0] == 0x00); // index of 0, referring to row in index file
REQUIRE(log_file[1] == 0x01); // arg_1 width is 1
REQUIRE(log_file[2] == 0x00); // arg_1 value
REQUIRE(log_file[3] == 0x01); // arg_2 width is 1
REQUIRE(log_file[4] == 0x01); // arg_2 value
REQUIRE(log_file[5] == 0x01); // arg_3 width is 1
REQUIRE(log_file[6] == 0x02); // arg_3 value
REQUIRE(index_file.size() == 21);
REQUIRE(index_file[0] == 0x11); // Length of the format string
REQUIRE(index_file[1] == 0x54); // 'T'
REQUIRE(index_file[2] == 0x68); // 'h'
REQUIRE(index_file[3] == 0x72); // 'r'
REQUIRE(index_file[4] == 0x65); // 'e'
REQUIRE(index_file[5] == 0x61); // 'a'
REQUIRE(index_file[6] == 0x64); // 'd'
REQUIRE(index_file[7] == 0x20); // ' '
REQUIRE(index_file[8] == 0x7b); // '{'
REQUIRE(index_file[9] == 0x7d); // '}'
REQUIRE(index_file[10] == 0x20); // ' '
REQUIRE(index_file[11] == 0x73); // 's'
REQUIRE(index_file[12] == 0x74); // 't'
REQUIRE(index_file[13] == 0x61); // 'a'
REQUIRE(index_file[14] == 0x72); // 'r'
REQUIRE(index_file[15] == 0x74); // 't'
REQUIRE(index_file[16] == 0x65); // 'e'
REQUIRE(index_file[17] == 0x64); // 'd'
REQUIRE(index_file[18] == 0x01); // num_args is 1
REQUIRE(index_file[19] == 0x05); // arg_1 is an uint64_t
REQUIRE(index_file[20] == 0x00); // arg_1 is not a constant
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
TEST_CASE(
"binary_log can run-length encode repeated log calls with args and handle "
"other single calls"
* test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
BINARY_LOG(log, "New iteration");
for (std::size_t j = 0; j < 2; ++j) {
BINARY_LOG(log, "Integer: {}", j);
}
BINARY_LOG(log, "Flushing files");
for (std::size_t j = 0; j < 3; ++j) {
auto filename = std::string {"some_file_"} + std::to_string(j);
BINARY_LOG(log, "Flushed file: {}", filename);
}
BINARY_LOG(log, "End of iteration");
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
REQUIRE(log_file.size() == 45);
auto expected_bytes_in_log_file =
std::vector<uint8_t> {// Index for "New iteration"
0x00,
// Index for "Integer: {}"
0x01,
// Arg_1 width is 1
0x01,
// Arg_1 value is 0
0x00,
// Arg_1 width is 1
0x01,
// Arg_1 value is 1
0x01,
// Index for "Flushing files"
0x02,
// Index for "Flushed file: {}"
0x03,
// Arg_1 string length
0x0b,
// Arg_1 string value: some_file_0
0x73,
0x6f,
0x6d,
0x65,
0x5f,
0x66,
0x69,
0x6c,
0x65,
0x5f,
0x30,
// Arg_1 string length
0x0b,
// Arg_1 string value: some_file_1
0x73,
0x6f,
0x6d,
0x65,
0x5f,
0x66,
0x69,
0x6c,
0x65,
0x5f,
0x31,
// Arg_1 string length
0x0b,
// Arg_1 string value: some_file_2
0x73,
0x6f,
0x6d,
0x65,
0x5f,
0x66,
0x69,
0x6c,
0x65,
0x5f,
0x32,
// Index for "End of iteration"
0x04};
for (std::size_t i = 0; i < expected_bytes_in_log_file.size(); ++i) {
REQUIRE(log_file[i] == expected_bytes_in_log_file[i]);
}
REQUIRE(index_file.size() == 84);
auto expected_bytes_in_index_file =
std::vector<uint8_t> {// Format string length for "New iteration"
0x0d,
// Format string value for "New iteration"
0x4e,
0x65,
0x77,
0x20,
0x69,
0x74,
0x65,
0x72,
0x61,
0x74,
0x69,
0x6f,
0x6e,
// Num args
0x00,
// Format string length for "Integer: {}"
0x0b,
// Format string value for "Integer: {}"
0x49,
0x6e,
0x74,
0x65,
0x67,
0x65,
0x72,
0x3a,
0x20,
0x7b,
0x7d,
// Num args
0x01,
// Arg type: uint64_t
0x05,
// Arg is constant?
0x00,
// Format string length for "Flushing files"
0x0e,
// Format string value for "Flushing files"
0x46,
0x6c,
0x75,
0x73,
0x68,
0x69,
0x6e,
0x67,
0x20,
0x66,
0x69,
0x6c,
0x65,
0x73,
// Num args
0x00,
// Format string length for "Flushed file: {}"
0x10,
// Format string value for "Flushed file: {}"
0x46,
0x6c,
0x75,
0x73,
0x68,
0x65,
0x64,
0x20,
0x66,
0x69,
0x6c,
0x65,
0x3a,
0x20,
0x7b,
0x7d,
// Num args
0x01,
// Arg type: string
0x0c,
// Arg is constant?
0x00,
// Format string length for "End of iteration"
0x10,
// Format string value for "End of iteration"
0x45,
0x6e,
0x64,
0x20,
0x6f,
0x66,
0x20,
0x69,
0x74,
0x65,
0x72,
0x61,
0x74,
0x69,
0x6f,
0x6e,
// Num args
0x00};
for (std::size_t i = 0; i < expected_bytes_in_index_file.size(); ++i) {
REQUIRE(index_file[i] == expected_bytes_in_index_file[i]);
}
REQUIRE(runlength_file.size() == 6);
auto expected_bytes_in_runlength_file =
std::vector<uint8_t> {// Index with runlength
0x01,
// Integer width for value
0x01,
// Run length value
0x02,
// Index with runlength
0x03,
// Integer width for value
0x01,
// Run length value
0x03};
for (std::size_t i = 0; i < expected_bytes_in_runlength_file.size(); ++i) {
REQUIRE(runlength_file[i] == expected_bytes_in_runlength_file[i]);
}
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}
template<typename T>
void save_data(binary_log::binary_log& log, T&& value)
{
BINARY_LOG(log, "{}", std::forward<T>(value));
}
TEST_CASE("binary_log can run-length encode calls to function template"
* test_suite("packer"))
{
{
binary_log::binary_log log(test_file);
save_data(log, 5);
save_data(log, 3.14);
save_data(log, "Hello, world!");
save_data(log, std::string("This is a string"));
save_data(log, true);
for (int i = 0; i < 3; ++i) {
// Should trigger RLE
save_data(log, i);
}
save_data(log, 2.7182818284590452353602874713527f);
save_data(log, 'a');
}
// The file should be flushed and closed.
REQUIRE(std::filesystem::exists(test_file));
REQUIRE(std::filesystem::exists(test_file_index));
REQUIRE(std::filesystem::exists(test_file_runlength));
auto log_file = read_file(test_file);
auto index_file = read_file(test_file_index);
auto runlength_file = read_file(test_file_runlength);
REQUIRE(log_file.size() == 61);
auto expected_bytes_in_log_file =
std::vector<uint8_t> {// Index for save_data(int)
0x00,
// Integer width for value
0x01,
// Integer value
0x05,
// Index for save_data(double)
0x01,
// Double value for 3.14
0x1f,
0x85,
0xeb,
0x51,
0xb8,
0x1e,
0x09,
0x40,
// Index for save_data(const char*)
0x02,
// String length for "Hello, world!"
0x0d,
// String value for "Hello, world!"
0x48,
0x65,
0x6c,
0x6c,
0x6f,
0x2c,
0x20,
0x77,
0x6f,
0x72,
0x6c,
0x64,
0x21,
// Index for save_data(std::string)
0x03,
// String length for "This is a string"
0x10,
// String value for "This is a string"
0x54,
0x68,
0x69,
0x73,
0x20,
0x69,
0x73,
0x20,
0x61,
0x20,
0x73,
0x74,
0x72,
0x69,
0x6e,
0x67,
// Index for save_data(bool)
0x04,
// Boolean value for true
0x01,
// Index for save_data(int)
0x05,
// Integer width for value
0x01,
// Integer value
0x00,
// Integer width for value
0x01,
// Integer value
0x01,
// Integer width for value
0x01,
// Integer value
0x02,
// Index for save_data(float)
0x06,
// Float value for 2.7182818284590452353602874713527
0x54,
0xf8,
0x2d,
0x40,
// Index for save_data(char)
0x07,
// Char value for 'a'
0x61};
for (std::size_t i = 0; i < expected_bytes_in_log_file.size(); ++i) {
REQUIRE(log_file[i] == expected_bytes_in_log_file[i]);
}
REQUIRE(index_file.size() == 48);
auto expected_bytes_in_index_file = std::vector<uint8_t> {
// Length of format string for save_data(int) "{}"
0x02,
// Format string value for save_data(int) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - int32
0x08,
// Is arg const?
0x00,
// Length of format string for save_data(double) "{}"
0x02,
// Format string value for save_data(double) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - double
0x0b,
// Is arg const?
0x00,
// Length of format string for save_data(const char*) "{}"
0x02,
// Format string value for save_data(const char*) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - const char*
0x0c,
// Is arg const?
0x00,
// Length of format string for save_data(std::string) "{}"
0x02,
// Format string value for save_data(std::string) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - std::string
0x0c,
// Is arg const?
0x00,
// Length of format string for save_data(bool) "{}"
0x02,
// Format string value for save_data(bool) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - bool
0x00,
// Is arg const?
0x00,
// Length of format string for save_data(int) "{}"
0x02,
// Format string value for save_data(int) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - int32
0x08,
// Is arg const?
0x00,
// Length of format string for save_data(float) "{}"
0x02,
// Format string value for save_data(float) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - float
0x0a,
// Is arg const?
0x00,
// Length of format string for save_data(char) "{}"
0x02,
// Format string value for save_data(char) "{}"
0x7b,
0x7d,
// Num args
0x01,
// Type of arg 0 - char
0x01,
// Is arg const?
0x00};
for (std::size_t i = 0; i < expected_bytes_in_index_file.size(); ++i) {
REQUIRE(index_file[i] == expected_bytes_in_index_file[i]);
}
REQUIRE(runlength_file.size() == 3);
auto expected_bytes_in_runlength_file =
std::vector<uint8_t> {0x05, 0x01, 0x03};
for (std::size_t i = 0; i < expected_bytes_in_runlength_file.size(); ++i) {
REQUIRE(runlength_file[i] == expected_bytes_in_runlength_file[i]);
}
remove(test_file);
remove(test_file_index);
remove(test_file_runlength);
}