Debian Codegraph @

tests/src/container/test_BitContainer.cpp

raw
1 #include <pbcopper/container/BitContainer.h>
2
3 #include <gtest/gtest.h>
4
5 #include <cstdint>
6 #include <limits>
7 #include <random>
8 #include <sstream>
9 #include <stdexcept>
10 #include <string_view>
11 #include <vector>
12
13 using namespace PacBio;
14
15 namespace PacBio {
16 namespace Container {
17
18 template <int32_t TotalBits, int32_t ElementBits>
19 bool operator==(const BitContainer<TotalBits, ElementBits>& lhs,
20 const std::vector<typename BitContainer<TotalBits, ElementBits>::ValueType>& rhs)
21 {
22 if (lhs.Size() != static_cast<int32_t>(rhs.size())) {
23 return false;
24 }
25
26 for (size_t i = 0; i < rhs.size(); ++i) {
27 if (lhs[i] != rhs[i]) {
28 return false;
29 }
30 }
31 return true;
32 }
33
34 } // namespace Container
35 } // namespace PacBio
36
37 TEST(Container_BitContainer, Constexpr)
38 {
39 // T G C A
40 // 3 2 1 0
41 constexpr Container::BitContainer<32, 8> CONSTEXPR_BCT{0x54'47'43'41U, 4};
42
43 EXPECT_EQ(sizeof(CONSTEXPR_BCT), 2 * sizeof(uint32_t));
44 EXPECT_EQ(CONSTEXPR_BCT.Capacity(), 4);
45 EXPECT_EQ(CONSTEXPR_BCT.Size(), 4);
46
47 EXPECT_EQ(CONSTEXPR_BCT[0], 'A');
48 EXPECT_EQ(CONSTEXPR_BCT[1], 'C');
49 EXPECT_EQ(CONSTEXPR_BCT[2], 'G');
50 EXPECT_EQ(CONSTEXPR_BCT[3], 'T');
51 }
52
53 TEST(Container_BitContainer, Equal)
54 {
55 const Container::BitContainer<32, 8> bct1{0x00'47'43'41U, 3};
56 const Container::BitContainer<32, 8> bct2{0x47'43'41U, 3};
57
58 EXPECT_EQ(bct1, bct2);
59 }
60
61 TEST(Container_BitContainer, NonEqual1)
62 {
63 const Container::BitContainer<32, 8> bct1{0x00'47'43'41U, 4};
64 const Container::BitContainer<32, 8> bct2{0x00'47'43'41U, 3};
65
66 EXPECT_NE(bct1, bct2);
67 }
68
69 TEST(Container_BitContainer, NonEqual2)
70 {
71 const Container::BitContainer<32, 8> bct1{0x00'46'43'41U, 3};
72 const Container::BitContainer<32, 8> bct2{0x00'47'43'41U, 3};
73
74 EXPECT_NE(bct1, bct2);
75 }
76
77 TEST(Container_BitContainer, VariadicCtor)
78 {
79 constexpr auto CONSTEXPR_BCT =
80 Container::BitContainer<32, 8>::MakeFromArray('G', 'A', 'T', 'C');
81
82 EXPECT_EQ(sizeof(CONSTEXPR_BCT), 2 * sizeof(uint32_t));
83 EXPECT_EQ(CONSTEXPR_BCT.Capacity(), 4);
84 EXPECT_EQ(CONSTEXPR_BCT.Size(), 4);
85
86 EXPECT_EQ(CONSTEXPR_BCT[0], 'G');
87 EXPECT_EQ(CONSTEXPR_BCT[1], 'A');
88 EXPECT_EQ(CONSTEXPR_BCT[2], 'T');
89 EXPECT_EQ(CONSTEXPR_BCT[3], 'C');
90 EXPECT_EQ(CONSTEXPR_BCT.RawData(), 0x43'54'41'47U);
91 }
92
93 TEST(Container_BitContainer, ConstexprDependencyInjection)
94 {
95 constexpr std::string_view STR{"ACGT"};
96 constexpr auto CONVERTER = [](const char c) {
97 if (c == 'T') {
98 return 0;
99 } else if (c == 'G') {
100 return 1;
101 } else if (c == 'C') {
102 return 2;
103 } else if (c == 'A') {
104 return 3;
105 }
106
107 throw std::logic_error{"encountered an invalid base"};
108 };
109
110 constexpr auto CONSTEXPR_BCT_DI =
111 Container::BitContainer<8, 2>::MakeFromTransform(CONVERTER, STR);
112 EXPECT_EQ(sizeof(CONSTEXPR_BCT_DI), 2 * sizeof(uint32_t));
113 EXPECT_EQ(CONSTEXPR_BCT_DI.Capacity(), 4);
114 EXPECT_EQ(CONSTEXPR_BCT_DI.Size(), 4);
115
116 EXPECT_EQ(CONSTEXPR_BCT_DI[0], 3);
117 EXPECT_EQ(CONSTEXPR_BCT_DI[1], 2);
118 EXPECT_EQ(CONSTEXPR_BCT_DI[2], 1);
119 EXPECT_EQ(CONSTEXPR_BCT_DI[3], 0);
120
121 EXPECT_EQ(CONSTEXPR_BCT_DI.RawData(), 0b00'01'10'11);
122
123 std::ostringstream os;
124 os << CONSTEXPR_BCT_DI;
125 EXPECT_EQ(os.str(), "BitContainer(Size=4, BitmaskContainer(3, 2, 1, 0)");
126 }
127
128 TEST(Container_BitContainer, Basic)
129 {
130 Container::BitContainer<8, 2> bct{0b11'00'10'01, 4};
131 EXPECT_EQ(sizeof(bct), 2 * sizeof(uint32_t));
132 EXPECT_EQ(bct.Capacity(), 4);
133 EXPECT_EQ(bct.Size(), 4);
134
135 EXPECT_EQ(bct[0], 0b01);
136 EXPECT_EQ(bct[1], 0b10);
137 EXPECT_EQ(bct[2], 0b00);
138 EXPECT_EQ(bct[3], 0b11);
139
140 bct.Insert(0, 0b00);
141 EXPECT_EQ(bct.Size(), 4);
142 EXPECT_EQ(bct.RawData(), 0b00'10'01'00);
143
144 bct.Remove(1);
145 EXPECT_EQ(bct.Size(), 3);
146 EXPECT_EQ(bct.RawData(), 0b00'10'00);
147
148 bct.Set(2, 0b11);
149 EXPECT_EQ(bct.Size(), 3);
150 EXPECT_EQ(bct.RawData(), 0b11'10'00);
151
152 bct.Insert(2, 0b01);
153 EXPECT_EQ(bct.Size(), 4);
154 EXPECT_EQ(bct.RawData(), 0b11'01'10'00);
155
156 bct.Clear();
157 EXPECT_EQ(bct.Size(), 0);
158 EXPECT_EQ(bct.RawData(), 0);
159 }
160
161 TEST(Container_BitContainer, PushBack)
162 {
163 Container::BitContainer<8, 2> bct{0b11, 1};
164 EXPECT_EQ(sizeof(bct), 2 * sizeof(uint32_t));
165 EXPECT_EQ(bct.Capacity(), 4);
166 EXPECT_EQ(bct.Size(), 1);
167
168 bct.PushBack(2);
169 bct.PushBack(0);
170 bct.PushBack(3);
171
172 EXPECT_EQ(bct[0], 0b11);
173 EXPECT_EQ(bct[1], 0b10);
174 EXPECT_EQ(bct[2], 0b00);
175 EXPECT_EQ(bct[3], 0b11);
176 EXPECT_EQ(bct.Size(), 4);
177 }
178
179 TEST(Container_BitContainer, Range)
180 {
181 using ContType = Container::BitContainer<32, 4>;
182
183 const auto bct = ContType::MakeFromArray(1, 7, 9, 15, 3, 6, 5, 4);
184 EXPECT_EQ(sizeof(bct), 2 * sizeof(uint32_t));
185 EXPECT_EQ(bct.Capacity(), 8);
186 EXPECT_EQ(bct.Size(), 8);
187
188 // copy the whole range
189 {
190 const auto bctNew = bct.Range(0, 8);
191 EXPECT_EQ(bctNew, bct);
192 }
193
194 // copy the whole range, but exceed length
195 {
196 const auto bctNew = bct.Range(0, 34837);
197 EXPECT_EQ(bctNew, bct);
198 }
199
200 // copy nothing from the start
201 {
202 const auto bctNew = bct.Range(0, 0);
203 EXPECT_EQ(bctNew, ContType{});
204 }
205
206 // copy nothing from the middle
207 {
208 const auto bctNew = bct.Range(4, 0);
209 EXPECT_EQ(bctNew, ContType{});
210 }
211
212 // copy nothing from the end
213 {
214 const auto bctNew = bct.Range(8, 2);
215 EXPECT_EQ(bctNew, ContType{});
216 }
217
218 // copy 1 base from the start
219 {
220 const auto bctNew = bct.Range(0, 1);
221 EXPECT_EQ(bctNew, ContType::MakeFromArray(1));
222 }
223
224 // copy 2 bases from the start
225 {
226 const auto bctNew = bct.Range(0, 2);
227 EXPECT_EQ(bctNew, ContType::MakeFromArray(1, 7));
228 }
229
230 // copy 1 base from the middle
231 {
232 const auto bctNew = bct.Range(4, 1);
233 EXPECT_EQ(bctNew, ContType::MakeFromArray(3));
234 }
235
236 // copy 2 bases from the middle
237 {
238 const auto bctNew = bct.Range(4, 2);
239 EXPECT_EQ(bctNew, ContType::MakeFromArray(3, 6));
240 }
241
242 // copy 1 base from the end
243 {
244 const auto bctNew = bct.Range(6, 1);
245 EXPECT_EQ(bctNew, ContType::MakeFromArray(5));
246 }
247
248 // copy 2 bases from the end
249 {
250 const auto bctNew = bct.Range(6, 2);
251 EXPECT_EQ(bctNew, ContType::MakeFromArray(5, 4));
252 }
253
254 // copy all bases from the 2nd base
255 {
256 const auto bctNew = bct.Range(1, 3457874);
257 EXPECT_EQ(bctNew, ContType::MakeFromArray(7, 9, 15, 3, 6, 5, 4));
258 }
259 }
260
261 template <int32_t TotalBits, int32_t ElementBits>
262 void TestBitContainer()
263 {
264 std::random_device rd;
265 std::mt19937 gen{rd()};
266
267 constexpr int32_t ROUNDS = 100'000;
268
269 Container::BitContainer<TotalBits, ElementBits> bct{};
270 std::vector<typename decltype(bct)::ValueType> vec;
271
272 std::uniform_int_distribution<typename decltype(bct)::ValueType> elementDistribution{
273 0, bct.MAXIMUM_VALUE};
274
275 for (int32_t i = 0; i < ROUNDS; ++i) {
276 const auto randVal = elementDistribution(gen);
277
278 if (vec.size() == 0) {
279 // can only insert
280 bct.Insert(0, randVal);
281 vec.insert(std::begin(vec), randVal);
282 } else {
283 std::uniform_int_distribution<int32_t> moveDistribution{
284 0, 1 + (static_cast<int32_t>(vec.size()) < bct.Capacity())};
285 std::uniform_int_distribution<int32_t> posDistribution(0, vec.size() - 1);
286 const int32_t moveType = moveDistribution(gen);
287 const int32_t pos = posDistribution(gen);
288
289 switch (moveType) {
290 case 0: // Set
291 bct.Set(pos, randVal);
292 vec[pos] = randVal;
293 break;
294 case 1: // Delete
295 bct.Remove(pos);
296 vec.erase(std::begin(vec) + pos);
297 break;
298 case 2: // Insert
299 bct.Insert(pos, randVal);
300 vec.insert(std::begin(vec) + pos, randVal);
301 break;
302 }
303 }
304
305 EXPECT_EQ(bct, vec);
306 }
307 }
308
309 TEST(Container_BitContainer, 8bit) { TestBitContainer<8, 1>(); }
310
311 TEST(Container_BitContainer, 16bit) { TestBitContainer<16, 4>(); }
312
313 TEST(Container_BitContainer, 32bit) { TestBitContainer<32, 8>(); }
314
315 TEST(Container_BitContainer, 64bit) { TestBitContainer<64, 32>(); }
316
317 TEST(Container_BitContainer, ReverseBasic)
318 {
319 auto bct = Container::BitContainer<32, 8>::MakeFromArray(1, 2, 3);
320 auto bctRev = Container::BitContainer<32, 8>::MakeFromArray(3, 2, 1);
321
322 EXPECT_EQ(bct.Capacity(), 4);
323 EXPECT_EQ(bct.Size(), 3);
324 EXPECT_EQ(bctRev.Capacity(), 4);
325 EXPECT_EQ(bctRev.Size(), 3);
326
327 bct.Reverse();
328 EXPECT_EQ(bct, bctRev);
329 }
330
331 TEST(Container_BitContainer, ReverseEmpty)
332 {
333 Container::BitContainer<64, 8> bct{0, 0};
334
335 EXPECT_EQ(bct.Capacity(), 8);
336 EXPECT_EQ(bct.Size(), 0);
337
338 bct.Reverse();
339 EXPECT_EQ(bct.Size(), 0);
340 EXPECT_EQ(bct.RawData(), 0U);
341 }
342
343 TEST(Container_BitContainer, ReverseOne)
344 {
345 Container::BitContainer<32, 8> bct{255U, 1};
346
347 EXPECT_EQ(bct.Capacity(), 4);
348 EXPECT_EQ(bct.Size(), 1);
349
350 bct.Reverse();
351 EXPECT_EQ(bct.Size(), 1);
352 EXPECT_EQ(bct.RawData(), 255U);
353 }
354
355 template <int32_t TotalBits, int32_t ElementBits>
356 void TestBitReverse()
357 {
358 std::random_device rd;
359 std::mt19937 gen{rd()};
360
361 constexpr int32_t ROUNDS = 100'000;
362
363 using BitVec = typename Container::BitContainer<TotalBits, ElementBits>;
364 constexpr int32_t CAPACITY = BitVec::Capacity();
365
366 std::uniform_int_distribution<typename BitVec::ValueType> elementDistribution{
367 0, BitVec::MAXIMUM_VALUE};
368 std::uniform_int_distribution<int32_t> lengthDistribution{1, CAPACITY};
369
370 for (int32_t i = 0; i < ROUNDS; ++i) {
371 const int32_t length = lengthDistribution(gen);
372
373 BitVec fwdBct{0, length};
374 std::vector<typename BitVec::ValueType> fwdVec(length);
375
376 for (int32_t j = 0; j < length; ++j) {
377 const auto val = elementDistribution(gen);
378
379 fwdBct.Set(j, val);
380 fwdVec[j] = val;
381 }
382
383 EXPECT_EQ(fwdBct, fwdVec);
384
385 // reverse both now
386 BitVec revBct = fwdBct;
387 std::vector<typename BitVec::ValueType> revVec = fwdVec;
388
389 revBct.Reverse();
390 std::reverse(std::begin(revVec), std::end(revVec));
391
392 EXPECT_EQ(revBct, revVec);
393 }
394 }
395
396 TEST(Container_BitContainer, Reverse8_1bit) { TestBitReverse<8, 1>(); }
397 TEST(Container_BitContainer, Reverse8_2bit) { TestBitReverse<8, 2>(); }
398 TEST(Container_BitContainer, Reverse8_4bit) { TestBitReverse<8, 4>(); }
399
400 TEST(Container_BitContainer, Reverse16_1bit) { TestBitReverse<16, 1>(); }
401 TEST(Container_BitContainer, Reverse16_2bit) { TestBitReverse<16, 2>(); }
402 TEST(Container_BitContainer, Reverse16_4bit) { TestBitReverse<16, 4>(); }
403 TEST(Container_BitContainer, Reverse16_8bit) { TestBitReverse<16, 8>(); }
404
405 TEST(Container_BitContainer, Reverse32_1bit) { TestBitReverse<32, 1>(); }
406 TEST(Container_BitContainer, Reverse32_2bit) { TestBitReverse<32, 2>(); }
407 TEST(Container_BitContainer, Reverse32_4bit) { TestBitReverse<32, 4>(); }
408 TEST(Container_BitContainer, Reverse32_8bit) { TestBitReverse<32, 8>(); }
409 TEST(Container_BitContainer, Reverse32_16bit) { TestBitReverse<32, 16>(); }
410
411 TEST(Container_BitContainer, Reverse64_1bit) { TestBitReverse<64, 1>(); }
412 TEST(Container_BitContainer, Reverse64_2bit) { TestBitReverse<64, 2>(); }
413 TEST(Container_BitContainer, Reverse64_4bit) { TestBitReverse<64, 4>(); }
414 TEST(Container_BitContainer, Reverse64_8bit) { TestBitReverse<64, 8>(); }
415 TEST(Container_BitContainer, Reverse64_16bit) { TestBitReverse<64, 16>(); }
416 TEST(Container_BitContainer, Reverse64_32bit) { TestBitReverse<64, 32>(); }
417
418 TEST(Container_BitContainer, Reverse128_1bit) { TestBitReverse<128, 1>(); }
419 TEST(Container_BitContainer, Reverse128_2bit) { TestBitReverse<128, 2>(); }
420 TEST(Container_BitContainer, Reverse128_4bit) { TestBitReverse<128, 4>(); }
421 TEST(Container_BitContainer, Reverse128_8bit) { TestBitReverse<128, 8>(); }
422 TEST(Container_BitContainer, Reverse128_16bit) { TestBitReverse<128, 16>(); }
423 TEST(Container_BitContainer, Reverse128_32bit) { TestBitReverse<128, 32>(); }
424 TEST(Container_BitContainer, Reverse128_64bit) { TestBitReverse<128, 64>(); }