Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "media/base/bit_reader.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace media {
     10 
     11 TEST(BitReaderTest, NormalOperationTest) {
     12   uint8 value8;
     13   uint64 value64;
     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
     17 
     18   EXPECT_TRUE(reader1.ReadBits(1, &value8));
     19   EXPECT_EQ(value8, 0);
     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));
     26   value8 = 0xff;
     27   EXPECT_TRUE(reader1.ReadBits(0, &value8));
     28   EXPECT_EQ(value8, 0);
     29 
     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));
     35 }
     36 
     37 TEST(BitReaderTest, ReadBeyondEndTest) {
     38   uint8 value8;
     39   uint8 buffer[] = {0x12};
     40   BitReader reader1(buffer, sizeof(buffer));
     41 
     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));
     46 }
     47 
     48 TEST(BitReaderTest, SkipBitsTest) {
     49   uint8 value8;
     50   uint8 buffer[] = { 0x0a, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
     51   BitReader reader1(buffer, sizeof(buffer));
     52 
     53   EXPECT_TRUE(reader1.SkipBits(2));
     54   EXPECT_TRUE(reader1.ReadBits(3, &value8));
     55   EXPECT_EQ(value8, 1);
     56   EXPECT_TRUE(reader1.SkipBits(11));
     57   EXPECT_TRUE(reader1.ReadBits(8, &value8));
     58   EXPECT_EQ(value8, 3);
     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));
     65 }
     66 
     67 }  // namespace media
     68