GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/zserio/BitStreamTest.cpp Lines: 287 287 100.0 %
Date: 2023-12-13 14:51:09 Branches: 883 2144 41.2 %

Line Branch Exec Source
1
#include <cstring>
2
#include <string>
3
#include <functional>
4
#include <array>
5
6
#include "zserio/BitStreamWriter.h"
7
#include "zserio/BitStreamReader.h"
8
#include "zserio/Types.h"
9
#include "zserio/CppRuntimeException.h"
10
#include "zserio/Vector.h"
11
12
#include "gtest/gtest.h"
13
14
namespace zserio
15
{
16
17
23
class BitStreamTest : public ::testing::Test
18
{
19
public:
20
23
    BitStreamTest() : m_byteBuffer(), m_externalWriter(m_byteBuffer.data(), m_byteBuffer.size()),
21

23
            m_dummyWriter(nullptr, 0)
22
    {
23
23
        m_byteBuffer.fill(0);
24
23
    }
25
26
protected:
27
    template <typename T, size_t N, typename U>
28
16
    void testImpl(const std::array<T, N>& values, std::function<void (BitStreamWriter&, U)> writerFunc,
29
            std::function<T(BitStreamReader&)> readerFunc, uint8_t maxStartBitPos)
30
    {
31














16
        testBitStreamValues(values, m_externalWriter, writerFunc, readerFunc, maxStartBitPos);
32














16
        testBitStreamValues(values, m_dummyWriter, writerFunc, readerFunc, maxStartBitPos);
33
16
    }
34
35
    template <typename T, size_t N, typename U>
36
32
    void testBitStreamValues(const std::array<T, N>& values, BitStreamWriter& writer,
37
            std::function<void (BitStreamWriter&, U)> writerFunc,
38
            std::function<T(BitStreamReader&)> readerFunc, uint8_t maxStartBitPos)
39
    {
40







1040
        for (uint8_t bitPos = 0; bitPos < maxStartBitPos; ++bitPos)
41
        {
42







1008
            if (bitPos > 0)
43







976
                writer.writeBits64(0, bitPos);
44







19150
            for (size_t i = 0; i < N; ++i)
45
            {
46














18142
                writerFunc(writer, values.at(i));
47
            }
48
49







1008
            if (!writer.hasWriteBuffer())
50
504
                continue;
51
52














504
            BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
53







504
            if (bitPos > 0)
54







488
                reader.readBits64(bitPos);
55







9575
            for (size_t i = 0; i < N; ++i)
56
            {
57













































































9071
                ASSERT_EQ(readerFunc(reader), values.at(i)) << "[bitPos=" << bitPos << "]";
58
            }
59
60







504
            writer.setBitPosition(0);
61







504
            m_byteBuffer.fill(0);
62
        }
63
    }
64
65
2
    void testReadBits(BitStreamWriter& writer)
66
    {
67
2
        writer.writeBits(1, 1);
68
2
        writer.writeBits(2, 2);
69
2
        writer.writeBits(42, 12);
70
2
        writer.writeBits(15999999, 24);
71
2
        writer.writeBits(7, 3);
72
73
2
        if (!writer.hasWriteBuffer())
74
2
            return;
75
76

1
        BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
77



1
        ASSERT_EQ(1, reader.readBits(1));
78



1
        ASSERT_EQ(2, reader.readBits(2));
79



1
        ASSERT_EQ(42, reader.readBits(12));
80



1
        ASSERT_EQ(15999999, reader.readBits(24));
81



1
        ASSERT_EQ(7, reader.readBits(3));
82
    }
83
84
2
    void testReadBits64(BitStreamWriter& writer)
85
    {
86
2
        writer.writeBits(1, 1);
87
2
        writer.writeBits64(UINT64_C(42424242424242), 48);
88
2
        writer.writeBits64(UINT64_C(0xFFFFFFFFFFFFFFFE), 64);
89
90
2
        if (!writer.hasWriteBuffer())
91
2
            return;
92
93

1
        BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
94



1
        ASSERT_EQ(1, reader.readBits(1));
95



1
        ASSERT_EQ(UINT64_C(42424242424242), reader.readBits64(48));
96



1
        ASSERT_EQ(UINT64_C(0xFFFFFFFFFFFFFFFE), reader.readBits64(64));
97
    }
98
99
2
    void testReadSignedBits(BitStreamWriter& writer)
100
    {
101
2
        writer.writeSignedBits(-1, 5);
102
2
        writer.writeSignedBits(3, 12);
103
2
        writer.writeSignedBits(-142, 9);
104
105
2
        if (!writer.hasWriteBuffer())
106
2
            return;
107
108

1
        BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
109



1
        ASSERT_EQ(-1, reader.readSignedBits(5));
110



1
        ASSERT_EQ(3, reader.readSignedBits(12));
111



1
        ASSERT_EQ(-142, reader.readSignedBits(9));
112
    }
113
114
2
    void testReadSignedBits64(BitStreamWriter& writer)
115
    {
116
2
        writer.writeSignedBits64(INT64_C(1), 4);
117
2
        writer.writeSignedBits64(INT64_C(-1), 48);
118
2
        writer.writeSignedBits64(INT64_C(-42424242), 61);
119
2
        writer.writeSignedBits64(INT64_C(-820816), 32);
120
121
2
        if (!writer.hasWriteBuffer())
122
2
            return;
123
124

1
        BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
125



1
        ASSERT_EQ(INT64_C(1), reader.readSignedBits(4));
126



1
        ASSERT_EQ(INT64_C(-1), reader.readSignedBits64(48));
127



1
        ASSERT_EQ(INT64_C(-42424242), reader.readSignedBits64(61));
128



1
        ASSERT_EQ(INT64_C(-820816), reader.readSignedBits64(32));
129
    }
130
131
2
    void testAlignedBytes(BitStreamWriter& writer)
132
    {
133
        // reads aligned data directly from buffer, bit cache should remain empty
134
2
        writer.writeBits(UINT8_C(0xCA), 8);
135
2
        writer.writeBits(UINT16_C(0xCAFE), 16);
136
2
        writer.writeBits(UINT32_C(0xCAFEC0), 24);
137
2
        writer.writeBits(UINT32_C(0xCAFEC0DE), 32);
138
2
        writer.writeBits64(UINT64_C(0xCAFEC0DEDE), 40);
139
2
        writer.writeBits64(UINT64_C(0xCAFEC0DEDEAD), 48);
140
2
        writer.writeBits64(UINT64_C(0xCAFEC0DEDEADFA), 56);
141
2
        writer.writeBits64(UINT64_C(0xCAFEC0DEDEADFACE), 64);
142
143
2
        if (!writer.hasWriteBuffer())
144
2
            return;
145
146

1
        BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
147



1
        ASSERT_EQ(UINT8_C(0xCA), reader.readBits(8));
148



1
        ASSERT_EQ(UINT16_C(0xCAFE), reader.readBits(16));
149



1
        ASSERT_EQ(UINT32_C(0xCAFEC0), reader.readBits(24));
150



1
        ASSERT_EQ(UINT32_C(0xCAFEC0DE), reader.readBits(32));
151



1
        ASSERT_EQ(UINT64_C(0xCAFEC0DEDE), reader.readBits64(40));
152



1
        ASSERT_EQ(UINT64_C(0xCAFEC0DEDEAD), reader.readBits64(48));
153



1
        ASSERT_EQ(UINT64_C(0xCAFEC0DEDEADFA), reader.readBits64(56));
154



1
        ASSERT_EQ(UINT64_C(0xCAFEC0DEDEADFACE), reader.readBits64(64));
155
    }
156
157
2
    void testSetBitPosition(BitStreamWriter& writer)
158
    {
159



2
        ASSERT_EQ(0, writer.getBitPosition());
160
2
        writer.writeBits(1, 1);
161



2
        ASSERT_EQ(1, writer.getBitPosition());
162
2
        writer.alignTo(4);
163



2
        ASSERT_EQ(4, writer.getBitPosition());
164
2
        writer.writeBits(5, 5);
165



2
        ASSERT_EQ(9, writer.getBitPosition());
166
2
        if (writer.hasWriteBuffer())
167
        {
168









2
            ASSERT_THROW(writer.setBitPosition(m_byteBuffer.size() * 8 + 1), CppRuntimeException);
169
        }
170
        else
171
        {
172
            // dummy buffer
173
1
            writer.setBitPosition(m_byteBuffer.size() * 8 + 1);
174



1
            ASSERT_EQ(m_byteBuffer.size() * 8 + 1, writer.getBitPosition());
175
        }
176
2
        writer.setBitPosition(4);
177



2
        ASSERT_EQ(4, writer.getBitPosition());
178
2
        writer.writeBits(3, 3);
179



2
        ASSERT_EQ(7, writer.getBitPosition());
180
181
2
        if (!writer.hasWriteBuffer())
182
1
            return;
183
184

1
        BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
185



1
        ASSERT_EQ(0, reader.getBitPosition());
186



1
        ASSERT_EQ(1, reader.readBits(1));
187



1
        ASSERT_EQ(1, reader.getBitPosition());
188
1
        reader.alignTo(4);
189



1
        ASSERT_EQ(4, reader.getBitPosition());
190



1
        ASSERT_EQ(3, reader.readBits(3));
191



1
        ASSERT_EQ(7, reader.getBitPosition());
192









2
        ASSERT_THROW(reader.setBitPosition(writer.getBitPosition() + 1), CppRuntimeException);
193
194
1
        reader.setBitPosition(4);
195



1
        ASSERT_EQ(4, reader.getBitPosition());
196



1
        ASSERT_EQ(3, reader.readBits(3));
197



1
        ASSERT_EQ(7, reader.getBitPosition());
198
    }
199
200
2
    void testAlignTo(BitStreamWriter& writer)
201
    {
202
2
        writer.writeBits(1, 1);
203
2
        writer.alignTo(4);
204



2
        ASSERT_EQ(4, writer.getBitPosition());
205
2
        writer.writeBits(1, 1);
206
2
        writer.alignTo(4);
207



2
        ASSERT_EQ(8, writer.getBitPosition());
208
2
        writer.writeBits(37, 11);
209
2
        writer.alignTo(8);
210



2
        ASSERT_EQ(24, writer.getBitPosition());
211
2
        writer.writeBits(1, 1);
212
2
        writer.alignTo(16);
213



2
        ASSERT_EQ(32, writer.getBitPosition());
214
2
        writer.writeBits(13, 13);
215
2
        writer.alignTo(32);
216



2
        ASSERT_EQ(64, writer.getBitPosition());
217
2
        writer.writeBits(42, 15);
218
2
        writer.alignTo(64);
219



2
        ASSERT_EQ(128, writer.getBitPosition());
220
2
        writer.writeBits(99, 9);
221



2
        ASSERT_EQ(137, writer.getBitPosition());
222
223
2
        if (!writer.hasWriteBuffer())
224
1
            return;
225
226

1
        BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag());
227



1
        ASSERT_EQ(1, reader.readBits(1));
228
1
        reader.alignTo(4);
229



1
        ASSERT_EQ(1, reader.readBits(1));
230
1
        reader.alignTo(4);
231



1
        ASSERT_EQ(37, reader.readBits(11));
232
1
        reader.alignTo(8);
233



1
        ASSERT_EQ(1, reader.readBits(1));
234
1
        reader.alignTo(16);
235



1
        ASSERT_EQ(13, reader.readBits(13));
236
1
        reader.alignTo(32);
237



1
        ASSERT_EQ(42, reader.readBits(15));
238
1
        reader.alignTo(64);
239



1
        ASSERT_EQ(99, reader.readBits(9));
240



1
        ASSERT_EQ(137, reader.getBitPosition());
241
    }
242
243
    std::array<uint8_t, 256> m_byteBuffer;
244
    BitStreamWriter m_externalWriter;
245
    BitStreamWriter m_dummyWriter;
246
};
247
248


