Coverage Report

Created: 2026-10-19 14:15

test/zserio/ArrayTest.cpp
Line
Count
Source
1
#include <array>
2
#include <limits>
3
#include <string>
4
#include <vector>
5
6
#include "gtest/gtest.h"
7
#include "test_object/std_allocator/ArrayBitmask.h"
8
#include "test_object/std_allocator/ArrayEnum.h"
9
#include "test_object/std_allocator/ArrayObject.h"
10
#include "test_object/std_allocator/ArrayParamObject.h"
11
#include "zserio/Array.h"
12
#include "zserio/ArrayTraits.h"
13
#include "zserio/BitBuffer.h"
14
#include "zserio/CppRuntimeException.h"
15
#include "zserio/Enums.h"
16
17
namespace zserio
18
{
19
20
using ArrayEnum = test_object::std_allocator::ArrayEnum;
21
using ArrayBitmask = test_object::std_allocator::ArrayBitmask;
22
using ArrayObject = test_object::std_allocator::ArrayObject;
23
using ArrayParamObject = test_object::std_allocator::ArrayParamObject;
24
25
namespace
26
{
27
28
class ArrayTestOwner
29
{};
30
31
class ArrayTestOwnerWithBitSize
32
{
33
public:
34
    explicit ArrayTestOwnerWithBitSize(uint8_t bitSize) :
35
            m_bitSize(bitSize)
36
7
    {}
37
38
    uint8_t getBitSize() const
39
3.87k
    {
40
3.87k
        return m_bitSize;
41
3.87k
    }
42
43
private:
44
    uint8_t m_bitSize;
45
};
46
47
template <typename ARRAY, typename OWNER_TYPE>
48
size_t arrayBitSizeOf(const ARRAY& array, const OWNER_TYPE& owner, size_t bitPosition)
49
304
{
50
304
    return array.bitSizeOf(owner, bitPosition);
51
304
}
52
53
template <typename ARRAY>
54
size_t arrayBitSizeOf(const ARRAY& array, const detail::DummyArrayOwner&, size_t bitPosition)
55
2.11k
{
56
2.11k
    return array.bitSizeOf(bitPosition);
57
2.11k
}
58
59
template <typename ARRAY, typename OWNER_TYPE>
60
size_t arrayBitSizeOfPacked(const ARRAY& array, const OWNER_TYPE& owner, size_t bitPosition)
61
96
{
62
96
    return array.bitSizeOfPacked(owner, bitPosition);
63
96
}
64
65
template <typename ARRAY>
66
size_t arrayBitSizeOfPacked(const ARRAY& array, const detail::DummyArrayOwner&, size_t bitPosition)
67
800
{
68
800
    return array.bitSizeOfPacked(bitPosition);
69
800
}
70
71
template <typename ARRAY, typename OWNER_TYPE>
72
size_t arrayInitializeOffsets(ARRAY& array, OWNER_TYPE& owner, size_t bitPosition)
73
304
{
74
304
    return array.initializeOffsets(owner, bitPosition);
75
304
}
76
77
template <typename ARRAY>
78
size_t arrayInitializeOffsets(ARRAY& array, detail::DummyArrayOwner&, size_t bitPosition)
79
2.11k
{
80
2.11k
    return array.initializeOffsets(bitPosition);
81
2.11k
}
82
83
template <typename ARRAY, typename OWNER_TYPE>
84
size_t arrayInitializeOffsetsPacked(ARRAY& array, OWNER_TYPE& owner, size_t bitPosition)
85
96
{
86
96
    return array.initializeOffsetsPacked(owner, bitPosition);
87
96
}
88
89
template <typename ARRAY>
90
size_t arrayInitializeOffsetsPacked(ARRAY& array, detail::DummyArrayOwner&, size_t bitPosition)
91
800
{
92
800
    return array.initializeOffsetsPacked(bitPosition);
93
800
}
94
95
template <typename ARRAY, typename OWNER_TYPE>
96
void arrayRead(ARRAY& array, OWNER_TYPE& owner, BitStreamReader& in, size_t arrayLength = 0)
97
340
{
98
340
    array.read(owner, in, arrayLength);
99
340
}
100
101
template <typename ARRAY>
102
void arrayRead(ARRAY& array, detail::DummyArrayOwner&, BitStreamReader& in, size_t arrayLength = 0)
103
2.36k
{
104
2.36k
    array.read(in, arrayLength);
105
2.36k
}
106
107
template <typename ARRAY, typename OWNER_TYPE>
108
void arrayReadPacked(ARRAY& array, OWNER_TYPE& owner, BitStreamReader& in, size_t arrayLength = 0)
109
108
{
110
108
    array.readPacked(owner, in, arrayLength);
111
108
}
112
113
template <typename ARRAY>
114
void arrayReadPacked(ARRAY& array, detail::DummyArrayOwner&, BitStreamReader& in, size_t arrayLength = 0)
115
900
{
116
900
    array.readPacked(in, arrayLength);
117
900
}
118
119
template <typename ARRAY, typename OWNER_TYPE>
120
void arrayWrite(const ARRAY& array, const OWNER_TYPE& owner, BitStreamWriter& out)
121
304
{
122
304
    array.write(owner, out);
123
304
}
124
125
template <typename ARRAY>
126
void arrayWrite(const ARRAY& array, const detail::DummyArrayOwner&, BitStreamWriter& out)
127
2.11k
{
128
2.11k
    array.write(out);
129
2.11k
}
130
131
template <typename ARRAY, typename OWNER_TYPE>
132
void arrayWritePacked(const ARRAY& array, const OWNER_TYPE& owner, BitStreamWriter& out)
133
96
{
134
96
    array.writePacked(owner, out);
135
96
}
136
137
template <typename ARRAY>
138
void arrayWritePacked(const ARRAY& array, const detail::DummyArrayOwner&, BitStreamWriter& out)
139
800
{
140
800
    array.writePacked(out);
141
800
}
142
143
class ElementBitSizeWithOwner
144
{
145
public:
146
    using OwnerType = ArrayTestOwnerWithBitSize;
147
148
    static uint8_t get(const ArrayTestOwnerWithBitSize& owner)
149
3.87k
    {
150
3.87k
        return owner.getBitSize();
151
3.87k
    }
152
};
153
154
template <uint8_t BIT_SIZE>
155
class ElementBitSizeWithoutOwner
156
{
157
public:
158
    static uint8_t get()
159
3.04k
    {
160
3.04k
        return BIT_SIZE;
161
3.04k
    }
162
};
163
164
class ArrayObjectArrayExpressions
165
{
166
public:
167
    using OwnerType = ArrayTestOwner;
168
169
    static void initializeOffset(ArrayTestOwner&, size_t, size_t)
170
96
    {}
171
172
    static void checkOffset(const ArrayTestOwner&, size_t, size_t)
173
332
    {}
174
};
175
176
class ArrayObjectElementFactory
177
{
178
public:
179
    using OwnerType = ArrayTestOwner;
180
    using allocator_type = std::allocator<uint8_t>;
181
182
    static void create(OwnerType&, std::vector<ArrayObject>& array, BitStreamReader& in, size_t)
183
300
    {
184
300
        array.emplace_back(in, array.get_allocator());
185
300
    }
186
187
    static void create(OwnerType&, std::vector<ArrayObject>& array,
188
            ArrayObject::ZserioPackingContext& contextNode, BitStreamReader& in, size_t)
189
172
    {
190
172
        array.emplace_back(contextNode, in, array.get_allocator());
191
172
    }
192
};
193
194
struct ArrayParamObjectOwner
195
{
196
    ArrayObject arrayObject;
197
};
198
199
class ArrayParamObjectArrayExpressions
200
{
201
public:
202
    using OwnerType = ArrayParamObjectOwner;
203
204
    static void initializeOffset(ArrayParamObjectOwner&, size_t, size_t)
205
96
    {}
206
207
    static void checkOffset(const ArrayParamObjectOwner&, size_t, size_t)
208
243
    {}
209
210
    static void initializeElement(ArrayParamObjectOwner& owner, ArrayParamObject& element, size_t)
211
192
    {
212
192
        element.initialize(owner.arrayObject);
213
192
    }
214
215
    static bool checkInitialization(const ArrayParamObjectOwner& owner, const ArrayParamObject& element)
216
192
    {
217
192
        return owner.arrayObject == element.getParam();
218
192
    }
219
};
220
221
class ArrayParamObjectElementFactory
222
{
223
public:
224
    using OwnerType = ArrayParamObjectOwner;
225
226
    static void create(ArrayParamObjectOwner& owner, ::zserio::vector<ArrayParamObject>& array,
227
            ::zserio::BitStreamReader& in, size_t)
228
148
    {
229
148
        array.emplace_back(in, owner.arrayObject, array.get_allocator());
230
148
    }
231
232
    static void create(ArrayParamObjectOwner& owner, ::zserio::vector<ArrayParamObject>& array,
233
            ArrayParamObject::ZserioPackingContext& context, ::zserio::BitStreamReader& in, size_t)
234
146
    {
235
146
        array.emplace_back(context, in, owner.arrayObject, array.get_allocator());
236
146
    }
237
};
238
239
} // namespace
240
241
class ArrayTest : public ::testing::Test
242
{
243
public:
244
    ArrayTest() :
245
            m_byteBuffer()
246
61
    {}
247
248
protected:
249
    template <typename ARRAY_TRAITS, typename RAW_ARRAY,
250
            typename ARRAY_EXPRESSIONS = detail::DummyArrayExpressions,
251
            typename OWNER_TYPE = typename detail::array_owner_type<ARRAY_TRAITS, ARRAY_EXPRESSIONS>::type,
252
            typename std::enable_if<!std::is_integral<OWNER_TYPE>::value, int>::type = 0>
253
    void testArray(const RAW_ARRAY& rawArray, size_t elementBitSize, OWNER_TYPE owner = OWNER_TYPE())
254
54
    {
255
54
        const size_t arraySize = rawArray.size();
256
54
        const size_t unalignedBitSize = elementBitSize * arraySize;
257
54
        const size_t alignedBitSize =
258
54
                (arraySize > 0) ? 
alignTo(8, elementBitSize) * (arraySize - 1) + elementBitSize38
:
016
;
259
54
        testArray<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(
260
54
                rawArray, unalignedBitSize, alignedBitSize, owner);
261
54
    }
262
263
    template <typename ARRAY_TRAITS, typename RAW_ARRAY,
264
            typename ARRAY_EXPRESSIONS = detail::DummyArrayExpressions,
265
            typename OWNER_TYPE = typename detail::array_owner_type<ARRAY_TRAITS, ARRAY_EXPRESSIONS>::type>
266
    void testArray(const RAW_ARRAY& rawArray, size_t unalignedBitSize, size_t alignedBitSize,
267
            OWNER_TYPE owner = OWNER_TYPE())
268
72
    {
269
72
        testArrayNormal<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(rawArray, unalignedBitSize, owner);
270
72
        testArrayAuto<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(
271
72
                rawArray, AUTO_LENGTH_BIT_SIZE + unalignedBitSize, owner);
272
72
        testArrayAligned<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(rawArray, alignedBitSize, owner);
273
72
        testArrayAlignedAuto<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(
274
72
                rawArray, AUTO_LENGTH_BIT_SIZE + alignedBitSize, owner);
275
72
        testArrayImplicit<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(rawArray, unalignedBitSize, owner);
276
72
    }
277
278
    template <typename ARRAY_TRAITS, typename RAW_ARRAY,
279
            typename ARRAY_EXPRESSIONS = detail::DummyArrayExpressions,
280
            typename OWNER_TYPE = typename detail::array_owner_type<ARRAY_TRAITS, ARRAY_EXPRESSIONS>::type>
281
    void testPackedArray(const RAW_ARRAY& rawArray, OWNER_TYPE owner = OWNER_TYPE())
282
18
    {
283
18
        testPackedArray<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(
284
18
                rawArray, UNKNOWN_BIT_SIZE, UNKNOWN_BIT_SIZE, owner);
285
18
    }
286
287
    template <typename ARRAY_TRAITS, typename RAW_ARRAY,
288
            typename ARRAY_EXPRESSIONS = detail::DummyArrayExpressions,
289
            typename OWNER_TYPE = typename detail::array_owner_type<ARRAY_TRAITS, ARRAY_EXPRESSIONS>::type>
290
    void testPackedArray(const RAW_ARRAY& rawArray, size_t unalignedBitSize, size_t alignedBitSize,
291
            OWNER_TYPE owner = OWNER_TYPE())
292
28
    {
293
28
        testPackedArrayNormal<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(rawArray, unalignedBitSize, owner);
294
28
        testPackedArrayAuto<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(rawArray,
295
28
                (unalignedBitSize != UNKNOWN_BIT_SIZE)
296
28
                        ? 
AUTO_LENGTH_BIT_SIZE + unalignedBitSize10
297
28
                        : 
UNKNOWN_BIT_SIZE18
,
298
28
                owner);
299
28
        testPackedArrayAligned<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(rawArray, alignedBitSize, owner);
300
28
        testPackedArrayAlignedAuto<ARRAY_TRAITS, RAW_ARRAY, ARRAY_EXPRESSIONS>(rawArray,
301
28
                (alignedBitSize != UNKNOWN_BIT_SIZE) ? 
AUTO_LENGTH_BIT_SIZE + alignedBitSize10
:
UNKNOWN_BIT_SIZE18
,
302
28
                owner);
303
28
    }
304
305
    size_t calcPackedBitSize(size_t elementBitSize, size_t arraySize, size_t maxDeltaBitSize)
306
1
    {
307
1
        return PACKING_DESCRIPTOR_BITSIZE + elementBitSize + (arraySize - 1) * (maxDeltaBitSize + 1);
308
1
    }
309
310
    size_t calcAlignedPackedBitSize(size_t elementBitSize, size_t arraySize, size_t maxDeltaBitSize)
311
1
    {
312
1
        const size_t firstElementWithDescriptorBitSize = PACKING_DESCRIPTOR_BITSIZE + elementBitSize;
313
1
        const size_t alignedFirstElementWithDescriptorBitSize = (firstElementWithDescriptorBitSize + 7) / 8 * 8;
314
1
        const size_t alignedMaxDeltaBitSize = (maxDeltaBitSize + 1 + 7) / 8 * 8;
315
316
1
        return alignedFirstElementWithDescriptorBitSize + (arraySize - 2) * alignedMaxDeltaBitSize +
317
1
                (maxDeltaBitSize + 1);
318
1
    }
319
320
    static const size_t PACKING_DESCRIPTOR_BITSIZE = 1 + 6;
321
322
private:
323
    template <typename ARRAY, typename ARRAY_EXPRESSIONS = typename ARRAY::ArrayExpressions,
324
            typename std::enable_if<has_initialize_element<ARRAY_EXPRESSIONS>::value, int>::type = 0>
325
    void initializeElements(ARRAY& array, typename ARRAY::OwnerType& owner)
326
64
    {
327
64
        array.initializeElements(owner);
328
64
        for (const auto& element : array.getRawArray())
329
192
        {
330
192
            ASSERT_TRUE(element.isInitialized());
331
192
            ASSERT_TRUE(ARRAY_EXPRESSIONS::checkInitialization(owner, element));
332
192
        }
333
64
    }
334
335
    template <typename ARRAY, typename ARRAY_EXPRESSIONS = typename ARRAY::ArrayExpressions,
336
            typename std::enable_if<!has_initialize_element<ARRAY_EXPRESSIONS>::value, int>::type = 0>
337
    void initializeElements(ARRAY&, typename ARRAY::OwnerType&)
338
3.24k
    {}
339
340
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
341
    void testArrayNormal(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
342
72
    {
343
72
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::NORMAL, ARRAY_EXPRESSIONS>;
344
345
72
        testArrayInvalidSize<ArrayT>(owner);
346
347
640
        for (uint8_t i = 0; i < 8; 
++i568
)
348
569
        {
349
569
            ArrayT array(rawArray);
350
351
569
            initializeElements(array, owner);
352
353
569
            const size_t bitSize = arrayBitSizeOf(array, owner, i);
354
569
            ASSERT_EQ(expectedBitSize, bitSize);
355
569
            ASSERT_EQ(i + bitSize, arrayInitializeOffsets(array, owner, i));
356
357
569
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
358
569
            if (i > 0)
359
497
            {
360
497
                writer.writeBits(0, i);
361
497
            }
362
569
            arrayWrite(array, owner, writer);
363
569
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
364
365
569
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
366
569
            if (i > 0)
367
497
            {
368
497
                ASSERT_EQ(0, reader.readBits(i));
369
497
            }
370
569
            ArrayT readArray;
371
569
            arrayRead(readArray, owner, reader, rawArray.size());
372
569
            ASSERT_EQ(array, readArray);
373
374
568
            testArrayCopiesAndMoves(array);
375
568
        }
376
72
    }
377
378
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
379
    void testArrayAuto(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
380
71
    {
381
71
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::AUTO, ARRAY_EXPRESSIONS>;
382
383
71
        testArrayInvalidSize<ArrayT>(owner);
384
385
639
        for (uint8_t i = 0; i < 8; 
++i568
)
386
568
        {
387
568
            ArrayT array(rawArray);
388
389
568
            initializeElements(array, owner);
390
391
568
            const size_t bitSize = arrayBitSizeOf(array, owner, i);
392
568
            ASSERT_EQ(expectedBitSize, bitSize);
393
568
            ASSERT_EQ(i + bitSize, arrayInitializeOffsets(array, owner, i));
394
395
568
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
396
568
            if (i > 0)
397
497
            {
398
497
                writer.writeBits(0, i);
399
497
            }
400
568
            arrayWrite(array, owner, writer);
401
568
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
402
403
568
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
404
568
            if (i > 0)
405
497
            {
406
497
                ASSERT_EQ(0, reader.readBits(i));
407
497
            }
408
568
            ArrayT readArray;
409
568
            arrayRead(readArray, owner, reader);
410
568
            ASSERT_EQ(array, readArray);
411
412
568
            testArrayCopiesAndMoves(array);
413
568
        }
414
71
    }
415
416
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
417
    void testArrayAligned(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
418
71
    {
419
71
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED, ARRAY_EXPRESSIONS>;
420
421
71
        testArrayInvalidSize<ArrayT>(owner);
422
423
639
        for (uint8_t i = 0; i < 8; 
++i568
)
424
568
        {
425
568
            ArrayT array(rawArray);
426
427
568
            initializeElements(array, owner);
428
429
568
            const size_t bitSize = arrayBitSizeOf(array, owner, i);
430
568
            if (expectedBitSize == 0)
431
200
            {
432
200
                ASSERT_EQ(expectedBitSize, bitSize);
433
200
            }
434
368
            else
435
368
            {
436
368
                ASSERT_EQ(alignTo(8, i) - i + expectedBitSize, bitSize);
437
368
            }
438
568
            ASSERT_EQ(i + bitSize, arrayInitializeOffsets(array, owner, i));
439
440
568
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
441
568
            if (i > 0)
442
497
            {
443
497
                writer.writeBits(0, i);
444
497
            }
445
568
            arrayWrite(array, owner, writer);
446
568
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
447
448
568
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
449
568
            if (i > 0)
450
497
            {
451
497
                ASSERT_EQ(0, reader.readBits(i));
452
497
            }
453
568
            ArrayT readArray;
454
568
            arrayRead(readArray, owner, reader, rawArray.size());
455
568
            ASSERT_EQ(array, readArray);
456
457
568
            testArrayCopiesAndMoves(array);
458
568
        }
459
71
    }
460
461
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
462
    void testArrayAlignedAuto(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
463
71
    {
464
71
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED_AUTO, ARRAY_EXPRESSIONS>;
465
466
71
        testArrayInvalidSize<ArrayT>(owner);
467
468
639
        for (uint8_t i = 0; i < 8; 
++i568
)
469
568
        {
470
568
            ArrayT array(rawArray);
471
472
568
            initializeElements(array, owner);
473
474
568
            const size_t bitSize = arrayBitSizeOf(array, owner, i);
475
476
568
            if (expectedBitSize == AUTO_LENGTH_BIT_SIZE)
477
200
            {
478
200
                ASSERT_EQ(expectedBitSize, bitSize);
479
200
            }
480
368
            else
481
368
            {
482
736
                ASSERT_EQ(alignTo(8, i) - i + expectedBitSize, bitSize) << expectedBitSize;
483
368
            }
484
568
            ASSERT_EQ(i + bitSize, arrayInitializeOffsets(array, owner, i));
485
486
568
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
487
568
            if (i > 0)
488
497
            {
489
497
                writer.writeBits(0, i);
490
497
            }
491
568
            arrayWrite(array, owner, writer);
492
568
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
493
494
568
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
495
568
            if (i > 0)
496
497
            {
497
497
                ASSERT_EQ(0, reader.readBits(i));
498
497
            }
499
568
            ArrayT readArray;
500
568
            arrayRead(readArray, owner, reader);
501
568
            ASSERT_EQ(array, readArray);
502
503
568
            testArrayCopiesAndMoves(array);
504
568
        }
505
71
    }
506
507
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE,
508
            typename std::enable_if<ARRAY_TRAITS::IS_BITSIZEOF_CONSTANT, int>::type = 0>
509
    void testArrayImplicit(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
510
40
    {
511
40
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::IMPLICIT, ARRAY_EXPRESSIONS>;
512
513
40
        if (detail::arrayTraitsConstBitSizeOf<ARRAY_TRAITS>(owner) % 8 != 0)
514
22
        {
515
22
            return; // implicit array allowed for types with constant bitsize rounded to bytes
516
22
        }
517
518
162
        
for (uint8_t i = 0; 18
i < 8;
++i144
)
519
144
        {
520
144
            ArrayT array(rawArray);
521
522
144
            initializeElements(array, owner);
523
524
144
            const size_t bitSize = arrayBitSizeOf(array, owner, i);
525
144
            ASSERT_EQ(expectedBitSize, bitSize);
526
144
            ASSERT_EQ(i + bitSize, arrayInitializeOffsets(array, owner, i));
527
528
144
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
529
144
            if (i > 0)
530
126
            {
531
126
                writer.writeBits(0, i);
532
126
            }
533
144
            arrayWrite(array, owner, writer);
534
144
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
535
536
144
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
537
144
            if (i > 0)
538
126
            {
539
126
                ASSERT_EQ(0, reader.readBits(i));
540
126
            }
541
144
            ArrayT readArray;
542
144
            arrayRead(readArray, owner, reader);
543
144
            ASSERT_EQ(array, readArray);
544
545
144
            testArrayCopiesAndMoves(array);
546
144
        }
547
18
    }
548
549
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE,
550
            typename std::enable_if<!ARRAY_TRAITS::IS_BITSIZEOF_CONSTANT, int>::type = 0>
551
    void testArrayImplicit(const RAW_ARRAY&, size_t, OWNER_TYPE&)
552
31
    {
553
        // implicit array not allowed for types with non-constant bitsize, so skip the test
554
31
    }
555
556
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
557
    void testPackedArrayNormal(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
558
28
    {
559
28
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::NORMAL, ARRAY_EXPRESSIONS>;
560
561
28
        testPackedArrayInvalidSize<ArrayT>(owner);
562
563
252
        for (uint8_t i = 0; i < 8; 
++i224
)
564
224
        {
565
224
            ArrayT array(rawArray);
566
567
224
            initializeElements(array, owner);
568
569
224
            const size_t bitSize = arrayBitSizeOfPacked(array, owner, i);
570
224
            if (expectedBitSize != UNKNOWN_BIT_SIZE)
571
80
            {
572
80
                ASSERT_EQ(expectedBitSize, bitSize);
573
80
            }
574
224
            ASSERT_EQ(i + bitSize, arrayInitializeOffsetsPacked(array, owner, i));
575
576
224
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
577
224
            if (i > 0)
578
196
            {
579
196
                writer.writeBits(0, i);
580
196
            }
581
224
            arrayWritePacked(array, owner, writer);
582
224
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
583
584
224
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
585
224
            if (i > 0)
586
196
            {
587
196
                ASSERT_EQ(0, reader.readBits(i));
588
196
            }
589
224
            ArrayT readArray;
590
224
            arrayReadPacked(readArray, owner, reader, rawArray.size());
591
224
            ASSERT_EQ(array, readArray);
592
593
224
            testArrayCopiesAndMoves(array);
594
224
        }
595
28
    }
596
597
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
598
    void testPackedArrayAuto(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
599
28
    {
600
28
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::AUTO, ARRAY_EXPRESSIONS>;
601
602
28
        testPackedArrayInvalidSize<ArrayT>(owner);
603
604
252
        for (uint8_t i = 0; i < 8; 
++i224
)
605
224
        {
606
224
            ArrayT array(rawArray);
607
608
224
            initializeElements(array, owner);
609
610
224
            const size_t bitSize = arrayBitSizeOfPacked(array, owner, i);
611
224
            if (expectedBitSize != UNKNOWN_BIT_SIZE)
612
80
            {
613
80
                ASSERT_EQ(expectedBitSize, bitSize);
614
80
            }
615
224
            ASSERT_EQ(i + bitSize, arrayInitializeOffsetsPacked(array, owner, i));
616
617
224
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
618
224
            if (i > 0)
619
196
            {
620
196
                writer.writeBits(0, i);
621
196
            }
622
224
            arrayWritePacked(array, owner, writer);
623
224
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
624
625
224
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
626
224
            if (i > 0)
627
196
            {
628
196
                ASSERT_EQ(0, reader.readBits(i));
629
196
            }
630
224
            ArrayT readArray;
631
224
            arrayReadPacked(readArray, owner, reader);
632
224
            ASSERT_EQ(array, readArray);
633
634
224
            testArrayCopiesAndMoves(array);
635
224
        }
636
28
    }
637
638
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
639
    void testPackedArrayAligned(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
640
28
    {
641
28
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED, ARRAY_EXPRESSIONS>;
642
643
28
        testPackedArrayInvalidSize<ArrayT>(owner);
644
645
252
        for (uint8_t i = 0; i < 8; 
++i224
)
646
224
        {
647
224
            ArrayT array(rawArray);
648
649
224
            initializeElements(array, owner);
650
651
224
            const size_t bitSize = arrayBitSizeOfPacked(array, owner, i);
652
224
            if (expectedBitSize != UNKNOWN_BIT_SIZE && 
i == 080
)
653
10
            {
654
10
                ASSERT_EQ(expectedBitSize, bitSize);
655
10
            }
656
224
            ASSERT_EQ(i + bitSize, arrayInitializeOffsetsPacked(array, owner, i));
657
658
224
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
659
224
            if (i > 0)
660
196
            {
661
196
                writer.writeBits(0, i);
662
196
            }
663
224
            arrayWritePacked(array, owner, writer);
664
224
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
665
666
224
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
667
224
            if (i > 0)
668
196
            {
669
196
                ASSERT_EQ(0, reader.readBits(i));
670
196
            }
671
224
            ArrayT readArray;
672
224
            arrayReadPacked(readArray, owner, reader, rawArray.size());
673
224
            ASSERT_EQ(array, readArray);
674
675
224
            testArrayCopiesAndMoves(array);
676
224
        }
677
28
    }
678
679
    template <typename ARRAY_TRAITS, typename RAW_ARRAY, typename ARRAY_EXPRESSIONS, typename OWNER_TYPE>
680
    void testPackedArrayAlignedAuto(const RAW_ARRAY& rawArray, size_t expectedBitSize, OWNER_TYPE& owner)
681
28
    {
682
28
        using ArrayT = Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED_AUTO, ARRAY_EXPRESSIONS>;
683
684
28
        testPackedArrayInvalidSize<ArrayT>(owner);
685
686
252
        for (uint8_t i = 0; i < 8; 
++i224
)
687
224
        {
688
224
            ArrayT array(rawArray);
689
690
224
            initializeElements(array, owner);
691
692
224
            const size_t bitSize = arrayBitSizeOfPacked(array, owner, i);
693
224
            if (expectedBitSize != UNKNOWN_BIT_SIZE && 
i == 080
)
694
10
            {
695
10
                ASSERT_EQ(expectedBitSize, bitSize);
696
10
            }
697
224
            ASSERT_EQ(i + bitSize, arrayInitializeOffsetsPacked(array, owner, i));
698
699
224
            BitStreamWriter writer(m_byteBuffer.data(), m_byteBuffer.size());
700
224
            if (i > 0)
701
196
            {
702
196
                writer.writeBits(0, i);
703
196
            }
704
224
            arrayWritePacked(array, owner, writer);
705
224
            ASSERT_EQ(i + bitSize, writer.getBitPosition());
706
707
224
            BitStreamReader reader(m_byteBuffer.data(), writer.getBitPosition(), BitsTag());
708
224
            if (i > 0)
709
196
            {
710
196
                ASSERT_EQ(0, reader.readBits(i));
711
196
            }
712
224
            ArrayT readArray;
713
224
            arrayReadPacked(readArray, owner, reader);
714
224
            ASSERT_EQ(array, readArray);
715
716
224
            testArrayCopiesAndMoves(array);
717
224
        }
718
28
    }
719
720
    template <typename ARRAY>
721
    void testArrayCopiesAndMoves(const ARRAY& array)
722
3.31k
    {
723
3.31k
        ARRAY arrayCopy(array);
724
3.31k
        ASSERT_EQ(array, arrayCopy);
725
3.31k
        ASSERT_EQ(array.getRawArray(), arrayCopy.getRawArray());
726
3.31k
        ASSERT_FALSE(array < arrayCopy);
727
3.31k
        ASSERT_FALSE(arrayCopy < array);
728
729
3.31k
        ARRAY arrayCopyAssigned;
730
3.31k
        arrayCopyAssigned = array;
731
3.31k
        ASSERT_EQ(array, arrayCopyAssigned);
732
3.31k
        ASSERT_EQ(array.getRawArray(), arrayCopyAssigned.getRawArray());
733
734
3.31k
        const ARRAY arrayMoved = std::move(arrayCopy);
735
3.31k
        ASSERT_EQ(array, arrayMoved);
736
3.31k
        ASSERT_EQ(array.getRawArray(), arrayMoved.getRawArray());
737
738
3.31k
        ARRAY arrayMoveAssigned;
739
3.31k
        arrayMoveAssigned = std::move(arrayCopyAssigned);
740
3.31k
        ASSERT_EQ(array, arrayMoveAssigned);
741
3.31k
        ASSERT_EQ(array.getRawArray(), arrayMoveAssigned.getRawArray());
742
743
3.31k
        ARRAY arrayCopyWithPropagateAllocator(PropagateAllocator, array, std::allocator<uint8_t>());
744
3.31k
        ASSERT_EQ(array, arrayCopyWithPropagateAllocator);
745
3.31k
        ASSERT_EQ(array.getRawArray(), arrayCopyWithPropagateAllocator.getRawArray());
746
747
3.31k
        testArrayNoInitCopiesAndMoves(array);
748
3.31k
    }
749
750
    template <typename ARRAY, typename OWNER>
751
    void testArrayInvalidSize(OWNER& owner)
752
285
    {
753
285
        const size_t bigSize = 1U << 30U; // for NORMAL array
754
285
        const std::array<uint8_t, 100> buffer{
755
285
                0xFF, 0xFF, 0xFF, 0x3F, // big array size for AUTO array
756
285
        };
757
285
        zserio::BitStreamReader reader(buffer.data(), buffer.size());
758
285
        ARRAY array;
759
285
        ASSERT_THROW(arrayRead(array, owner, reader, bigSize), CppRuntimeException);
760
285
    }
761
762
    template <typename ARRAY, typename OWNER>
763
    void testPackedArrayInvalidSize(OWNER& owner)
764
112
    {
765
112
        const size_t bigSize = 1U << 30U; // for NORMAL array
766
112
        const std::array<uint8_t, 100> buffer{
767
112
                0xFF, 0xFF, 0xFF, 0x3F, // big array size for AUTO array
768
112
        };
769
112
        zserio::BitStreamReader reader(buffer.data(), buffer.size());
770
112
        ARRAY array;
771
112
        ASSERT_THROW(arrayReadPacked(array, owner, reader, bigSize), CppRuntimeException);
772
112
    }
773
774
    template <typename RAW_ARRAY>
775
    void checkUninitialized(const RAW_ARRAY& rawArray)
776
320
    {
777
320
        for (const auto& element : rawArray)
778
960
        {
779
960
            ASSERT_FALSE(element.isInitialized());
780
960
        }
781
320
    }
782
783
    template <typename ARRAY, typename T = typename ARRAY::RawArray::value_type,
784
            typename std::enable_if<std::is_constructible<T, NoInitT, T>::value, int>::type = 0>
785
    void testArrayNoInitCopiesAndMoves(const ARRAY& array)
786
64
    {
787
64
        ARRAY arrayCopy(NoInit, array);
788
64
        ASSERT_EQ(array.getRawArray().size(), arrayCopy.getRawArray().size());
789
64
        checkUninitialized(arrayCopy.getRawArray());
790
791
64
        ARRAY arrayCopyAssigned;
792
64
        arrayCopyAssigned.assign(NoInit, array);
793
64
        ASSERT_EQ(array.getRawArray().size(), arrayCopyAssigned.getRawArray().size());
794
64
        checkUninitialized(arrayCopyAssigned.getRawArray());
795
796
64
        const ARRAY arrayMoved(NoInit, std::move(arrayCopy));
797
64
        ASSERT_EQ(array.getRawArray().size(), arrayMoved.getRawArray().size());
798
64
        checkUninitialized(arrayMoved.getRawArray());
799
800
64
        ARRAY arrayMoveAssigned;
801
64
        arrayMoveAssigned.assign(NoInit, std::move(arrayCopyAssigned));
802
64
        ASSERT_EQ(array.getRawArray().size(), arrayMoveAssigned.getRawArray().size());
803
64
        checkUninitialized(arrayMoveAssigned.getRawArray());
804
805
64
        ARRAY arrayCopyWithPropagateAllocator(PropagateAllocator, NoInit, array, std::allocator<uint8_t>());
806
64
        ASSERT_EQ(array.getRawArray().size(), arrayCopyWithPropagateAllocator.getRawArray().size());
807
64
        checkUninitialized(arrayCopyWithPropagateAllocator.getRawArray());
808
64
    }
809
810
    template <typename ARRAY, typename T = typename ARRAY::RawArray::value_type,
811
            typename std::enable_if<!std::is_constructible<T, NoInitT, T>::value, int>::type = 0>
812
    void testArrayNoInitCopiesAndMoves(const ARRAY&)
813
3.24k
    {}
814
815
    static const size_t AUTO_LENGTH_BIT_SIZE = 8;
816
    static const size_t UNKNOWN_BIT_SIZE = std::numeric_limits<size_t>::max();
817
818
    std::array<uint8_t, 256> m_byteBuffer;
819
};
820
821
TEST_F(ArrayTest, intField4Array)
822
1
{
823
1
    const size_t NUM_BITS = 4;
824
1
    std::vector<int8_t> rawArray = {
825
1
            -static_cast<int8_t>(1U << (NUM_BITS - 1)), 7, static_cast<int8_t>(1U << (NUM_BITS - 1)) - 1};
826
1
    testArray<BitFieldArrayTraits<int8_t, NUM_BITS>>(rawArray, NUM_BITS);
827
828
    // empty
829
1
    testArray<BitFieldArrayTraits<int8_t, NUM_BITS>>(std::vector<int8_t>(), NUM_BITS);
830
1
}
831
832
TEST_F(ArrayTest, intField12Array)
833
1
{
834
1
    constexpr size_t NUM_BITS = 12;
835
1
    std::vector<int16_t> rawArray = {
836
1
            -static_cast<int16_t>(1U << (NUM_BITS - 1)), 7, static_cast<int16_t>(1U << (NUM_BITS - 1)) - 1};
837
1
    testArray<BitFieldArrayTraits<int16_t, NUM_BITS>>(rawArray, NUM_BITS);
838
1
}
839
840
TEST_F(ArrayTest, intField20Array)
841
1
{
842
1
    constexpr size_t NUM_BITS = 20;
843
1
    std::vector<int32_t> rawArray = {
844
1
            -static_cast<int32_t>(1U << (NUM_BITS - 1)), 7, static_cast<int32_t>(1U << (NUM_BITS - 1)) - 1};
845
1
    testArray<BitFieldArrayTraits<int32_t, NUM_BITS>>(rawArray, NUM_BITS);
846
1
}
847
848
TEST_F(ArrayTest, intField36Array)
849
1
{
850
1
    constexpr size_t NUM_BITS = 36;
851
1
    std::vector<int64_t> rawArray = {-static_cast<int64_t>(UINT64_C(1) << (NUM_BITS - 1)), 7,
852
1
            static_cast<int64_t>(UINT64_C(1) << (NUM_BITS - 1)) - 1};
853
1
    testArray<BitFieldArrayTraits<int64_t, NUM_BITS>>(rawArray, NUM_BITS);
854
1
}
855
856
TEST_F(ArrayTest, bitField4Array)
857
1
{
858
1
    constexpr size_t NUM_BITS = 4;
859
1
    std::vector<uint8_t> rawArray = {0, 7, (1U << NUM_BITS) - 1};
860
1
    testArray<BitFieldArrayTraits<uint8_t, NUM_BITS>>(rawArray, NUM_BITS);
861
862
    // empty
863
1
    testArray<BitFieldArrayTraits<uint8_t, NUM_BITS>>(std::vector<uint8_t>(), NUM_BITS);
864
1
}
865
866
TEST_F(ArrayTest, bitField12Array)
867
1
{
868
1
    constexpr size_t NUM_BITS = 12;
869
1
    std::vector<uint16_t> rawArray = {0, 7, (1U << NUM_BITS) - 1};
870
1
    testArray<BitFieldArrayTraits<uint16_t, NUM_BITS>>(rawArray, NUM_BITS);
871
1
}
872
873
TEST_F(ArrayTest, bitField20Array)
874
1
{
875
1
    constexpr size_t NUM_BITS = 20;
876
1
    std::vector<uint32_t> rawArray = {0, 7, (1U << NUM_BITS) - 1};
877
1
    testArray<BitFieldArrayTraits<uint32_t, NUM_BITS>>(rawArray, NUM_BITS);
878
1
}
879
880
TEST_F(ArrayTest, bitField36Array)
881
1
{
882
1
    constexpr size_t NUM_BITS = 36;
883
1
    std::vector<uint64_t> rawArray = {0, 7, (UINT64_C(1) << NUM_BITS) - 1};
884
1
    testArray<BitFieldArrayTraits<uint64_t, NUM_BITS>>(rawArray, NUM_BITS);
885
1
}
886
887
TEST_F(ArrayTest, dynamicIntField4Array)
888
1
{
889
1
    constexpr size_t NUM_BITS = 4;
890
1
    std::vector<int8_t> rawArray = {
891
1
            -static_cast<int8_t>(1U << (NUM_BITS - 1)), 7, static_cast<int8_t>(1U << (NUM_BITS - 1)) - 1};
892
1
    testArray<DynamicBitFieldArrayTraits<int8_t, ElementBitSizeWithOwner>>(
893
1
            rawArray, NUM_BITS, ArrayTestOwnerWithBitSize(NUM_BITS));
894
895
    // empty
896
1
    testArray<DynamicBitFieldArrayTraits<int8_t, ElementBitSizeWithoutOwner<NUM_BITS>>>(
897
1
            std::vector<int8_t>(), NUM_BITS);
898
1
}
899
900
TEST_F(ArrayTest, dynamicIntField8Array)
901
1
{
902
1
    constexpr size_t NUM_BITS = 8; // aligned to allow implicit array
903
1
    std::vector<int8_t> rawArray = {INT8_MIN, 7, INT8_MAX};
904
1
    testArray<DynamicBitFieldArrayTraits<int8_t, ElementBitSizeWithOwner>>(
905
1
            rawArray, NUM_BITS, ArrayTestOwnerWithBitSize(NUM_BITS));
906
1
}
907
908
TEST_F(ArrayTest, dynamicIntField12Array)
909
1
{
910
1
    constexpr size_t NUM_BITS = 12;
911
1
    std::vector<int16_t> rawArray = {
912
1
            -static_cast<int16_t>(1U << (NUM_BITS - 1)), 7, static_cast<int16_t>(1U << (NUM_BITS - 1)) - 1};
913
1
    testArray<DynamicBitFieldArrayTraits<int16_t, ElementBitSizeWithoutOwner<NUM_BITS>>>(rawArray, NUM_BITS);
914
1
}
915
916
TEST_F(ArrayTest, dynamicIntField20Array)
917
1
{
918
1
    constexpr size_t NUM_BITS = 20;
919
1
    std::vector<int32_t> rawArray = {
920
1
            -static_cast<int32_t>(1U << (NUM_BITS - 1)), 7, static_cast<int32_t>(1U << (NUM_BITS - 1)) - 1};
921
1
    testArray<DynamicBitFieldArrayTraits<int32_t, ElementBitSizeWithOwner>>(
922
1
            rawArray, NUM_BITS, ArrayTestOwnerWithBitSize(NUM_BITS));
923
1
}
924
925
TEST_F(ArrayTest, dynamicIntField36Array)
926
1
{
927
1
    constexpr size_t NUM_BITS = 36;
928
1
    std::vector<int64_t> rawArray = {-static_cast<int64_t>(UINT64_C(1) << (NUM_BITS - 1)), 7,
929
1
            static_cast<int64_t>(UINT64_C(1) << (NUM_BITS - 1)) - 1};
930
1
    testArray<DynamicBitFieldArrayTraits<int64_t, ElementBitSizeWithoutOwner<NUM_BITS>>>(rawArray, NUM_BITS);
931
1
}
932
933
TEST_F(ArrayTest, dynamicBitField4Array)
934
1
{
935
1
    constexpr size_t NUM_BITS = 4;
936
1
    std::vector<uint8_t> rawArray = {0, 7, (1U << NUM_BITS) - 1};
937
1
    testArray<DynamicBitFieldArrayTraits<uint8_t, ElementBitSizeWithOwner>>(
938
1
            rawArray, NUM_BITS, ArrayTestOwnerWithBitSize(NUM_BITS));
939
940
    // empty
941
1
    testArray<DynamicBitFieldArrayTraits<uint8_t, ElementBitSizeWithoutOwner<NUM_BITS>>>(
942
1
            std::vector<uint8_t>(), NUM_BITS);
943
1
}
944
945
TEST_F(ArrayTest, dynamicBitField8Array)
946
1
{
947
1
    constexpr size_t NUM_BITS = 8; // aligned to allow implicit array
948
1
    std::vector<uint8_t> rawArray = {0, 7, UINT8_MAX};
949
1
    testArray<DynamicBitFieldArrayTraits<uint8_t, ElementBitSizeWithOwner>>(
950
1
            rawArray, NUM_BITS, ArrayTestOwnerWithBitSize(NUM_BITS));
951
1
}
952
953
TEST_F(ArrayTest, dynamicBitField12Array)
954
1
{
955
1
    constexpr size_t NUM_BITS = 12;
956
1
    std::vector<uint16_t> rawArray = {0, 7, (1U << NUM_BITS) - 1};
957
1
    testArray<DynamicBitFieldArrayTraits<uint16_t, ElementBitSizeWithoutOwner<NUM_BITS>>>(rawArray, NUM_BITS);
958
1
}
959
960
TEST_F(ArrayTest, dynamicBitField20Array)
961
1
{
962
1
    constexpr size_t NUM_BITS = 20;
963
1
    std::vector<uint32_t> rawArray = {0, 7, (1U << NUM_BITS) - 1};
964
1
    testArray<DynamicBitFieldArrayTraits<uint32_t, ElementBitSizeWithOwner>>(
965
1
            rawArray, NUM_BITS, ArrayTestOwnerWithBitSize(NUM_BITS));
966
1
}
967
968
TEST_F(ArrayTest, dynamicBitField36Array)
969
1
{
970
1
    constexpr size_t NUM_BITS = 36;
971
1
    std::vector<uint64_t> rawArray = {0, 7, (UINT64_C(1) << NUM_BITS) - 1};
972
1
    testArray<DynamicBitFieldArrayTraits<uint64_t, ElementBitSizeWithoutOwner<NUM_BITS>>>(rawArray, NUM_BITS);
973
1
}
974
975
TEST_F(ArrayTest, stdInt8Array)
976
1
{
977
1
    std::vector<int8_t> rawArray = {INT8_MIN, 7, INT8_MAX};
978
1
    testArray<StdIntArrayTraits<int8_t>>(rawArray, 8);
979
980
    // empty
981
1
    testArray<StdIntArrayTraits<int8_t>>(std::vector<int8_t>(), 8);
982
1
}
983
984
TEST_F(ArrayTest, stdInt16Array)
985
1
{
986
1
    std::vector<int16_t> rawArray = {INT16_MIN, 7, INT16_MAX};
987
1
    testArray<StdIntArrayTraits<int16_t>>(rawArray, 16);
988
1
}
989
990
TEST_F(ArrayTest, stdInt32Array)
991
1
{
992
1
    std::vector<int32_t> rawArray = {INT32_MIN, 7, INT32_MAX};
993
1
    testArray<StdIntArrayTraits<int32_t>>(rawArray, 32);
994
1
}
995
996
TEST_F(ArrayTest, stdInt64Array)
997
1
{
998
1
    std::vector<int64_t> rawArray = {INT64_MIN, 7, INT64_MAX};
999
1
    testArray<StdIntArrayTraits<int64_t>>(rawArray, 64);
1000
1
}
1001
1002
TEST_F(ArrayTest, stdUInt8Array)
1003
1
{
1004
1
    std::vector<uint8_t> rawArray = {0, 7, UINT8_MAX};
1005
1
    testArray<StdIntArrayTraits<uint8_t>>(rawArray, 8);
1006
1007
    // empty
1008
1
    testArray<StdIntArrayTraits<uint8_t>>(std::vector<uint8_t>(), 8);
1009
1
}
1010
1011
TEST_F(ArrayTest, stdUInt16Array)
1012
1
{
1013
1
    std::vector<uint16_t> rawArray = {0, 7, UINT16_MAX};
1014
1
    testArray<StdIntArrayTraits<uint16_t>>(rawArray, 16);
1015
1
}
1016
1017
TEST_F(ArrayTest, stdUInt32Array)
1018
1
{
1019
1
    std::vector<uint32_t> rawArray = {0, 7, UINT32_MAX};
1020
1
    testArray<StdIntArrayTraits<uint32_t>>(rawArray, 32);
1021
1
}
1022
1023
TEST_F(ArrayTest, stdUInt64Array)
1024
1
{
1025
1
    std::vector<uint64_t> rawArray = {0, 7, UINT64_MAX};
1026
1
    testArray<StdIntArrayTraits<uint64_t>>(rawArray, 64);
1027
1
}
1028
1029
TEST_F(ArrayTest, varInt16Array)
1030
1
{
1031
1
    std::vector<int16_t> rawArray = {static_cast<int16_t>(1U << 5U), static_cast<int16_t>(1U << (5U + 8))};
1032
1
    const size_t bitSize = 8 * (1 + 2);
1033
1
    testArray<VarIntNNArrayTraits<int16_t>>(rawArray, bitSize, bitSize);
1034
1035
    // empty
1036
1
    testArray<VarIntNNArrayTraits<int16_t>>(std::vector<int16_t>(), 0, 0);
1037
1
}
1038
1039
TEST_F(ArrayTest, varInt32Array)
1040
1
{
1041
1
    std::vector<int32_t> rawArray = {
1042
1
            static_cast<int32_t>(1U << 5U),
1043
1
            static_cast<int32_t>(1U << (5U + 7)),
1044
1
            static_cast<int32_t>(1U << (5U + 7 + 7)),
1045
1
            static_cast<int32_t>(1U << (5U + 7 + 7 + 8)),
1046
1
    };
1047
1
    const size_t bitSize = 8 * (1 + 2 + 3 + 4);
1048
1
    testArray<VarIntNNArrayTraits<int32_t>>(rawArray, bitSize, bitSize);
1049
1050
    // empty
1051
1
    testArray<VarIntNNArrayTraits<int32_t>>(std::vector<int32_t>(), 0, 0);
1052
1
}
1053
1054
TEST_F(ArrayTest, varInt64Array)
1055
1
{
1056
1
    std::vector<int64_t> rawArray = {
1057
1
            static_cast<int64_t>(UINT64_C(1) << 5U),
1058
1
            static_cast<int64_t>(UINT64_C(1) << (5U + 7)),
1059
1
            static_cast<int64_t>(UINT64_C(1) << (5U + 7 + 7)),
1060
1
            static_cast<int64_t>(UINT64_C(1) << (5U + 7 + 7 + 7)),
1061
1
            static_cast<int64_t>(UINT64_C(1) << (5U + 7 + 7 + 7 + 7)),
1062
1
            static_cast<int64_t>(UINT64_C(1) << (5U + 7 + 7 + 7 + 7 + 7)),
1063
1
            static_cast<int64_t>(UINT64_C(1) << (5U + 7 + 7 + 7 + 7 + 7 + 7)),
1064
1
            static_cast<int64_t>(UINT64_C(1) << (5U + 7 + 7 + 7 + 7 + 7 + 7 + 8)),
1065
1
    };
1066
1
    const size_t bitSize = 8 * (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
1067
1
    testArray<VarIntNNArrayTraits<int64_t>>(rawArray, bitSize, bitSize);
1068
1069
    // empty
1070
1
    testArray<VarIntNNArrayTraits<int64_t>>(std::vector<int64_t>(), 0, 0);
1071
1
}
1072
1073
TEST_F(ArrayTest, varUInt16Array)
1074
1
{
1075
1
    std::vector<uint16_t> rawArray = {1U << 6U, 1U << (6U + 8)};
1076
1
    const size_t bitSize = 8 * (1 + 2);
1077
1
    testArray<VarIntNNArrayTraits<uint16_t>>(rawArray, bitSize, bitSize);
1078
1079
    // empty
1080
1
    testArray<VarIntNNArrayTraits<uint16_t>>(std::vector<uint16_t>(), 0, 0);
1081
1
}
1082
1083
TEST_F(ArrayTest, varUInt32Array)
1084
1
{
1085
1
    std::vector<uint32_t> rawArray = {1U << 6U, 1U << (6U + 7), 1U << (6U + 7 + 7), 1U << (6U + 7 + 7 + 8)};
1086
1
    const size_t bitSize = 8 * (1 + 2 + 3 + 4);
1087
1
    testArray<VarIntNNArrayTraits<uint32_t>>(rawArray, bitSize, bitSize);
1088
1089
    // empty
1090
1
    testArray<VarIntNNArrayTraits<uint32_t>>(std::vector<uint32_t>(), 0, 0);
1091
1
}
1092
1093
TEST_F(ArrayTest, varUInt64Array)
1094
1
{
1095
1
    std::vector<uint64_t> rawArray = {
1096
1
            UINT64_C(1) << 6U,
1097
1
            UINT64_C(1) << (6U + 7),
1098
1
            UINT64_C(1) << (6U + 7 + 7),
1099
1
            UINT64_C(1) << (6U + 7 + 7 + 7),
1100
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7),
1101
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7 + 7),
1102
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7 + 7 + 7),
1103
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7 + 7 + 7 + 8),
1104
1
    };
1105
1
    const size_t bitSize = 8 * (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
1106
1
    testArray<VarIntNNArrayTraits<uint64_t>>(rawArray, bitSize, bitSize);
1107
1108
    // empty
1109
1
    testArray<VarIntNNArrayTraits<uint64_t>>(std::vector<uint64_t>(), 0, 0);
1110
1
}
1111
1112
TEST_F(ArrayTest, varIntArray)
1113
1
{
1114
1
    std::vector<int64_t> rawArray;
1115
    // 1 byte
1116
1
    rawArray.push_back(0);
1117
1
    rawArray.push_back(-1);
1118
1
    rawArray.push_back(1);
1119
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 6U) + 1);
1120
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 6U) - 1);
1121
    // 2 bytes
1122
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 13U) + 1);
1123
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 13U) - 1);
1124
    // 3 bytes
