Use UsbIds::GetVendorName to retrieve USB vendor name.
[chromium-blink-merge.git] / media / base / bit_reader_unittest.cc
blob88431ef1ab2a040cbedbb02d50e0e47784f01ff4
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"
9 namespace media {
11 static void SetBit(uint8* buf, size_t size, size_t bit_pos) {
12 size_t byte_pos = bit_pos / 8;
13 bit_pos -= byte_pos * 8;
14 DCHECK_LT(byte_pos, size);
15 buf[byte_pos] |= (1 << (7 - bit_pos));
18 TEST(BitReaderTest, NormalOperationTest) {
19 uint8 value8;
20 uint64 value64;
21 // 0101 0101 1001 1001 repeats 4 times
22 uint8 buffer[] = {0x55, 0x99, 0x55, 0x99, 0x55, 0x99, 0x55, 0x99};
23 BitReader reader1(buffer, 6); // Initialize with 6 bytes only
25 EXPECT_TRUE(reader1.ReadBits(1, &value8));
26 EXPECT_EQ(value8, 0);
27 EXPECT_TRUE(reader1.ReadBits(8, &value8));
28 EXPECT_EQ(value8, 0xab); // 1010 1011
29 EXPECT_TRUE(reader1.ReadBits(7, &value64));
30 EXPECT_TRUE(reader1.ReadBits(32, &value64));
31 EXPECT_EQ(value64, 0x55995599u);
32 EXPECT_FALSE(reader1.ReadBits(1, &value8));
33 value8 = 0xff;
34 EXPECT_TRUE(reader1.ReadBits(0, &value8));
35 EXPECT_EQ(value8, 0);
37 BitReader reader2(buffer, 8);
38 EXPECT_TRUE(reader2.ReadBits(64, &value64));
39 EXPECT_EQ(value64, 0x5599559955995599ull);
40 EXPECT_FALSE(reader2.ReadBits(1, &value8));
41 EXPECT_TRUE(reader2.ReadBits(0, &value8));
44 TEST(BitReaderTest, ReadBeyondEndTest) {
45 uint8 value8;
46 uint8 buffer[] = {0x12};
47 BitReader reader1(buffer, sizeof(buffer));
49 EXPECT_TRUE(reader1.ReadBits(4, &value8));
50 EXPECT_FALSE(reader1.ReadBits(5, &value8));
51 EXPECT_FALSE(reader1.ReadBits(1, &value8));
52 EXPECT_TRUE(reader1.ReadBits(0, &value8));
55 TEST(BitReaderTest, SkipBitsTest) {
56 uint8 value8;
57 uint8 buffer[] = { 0x0a, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
58 BitReader reader1(buffer, sizeof(buffer));
60 EXPECT_TRUE(reader1.SkipBits(2));
61 EXPECT_TRUE(reader1.ReadBits(3, &value8));
62 EXPECT_EQ(value8, 1);
63 EXPECT_TRUE(reader1.SkipBits(11));
64 EXPECT_TRUE(reader1.ReadBits(8, &value8));
65 EXPECT_EQ(value8, 3);
66 EXPECT_TRUE(reader1.SkipBits(76));
67 EXPECT_TRUE(reader1.ReadBits(4, &value8));
68 EXPECT_EQ(value8, 13);
69 EXPECT_FALSE(reader1.SkipBits(100));
70 EXPECT_TRUE(reader1.SkipBits(0));
71 EXPECT_FALSE(reader1.SkipBits(1));
74 TEST(BitReaderTest, VariableSkipBitsTest) {
75 uint8 buffer[256] = {0};
77 // The test alternates between ReadBits and SkipBits.
78 // The first number is the number of bits to read, the second one is the
79 // number of bits to skip. The number of bits to read was arbitrarily chosen
80 // while the number of bits to skip was chosen so as to cover from small skips
81 // to large skips.
82 const size_t pattern_read_skip[][2] = {
83 { 5, 17 },
84 { 4, 34 },
85 { 0, 44 },
86 { 3, 4 }, // Note: aligned read.
87 { 7, 7 }, // Note: both read&skip cross byte boundary.
88 { 17, 68 },
89 { 7, 102 },
90 { 9, 204 },
91 { 3, 408 } };
93 // Set bits to one only for the first and last bit of each read
94 // in the pattern.
95 size_t pos = 0;
96 for (size_t k = 0; k < arraysize(pattern_read_skip); ++k) {
97 const size_t read_bit_count = pattern_read_skip[k][0];
98 if (read_bit_count > 0) {
99 SetBit(buffer, sizeof(buffer), pos);
100 SetBit(buffer, sizeof(buffer), pos + read_bit_count - 1);
101 pos += read_bit_count;
103 pos += pattern_read_skip[k][1];
106 // Run the test.
107 BitReader bit_reader(buffer, sizeof(buffer));
108 EXPECT_EQ(bit_reader.bits_available(), static_cast<int>(sizeof(buffer) * 8));
109 for (size_t k = 0; k < arraysize(pattern_read_skip); ++k) {
110 const size_t read_bit_count = pattern_read_skip[k][0];
111 if (read_bit_count > 0) {
112 int value;
113 EXPECT_TRUE(bit_reader.ReadBits(read_bit_count, &value));
114 EXPECT_EQ(value, 1 | (1 << (read_bit_count - 1)));
116 EXPECT_TRUE(bit_reader.SkipBits(pattern_read_skip[k][1]));
120 TEST(BitReaderTest, BitsReadTest) {
121 int value;
122 bool flag;
123 uint8 buffer[] = { 0x0a, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
124 BitReader reader1(buffer, sizeof(buffer));
125 EXPECT_EQ(reader1.bits_available(), 120);
127 EXPECT_TRUE(reader1.SkipBits(2));
128 EXPECT_EQ(reader1.bits_read(), 2);
129 EXPECT_EQ(reader1.bits_available(), 118);
130 EXPECT_TRUE(reader1.ReadBits(3, &value));
131 EXPECT_EQ(reader1.bits_read(), 5);
132 EXPECT_EQ(reader1.bits_available(), 115);
133 EXPECT_TRUE(reader1.ReadFlag(&flag));
134 EXPECT_EQ(reader1.bits_read(), 6);
135 EXPECT_EQ(reader1.bits_available(), 114);
136 EXPECT_TRUE(reader1.SkipBits(76));
137 EXPECT_EQ(reader1.bits_read(), 82);
138 EXPECT_EQ(reader1.bits_available(), 38);
141 } // namespace media