802
TEST_F(BitStreamTest, readBits)
249
{
250
1
    testReadBits(m_externalWriter);
251
1
    testReadBits(m_dummyWriter);
252
1
}
253
254


802
TEST_F(BitStreamTest, readBits64)
255
{
256
1
    testReadBits64(m_externalWriter);
257
1
    testReadBits64(m_dummyWriter);
258
1
}
259
260


802
TEST_F(BitStreamTest, readSignedBits)
261
{
262
1
    testReadSignedBits(m_externalWriter);
263
1
    testReadSignedBits(m_dummyWriter);
264
1
}
265
266


802
TEST_F(BitStreamTest, readSignedBits64)
267
{
268
1
    testReadSignedBits64(m_externalWriter);
269
1
    testReadSignedBits64(m_dummyWriter);
270
1
}
271
272


802
TEST_F(BitStreamTest, alignedBytes)
273
{
274
1
    testAlignedBytes(m_externalWriter);
275
1
    testAlignedBytes(m_dummyWriter);
276
1
}
277
278


802
TEST_F(BitStreamTest, readVarInt64)
279
{
280
    const std::array<int64_t, 33> values =
281
    {
282
        INT64_C(0),
283
        INT64_C(-32),
284
        INT64_C(32),
285
        INT64_C(-4096),
286
        INT64_C(4096),
287
        INT64_C(-524288),
288
        INT64_C(524288),
289
        INT64_C(-67108864),
290
        INT64_C(67108864),
291
        INT64_C(-8589934592),
292
        INT64_C(8589934592),
293
        INT64_C(-1099511627776),
294
        INT64_C(1099511627776),
295
        INT64_C(-140737488355328),
296
        INT64_C(140737488355328),
297
        INT64_C(-18014398509481984),
298
        INT64_C(18014398509481984),
299
300
        ( INT64_C(1) << (0 ) ),
301
        ( INT64_C(1) << (6 ) ) - 1,
302
303
        ( INT64_C(1) << (6 ) ),
304
        ( INT64_C(1) << (6+7 ) ) - 1,
305
306
        ( INT64_C(1) << (6+7 ) ),
307
        ( INT64_C(1) << (6+7+7 ) ) - 1,
308
309
        ( INT64_C(1) << (6+7+7 ) ),
310
        ( INT64_C(1) << (6+7+7+7 ) ) - 1,
311
312
        ( INT64_C(1) << (6+7+7+7 ) ),
313
        ( INT64_C(1) << (6+7+7+7 +7 ) ) - 1,
314
315
        ( INT64_C(1) << (6+7+7+7 +7 ) ),
316
        ( INT64_C(1) << (6+7+7+7 +7+7 ) ) - 1,
317
318
        ( INT64_C(1) << (6+7+7+7 +7+7 ) ),
319
        ( INT64_C(1) << (6+7+7+7 +7+7+7 ) ) - 1,
320
321
        ( INT64_C(1) << (6+7+7+7 +7+7+7 ) ),
322
        ( INT64_C(1) << (6+7+7+7 +7+7+7+8 ) ) - 1
323
1
    };
324
325
2
    std::function<void (BitStreamWriter&, int64_t)> writerFunc = &BitStreamWriter::writeVarInt64;
326
2
    std::function<int64_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarInt64;
327
328

1
    testImpl(values, writerFunc, readerFunc, 63);
329
1
}
330
331


