1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/base/arraysize.h"
12 #include "webrtc/base/bitbuffer.h"
13 #include "webrtc/base/bytebuffer.h"
14 #include "webrtc/base/common.h"
15 #include "webrtc/base/gunit.h"
16
17 namespace rtc {
18
TEST(BitBufferTest,ConsumeBits)19 TEST(BitBufferTest, ConsumeBits) {
20 const uint8_t bytes[64] = {0};
21 BitBuffer buffer(bytes, 32);
22 uint64_t total_bits = 32 * 8;
23 EXPECT_EQ(total_bits, buffer.RemainingBitCount());
24 EXPECT_TRUE(buffer.ConsumeBits(3));
25 total_bits -= 3;
26 EXPECT_EQ(total_bits, buffer.RemainingBitCount());
27 EXPECT_TRUE(buffer.ConsumeBits(3));
28 total_bits -= 3;
29 EXPECT_EQ(total_bits, buffer.RemainingBitCount());
30 EXPECT_TRUE(buffer.ConsumeBits(15));
31 total_bits -= 15;
32 EXPECT_EQ(total_bits, buffer.RemainingBitCount());
33 EXPECT_TRUE(buffer.ConsumeBits(37));
34 total_bits -= 37;
35 EXPECT_EQ(total_bits, buffer.RemainingBitCount());
36
37 EXPECT_FALSE(buffer.ConsumeBits(32 * 8));
38 EXPECT_EQ(total_bits, buffer.RemainingBitCount());
39 }
40
TEST(BitBufferTest,ReadBytesAligned)41 TEST(BitBufferTest, ReadBytesAligned) {
42 const uint8_t bytes[] = {0x0A, 0xBC, 0xDE, 0xF1, 0x23, 0x45, 0x67, 0x89};
43 uint8_t val8;
44 uint16_t val16;
45 uint32_t val32;
46 BitBuffer buffer(bytes, 8);
47 EXPECT_TRUE(buffer.ReadUInt8(&val8));
48 EXPECT_EQ(0x0Au, val8);
49 EXPECT_TRUE(buffer.ReadUInt8(&val8));
50 EXPECT_EQ(0xBCu, val8);
51 EXPECT_TRUE(buffer.ReadUInt16(&val16));
52 EXPECT_EQ(0xDEF1u, val16);
53 EXPECT_TRUE(buffer.ReadUInt32(&val32));
54 EXPECT_EQ(0x23456789u, val32);
55 }
56
TEST(BitBufferTest,ReadBytesOffset4)57 TEST(BitBufferTest, ReadBytesOffset4) {
58 const uint8_t bytes[] = {0x0A, 0xBC, 0xDE, 0xF1, 0x23,
59 0x45, 0x67, 0x89, 0x0A};
60 uint8_t val8;
61 uint16_t val16;
62 uint32_t val32;
63 BitBuffer buffer(bytes, 9);
64 EXPECT_TRUE(buffer.ConsumeBits(4));
65
66 EXPECT_TRUE(buffer.ReadUInt8(&val8));
67 EXPECT_EQ(0xABu, val8);
68 EXPECT_TRUE(buffer.ReadUInt8(&val8));
69 EXPECT_EQ(0xCDu, val8);
70 EXPECT_TRUE(buffer.ReadUInt16(&val16));
71 EXPECT_EQ(0xEF12u, val16);
72 EXPECT_TRUE(buffer.ReadUInt32(&val32));
73 EXPECT_EQ(0x34567890u, val32);
74 }
75
TEST(BitBufferTest,ReadBytesOffset3)76 TEST(BitBufferTest, ReadBytesOffset3) {
77 // The pattern we'll check against is counting down from 0b1111. It looks
78 // weird here because it's all offset by 3.
79 // Byte pattern is:
80 // 56701234
81 // 0b00011111,
82 // 0b11011011,
83 // 0b10010111,
84 // 0b01010011,
85 // 0b00001110,
86 // 0b11001010,
87 // 0b10000110,
88 // 0b01000010
89 // xxxxx <-- last 5 bits unused.
90
91 // The bytes. It almost looks like counting down by two at a time, except the
92 // jump at 5->3->0, since that's when the high bit is turned off.
93 const uint8_t bytes[] = {0x1F, 0xDB, 0x97, 0x53, 0x0E, 0xCA, 0x86, 0x42};
94
95 uint8_t val8;
96 uint16_t val16;
97 uint32_t val32;
98 BitBuffer buffer(bytes, 8);
99 EXPECT_TRUE(buffer.ConsumeBits(3));
100 EXPECT_TRUE(buffer.ReadUInt8(&val8));
101 EXPECT_EQ(0xFEu, val8);
102 EXPECT_TRUE(buffer.ReadUInt16(&val16));
103 EXPECT_EQ(0xDCBAu, val16);
104 EXPECT_TRUE(buffer.ReadUInt32(&val32));
105 EXPECT_EQ(0x98765432u, val32);
106 // 5 bits left unread. Not enough to read a uint8_t.
107 EXPECT_EQ(5u, buffer.RemainingBitCount());
108 EXPECT_FALSE(buffer.ReadUInt8(&val8));
109 }
110
TEST(BitBufferTest,ReadBits)111 TEST(BitBufferTest, ReadBits) {
112 // Bit values are:
113 // 0b01001101,
114 // 0b00110010
115 const uint8_t bytes[] = {0x4D, 0x32};
116 uint32_t val;
117 BitBuffer buffer(bytes, 2);
118 EXPECT_TRUE(buffer.ReadBits(&val, 3));
119 // 0b010
120 EXPECT_EQ(0x2u, val);
121 EXPECT_TRUE(buffer.ReadBits(&val, 2));
122 // 0b01
123 EXPECT_EQ(0x1u, val);
124 EXPECT_TRUE(buffer.ReadBits(&val, 7));
125 // 0b1010011
126 EXPECT_EQ(0x53u, val);
127 EXPECT_TRUE(buffer.ReadBits(&val, 2));
128 // 0b00
129 EXPECT_EQ(0x0u, val);
130 EXPECT_TRUE(buffer.ReadBits(&val, 1));
131 // 0b1
132 EXPECT_EQ(0x1u, val);
133 EXPECT_TRUE(buffer.ReadBits(&val, 1));
134 // 0b0
135 EXPECT_EQ(0x0u, val);
136
137 EXPECT_FALSE(buffer.ReadBits(&val, 1));
138 }
139
TEST(BitBufferTest,SetOffsetValues)140 TEST(BitBufferTest, SetOffsetValues) {
141 uint8_t bytes[4] = {0};
142 BitBufferWriter buffer(bytes, 4);
143
144 size_t byte_offset, bit_offset;
145 // Bit offsets are [0,7].
146 EXPECT_TRUE(buffer.Seek(0, 0));
147 EXPECT_TRUE(buffer.Seek(0, 7));
148 buffer.GetCurrentOffset(&byte_offset, &bit_offset);
149 EXPECT_EQ(0u, byte_offset);
150 EXPECT_EQ(7u, bit_offset);
151 EXPECT_FALSE(buffer.Seek(0, 8));
152 buffer.GetCurrentOffset(&byte_offset, &bit_offset);
153 EXPECT_EQ(0u, byte_offset);
154 EXPECT_EQ(7u, bit_offset);
155 // Byte offsets are [0,length]. At byte offset length, the bit offset must be
156 // 0.
157 EXPECT_TRUE(buffer.Seek(0, 0));
158 EXPECT_TRUE(buffer.Seek(2, 4));
159 buffer.GetCurrentOffset(&byte_offset, &bit_offset);
160 EXPECT_EQ(2u, byte_offset);
161 EXPECT_EQ(4u, bit_offset);
162 EXPECT_TRUE(buffer.Seek(4, 0));
163 EXPECT_FALSE(buffer.Seek(5, 0));
164 buffer.GetCurrentOffset(&byte_offset, &bit_offset);
165 EXPECT_EQ(4u, byte_offset);
166 EXPECT_EQ(0u, bit_offset);
167 EXPECT_FALSE(buffer.Seek(4, 1));
168
169 // Disable death test on Android because it relies on fork() and doesn't play
170 // nicely.
171 #if defined(GTEST_HAS_DEATH_TEST)
172 #if !defined(WEBRTC_ANDROID)
173 // Passing a NULL out parameter is death.
174 EXPECT_DEATH(buffer.GetCurrentOffset(&byte_offset, NULL), "");
175 #endif
176 #endif
177 }
178
GolombEncoded(uint32_t val)179 uint64_t GolombEncoded(uint32_t val) {
180 val++;
181 uint32_t bit_counter = val;
182 uint64_t bit_count = 0;
183 while (bit_counter > 0) {
184 bit_count++;
185 bit_counter >>= 1;
186 }
187 return static_cast<uint64_t>(val) << (64 - (bit_count * 2 - 1));
188 }
189
TEST(BitBufferTest,GolombUint32Values)190 TEST(BitBufferTest, GolombUint32Values) {
191 ByteBuffer byteBuffer;
192 byteBuffer.Resize(16);
193 BitBuffer buffer(reinterpret_cast<const uint8_t*>(byteBuffer.Data()),
194 byteBuffer.Capacity());
195 // Test over the uint32_t range with a large enough step that the test doesn't
196 // take forever. Around 20,000 iterations should do.
197 const int kStep = std::numeric_limits<uint32_t>::max() / 20000;
198 for (uint32_t i = 0; i < std::numeric_limits<uint32_t>::max() - kStep;
199 i += kStep) {
200 uint64_t encoded_val = GolombEncoded(i);
201 byteBuffer.Clear();
202 byteBuffer.WriteUInt64(encoded_val);
203 uint32_t decoded_val;
204 EXPECT_TRUE(buffer.Seek(0, 0));
205 EXPECT_TRUE(buffer.ReadExponentialGolomb(&decoded_val));
206 EXPECT_EQ(i, decoded_val);
207 }
208 }
209
TEST(BitBufferTest,SignedGolombValues)210 TEST(BitBufferTest, SignedGolombValues) {
211 uint8_t golomb_bits[] = {
212 0x80, // 1
213 0x40, // 010
214 0x60, // 011
215 0x20, // 00100
216 0x38, // 00111
217 };
218 int32_t expected[] = {0, 1, -1, 2, -3};
219 for (size_t i = 0; i < sizeof(golomb_bits); ++i) {
220 BitBuffer buffer(&golomb_bits[i], 1);
221 int32_t decoded_val;
222 ASSERT_TRUE(buffer.ReadSignedExponentialGolomb(&decoded_val));
223 EXPECT_EQ(expected[i], decoded_val)
224 << "Mismatch in expected/decoded value for golomb_bits[" << i
225 << "]: " << static_cast<int>(golomb_bits[i]);
226 }
227 }
228
TEST(BitBufferTest,NoGolombOverread)229 TEST(BitBufferTest, NoGolombOverread) {
230 const uint8_t bytes[] = {0x00, 0xFF, 0xFF};
231 // Make sure the bit buffer correctly enforces byte length on golomb reads.
232 // If it didn't, the above buffer would be valid at 3 bytes.
233 BitBuffer buffer(bytes, 1);
234 uint32_t decoded_val;
235 EXPECT_FALSE(buffer.ReadExponentialGolomb(&decoded_val));
236
237 BitBuffer longer_buffer(bytes, 2);
238 EXPECT_FALSE(longer_buffer.ReadExponentialGolomb(&decoded_val));
239
240 BitBuffer longest_buffer(bytes, 3);
241 EXPECT_TRUE(longest_buffer.ReadExponentialGolomb(&decoded_val));
242 // Golomb should have read 9 bits, so 0x01FF, and since it is golomb, the
243 // result is 0x01FF - 1 = 0x01FE.
244 EXPECT_EQ(0x01FEu, decoded_val);
245 }
246
TEST(BitBufferWriterTest,SymmetricReadWrite)247 TEST(BitBufferWriterTest, SymmetricReadWrite) {
248 uint8_t bytes[16] = {0};
249 BitBufferWriter buffer(bytes, 4);
250
251 // Write some bit data at various sizes.
252 EXPECT_TRUE(buffer.WriteBits(0x2u, 3));
253 EXPECT_TRUE(buffer.WriteBits(0x1u, 2));
254 EXPECT_TRUE(buffer.WriteBits(0x53u, 7));
255 EXPECT_TRUE(buffer.WriteBits(0x0u, 2));
256 EXPECT_TRUE(buffer.WriteBits(0x1u, 1));
257 EXPECT_TRUE(buffer.WriteBits(0x1ABCDu, 17));
258 // That should be all that fits in the buffer.
259 EXPECT_FALSE(buffer.WriteBits(1, 1));
260
261 EXPECT_TRUE(buffer.Seek(0, 0));
262 uint32_t val;
263 EXPECT_TRUE(buffer.ReadBits(&val, 3));
264 EXPECT_EQ(0x2u, val);
265 EXPECT_TRUE(buffer.ReadBits(&val, 2));
266 EXPECT_EQ(0x1u, val);
267 EXPECT_TRUE(buffer.ReadBits(&val, 7));
268 EXPECT_EQ(0x53u, val);
269 EXPECT_TRUE(buffer.ReadBits(&val, 2));
270 EXPECT_EQ(0x0u, val);
271 EXPECT_TRUE(buffer.ReadBits(&val, 1));
272 EXPECT_EQ(0x1u, val);
273 EXPECT_TRUE(buffer.ReadBits(&val, 17));
274 EXPECT_EQ(0x1ABCDu, val);
275 // And there should be nothing left.
276 EXPECT_FALSE(buffer.ReadBits(&val, 1));
277 }
278
TEST(BitBufferWriterTest,SymmetricBytesMisaligned)279 TEST(BitBufferWriterTest, SymmetricBytesMisaligned) {
280 uint8_t bytes[16] = {0};
281 BitBufferWriter buffer(bytes, 16);
282
283 // Offset 3, to get things misaligned.
284 EXPECT_TRUE(buffer.ConsumeBits(3));
285 EXPECT_TRUE(buffer.WriteUInt8(0x12u));
286 EXPECT_TRUE(buffer.WriteUInt16(0x3456u));
287 EXPECT_TRUE(buffer.WriteUInt32(0x789ABCDEu));
288
289 buffer.Seek(0, 3);
290 uint8_t val8;
291 uint16_t val16;
292 uint32_t val32;
293 EXPECT_TRUE(buffer.ReadUInt8(&val8));
294 EXPECT_EQ(0x12u, val8);
295 EXPECT_TRUE(buffer.ReadUInt16(&val16));
296 EXPECT_EQ(0x3456u, val16);
297 EXPECT_TRUE(buffer.ReadUInt32(&val32));
298 EXPECT_EQ(0x789ABCDEu, val32);
299 }
300
TEST(BitBufferWriterTest,SymmetricGolomb)301 TEST(BitBufferWriterTest, SymmetricGolomb) {
302 char test_string[] = "my precious";
303 uint8_t bytes[64] = {0};
304 BitBufferWriter buffer(bytes, 64);
305 for (size_t i = 0; i < arraysize(test_string); ++i) {
306 EXPECT_TRUE(buffer.WriteExponentialGolomb(test_string[i]));
307 }
308 buffer.Seek(0, 0);
309 for (size_t i = 0; i < arraysize(test_string); ++i) {
310 uint32_t val;
311 EXPECT_TRUE(buffer.ReadExponentialGolomb(&val));
312 EXPECT_LE(val, std::numeric_limits<uint8_t>::max());
313 EXPECT_EQ(test_string[i], static_cast<char>(val));
314 }
315 }
316
TEST(BitBufferWriterTest,WriteClearsBits)317 TEST(BitBufferWriterTest, WriteClearsBits) {
318 uint8_t bytes[] = {0xFF, 0xFF};
319 BitBufferWriter buffer(bytes, 2);
320 EXPECT_TRUE(buffer.ConsumeBits(3));
321 EXPECT_TRUE(buffer.WriteBits(0, 1));
322 EXPECT_EQ(0xEFu, bytes[0]);
323 EXPECT_TRUE(buffer.WriteBits(0, 3));
324 EXPECT_EQ(0xE1u, bytes[0]);
325 EXPECT_TRUE(buffer.WriteBits(0, 2));
326 EXPECT_EQ(0xE0u, bytes[0]);
327 EXPECT_EQ(0x7F, bytes[1]);
328 }
329
330 } // namespace rtc
331