GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include <cstring> |
||
2 |
#include <array> |
||
3 |
|||
4 |
#include "zserio/BitStreamReader.h" |
||
5 |
#include "zserio/CppRuntimeException.h" |
||
6 |
|||
7 |
#include "gtest/gtest.h" |
||
8 |
|||
9 |
namespace zserio |
||
10 |
{ |
||
11 |
|||
12 |
✗✓ | 12 |
class BitStreamReaderTest : public ::testing::Test |
13 |
{ |
||
14 |
public: |
||
15 |
✓✗ | 12 |
BitStreamReaderTest() : m_byteBuffer(), m_reader(m_byteBuffer.data(), m_byteBuffer.size()) |
16 |
{ |
||
17 |
✓✗ | 12 |
m_byteBuffer.fill(0); |
18 |
12 |
} |
|
19 |
|||
20 |
protected: |
||
21 |
std::array<uint8_t, 16> m_byteBuffer; |
||
22 |
BitStreamReader m_reader; |
||
23 |
}; |
||
24 |
|||
25 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, spanConstructor) |
26 |
{ |
||
27 |
1 |
const std::array<const uint8_t, 3> data = {0xAE, 0xEA, 0x80}; |
|
28 |
1 |
const Span<const uint8_t> span(data); |
|
29 |
✓✗ | 1 |
BitStreamReader reader(span); |
30 |
|||
31 |
✓✗✗✓ ✗✗✗✗ ✗✗✓✗ |
1 |
ASSERT_EQ(span.size() * 8, reader.getBufferBitSize()); |
32 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗ |
1 |
ASSERT_EQ(0xAEE, reader.readBits(12)); |
33 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗ |
1 |
ASSERT_EQ(0xA, reader.readBits(4)); |
34 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
1 |
ASSERT_EQ(0x80, reader.readBits(8)); |
35 |
|||
36 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗ |
2 |
ASSERT_THROW(reader.readBits(1), CppRuntimeException); |
37 |
} |
||
38 |
|||
39 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, spanConstructorWithBitSize) |
40 |
{ |
||
41 |
1 |
const std::array<const uint8_t, 3> data = {0xAE, 0xEA, 0x80}; |
|
42 |
1 |
const Span<const uint8_t> span(data); |
|
43 |
✓✗ | 1 |
BitStreamReader reader(span, 23); |
44 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗ |
2 |
ASSERT_THROW(BitStreamReader wrongReader(span, 25), CppRuntimeException); |
45 |
|||
46 |
✓✗✗✓ ✗✗✗✗ ✗✗✓✗ |
1 |
ASSERT_EQ(23, reader.getBufferBitSize()); |
47 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗ |
1 |
ASSERT_EQ(0xAEE, reader.readBits(12)); |
48 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗ |
1 |
ASSERT_EQ(0xA, reader.readBits(4)); |
49 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
1 |
ASSERT_EQ(0x40, reader.readBits(7)); |
50 |
|||
51 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✓ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗ |
2 |
ASSERT_THROW(reader.readBits(1), CppRuntimeException); |
52 |
} |
||
53 |
|||
54 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, bitBufferConstructor) |
55 |
{ |
||
56 |
✓✗ | 2 |
const std::vector<uint8_t> data = {0xAE, 0xEA, 0x80}; |
57 |
✓✗✓✗ ✓✗ |
2 |
BitBuffer bitBuffer(data, 17); |
58 |
✓✗ | 1 |
BitStreamReader reader(bitBuffer); |
59 |
|||
60 |
✓✗✗✓ ✗✗✗✗ ✗✗✓✗ |
1 |
ASSERT_EQ(bitBuffer.getBitSize(), reader.getBufferBitSize()); |
61 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗ |
1 |
ASSERT_EQ(0xAEE, reader.readBits(12)); |
62 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗ |
1 |
ASSERT_EQ(0xA, reader.readBits(4)); |
63 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
1 |
ASSERT_EQ(1, reader.readBits(1)); |
64 |
|||
65 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✓✗ ✗ |
2 |
ASSERT_THROW(reader.readBits(1), CppRuntimeException); |
66 |
|||
67 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✓✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ |
2 |
ASSERT_THROW(BitStreamReader(nullptr, std::numeric_limits<size_t>::max() / 8), |
68 |
CppRuntimeException); |
||
69 |
} |
||
70 |
|||
71 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, bitBufferConstructorOverflow) |
72 |
{ |
||
73 |
✓✗ | 2 |
const std::vector<uint8_t> data = {0xFF, 0xFF, 0xF0}; |
74 |
✓✗✓✗ ✓✗ |
2 |
BitBuffer bitBuffer(data, 19); |
75 |
✓✗ | 1 |
BitStreamReader reader(bitBuffer); |
76 |
|||
77 |
✓✗✗✓ ✗✗✗✗ ✗✗✓✗ ✓✗✗ |
1 |
ASSERT_EQ(bitBuffer.getBitSize(), reader.getBufferBitSize()); |
78 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✓✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ |
2 |
ASSERT_THROW(reader.readBits(20), CppRuntimeException); |
79 |
} |
||
80 |
|||
81 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, readUnalignedData) |
82 |
{ |
||
83 |
// number expected to read at offset |
||
84 |
1 |
const uint8_t testValue = 123; |
|
85 |
|||
86 |
✓✓ | 66 |
for (uint8_t offset = 0; offset <= 64; ++offset) |
87 |
{ |
||
88 |
✓✗ | 130 |
BitBuffer buffer(8 + offset); |
89 |
|||
90 |
// write test value at offset to data buffer |
||
91 |
✓✗ | 65 |
buffer.getData()[offset / 8U] |= static_cast<uint8_t>(testValue >> (offset % 8U)); |
92 |
✓✓ | 65 |
if (offset % 8 != 0) // don't write behind the buffer |
93 |
✓✗ | 56 |
buffer.getData()[offset / 8U + 1] |= static_cast<uint8_t>(testValue << (8U - (offset % 8U))); |
94 |
|||
95 |
✓✗ | 65 |
BitStreamReader reader(buffer); |
96 |
|||
97 |
// read offset bits |
||
98 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗ |
65 |
ASSERT_EQ(0, reader.readBits64(offset)); |
99 |
|||
100 |
// read magic number |
||
101 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
65 |
ASSERT_EQ(testValue, reader.readBits(8)) << "Offset: " << offset; |
102 |
|||
103 |
// check eof |
||
104 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✓✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ |
130 |
ASSERT_THROW(reader.readBits(1), CppRuntimeException) << "Offset: " << offset; |
105 |
} |
||
106 |
} |
||
107 |
|||
108 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, readBits) |
109 |
{ |
||
110 |
// check invalid bitlength acceptance |
||
111 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✓✗ ✗ |
2 |
ASSERT_THROW(m_reader.readBits(255), CppRuntimeException); |
112 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗ |
2 |
ASSERT_THROW(m_reader.readBits(33), CppRuntimeException); |
113 |
|||
114 |
// return 0 for 0 bits |
||
115 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
1 |
ASSERT_EQ(0, m_reader.readBits(0)); |
116 |
} |
||
117 |
|||
118 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, readBits64) |
119 |
{ |
||
120 |
// check invalid bit length acceptance |
||
121 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✓✗ ✗ |
2 |
ASSERT_THROW(m_reader.readBits64(255), CppRuntimeException); |
122 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗ |
2 |
ASSERT_THROW(m_reader.readBits64(65), CppRuntimeException); |
123 |
|||
124 |
// return 0 for 0 bits |
||
125 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
1 |
ASSERT_EQ(0, m_reader.readBits64(0)); |
126 |
} |
||
127 |
|||
128 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, readSignedBits) |
129 |
{ |
||
130 |
// check invalid bit length acceptance |
||
131 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✓✗ ✗ |
2 |
ASSERT_THROW(m_reader.readSignedBits(255), CppRuntimeException); |
132 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗ |
2 |
ASSERT_THROW(m_reader.readSignedBits(33), CppRuntimeException); |
133 |
|||
134 |
// return 0 for 0 bits |
||
135 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
1 |
ASSERT_EQ(0, m_reader.readSignedBits(0)); |
136 |
} |
||
137 |
|||
138 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, readSignedBits64) |
139 |
{ |
||
140 |
// check invalid bit length acceptance |
||
141 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✓✗ ✗ |
2 |
ASSERT_THROW(m_reader.readSignedBits64(255), CppRuntimeException); |
142 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗ |
2 |
ASSERT_THROW(m_reader.readSignedBits64(65), CppRuntimeException); |
143 |
|||
144 |
// return 0 for 0 bits |
||
145 |
✓✗✓✗ ✗✓✗✗ ✗✗✗✗ ✓✗✓✗ ✗ |
1 |
ASSERT_EQ(0, m_reader.readSignedBits64(0)); |
146 |
} |
||
147 |
|||
148 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, readVarSize) |
149 |
{ |
||
150 |
{ |
||
151 |
// overflow, 2^32 - 1 is too much ({ 0x83, 0xFF, 0xFF, 0xFF, 0xFF } is the maximum) |
||
152 |
1 |
const std::array<uint8_t, 5> buffer = { 0x87, 0xFF, 0xFF, 0xFF, 0xFF }; |
|
153 |
✓✗ | 1 |
zserio::BitStreamReader reader(buffer.data(), buffer.size()); |
154 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✓✗ ✗ |
2 |
ASSERT_THROW(reader.readVarSize(), CppRuntimeException); |
155 |
} |
||
156 |
|||
157 |
{ |
||
158 |
// overflow, 2^36 - 1 is too much ({ 0x83, 0xFF, 0xFF, 0xFF, 0xFF } is the maximum) |
||
159 |
1 |
const std::array<uint8_t, 5> buffer = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
|
160 |
✓✗ | 1 |
zserio::BitStreamReader reader(buffer.data(), buffer.size()); |
161 |
✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✓ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗ |
2 |
ASSERT_THROW(reader.readVarSize(), CppRuntimeException); |
162 |
} |
||
163 |
} |
||
164 |
|||
165 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, getBitPosition) |
166 |
{ |
||
167 |
✓✗✗✓ ✗✗✗✗ ✗✗✓✗ |
1 |
ASSERT_EQ(0, m_reader.getBitPosition()); |
168 |
1 |
m_reader.readBits(10); |
|
169 |
✓✗✗✓ ✗✗✗✗ ✗✗✓✗ |
1 |
ASSERT_EQ(10, m_reader.getBitPosition()); |
170 |
} |
||
171 |
|||
172 |
✓✗✓✗ ✓✗✗✓ |
802 |
TEST_F(BitStreamReaderTest, getBufferBitSize) |
173 |
{ |
||
174 |
✓✗✗✓ ✗✗✗✗ ✗✗✓✗ |
1 |
ASSERT_EQ(m_byteBuffer.size() * 8, m_reader.getBufferBitSize()); |
175 |
} |
||
176 |
|||
177 |
✓✗✓✗ |
2394 |
} // namespace zserio |
Generated by: GCOVR (Version 4.2) |