802
TEST_F(BitStreamTest, readVarInt32)
332
{
333
    const std::array<int32_t, 17> values =
334
    {
335
        static_cast<int32_t>(0),
336
        static_cast<int32_t>(-32),
337
        static_cast<int32_t>(32),
338
        static_cast<int32_t>(-4096),
339
        static_cast<int32_t>(4096),
340
        static_cast<int32_t>(-524288),
341
        static_cast<int32_t>(524288),
342
        static_cast<int32_t>(-67108864),
343
        static_cast<int32_t>(67108864),
344
345
        static_cast<int32_t>(1U << (0U)),
346
        static_cast<int32_t>(1U << (6U)) - 1,
347
348
        static_cast<int32_t>(1U << (6U)),
349
        static_cast<int32_t>(1U << (6U+7)) - 1,
350
351
        static_cast<int32_t>(1U << (6U+7)),
352
        static_cast<int32_t>(1U << (6U+7+7)) - 1,
353
354
        static_cast<int32_t>(1U << (6U+7+7)),
355
        static_cast<int32_t>(1U << (6U+7+7+8)) - 1,
356
1
    };
357
358
2
    std::function<void (BitStreamWriter&, int32_t)> writerFunc = &BitStreamWriter::writeVarInt32;
359
2
    std::function<int32_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarInt32;
360
361

1
    testImpl(values, writerFunc, readerFunc, 31);
362
1
}
363
364