1125
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 20U) + 1);
1126
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 20U) - 1);
1127
    // 4 bytes
1128
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 27U) + 1);
1129
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 27U) - 1);
1130
    // 5 bytes
1131
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 34U) + 1);
1132
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 34U) - 1);
1133
    // 6 bytes
1134
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 41U) + 1);
1135
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 41U) - 1);
1136
    // 7 bytes
1137
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 48U) + 1);
1138
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 48U) - 1);
1139
    // 8 bytes
1140
1
    rawArray.push_back(-static_cast<int64_t>(UINT64_C(1) << 55U) + 1);
1141
1
    rawArray.push_back(static_cast<int64_t>(UINT64_C(1) << 55U) - 1);
1142
    // 9 bytes
1143
1
    rawArray.push_back(INT64_MIN + 1);
1144
1
    rawArray.push_back(INT64_MAX);
1145
    // 1 byte - special case, INT64_MIN stored as -0
1146
1
    rawArray.push_back(INT64_MIN);
1147
1
    const size_t bitSize = 8 * (3 + 2 * (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) + 1);
1148
1
    testArray<VarIntArrayTraits<int64_t>>(rawArray, bitSize, bitSize);
1149
1150
    // empty
1151
1
    testArray<VarIntArrayTraits<int64_t>>(std::vector<int64_t>(), 0, 0);
