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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
|
/**
* Copyright (C) 2021-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/bson/util/simple8b.h"
#include "mongo/unittest/unittest.h"
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
#include <vector>
using namespace mongo;
template <typename T>
void assertValuesEqual(const Simple8b<T>& actual, const std::vector<boost::optional<T>>& expected) {
auto it = actual.begin();
auto end = actual.end();
size_t i = 0;
for (; i < expected.size() && it != end; ++i, ++it) {
ASSERT_EQ(*it, expected[i]);
}
ASSERT(it == end);
ASSERT_EQ(i, expected.size());
}
template <typename T>
std::pair<SharedBuffer, int> buildSimple8b(const std::vector<boost::optional<T>>& expectedValues) {
BufBuilder _buffer;
Simple8bBuilder<T> builder([&_buffer](uint64_t simple8bBlock) {
_buffer.appendNum(simple8bBlock);
return true;
});
for (const auto& elem : expectedValues) {
if (elem) {
ASSERT_TRUE(builder.append(*elem));
} else {
builder.skip();
}
}
builder.flush();
auto size = _buffer.len();
return {_buffer.release(), size};
}
template <typename T>
void testSimple8b(const std::vector<boost::optional<T>>& expectedValues,
const std::vector<uint8_t>& expectedBinary) {
auto [buffer, size] = buildSimple8b(expectedValues);
ASSERT_EQ(size, expectedBinary.size());
if (size > 0) {
ASSERT_EQ(memcmp(buffer.get(), expectedBinary.data(), size), 0);
}
Simple8b<T> s8b(buffer.get(), size);
assertValuesEqual(s8b, expectedValues);
}
template <typename T>
void testSimple8b(const std::vector<boost::optional<T>>& expectedValues) {
auto [buffer, size] = buildSimple8b<T>(expectedValues);
Simple8b<T> s8b(buffer.get(), size);
assertValuesEqual(s8b, expectedValues);
}
TEST(Simple8b, NoValues) {
std::vector<boost::optional<uint64_t>> expectedInts = {};
std::vector<uint8_t> expectedBinary = {};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, OnlySkip) {
std::vector<boost::optional<uint64_t>> expectedInts = {boost::none};
// The selector is 14 and the remaining 60 bits of data are all 1s, which represents skip.
std::vector<uint8_t> expectedBinary{
0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // 1st word.
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, OneValue) {
std::vector<boost::optional<uint64_t>> expectedInts = {1};
// The selector is 14 and there is only 1 bucket with the value 1.
std::vector<uint8_t> expectedBinary{0x1E, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; // 1st word.
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, MaxValue) {
std::vector<boost::optional<uint64_t>> expectedInts = {0xFFFFFFFFFFFFFFE};
// The selector is 14 and there is only 1 bucket with the max possible value 0xFFFFFFFFFFFFFFE.
std::vector<uint8_t> expectedBinary{
0xEE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // 1st word.
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, MultipleValues) {
std::vector<boost::optional<uint64_t>> expectedInts = {1, 2, 3};
// The selector is 12 and there are 3 bucket with the values 1, 2 and 3.
std::vector<uint8_t> expectedBinary{0x1C, 0x0, 0x0, 0x2, 0x0, 0x30, 0x0, 0x0}; // 1st word.
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, MaxValues) {
std::vector<boost::optional<uint64_t>> expectedInts(60, 1);
// The selector is 2 and there are 30 bucket with the same value 0b01. 0x55 = 0b01010101.
std::vector<uint8_t> expectedBinary{
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 1st word.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55 // 2nd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, EncodeWithTrailingDirtyBits) {
std::vector<boost::optional<uint64_t>> expectedInts(7, 1);
// The selector is 8 and there are 7 bucket with the same value 0b00000001.
// The last 4 bits are dirty/unused.
std::vector<uint8_t> expectedBinary{
0x08, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; // 1st word.
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, FullBuffer) {
std::vector<boost::optional<uint64_t>> expectedInts(120, 1);
// The selector is 2 and there are 30 bucket with the same value 0b01. 0x55 = 0b01010101.
std::vector<uint8_t> expectedBinary{
0x52, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, // 1st word.
0x52, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, // 2nd word.
0x52, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, // 3rd word.
0x52, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 // 4th word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, MaxValueBuffer) {
std::vector<boost::optional<uint64_t>> expectedInts(3, 0xFFFFFFFFFFFFFFE);
// The selector is 14 and there is only 1 bucket with the max possible value 0xFFFFFFFFFFFFFFE.
std::vector<uint8_t> expectedBinary{
0xEE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 1st word.
0xEE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 2nd word.
0xEE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 3rd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, TrySomeSmallValues) {
std::vector<boost::optional<uint64_t>> expectedInts;
for (size_t num = 0; num <= 0x1FFFF; ++num) {
expectedInts.push_back(num);
}
testSimple8b(expectedInts);
}
TEST(Simple8b, TrySomeLargeValues) {
std::vector<boost::optional<uint64_t>> expectedInts;
for (size_t num = 0xF00000000; num <= 0xF0001FFFF; ++num) {
expectedInts.push_back(num);
}
testSimple8b(expectedInts);
}
TEST(Simple8b, BreakPendingIntoMultipleSimple8bBlocks) {
std::vector<boost::optional<uint64_t>> expectedInts(57, 1);
// 15 is 0b1111 and can not be added to the current word because it would overflow.
// We can not form a 57 bit word because we would be unable to determine
// if the last 3 bits are empty or unused.
// Therefore, we must form a word with 30 integers of 1's, 20 integers of 1's
// and the current vector would have seven 1's and one 15.
expectedInts.push_back(15);
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b01. 0x55 = 0b01010101.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 1st word.
// The selector is 3 and there are 20 bucket with the same value 0b01.
// 0x24 = 0b00100100, 0x49 = 0b01001001 and 0x92 = 0b10010010.
0x93,
0x24,
0x49,
0x92,
0x24,
0x49,
0x92,
0x24, // 2nd word.
// The selector is 7 and there are 8 bucket of 0b01 except the last bucket which is 0b1111.
// 0xE0 = 0b11100000 and 0x1 = 0x00000001 and together the last bucket is 0b1111.
0x07,
0x81,
0x40,
0x20,
0x10,
0x08,
0x04,
0x1E // 3rd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, BreakPendingValuesIntoMultipleSimple8bWords) {
std::vector<boost::optional<uint64_t>> expectedInts(50, 0);
// 0xFFFFFFFFFFFF is 48 bits and can not be added to the current word because it would overflow.
// We can not form a 57 bit word because we would be unable to determine
// if the last 3 bits are empty or unused. Therefore, we must form a word with 30 integers
// of 0's and 20 integers of 0's in the same append() iteration.
expectedInts.push_back(0xFFFFFFFFFFFF); // 48 bit value.
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b00.
0x2,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 1st word.
// The selector is 3 and there are 20 bucket with the same value 0b00.
0x3,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 2nd word.
// The selector is 14 and there is only 1 bucket with 0xFFFFFFFFFFFF.
0xFE,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xF,
0x0 // 3rd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, SkipAtSecondToLast) {
std::vector<boost::optional<uint64_t>> expectedInts(3, 3);
expectedInts.push_back(boost::none);
expectedInts.push_back(7);
// The selector is 10 and there are 5 bucket with 12 bit buckets.
// 0xFF and 0x7F is 15 1's. The skip is 12 1's and 7 is 3 1's.
std::vector<uint8_t> expectedBinary{0x3A, 0x0, 0x3, 0x30, 0x0, 0xFF, 0x7F, 0x0}; // 1st word.
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, SkipInMiddle) {
std::vector<boost::optional<uint64_t>> expectedInts(50, 1);
expectedInts.push_back(boost::none);
expectedInts.insert(expectedInts.end(), 50, 1);
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b01. 0x55 = 0b01010101.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 1st word.
// The selector is 2 and there are 30 bucket with the same value 0b01
// except the skip, which is the 0x7 in the 6th byte. 0x55 = 0b01010101.
0x52,
0x55,
0x55,
0x55,
0x55,
0x75,
0x55,
0x55, // 2nd word.
// The selector is 2 and there are 30 bucket with the same value 0b01. 0x55 = 0b01010101.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 3rd word.
// The selector is 6 and there are 10 bucket with the same value 0b000001.
0x16,
0x4,
0x41,
0x10,
0x4,
0x41,
0x10,
0x4, // 4th word.
// The selector is 14 and there is only 1 bucket with the value 1.
0x1E,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0 // 5th word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, TrailingSkips) {
std::vector<boost::optional<uint64_t>> expectedInts(48, 1);
expectedInts.insert(expectedInts.end(), 2, boost::none);
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b01. 0x55 = 0b01010101.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 1st word.
// The selector is 3 and there are 20 bucket with the same value 0b001.
// except the last 2 buckets, which are skips. 0xFC = 11111100, which is exactly 2 skips.
0x93,
0x24,
0x49,
0x92,
0x24,
0x49,
0x92,
0xFC // 2nd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, LeadingSkips) {
std::vector<boost::optional<uint64_t>> expectedInts = {3, 8, 13};
expectedInts.insert(expectedInts.begin(), 2, boost::none);
// The selector is 10 and there are 5 bucket with 12 bit buckets.
// The first two buckets are skips.
std::vector<uint8_t> expectedBinary{0xFA, 0xFF, 0xFF, 0x3F, 0x0, 0x8, 0xD0, 0x0}; // 1st word.
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, WordOfSkips) {
std::vector<boost::optional<uint64_t>> expectedInts(30, boost::none);
uint64_t numWithMoreThanThirtyBits = (1ull << 30) + 1;
expectedInts.push_back(numWithMoreThanThirtyBits);
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b11, referring to skip.
0xF2,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF, // 1st word.
// The selector is 14 and there is only bucket with the value
// 0b100000000000000000000000000000.
0x1E,
0x0,
0x0,
0x0,
0x4,
0x0,
0x0,
0x0 // 2nd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, LargeSkipsFirst) {
std::vector<boost::optional<uint64_t>> expectedInts;
for (uint32_t i = 0; i < 10; ++i) {
expectedInts.push_back(boost::none);
expectedInts.push_back(64);
}
std::vector<uint8_t> expectedChar{
// The selector is 7 and the extension value is 1, so the values alternate
// between 0b111111 (skip) and 0b010110 (64).
0x17,
0xBF,
0xF5,
0x5B,
0xBF,
0xF5,
0x5B,
0x3F, // 1st word.
// The selector is 7 and the extension value is 1, so the values alternate
// between 0b111111 (skip) and 0b010110 (64).
0x17,
0xD6,
0x6F,
0xFD,
0xD6,
0x6F,
0xFD,
0x16, // 2nd word.
// The selector is 13 and there are 2 buckets with one skip and then 0b010000 (64).
0xFD,
0xFF,
0xFF,
0xFF,
0x03,
0x01,
0x0,
0x0, // 3rd word.
};
testSimple8b(expectedInts, expectedChar);
}
TEST(Simple8b, RleZeroThenRleAnotherValue) {
std::vector<boost::optional<uint64_t>> expectedInts(1920, 0);
expectedInts.insert(expectedInts.end(), 270, 1);
std::vector<uint8_t> expectedChar{
// The selector is 15 and the word is a RLE encoding with count = 16.
// The default RLE value is 0 if it is the first number.
0xFF,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 1st word.
// The selector is 2 and there are 30 bucket with the same value 0b01. 0x55 = 0b01010101.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 2nd word.
// The selector is 15 and the word is a RLE encoding with count = 2.
0x1F,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 3rd word.
};
testSimple8b(expectedInts, expectedChar);
}
TEST(Simple8b, MultipleFlushes) {
BufBuilder buffer;
Simple8bBuilder<uint64_t> s8b([&buffer](uint64_t simple8bBlock) {
buffer.appendNum(simple8bBlock);
return true;
});
std::vector<uint64_t> values = {1};
for (size_t i = 0; i < values.size(); ++i) {
ASSERT_TRUE(s8b.append(values[i]));
}
s8b.flush();
values[0] = 2;
for (size_t i = 0; i < values.size(); ++i) {
ASSERT_TRUE(s8b.append(values[i]));
}
std::vector<uint8_t> expectedBinary{
// The selector is 14 and there is only 1 bucket with the value 1.
0x1E,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 1st word.
// The selector is 14 and there is only 1 bucket with the value 2.
0x2E,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0 // 2nd word.
};
s8b.flush();
char* hex = buffer.buf();
size_t len = buffer.len();
ASSERT_EQ(len, expectedBinary.size());
for (size_t i = 0; i < len; ++i) {
ASSERT_EQ(static_cast<uint8_t>(*hex), expectedBinary[i]) << i;
++hex;
}
}
TEST(Simple8b, Selector7BaseTest) {
// 57344 = 1110000000000000 = 3 value bits and 13 zeros
// This should be encoded as:
// [(111) (1101)] x 8 [0010] [0111] = FBF7EFDFBF7EFD27
// This is in hex: 2FBF7EFDFBF7EFD7
uint64_t val = 57344;
std::vector<boost::optional<uint64_t>> expectedInts(8, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x27, 0xFD, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector7BaseTestAndSkips) {
// 57344 = 1110000000000000 = 3 value bits and 13 zeros
// This should be encoded with alternating skips as:
// [(111) (1111) (111) (1101)] x 4 [0010] [0111] = FFF7FFDFFF7FFD27
uint64_t val = 57344;
std::vector<boost::optional<uint64_t>> expectedInts;
for (uint32_t i = 0; i < 4; i++) {
expectedInts.push_back(val);
expectedInts.push_back(boost::none);
}
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x27, 0xFD, 0x7F, 0xFF, 0xDF, 0xFF, 0xF7, 0xFF};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector7SingleZero) {
// 30 = 11110 so a single zero
// This should not be encoded with selector 7 since it will take 4 extra bits to store the
// count of zeros
// This should be encoded as:
// [11110] x 12 [0101] = F7BDEF7BDEF7BDE5
uint64_t val = 30;
std::vector<boost::optional<uint64_t>> expectedInts(12, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0xE5, 0xBD, 0xF7, 0xDE, 0x7B, 0xEF, 0xBD, 0xF7};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector7SkipEncoding) {
// 229376 = 111000000000000000 = 3 value bits and 15 zeros which would be stored as 111-1111
// using selector 7. However, we will add a padding bit to store as 0111-1111
// This should be encoded as:
// [(0111) (1111)] x7 [0011] [0111] = 7F7F7F7F7F7F7F37
uint64_t val = 229376;
std::vector<boost::optional<uint64_t>> expectedInts(7, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x37, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simpl8b, Selector7IntegrationWithBaseEncoding) {
// Base value = 1011 = 11
// Selector 7 value = 12615680 = 110000000001 + 15 zeros.
// We should encode this as:
// [(01100000001) (1111)] x 2 [(00000001011) (0000)] x 2 [0110] [0111] = 607
// D81F02C00B067
uint64_t val = 11;
std::vector<boost::optional<uint64_t>> expectedInts(2, val);
val = 12615680;
expectedInts.insert(expectedInts.end(), 2, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x67, 0xB0, 0x00, 0x2C, 0xF0, 0x81, 0x7D, 0x60};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simpl8b, Selector7IntegrationWithBaseEncodingOppositeDirection) {
// Base value = 1011 = 11
// Selector 7 value = 12615680 = 110000000001 + 15 zeros.
// We should encode this as:
// [(00000001011) (0000)] x 2 [(01100000001) (1111)] x 2 [0110] [0111] = 2C0
// 0B0607D81F67
uint64_t val = 12615680;
std::vector<boost::optional<uint64_t>> expectedInts(2, val);
val = 11;
expectedInts.insert(expectedInts.end(), 2, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x67, 0x1F, 0xD8, 0x07, 0x06, 0x0B, 0xC0, 0x02};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallBaseTest) {
// 0x500000 = 101 + 20 zeros. This should be stored as (0101 0101) where the second value of 4
// is the nibble shift of 5*4. The first value is 0101 because we store at least 4 bits. This
// should be encoded as
// [(0101) (0101)] x 7 [0001] [1000] = 7575757575757518
//
uint64_t val = 0x500000;
std::vector<boost::optional<uint64_t>> expectedInts(7, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x18, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallBaseTestAndSkip) {
// 7340032 = 111 + 20 zeros. This should be stored as (0111 0101) where the second value of 4 is
// the nibble shift of 4*4. The first value is 0111 because we store at least 4 values. Then we
// have a value of all 1s for skip.
// This should be encoded as
// [(0111) (0101)] [(1111 1111) (0111 0101)] x 3 [0001] [1000] = 75FF75FF75FF7518
uint64_t val = 7340032;
std::vector<boost::optional<uint64_t>> expectedInts;
expectedInts.push_back(val);
for (uint32_t i = 0; i < 3; i++) {
expectedInts.push_back(boost::none);
expectedInts.push_back(val);
}
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x18, 0x75, 0xFF, 0x75, 0xFF, 0x75, 0xFF, 0x75};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallSkipEncoding) {
// A perfect skip value is one that aligns perfectly with the boundary. 1111 with 60 zeros does
// that. They need to be padded with an extra zero and cause the Selector to be 2 instead of 1
// bumping out the last skip to the next block.
// This should be encoded as
// [(01111 1111) x 6] [0010] [1000] = 3FEFFFFBFFFEFF28
//
uint64_t val = 17293822569102704640ull;
std::vector<boost::optional<uint64_t>> expectedInts;
for (uint32_t i = 0; i < 3; i++) {
expectedInts.push_back(val);
expectedInts.push_back(boost::none);
}
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x28, 0xFF, 0xFE, 0xFF, 0xFB, 0xFF, 0xEF, 0x3F};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallNibbleShift) {
// 7864320 = 1111 + 19 zeros. This is a value that should have 3 trailing zeros due to nibble.
// So we should encode as:
// [(1111000) (0100)] x 4 [0011] [1000] = 784F09E13C278438
uint64_t val = 7864320;
std::vector<boost::optional<uint64_t>> expectedInts(5, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x38, 0x84, 0x27, 0x3C, 0xE1, 0x09, 0x4F, 0x78};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallBitsAndNibble) {
// 549789368320 = 1(13 zeros)1 + 25 zeros. This is a value that should have 1 trailing zeros due
// to nibble. So we should encode as:
// [(0000001(13 zeros)10) (0110)] x2 [0110] [1000] = 80026008002668
uint64_t val = 549789368320;
std::vector<boost::optional<uint64_t>> expectedInts(2, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x68, 0x26, 0x00, 0x08, 0x60, 0x02, 0x80, 0x00};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallAddSelector7FirstThen8) {
// This tests that what only requires a seven selector value will be properly encoded as a eight
// selector value. The 57344 should be encoded as 3 ones and 13 zeros. The next value requires
// selector 8 which is 7340032 (3 ones and 20 zeros). We should choose a selector requiring 4
// valu bits to store these.
// This should be encoded as
// [(0111) (0101) x 3] [(1110 0011) x 4] [0001] [1000] = 757575E3E3E3E318
uint64_t val = 57344;
std::vector<boost::optional<uint64_t>> expectedInts(4, val);
val = 7340032;
expectedInts.insert(expectedInts.end(), 3, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x18, 0xE3, 0xE3, 0xE3, 0xE3, 0x75, 0x75, 0x75};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallAddBaseSelectorThen7Then8) {
// This tests a combination of selector switches, rounding, and nibble shifts for our simple8b
// encoding. The first value of 6 is 110 which should be encoded as 0110 0000. The 57344 should
// be encoded as 3 ones and 13 zeros. The next value requires selector 8 which is 7340032 (3
// ones and 20 zeros). We should choose a selector requiring 4 valu bits to store these. This
// sohould be encoded as: [(0111) (0101) x 3] [(1110 0011) x 3] [0110 0000] [0001] [1000] =
// 17575E3E3E3E3608
uint64_t val = 6;
std::vector<boost::optional<uint64_t>> expectedInts(1, val);
val = 57344;
expectedInts.insert(expectedInts.end(), 4, val);
val = 7340032;
expectedInts.insert(expectedInts.end(), 2, val);
// test that buffer was correct 17474E3E3E3E3608
std::vector<uint8_t> expectedBinary = {0x18, 0x60, 0xE3, 0xE3, 0xE3, 0xE3, 0x75, 0x75};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8SmallStartWith8SelectorAndAddSmallerValues) {
// This tests a combination of selector switches, rounding, and nibble shifts for our simple8b
// encoding. TThe first value requires selector 8 which is 7340032 (3 ones
// and 20 zeros). The 57344 should
// be encoded as 3 ones and 13 zeros. The next value of 6 is 110 which should be encoded as 0110
// 0000. We should choose a selector requiring 4 value bits to store these. This should be
// encoded as
// [0110 0000] [(1110 0011) x 3] [(0111) (0101) x 3] [0001] [1000] = 60E3E3E375757518
uint64_t val = 7340032;
std::vector<boost::optional<uint64_t>> expectedInts(3, val);
val = 57344;
expectedInts.insert(expectedInts.end(), 3, val);
val = 6;
expectedInts.insert(expectedInts.end(), 1, val);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x18, 0x75, 0x75, 0x75, 0xE3, 0xE3, 0xE3, 0x60};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Rle) {
std::vector<boost::optional<uint64_t>> expectedInts(180, 1);
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b01.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 1st word.
// The selector is 15 and the word is RLE encoding with count = 1.
0x0F,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 2nd word.
// The selector is 2 and there are 30 bucket with the same value 0b01.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 3rd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, MultipleRleWords) {
std::vector<boost::optional<uint64_t>> expectedInts(30 + (16 * 120 * 2), 1);
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b01.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 1st word.
// The selector is 15 and the word is RLE with max count = 16.
0xFF,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 2nd word.
// The selector is 15 and the word is RLE with max count = 16.
0xFF,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 3rd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RleSkip) {
std::vector<boost::optional<uint64_t>> expectedInts(240, boost::none);
std::vector<uint8_t> expectedBinary{
// The selector is 1 and there are 60 bucket with skip.
0xF1,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF, // 1st word.
// The selector is 15 and the word is RLE encoding with count = 1.
0x0F,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 2nd word.
// The selector is 1 and there are 60 bucket with skip.
0xF1,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF, // 3rd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, FlushSkipRLE) {
// Make sure that flushing skips does not re-enable RLE when it fits a full Simple8b. We need at
// least 121 skips to verify this (60+60+1)
std::vector<boost::optional<uint64_t>> expectedInts(121, boost::none);
std::vector<uint8_t> expectedBinary{
// The selector is 1 and there are 60 bucket with skip.
0xF1,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF, // 1st word.
// The selector is 1 and there are 60 bucket with skip.
0xF1,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF, // 2nd word.
// The selector is 14 and there are 1 bucket with skip.
0xFE,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF, // 3rd word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RleChangeOfValue) {
std::vector<boost::optional<uint64_t>> expectedInts(300, 1);
expectedInts.push_back(7);
std::vector<uint8_t> expectedBinary{
// The selector is 2 and there are 30 bucket with the same value 0b01.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 1st word.
// The selector is 15 and the word is RLE encoding with count = 2.
0x1F,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 2nd word.
// The selector is 2 and there are 30 bucket with the same value 0b01.
0x52,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55, // 3rd word.
// The selector is 14 and there is only one bucket with the value 7.
0x7E,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 4th word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RleFront) {
std::vector<boost::optional<uint64_t>> expectedInts(240, 0);
std::vector<uint8_t> expectedBinary{
// The selector is 15 and the word is RLE encoding with count = 2.
0x1F,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0, // 1st word.
};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, EightSelectorLargeBase) {
// 8462480737302404222943232 = 111 + 80 zeros. This should be stored as (111 10100) where the
// second value of 20 is the nibble shift of 4*20. The first value is 0111 because we store at
// least 4 values. This should be encoded as [(111) (10100)] x6 [1000] [1000] = //
// 81E8F47A3D1E8F48
uint128_t val = absl::MakeUint128(0x70000, 0x0);
std::vector<boost::optional<uint128_t>> expectedInts = {val, val, val, val, val, val};
std::vector<uint8_t> expectedBinary = {0x88, 0xF4, 0xE8, 0xD1, 0xA3, 0x47, 0x8F, 0x1E};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, UInt128Zero) {
// Have a large value that forces the extended selectors to be used. Then we check that zeros
// are handled correctly for them.
uint128_t val =
absl::MakeUint128(0x70000, 0x0); // Stored as 0xF4, [value=(111) nibble count=(10100)]
uint128_t zero = absl::MakeUint128(0x0, 0x0);
// 5 values with Selector8Large = 0x98
std::vector<boost::optional<uint128_t>> expectedInts = {val, zero, zero, zero, zero};
std::vector<uint8_t> expectedBinary = {0x98, 0xF4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8LargeBaseTestAndSkip) {
// 8462480737302404222943232 = 111 + 80 zeros. This should be stored as (0111 10100) where the
// second value of 20 is the nibble shift of 4*20. The first value is 0111 because we store at
// least 4 values. With skip this should be encoded as:
// [(1111) (11111) (0111) (10100)] x3 [1000] = 83FEF4FFBD3FEF48
uint128_t val = absl::MakeUint128(0x70000, 0x0);
std::vector<boost::optional<uint128_t>> expectedInts;
for (uint32_t i = 0; i < 3; i++) {
expectedInts.push_back(val);
expectedInts.push_back(boost::none);
}
std::vector<uint8_t> expectedBinary = {0x88, 0xF4, 0xFE, 0xD3, 0xFB, 0x4F, 0xEF, 0x3F};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8LargeSkipEncoding) {
// A perfect skip value is one that aligns perfectly with the boundary. 1111 with 124 zeros does
// that.
// This should be encoded as
// [(001111 11111) x 5] [1001] [1000] = 1FF3FE7FCFF9FF98
uint128_t val = absl::MakeUint128(0xF000000000000000, 0x0);
std::vector<boost::optional<uint128_t>> expectedInts = {val, val, val, val, val};
std::vector<uint8_t> expectedBinary = {0x98, 0xFF, 0xF9, 0xCF, 0x7F, 0xFE, 0xF3, 0x1F};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector8LargeNibbleShift) {
// 170141183460469231731687303715884105728= 1 + 127 zeros. This is a value that should have 3
// trailing zeros due to nibble. So we should encode as:
// [(1000) (11111)] x6 [1000] [1000] = 23F1F8FC7E3F1F88
uint128_t val = absl::MakeUint128(0x8000000000000000, 0x0);
std::vector<boost::optional<uint128_t>> expectedInts = {val, val, val, val, val, val};
std::vector<uint8_t> expectedBinary = {0x88, 0x1F, 0x3F, 0x7E, 0xFC, 0xF8, 0xF1, 0x23};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Test128WithSmallValue) {
// This tests that if we use a small int128_t, we still correctly store.
// 57344 = 1110000000000000 = 3 value bits and 13 zeros
// This should be encoded as:
// [(111) (1101)] x 8 [0010] [0111] = FBF7EFDFBF7EFD27
uint128_t val = 57344;
std::vector<boost::optional<uint128_t>> expectedInts = {val, val, val, val, val, val, val, val};
std::vector<uint8_t> expectedBinary = {0x27, 0xFD, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, Selector7FullSelector1) {
// Selector 7 value = 64 = 1 + 6 zeros.
// We should encode this as:
// [(01) (0110)] x 9 [0001] [0111] = 1659659659659617
std::vector<boost::optional<uint64_t>> expectedInts(9, 64);
// test that buffer was correct
std::vector<uint8_t> expectedBinary = {0x17, 0x96, 0x65, 0x59, 0x96, 0x65, 0x59, 0x16};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RleSevenSelector) {
// Selector 7 value
// 57344 = 3 value bits and 13 zeros
// This should be encoded as
// [(111) (1101)] x8 [0010] [0111] = FBF7EFDFBF7EFD27 + 0xF (rle) + repeat seven selector
uint128_t val = 57344;
std::vector<boost::optional<uint128_t>> expectedInts(136, val);
std::vector<uint8_t> expectedBinary = {0x27, 0xFD, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB,
0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x27, 0xFD, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RleEightSelectorSmall) {
// Selector 8 value
// 7340032 = 1110000000000000 = 3 value bits and 20 zeros
// This should be encoded as
// [(0111) (0101)] x7 [0001] [1000] = 7575757575757518 + 0xF (rle) + repeat eight selector
uint128_t val = 7340032;
std::vector<boost::optional<uint128_t>> expectedInts(134, val);
std::vector<uint8_t> expectedBinary = {0x18, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75,
0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RleEightSelectorLarge) {
// Selector 8 value
// 8462480737302404222943232= 111 + 80 zeros
// This should be encoded as
// [(0111) (10000)] x6 [1000] [1000] = 1E8F47A3D1E8F488 + 0xF (rle) + repeat eight selector
uint128_t val = absl::MakeUint128(0x70000, 0x0);
std::vector<boost::optional<uint128_t>> expectedInts(132, val);
std::vector<uint8_t> expectedBinary = {0x88, 0xF4, 0xE8, 0xD1, 0xA3, 0x47, 0x8F, 0x1E,
0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x88, 0xF4, 0xE8, 0xD1, 0xA3, 0x47, 0x8F, 0x1E};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RleFlushResetsRle) {
BufBuilder buffer;
Simple8bBuilder<uint64_t> builder([&buffer](uint64_t simple8bBlock) {
buffer.appendNum(simple8bBlock);
return true;
});
// Write a single 1 and flush. Then we add 120 more 1s and check that this does not start RLE.
ASSERT_TRUE(builder.append(1));
builder.flush();
for (int i = 0; i < 120; ++i) {
ASSERT_TRUE(builder.append(1));
}
builder.flush();
auto size = buffer.len();
auto sharedBuffer = buffer.release();
std::vector<uint8_t> simple8bBlockOne1 = {0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<uint8_t> simple8bBlockThirty1s = {0x52, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
std::vector<uint8_t> expectedBinary;
expectedBinary.insert(expectedBinary.end(), simple8bBlockOne1.begin(), simple8bBlockOne1.end());
for (int i = 0; i < 4; ++i) {
expectedBinary.insert(
expectedBinary.end(), simple8bBlockThirty1s.begin(), simple8bBlockThirty1s.end());
}
ASSERT_EQ(size, expectedBinary.size());
ASSERT_EQ(memcmp(sharedBuffer.get(), expectedBinary.data(), size), 0);
Simple8b<uint64_t> s8b(sharedBuffer.get(), size);
assertValuesEqual(s8b, std::vector<boost::optional<uint64_t>>(121, 1));
}
TEST(Simple8b, RleFlushResetsPossibleSelectors) {
BufBuilder buffer;
Simple8bBuilder<uint64_t> builder([&buffer](uint64_t simple8bBlock) {
buffer.appendNum(simple8bBlock);
return true;
});
// Write a large value with many trailing zeros that does not fit in the base selector, we then
// flush and make sure that we can write a value that only fits in the base selector. We should
// have reset possible selectors as part of the flush.
std::vector<boost::optional<uint64_t>> expectedInts = {0x8000000000000000, 0x0FFFFFFFFFFFFFFE};
ASSERT_TRUE(builder.append(*expectedInts[0]));
builder.flush();
ASSERT_TRUE(builder.append(*expectedInts[1]));
builder.flush();
auto size = buffer.len();
auto sharedBuffer = buffer.release();
Simple8b<uint64_t> s8b(sharedBuffer.get(), size);
assertValuesEqual(s8b, expectedInts);
}
TEST(Simple8b, FlushResetsLastInPreviousWhenFlushingRle) {
BufBuilder buffer;
Simple8bBuilder<uint64_t> builder([&buffer](uint64_t simple8bBlock) {
buffer.appendNum(simple8bBlock);
return true;
});
// Write 150 1s and flush. This should result in a word with 30 1s followed by RLE. We make sure
// that last value written is reset when RLE is the last thing we flush.
for (int i = 0; i < 150; ++i) {
ASSERT_TRUE(builder.append(1));
}
builder.flush();
// Last value written is only used for RLE so append 120 values of the same value and make sure
// this does _NOT_ start RLE as flush occured in between.
for (int i = 0; i < 120; ++i) {
ASSERT_TRUE(builder.append(1));
}
builder.flush();
auto size = buffer.len();
auto sharedBuffer = buffer.release();
std::vector<uint8_t> simple8bBlockThirty1s = {0x52, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
std::vector<uint8_t> simple8bBlockRLE = {0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<uint8_t> expectedBinary = simple8bBlockThirty1s;
expectedBinary.insert(expectedBinary.end(), simple8bBlockRLE.begin(), simple8bBlockRLE.end());
for (int i = 0; i < 4; ++i) {
expectedBinary.insert(
expectedBinary.end(), simple8bBlockThirty1s.begin(), simple8bBlockThirty1s.end());
}
ASSERT_EQ(size, expectedBinary.size());
ASSERT_EQ(memcmp(sharedBuffer.get(), expectedBinary.data(), size), 0);
Simple8b<uint64_t> s8b(sharedBuffer.get(), size);
assertValuesEqual(s8b, std::vector<boost::optional<uint64_t>>(270, 1));
}
TEST(Simple8b, FlushResetsLastInPreviousWhenFlushingRleZeroRleAfter) {
BufBuilder buffer;
Simple8bBuilder<uint64_t> builder([&buffer](uint64_t simple8bBlock) {
buffer.appendNum(simple8bBlock);
return true;
});
// Write 150 1s and flush. This should result in a word with 30 1s followed by RLE. We make sure
// that last value written is reset when RLE is the last thing we flush.
for (int i = 0; i < 150; ++i) {
ASSERT_TRUE(builder.append(1));
}
builder.flush();
auto sizeAfterFlush = buffer.len();
// Write 120 0s. They should be encoded as a single RLE block.
for (int i = 0; i < 120; ++i) {
ASSERT_TRUE(builder.append(0));
}
builder.flush();
auto size = buffer.len();
auto sharedBuffer = buffer.release();
std::vector<uint8_t> simple8bBlockThirty1s = {0x52, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
std::vector<uint8_t> simple8bBlockRLE = {0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<uint8_t> expectedBinary = simple8bBlockThirty1s;
for (int i = 0; i < 2; ++i) {
expectedBinary.insert(
expectedBinary.end(), simple8bBlockRLE.begin(), simple8bBlockRLE.end());
}
ASSERT_EQ(size, expectedBinary.size());
ASSERT_EQ(memcmp(sharedBuffer.get(), expectedBinary.data(), size), 0);
{
// Reading all values as one block would be interpreted as everything is 1s as we wrote a
// RLE block immediately after a block containing 1 values.
Simple8b<uint64_t> s8b(sharedBuffer.get(), size);
assertValuesEqual(s8b, std::vector<boost::optional<uint64_t>>(270, 1));
}
// In practise the binary is split up in two parts where we can initialize the second part on
// how the RLE should be interpreted.
{
Simple8b<uint64_t> s8b(sharedBuffer.get(), sizeAfterFlush);
assertValuesEqual(s8b, std::vector<boost::optional<uint64_t>>(150, 1));
}
{
Simple8b<uint64_t> s8b(
sharedBuffer.get() + sizeAfterFlush, size - sizeAfterFlush, 0 /* previous */);
assertValuesEqual(s8b, std::vector<boost::optional<uint64_t>>(120, 0));
}
}
TEST(Simple8b, EightSelectorLargeMax) {
// Selector 8 value
// 1111 + 124 zeros
// This should be encoded as
// [001111] [11111] x5 [1001] [1000] = 1FF3FE7FCFF9FF98
uint128_t val = absl::MakeUint128(0xF000000000000000, 0x0);
std::vector<boost::optional<uint128_t>> expectedInts(5, val);
std::vector<uint8_t> expectedBinary = {0x98, 0xFF, 0xF9, 0xCF, 0x7F, 0xFE, 0xF3, 0x1f};
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, RLELargeCount) {
std::vector<boost::optional<uint64_t>> expectedInts(257 * 120, 0);
std::vector<uint8_t> RLEblock16Count = {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<uint8_t> RLEblock1Count = {0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<uint8_t> expectedBinary;
for (int i = 0; i < 16; ++i) {
expectedBinary.insert(expectedBinary.end(), RLEblock16Count.begin(), RLEblock16Count.end());
}
expectedBinary.insert(expectedBinary.end(), RLEblock1Count.begin(), RLEblock1Count.end());
testSimple8b(expectedInts, expectedBinary);
}
TEST(Simple8b, ValueTooLarge) {
// This value needs 61 bits which it too large for Simple8b
uint64_t value = 0x1FFFFFFFFFFFFFFF;
Simple8bBuilder<uint64_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ValueTooLargeMaxUInt64) {
// Make sure we handle uint64_t max correctly.
uint64_t value = std::numeric_limits<uint64_t>::max();
Simple8bBuilder<uint64_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ValueTooLargeMaxUInt128) {
// Make sure we handle uint128_t max correctly.
uint128_t value = std::numeric_limits<uint128_t>::max();
Simple8bBuilder<uint128_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ValueTooLargeMaxUInt64AsUInt128) {
// Make sure we handle uint128_t max correctly.
uint128_t value = std::numeric_limits<uint64_t>::max();
Simple8bBuilder<uint128_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ValueTooManyTrailingFor8SmallTooManyMeaningfulFor8Large) {
// This value has 52 meaningful bits and 61 trailing zeros. This is too many trailing zeros for
// Selector 8 Small and too many meaningful bits for Selector 8 Large.
uint128_t value = absl::MakeUint128(0x1FFFFF0FFFFFF, 0xE000000000000000);
Simple8bBuilder<uint128_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ValueTooLargeMax8SmallAddForSkipPattern) {
// This value has 52 meaningful bits and 60 trailing zeros. But one extra 0 needs to be added to
// the meaningful bits to differentiate from the missing value pattern to be able to store in
// Extended 8 Small which brings it to 53 bits which is too many. Extended 8 Large can't be used
// either as it can only store 51 meaningful bits.
uint128_t value = absl::MakeUint128(0xFFFFFFFFFFFF, 0xF000000000000000);
Simple8bBuilder<uint128_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ValueTooLargeTrailingZerosNotDivisibleBy4) {
// This value has 52 meaningful bits and 59 trailing zeros. But 3 of the trailing bits need to
// be stored in the data bits as it's not divisible by 4. This brings the data bits to 55 which
// it too large.
uint128_t value = absl::MakeUint128(0x7FFFFFFFFFFF, 0xF800000000000000);
Simple8bBuilder<uint128_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ValueTooLargeBitCountUsedForExtendedSelectors) {
// This value has 63 meaningful bits and does not fit in Simple8b. When evaluating the extended
// selectors it will almost fit as it can pack the 9 trailing zero in the count but the amount
// of bits required will still be too large. Make sure append takes into the account the number
// of bits used for the count when checking if the value can be stored.
uint64_t value = 0x646075fffc000200;
Simple8bBuilder<uint64_t> builder([](uint64_t) {
ASSERT(false);
return true;
});
ASSERT_FALSE(builder.append(value));
}
TEST(Simple8b, ResetRLEAfterLargeValue) {
uint8_t kRleMultiplier = 120;
uint8_t kBaseSelectorMask = 0x000000000000000F;
uint8_t kRleSelector = 15;
// Large value that can be only be stored in the extended selectors that encodes a bit shift
uint64_t large = 0xC000000000000000;
BufBuilder buf;
Simple8bBuilder<uint64_t> b([&buf](uint64_t simple8bBlock) {
buf.appendNum(simple8bBlock);
return true;
});
// Write as many of these large values we need to ensure a non-RLE block is written followed by
// an RLE block.
for (int i = 0; i < kRleMultiplier + 7; ++i) {
ASSERT_TRUE(b.append(large));
}
// Add a large value that can only fit in the base selector which can encode up to 60 meaningful
// bits. When terminating RLE we should completely reset to allow this value to be appended.
ASSERT_TRUE(b.append(0x07FFFFFFFFFFFFFF));
b.flush();
auto size = buf.len();
auto data = buf.release();
// The second block should be an RLE block
ASSERT_GT(size, 16);
uint64_t secondBlock =
ConstDataView(data.get() + sizeof(uint64_t)).read<LittleEndian<uint64_t>>();
ASSERT_TRUE((secondBlock & kBaseSelectorMask) == kRleSelector);
}
|