802
TEST_F(BitStreamTest, readVarInt16)
365
{
366
    const std::array<int16_t, 9> values =
367
    {
368
        static_cast<int16_t>(0),
369
        static_cast<int16_t>(-32),
370
        static_cast<int16_t>(32),
371
        static_cast<int16_t>(-4096),
372
        static_cast<int16_t>(4096),
373
374
        static_cast<int16_t>(1U << (0U)),
375
        static_cast<int16_t>(1U << (6U)) - 1,
376
377
        static_cast<int16_t>(1U << (6U)),
378
        static_cast<int16_t>(1U << (6+8U)) - 1,
379
1
    };
380
381
2
    std::function<void (BitStreamWriter&, int16_t)> writerFunc = &BitStreamWriter::writeVarInt16;
382
2
    std::function<int16_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarInt16;
383
384

1
    testImpl(values, writerFunc, readerFunc, 15);
385
1
}
386
387


802
TEST_F(BitStreamTest, readVarUInt64)
388
{
389
    const std::array<uint64_t, 19> values =
390
    {
391
        0,
392
        262144,
393
        524288,
394
395
        (UINT64_C(1) << (0U)),
396
        (UINT64_C(1) << (7U)) - 1,
397
398
        (UINT64_C(1) << (7U)),
399
        (UINT64_C(1) << (7U+7)) - 1,
400
401
        (UINT64_C(1) << (7U+7)),
402
        (UINT64_C(1) << (7U+7+7)) - 1,
403
404
        (UINT64_C(1) << (7U+7+7)),
405
        (UINT64_C(1) << (7U+7+7+7)) - 1,
406
407
        (UINT64_C(1) << (7U+7+7+7)),
408
        (UINT64_C(1) << (7U+7+7+7 +7)) - 1,
409
410
        (UINT64_C(1) << (7U+7+7+7 +7)),
411
        (UINT64_C(1) << (7U+7+7+7 +7+7)) - 1,
412
413
        (UINT64_C(1) << (7U+7+7+7 +7+7)),
414
        (UINT64_C(1) << (7U+7+7+7 +7+7+7)) - 1,
415
416
        (UINT64_C(1) << (7U+7+7+7 +7+7+7)),
417
        (UINT64_C(1) << (7U+7+7+7 +7+7+7+8)) - 1,
418
1
    };
419
420
2
    std::function<void (BitStreamWriter&, uint64_t)> writerFunc = &BitStreamWriter::writeVarUInt64;
421
2
    std::function<uint64_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarUInt64;
422
423

1
    testImpl(values, writerFunc, readerFunc, 63);
424
1
}
425
426