1152
1
}
1153
1154
TEST_F(ArrayTest, varUIntArray)
1155
1
{
1156
1
    std::vector<uint64_t> rawArray;
1157
    // 1 byte
1158
1
    rawArray.push_back(0);
1159
1
    rawArray.push_back(1);
1160
1
    rawArray.push_back((UINT64_C(1) << 7U) - 1);
1161
    // 2 bytes
1162
1
    rawArray.push_back((UINT64_C(1) << 14U) - 1);
1163
    // 3 bytes
1164
1
    rawArray.push_back((UINT64_C(1) << 21U) - 1);
1165
    // 4 bytes
1166
1
    rawArray.push_back((UINT64_C(1) << 28U) - 1);
1167
    // 5 bytes
1168
1
    rawArray.push_back((UINT64_C(1) << 35U) - 1);
1169
    // 6 bytes
1170
1
    rawArray.push_back((UINT64_C(1) << 42U) - 1);
1171
    // 7 bytes
1172
1
    rawArray.push_back((UINT64_C(1) << 49U) - 1);
1173
    // 8 bytes
1174
1
    rawArray.push_back((UINT64_C(1) << 56U) - 1);
1175
    // 9 bytes
1176
1
    rawArray.push_back(UINT64_MAX);
1177
1
    const size_t bitSize = 8 * (2 + (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9));
1178
1
    testArray<VarIntArrayTraits<uint64_t>>(rawArray, bitSize, bitSize);
1179
1180
    // empty
1181
1
    testArray<VarIntArrayTraits<uint64_t>>(std::vector<uint64_t>(), 0, 0);
1182
1
}
1183
1184
TEST_F(ArrayTest, varSizeArray)
1185
1
{
1186
1
    std::vector<uint32_t> rawArray = {
1187
1
            UINT32_C(1) << 6U,
1188
1
            UINT32_C(1) << (6U + 7),
1189
1
            UINT32_C(1) << (6U + 7 + 7),
1190
1
            UINT32_C(1) << (6U + 7 + 7 + 7),
1191
1
            UINT32_C(1) << (1U + 7 + 7 + 7 + 8),
1192
1
    };
1193
1
    const size_t bitSize = 8 * (1 + 2 + 3 + 4 + 5);
1194
1
    testArray<VarSizeArrayTraits>(rawArray, bitSize, bitSize);
1195
1196
    // empty
1197
1
    testArray<VarSizeArrayTraits>(std::vector<uint32_t>(), 0, 0);
1198
1
}
1199
1200
TEST_F(ArrayTest, float16Array)
1201
1
{
1202
1
    const size_t elementBitSize = 16;
1203
1
    std::vector<float> rawArray = {-9.0, 0.0, 10.0};
1204
1
    testArray<Float16ArrayTraits>(rawArray, elementBitSize);
1205
1206
    // empty
1207
1
    testArray<Float16ArrayTraits>(std::vector<float>(), elementBitSize);
1208
1
}
1209
1210
TEST_F(ArrayTest, float32Array)
1211
1
{
1212
1
    const size_t elementBitSize = 32;
1213
1
    std::vector<float> rawArray = {-9.0, 0.0, 10.0};
1214
1
    testArray<Float32ArrayTraits>(rawArray, elementBitSize);
1215
1216
    // empty
1217
1
    testArray<Float32ArrayTraits>(std::vector<float>(), elementBitSize);
1218
1
}
1219
1220
TEST_F(ArrayTest, float64Array)
1221
1
{
1222
1
    const size_t elementBitSize = 64;
1223
1
    std::vector<double> rawArray = {-9.0, 0.0, 10.0};
1224
1
    testArray<Float64ArrayTraits>(rawArray, elementBitSize);
1225
1226
    // empty
1227
1
    testArray<Float64ArrayTraits>(std::vector<double>(), elementBitSize);
1228
1
}
1229
1230
TEST_F(ArrayTest, boolArray)
1231
1
{
1232
1
    const size_t elementBitSize = 1;
1233
1
    std::vector<bool> rawArray = {false, true};
1234
1
    testArray<BoolArrayTraits>(rawArray, elementBitSize);
1235
1236
    // empty
1237
1
    testArray<BoolArrayTraits>(std::vector<bool>(), elementBitSize);
1238
1
}
1239
1240
TEST_F(ArrayTest, bytesArray)
1241
1
{
1242
1
    const size_t bytesLengthBitSize = 8;
1243
1
    const size_t bytesBitSize = 2 * 8;
1244
1
    const size_t elementBitSize = bytesLengthBitSize + bytesBitSize;
1245
1
    std::vector<std::vector<uint8_t>> rawArray = {{{{1, 255}}, {{127, 128}}}};
1246
1
    testArray<BytesArrayTraits>(rawArray, elementBitSize);
1247
1248
    // empty
1249
1
    testArray<BytesArrayTraits>(std::vector<std::vector<uint8_t>>(), elementBitSize);
1250
1
}
1251
1252
TEST_F(ArrayTest, stringArray)
1253
1
{
1254
1
    const size_t stringLengthBitSize = 8;
1255
1
    const size_t stringBitSize = (sizeof("StringX") - 1) * 8; // without terminating character
1256
1
    const size_t elementBitSize = stringLengthBitSize + stringBitSize;
1257
1
    std::vector<std::string> rawArray = {"String0", "String1", "String2"};
1258
1
    testArray<StringArrayTraits>(rawArray, elementBitSize);
1259
1260
    // empty
1261
1
    testArray<StringArrayTraits>(std::vector<std::string>(), 0);
1262
1
}
1263
1264
TEST_F(ArrayTest, bitBufferArray)
1265
1
{
1266
1
    const size_t bitBufferLengthBitSize = 8;
1267
1
    const size_t bitBufferBitSize = 10;
1268
1
    const size_t elementBitSize = bitBufferLengthBitSize + bitBufferBitSize;
1269
1
    std::vector<BitBuffer> rawArray = {
1270
1
            BitBuffer(bitBufferBitSize), BitBuffer(bitBufferBitSize), BitBuffer(bitBufferBitSize)};
1271
1
    testArray<BitBufferArrayTraits>(rawArray, elementBitSize);
1272
1273
    // empty
1274
1
    testArray<BitBufferArrayTraits>(std::vector<BitBuffer>(), 0);
1275
1
}
1276
1277
TEST_F(ArrayTest, enumArray)
1278
1
{
1279
1
    std::vector<ArrayEnum> rawArray = {ArrayEnum::VALUE1, ArrayEnum::VALUE2, ArrayEnum::VALUE3};
1280
1
    const size_t elementBitSize = 8;
1281
1
    testArray<EnumArrayTraits<ArrayEnum>>(rawArray, elementBitSize);
1282
1283
    // empty
1284
1
    testArray<EnumArrayTraits<ArrayEnum>>(std::vector<ArrayEnum>(), elementBitSize);
1285
1286
1
    std::vector<ArrayEnum> invalidRawArray = {static_cast<ArrayEnum>(10)};
1287
1
    ASSERT_THROW(testArray<EnumArrayTraits<ArrayEnum>>(invalidRawArray, elementBitSize), CppRuntimeException);
1288
1
}
1289
1290
TEST_F(ArrayTest, bitmaskArray)
1291
1
{
1292
1
    std::vector<ArrayBitmask> rawArray = {
1293
1
            ArrayBitmask::Values::READ, ArrayBitmask::Values::WRITE, ArrayBitmask::Values::CREATE};
1294
1
    const size_t elementBitSize = 8;
1295
1
    testArray<BitmaskArrayTraits<ArrayBitmask>>(rawArray, elementBitSize);
1296
1297
    // empty
1298
1
    testArray<BitmaskArrayTraits<ArrayBitmask>>(std::vector<ArrayBitmask>(), elementBitSize);
1299
1
}
1300
1301
TEST_F(ArrayTest, objectArray)
1302
1
{
1303
1
    std::vector<ArrayObject> rawArray = {ArrayObject(0xAB), ArrayObject(0xCD), ArrayObject(0xEF)};
1304
1
    testArray<ObjectArrayTraits<ArrayObject, ArrayObjectElementFactory>, std::vector<ArrayObject>,
1305
1
            ArrayObjectArrayExpressions>(rawArray, 31);
1306
1307
    // empty
1308
1
    testArray<ObjectArrayTraits<ArrayObject, ArrayObjectElementFactory>, std::vector<ArrayObject>,
1309
1
            ArrayObjectArrayExpressions>(std::vector<ArrayObject>(), 31);
1310
1
}
1311
1312
TEST_F(ArrayTest, paramObjectArray)
1313
1
{
1314
1
    ArrayParamObjectOwner owner{ArrayObject(0xEF)};
1315
1
    std::vector<ArrayParamObject> rawArray = {
1316
1
            ArrayParamObject(0xAB), ArrayParamObject(0xBC), ArrayParamObject(0xCD)};
1317
1
    testArray<ObjectArrayTraits<ArrayParamObject, ArrayParamObjectElementFactory>,
1318
1
            std::vector<ArrayParamObject>, ArrayParamObjectArrayExpressions>(rawArray, 32, owner);
1319
1
}
1320
1321
TEST_F(ArrayTest, stdInt8PackedArray)
1322
1
{
1323
1
    std::vector<int8_t> rawArray = {-4, -3, -1, 0, 2, 4, 6, 8, 10, 10, 11};
1324
1
    testPackedArray<StdIntArrayTraits<int8_t>>(rawArray);
1325
1
}
1326
1327
TEST_F(ArrayTest, stdInt64PackedArray)
1328
1
{
1329
    // will not be packed
1330
1
    std::vector<int64_t> rawArray = {INT64_MIN, 1, -1, INT64_MAX};
1331
1
    testPackedArray<StdIntArrayTraits<int64_t>>(rawArray);
1332
1
}
1333
1334
TEST_F(ArrayTest, stdUInt64PackedArray)
1335
1
{
1336
    // will have maxBitNumber 62 bits
1337
1
    std::vector<uint64_t> rawArray = {0, INT64_MAX / 2, 100, 200, 300, 400, 500, 600, 700};
1338
1
    testPackedArray<StdIntArrayTraits<uint64_t>>(rawArray);
1339
1
}
1340
1341
TEST_F(ArrayTest, bitField64PackedArray)
1342
1
{
1343
1
    using ArrayTraits = BitFieldArrayTraits<uint64_t, 64>;
1344
1345
    // none-zero delta
1346
1
    std::vector<uint64_t> rawArray1 = {10, 11, 12};
1347
1
    const size_t array1MaxDeltaBitSize = 1;
1348
1
    const size_t array1BitSizeOf = calcPackedBitSize(64, rawArray1.size(), array1MaxDeltaBitSize);
1349
1
    const size_t array1AlignedBitSizeOf = calcAlignedPackedBitSize(64, rawArray1.size(), array1MaxDeltaBitSize);
1350
1
    testPackedArray<ArrayTraits>(rawArray1, array1BitSizeOf, array1AlignedBitSizeOf);
1351
1352
    // zero delta
1353
1
    std::vector<uint64_t> rawArray2 = {10, 10, 10};
1354
1
    const size_t array2BitSizeOf = PACKING_DESCRIPTOR_BITSIZE + 64;
1355
1
    const size_t array2AlignedBitSizeOf = PACKING_DESCRIPTOR_BITSIZE + 64 +
1356
1
            /* alignment before element 2 */ (8 - PACKING_DESCRIPTOR_BITSIZE);
1357
1
    testPackedArray<ArrayTraits>(rawArray2, array2BitSizeOf, array2AlignedBitSizeOf);
1358
1359
    // one-element array
1360
1
    std::vector<uint64_t> rawArray3 = {10};
1361
1
    const size_t array3BitSizeOf = 1 + 64;
1362
1
    const size_t array3AlignedBitSizeOf = 1 + 64; // no alignment before first element, no more elements
1363
1
    testPackedArray<ArrayTraits>(rawArray3, array3BitSizeOf, array3AlignedBitSizeOf);
1364
1365
    // empty array
1366
1
    std::vector<uint64_t> rawArray4 = {};
1367
1
    const size_t array4BitSizeOf = 0;
1368
1
    const size_t array4AlignedBitSizeOf = 0;
1369
1
    testPackedArray<ArrayTraits>(rawArray4, array4BitSizeOf, array4AlignedBitSizeOf);
1370
1371
    // packing not enabled, delta is too big
1372
1
    testPackedArray<ArrayTraits>(std::vector<uint64_t>{0, UINT64_MAX});
1373
1
    testPackedArray<ArrayTraits>(std::vector<uint64_t>{UINT64_MAX, UINT64_MAX / 2, 0});
1374
1375
    // will have maxBitNumber 62 bits
1376
1
    testPackedArray<ArrayTraits>(
1377
1
            std::vector<uint64_t>{0, static_cast<uint64_t>(INT64_MAX / 2), 100, 200, 300, 400, 500, 600, 700});
1378
1
}
1379
1380
TEST_F(ArrayTest, bitField8PackedArray)
1381
1
{
1382
1
    using ArrayTraits = BitFieldArrayTraits<uint8_t, 8>;
1383
1384
    // will not be packed because unpacked 8bit values will be more efficient
1385
1
    std::vector<uint8_t> rawArray1 = {UINT8_MAX, 0, 10, 20, 30, 40}; // max_bit_number 8, delta needs 9 bits
1386
1
    const size_t array1BitSizeOf = 1 + 6 * 8;
1387
1
    const size_t array1AlignedBitSizeOf = 1 + 8 + /* alignment */ 7 + 5 * 8;
1388
1
    testPackedArray<ArrayTraits>(rawArray1, array1BitSizeOf, array1AlignedBitSizeOf);
1389
1390
    // will not be packed because unpacked 8bit values will be more efficient
1391
    // (6 bits more are needed to store max_bit_number in descriptor if packing was enabled)
1392
1
    std::vector<uint8_t> rawArray2 = {
1393
1
            UINT8_MAX, UINT8_MAX / 2 + 1, 10, 20, 30, 40}; // max_bit_number 7, delta needs 8 bits
1394
1
    const size_t array2BitSizeOf = 1 + 6 * 8;
1395
1
    const size_t array2AlignedBitSizeOf = 1 + 8 + /* alignment */ 7 + 5 * 8;
1396
1
    testPackedArray<ArrayTraits>(rawArray2, array2BitSizeOf, array2AlignedBitSizeOf);
1397
1
}
1398
1399
TEST_F(ArrayTest, intField64PackedArray)
1400
1
{
1401
1
    using ArrayTraits = BitFieldArrayTraits<int64_t, 64>;
1402
1403
1
    testPackedArray<ArrayTraits>(std::vector<int64_t>{-10, 11, -12});
1404
1
    testPackedArray<ArrayTraits>(std::vector<int64_t>{-10, -10, -10}); // zero delta
1405
1406
1
    testPackedArray<ArrayTraits>(std::vector<int64_t>{}); // empty
1407
1
    testPackedArray<ArrayTraits>(std::vector<int64_t>{10}); // single element
1408
1409
    // packing not enabled, delta is too big
1410
1
    testPackedArray<ArrayTraits>(std::vector<int64_t>{INT64_MIN, INT64_MAX});
1411
1
    testPackedArray<ArrayTraits>(std::vector<int64_t>{INT64_MIN, 0, INT64_MAX});
1412
1
}
1413
1414
TEST_F(ArrayTest, intField16PackedArray)
1415
1
{
1416
1
    using ArrayTraits = BitFieldArrayTraits<int16_t, 16>;
1417
1418
    // will not be packed because unpacked 16bit values will be more efficient
1419
    // (6 bits more are needed to store max_bit_number in descriptor if packing was enabled)
1420
1
    std::vector<int16_t> rawArray = {INT16_MIN, -1, 10, 20, 30, 40}; // max_bit_number 15, delta needs 16 bits
1421
1
    const size_t unpackedBitSizeOf = 1 + 6 * 16;
1422
1
    const size_t unpackedAlignedBitSizeOf = 1 + 16 + /* alignment */ 7 + 5 * 16;
1423
1
    testPackedArray<ArrayTraits>(rawArray, unpackedBitSizeOf, unpackedAlignedBitSizeOf);
1424
1
}
1425
1426
TEST_F(ArrayTest, dynamicBitField8PackedArray)
1427
1
{
1428
    // will not be packed because unpacked 8bit values will be more efficient
1429
1
    std::vector<uint8_t> rawArray1 = {UINT8_MAX, 0, 10, 20, 30, 40}; // max_bit_number 8, delta needs 9 bits
1430
1
    const size_t array1BitSizeOf = 1 + 6 * 8;
1431
1
    const size_t array1AlignedBitSizeOf = 1 + 8 + /* alignment */ 7 + 5 * 8;
1432
1
    testPackedArray<DynamicBitFieldArrayTraits<uint8_t, ElementBitSizeWithOwner>>(
1433
1
            rawArray1, array1BitSizeOf, array1AlignedBitSizeOf, ArrayTestOwnerWithBitSize(8));
1434
1435
    // will not be packed because unpacked 8bit values will be more efficient
1436
    // (6 bits more are needed to store max_bit_number in descriptor if packing was enabled)
1437
1
    std::vector<uint8_t> rawArray2 = {
1438
1
            UINT8_MAX, UINT8_MAX / 2 + 1, 10, 20, 30, 40}; // max_bit_number 7, delta needs 8 bits
1439
1
    const size_t array2BitSizeOf = 1 + 6 * 8;
1440
1
    const size_t array2AlignedBitSizeOf = 1 + 8 + /* alignment */ 7 + 5 * 8;
1441
1
    testPackedArray<DynamicBitFieldArrayTraits<uint8_t, ElementBitSizeWithoutOwner<8>>>(
1442
1
            rawArray2, array2BitSizeOf, array2AlignedBitSizeOf);
1443
1
}
1444
1445
TEST_F(ArrayTest, varUInt64PackedArray)
1446
1
{
1447
1
    std::vector<uint64_t> rawArray = {
1448
1
            UINT64_C(1) << 6U,
1449
1
            UINT64_C(1) << (6U + 7),
1450
1
            UINT64_C(1) << (6U + 7 + 7),
1451
1
            UINT64_C(1) << (6U + 7 + 7 + 7),
1452
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7),
1453
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7 + 7),
1454
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7 + 7 + 7),
1455
1
            UINT64_C(1) << (6U + 7 + 7 + 7 + 7 + 7 + 7 + 8),
