1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/bit_reader.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(BitReaderTest
, NormalOperationTest
) {
14 // 0101 0101 1001 1001 repeats 4 times
15 uint8 buffer
[] = {0x55, 0x99, 0x55, 0x99, 0x55, 0x99, 0x55, 0x99};
16 BitReader
reader1(buffer
, 6); // Initialize with 6 bytes only
18 EXPECT_TRUE(reader1
.ReadBits(1, &value8
));
20 EXPECT_TRUE(reader1
.ReadBits(8, &value8
));
21 EXPECT_EQ(value8
, 0xab); // 1010 1011
22 EXPECT_TRUE(reader1
.ReadBits(7, &value64
));
23 EXPECT_TRUE(reader1
.ReadBits(32, &value64
));
24 EXPECT_EQ(value64
, 0x55995599u
);
25 EXPECT_FALSE(reader1
.ReadBits(1, &value8
));
27 EXPECT_TRUE(reader1
.ReadBits(0, &value8
));
30 BitReader
reader2(buffer
, 8);
31 EXPECT_TRUE(reader2
.ReadBits(64, &value64
));
32 EXPECT_EQ(value64
, 0x5599559955995599ull
);
33 EXPECT_FALSE(reader2
.ReadBits(1, &value8
));
34 EXPECT_TRUE(reader2
.ReadBits(0, &value8
));
37 TEST(BitReaderTest
, ReadBeyondEndTest
) {
39 uint8 buffer
[] = {0x12};
40 BitReader
reader1(buffer
, sizeof(buffer
));
42 EXPECT_TRUE(reader1
.ReadBits(4, &value8
));
43 EXPECT_FALSE(reader1
.ReadBits(5, &value8
));
44 EXPECT_FALSE(reader1
.ReadBits(1, &value8
));
45 EXPECT_TRUE(reader1
.ReadBits(0, &value8
));
48 TEST(BitReaderTest
, SkipBitsTest
) {
50 uint8 buffer
[] = { 0x0a, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
51 BitReader
reader1(buffer
, sizeof(buffer
));
53 EXPECT_TRUE(reader1
.SkipBits(2));
54 EXPECT_TRUE(reader1
.ReadBits(3, &value8
));
56 EXPECT_TRUE(reader1
.SkipBits(11));
57 EXPECT_TRUE(reader1
.ReadBits(8, &value8
));
59 EXPECT_TRUE(reader1
.SkipBits(76));
60 EXPECT_TRUE(reader1
.ReadBits(4, &value8
));
61 EXPECT_EQ(value8
, 13);
62 EXPECT_FALSE(reader1
.SkipBits(100));
63 EXPECT_TRUE(reader1
.SkipBits(0));
64 EXPECT_FALSE(reader1
.SkipBits(1));
67 TEST(BitReaderTest
, BitsReadTest
) {
70 uint8 buffer
[] = { 0x0a, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
71 BitReader
reader1(buffer
, sizeof(buffer
));
72 EXPECT_EQ(reader1
.bits_available(), 120);
74 EXPECT_TRUE(reader1
.SkipBits(2));
75 EXPECT_EQ(reader1
.bits_read(), 2);
76 EXPECT_EQ(reader1
.bits_available(), 118);
77 EXPECT_TRUE(reader1
.ReadBits(3, &value
));
78 EXPECT_EQ(reader1
.bits_read(), 5);
79 EXPECT_EQ(reader1
.bits_available(), 115);
80 EXPECT_TRUE(reader1
.ReadFlag(&flag
));
81 EXPECT_EQ(reader1
.bits_read(), 6);
82 EXPECT_EQ(reader1
.bits_available(), 114);
83 EXPECT_TRUE(reader1
.SkipBits(76));
84 EXPECT_EQ(reader1
.bits_read(), 82);
85 EXPECT_EQ(reader1
.bits_available(), 38);