802
TEST_F(BitStreamTest, readVarUInt32)
427
{
428
    const std::array<uint32_t, 11> values =
429
    {
430
        0,
431
        65536,
432
        131072,
433
434
        (1U << (0U)),
435
        (1U << (7U)) - 1,
436
437
        (1U << (7U)),
438
        (1U << (7U+7)) - 1,
439
440
        (1U << (7U+7)),
441
        (1U << (7U+7+7)) - 1,
442
443
        (1U << (7U+7+7)),
444
        (1U << (7U+7+7+8)) - 1,
445
1
    };
446
447
2
    std::function<void (BitStreamWriter&, uint32_t)> writerFunc = &BitStreamWriter::writeVarUInt32;
448
2
    std::function<uint32_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarUInt32;
449
450

1
    testImpl(values, writerFunc, readerFunc, 31);
451
1
}
452
453


802
TEST_F(BitStreamTest, readVarUInt16)
454
{
455
    const std::array<uint16_t, 7> values =
456
    {
457
        0,
458
        8192,
459
        16384,
460
461
        (1U << (0U)),
462
        (1U << (6U)) - 1,
463
464
        (1U << (6U)),
465
        (1U << (6U+8)) - 1,
466
1
    };
467
468
2
    std::function<void (BitStreamWriter&, uint16_t)> writerFunc = &BitStreamWriter::writeVarUInt16;
469
2
    std::function<uint16_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarUInt16;
470
471

1
    testImpl(values, writerFunc, readerFunc, 15);
472
1
}
473
474