1456
1
    };
1457
1
    testPackedArray<VarIntNNArrayTraits<uint64_t>>(rawArray);
1458
1459
1
    std::vector<uint64_t> unpackedRawArray = {UINT64_C(5000000), 0, 0, 0, 0, 0, 0};
1460
1
    const size_t unpackedBitSizeOf = 1 + 32 + 6 * 8;
1461
1
    const size_t unpackedAlignedBitSizeOf = 1 + 32 + /* alignment */ 7 + 6 * 8;
1462
1
    testPackedArray<VarIntNNArrayTraits<uint64_t>>(
1463
1
            unpackedRawArray, unpackedBitSizeOf, unpackedAlignedBitSizeOf);
1464
1
}
1465
1466
TEST_F(ArrayTest, varSizePackedArray)
1467
1
{
1468
1
    std::vector<uint32_t> rawArray = {
1469
1
            UINT32_C(1) << 6U,
1470
1
            UINT32_C(1) << (6U + 7),
1471
1
            UINT32_C(1) << (6U + 7 + 7),
1472
1
            UINT32_C(1) << (6U + 7 + 7 + 7),
1473
1
            UINT32_C(1) << (1U + 7 + 7 + 7 + 8),
1474
1
    };
1475
1
    testPackedArray<VarSizeArrayTraits>(rawArray);
1476
1
}
1477
1478
TEST_F(ArrayTest, enumPackedArray)
1479
1
{
1480
1
    std::vector<ArrayEnum> rawArray = {ArrayEnum::VALUE1, ArrayEnum::VALUE2, ArrayEnum::VALUE3};
1481
1
    testPackedArray<EnumArrayTraits<ArrayEnum>>(rawArray);
1482
1
}
1483
1484
TEST_F(ArrayTest, bitmaskPackedArray)
1485
1
{
1486
1
    std::vector<ArrayBitmask> rawArray = {
1487
1
            ArrayBitmask::Values::READ, ArrayBitmask::Values::WRITE, ArrayBitmask::Values::CREATE};
1488
1
    testPackedArray<BitmaskArrayTraits<ArrayBitmask>>(rawArray);
1489
1
}
1490
1491
TEST_F(ArrayTest, objectPackedArray)
1492
1
{
1493
1
    std::vector<ArrayObject> rawArray = {ArrayObject(0xAB), ArrayObject(0xCD), ArrayObject(0xEF)};
1494
1
    testPackedArray<ObjectArrayTraits<ArrayObject, ArrayObjectElementFactory>, std::vector<ArrayObject>,
1495
1
            ArrayObjectArrayExpressions>(rawArray);
1496
1
}
1497
1498
TEST_F(ArrayTest, paramObjectPackedArray)
1499
1
{
1500
1
    ArrayParamObjectOwner owner{ArrayObject(0xEF)};
1501
1
    std::vector<ArrayParamObject> rawArray = {
1502
1
            ArrayParamObject(0xAB), ArrayParamObject(0xBC), ArrayParamObject(0xCD)};
1503
1
    testPackedArray<ObjectArrayTraits<ArrayParamObject, ArrayParamObjectElementFactory>,
1504
1
            std::vector<ArrayParamObject>, ArrayParamObjectArrayExpressions>(rawArray, owner);
1505
1
}
1506
1507
TEST_F(ArrayTest, createOptionalArray)
1508
1
{
1509
1
    using ArrayT = Array<std::vector<uint8_t>, BitFieldArrayTraits<uint8_t, 8>, ArrayType::NORMAL>;
1510
1
    InplaceOptionalHolder<ArrayT> optionalArray = createOptionalArray<ArrayT>(NullOpt);
1511
1
    ASSERT_FALSE(optionalArray.hasValue());
1512
1513
1
    const std::vector<uint8_t> array = {0, 7, UINT8_MAX};
1514
1
    optionalArray = createOptionalArray<ArrayT>(array);
1515
1
    ASSERT_TRUE(optionalArray.hasValue());
1516
1
    ASSERT_EQ(array, optionalArray->getRawArray());
1517
1
}
1518
1519
} // namespace zserio