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