802
TEST_F(BitStreamTest, readVarInt)
475
{
476
    const std::array<int64_t, 38> values =
477
    {
478
        // 1 byte
479
        0,
480
        -1,
481
        1,
482
        -static_cast<int64_t>(UINT64_C(1) << 6U) + 1,
483
        static_cast<int64_t>(UINT64_C(1) << 6U) - 1,
484
        // 2 bytes
485
        -static_cast<int64_t>(UINT64_C(1) << 6U),
486
        static_cast<int64_t>(UINT64_C(1) << 6U),
487
        -static_cast<int64_t>(UINT64_C(1) << 13U) + 1,
488
        static_cast<int64_t>(UINT64_C(1) << 13U) - 1,
489
        // 3 bytes
490
        -static_cast<int64_t>(UINT64_C(1) << 13U),
491
        static_cast<int64_t>(UINT64_C(1) << 13U),
492
        -static_cast<int64_t>(UINT64_C(1) << 20U) + 1,
493
        static_cast<int64_t>(UINT64_C(1) << 20U) - 1,
494
        // 4 bytes
495
        -static_cast<int64_t>(UINT64_C(1) << 20U),
496
        static_cast<int64_t>(UINT64_C(1) << 20U),
497
        -static_cast<int64_t>(UINT64_C(1) << 27U) + 1,
498
        static_cast<int64_t>(UINT64_C(1) << 27U) - 1,
499
        // 5 bytes
500
        -static_cast<int64_t>(UINT64_C(1) << 27U),
501
        static_cast<int64_t>(UINT64_C(1) << 27U),
502
        -static_cast<int64_t>(UINT64_C(1) << 34U) + 1,
503
        static_cast<int64_t>(UINT64_C(1) << 34U) - 1,
504
        // 6 bytes
505
        -static_cast<int64_t>(UINT64_C(1) << 34U),
506
        static_cast<int64_t>(UINT64_C(1) << 34U),
507
        -static_cast<int64_t>(UINT64_C(1) << 41U) + 1,
508
        static_cast<int64_t>(UINT64_C(1) << 41U) - 1,
509
        // 7 bytes
510
        -static_cast<int64_t>(UINT64_C(1) << 41U),
511
        static_cast<int64_t>(UINT64_C(1) << 41U),
512
        -static_cast<int64_t>(UINT64_C(1) << 48U) + 1,
513
        static_cast<int64_t>(UINT64_C(1) << 48U) - 1,
514
        // 8 bytes
515
        -static_cast<int64_t>(UINT64_C(1) << 48U),
516
        static_cast<int64_t>(UINT64_C(1) << 48U),
517
        -static_cast<int64_t>(UINT64_C(1) << 55U) + 1,
518
        static_cast<int64_t>(UINT64_C(1) << 55U) - 1,
519
        // 9 bytes
520
        -static_cast<int64_t>(UINT64_C(1) << 55U),
521
        static_cast<int64_t>(UINT64_C(1) << 55U),
522
        INT64_MIN + 1,
523
        INT64_MAX,
524
525
        // special case - stored as -0 (1 byte)
526
        INT64_MIN,
527
1
    };
528
529
2
    std::function<void (BitStreamWriter&, int64_t)> writerFunc = &BitStreamWriter::writeVarInt;
530
2
    std::function<int64_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarInt;
531
532

1
    testImpl(values, writerFunc, readerFunc, 63);
533
1
}
534
535


802
TEST_F(BitStreamTest, readVarUInt)
536
{
537
    const std::array<uint64_t, 19> values =
538
    {
539
        // 1 byte
540
        0,
541
        1,
542
        (UINT64_C(1) << 7U) - 1,
543
        // 2 bytes
544
        (UINT64_C(1) << 7U),
545
        (UINT64_C(1) << 14U) - 1,
546
        // 3 bytes
547
        (UINT64_C(1) << 14U),
548
        (UINT64_C(1) << 21U) - 1,
549
        // 4 bytes
550
        (UINT64_C(1) << 21U),
551
        (UINT64_C(1) << 28U) - 1,
552
        // 5 bytes
553
        (UINT64_C(1) << 28U),
554
        (UINT64_C(1) << 35U) - 1,
555
        // 6 bytes
556
        (UINT64_C(1) << 35U),
557
        (UINT64_C(1) << 42U) - 1,
558
        // 7 bytes
559
        (UINT64_C(1) << 42U),
560
        (UINT64_C(1) << 49U) - 1,
561
        // 8 bytes
562
        (UINT64_C(1) << 49U),
563
        (UINT64_C(1) << 56U) - 1,
564
        // 9 bytes
565
        (UINT64_C(1) << 56U),
566
        UINT64_MAX
567
1
    };
568
569
2
    std::function<void (BitStreamWriter&, uint64_t)> writerFunc = &BitStreamWriter::writeVarUInt;
570
2
    std::function<uint64_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarUInt;
571
572

1
    testImpl(values, writerFunc, readerFunc, 63);
573
1
}
574
575


802
TEST_F(BitStreamTest, readVarSize)
576
{
577
    const std::array<uint32_t, 13> values =
578
    {
579
        0,
580
        65536,
581
        131072,
582
583
        (1U << (0U)),
584
        (1U << (7U)) - 1,
585
586
        (1U << (7U)),
587
        (1U << (7U+7)) - 1,
588
589
        (1U << (7U+7)),
590
        (1U << (7U+7+7)) - 1,
591
592
        (1U << (7U+7+7)),
593
        (1U << (7U+7+7+7)) - 1,
594
595
        (1U << (7U+7+7+7)),
596
        (1U << (7U+7+7+7+3)) - 1,
597
1
    };
598
599
2
    std::function<void (BitStreamWriter&, uint32_t)> writerFunc = &BitStreamWriter::writeVarSize;
600
2
    std::function<uint32_t(BitStreamReader&)> readerFunc = &BitStreamReader::readVarSize;
601
602

1
    testImpl(values, writerFunc, readerFunc, 31);
603
1
}
604
605


802
TEST_F(BitStreamTest, readFloat16)
606
{
607
1
    const std::array<float, 6> values = { 2.0, -2.0, 0.6171875, 0.875, 9.875, 42.5 };
608
609
2
    std::function<void (BitStreamWriter&, float)> writerFunc = &BitStreamWriter::writeFloat16;
610
2
    std::function<float(BitStreamReader&)> readerFunc = &BitStreamReader::readFloat16;
611
612

1
    testImpl(values, writerFunc, readerFunc, 15);
613
1
}
614
615


802
TEST_F(BitStreamTest, readFloat32)
616
{
617
1
    const std::array<float, 6> values = { 2.0, -2.0, 0.6171875, 0.875, 9.875, 42.5 };
618
619
2
    std::function<void (BitStreamWriter&, float)> writerFunc = &BitStreamWriter::writeFloat32;
620
2
    std::function<float(BitStreamReader&)> readerFunc = &BitStreamReader::readFloat32;
621
622

1
    testImpl(values, writerFunc, readerFunc, 31);
623
1
}
624
625


802
TEST_F(BitStreamTest, readFloat64)
626
{
627
1
    const std::array<double, 6> values = { 2.0, -2.0, 0.6171875, 0.875, 9.875, 42.5 };
628
629
2
    std::function<void (BitStreamWriter&, double)> writerFunc = &BitStreamWriter::writeFloat64;
630
2
    std::function<double(BitStreamReader&)> readerFunc = &BitStreamReader::readFloat64;
631
632

1
    testImpl(values, writerFunc, readerFunc, 61);
633
1
}
634
635


802
TEST_F(BitStreamTest, readString)
636
{
637
    const std::array<std::string, 3> values =
638
    {
639
        "Hello World",
640
        "\n\t%^@(*aAzZ01234569$%^!?<>[]](){}-=+~:;/|\\\"\'Hello World2\0nonWrittenPart",
641
        "Price: \xE2\x82\xAC 3 what's this? -> \xC2\xA2" /* '€' '¢' */
642


2
    };
643
644
2
    std::function<void (BitStreamWriter&, const std::string&)> writerFunc = &BitStreamWriter::writeString;
645
    std::function<std::string (BitStreamReader&)> readerFunc =
646
3
            std::bind(&BitStreamReader::readString<std::allocator<char>>,
647
3
                    std::placeholders::_1, std::allocator<char>());
648
649

1
    testImpl(values, writerFunc, readerFunc, 7);
650
1
}
651
652


802
TEST_F(BitStreamTest, readBool)
653
{
654
1
    const std::array<bool, 2> values = {true, false};
655
656
2
    std::function<void (BitStreamWriter&, bool)> writerFunc = &BitStreamWriter::writeBool;
657
2
    std::function<bool(BitStreamReader&)> readerFunc = &BitStreamReader::readBool;
658
659

1
    testImpl(values, writerFunc, readerFunc, 1);
660
1
}
661
662


802
TEST_F(BitStreamTest, readBitBuffer)
663
{
664
    const std::array<BitBuffer, 2> values =
665
    {
666
        BitBuffer(std::vector<uint8_t>({0xAB, 0xE0}), 11),
667
        BitBuffer(std::vector<uint8_t>({0xAB, 0xCD, 0xFE}), 23)
668



2
    };
669
670
    std::function<void (BitStreamWriter&, const BitBuffer&)> writerFunc =
671
2
                &BitStreamWriter::writeBitBuffer<std::allocator<uint8_t>>;
672
    std::function<BitBuffer (BitStreamReader&)> readerFunc =
673
3
            std::bind(&BitStreamReader::readBitBuffer<
674
3
                    std::allocator<uint8_t>>, std::placeholders::_1, std::allocator<uint8_t>());
675
676

1
    testImpl(values, writerFunc, readerFunc, 7);
677
1
}
678
679


802
TEST_F(BitStreamTest, readBytes)
680
{
681
    const std::array<vector<uint8_t>, 2> values =
682
    {
683
        vector<uint8_t>{{0, 255}},
684
        vector<uint8_t>{{1, 127, 128, 254}},
685


2
    };
686
687
    std::function<void (BitStreamWriter&, const vector<uint8_t>&)> writerFunc =
688
2
                &BitStreamWriter::writeBytes;
689
    std::function<vector<uint8_t> (BitStreamReader&)> readerFunc =
690
3
            std::bind(&BitStreamReader::readBytes<
691
3
                    std::allocator<uint8_t>>, std::placeholders::_1, std::allocator<uint8_t>());
692
693

1
    testImpl(values, writerFunc, readerFunc, 7);
694
1
}
695
696


802
TEST_F(BitStreamTest, setBitPosition)
697
{
698
1
    testSetBitPosition(m_externalWriter);
699
1
    testSetBitPosition(m_dummyWriter);
700
1
}
701
702


802
TEST_F(BitStreamTest, alignTo)
703
{
704
1
    testAlignTo(m_externalWriter);
705
1
    testAlignTo(m_dummyWriter);
706
1
}
707
708

2394
} // namespace zserio