1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gtest/gtest.h"
17 #include "common/any.h"
18 #define private public
19 #define protected public
20 #define UNIT_TEST 1
21
22 #include "plugin/plugins/ffmpeg_adapter/utils/bit_reader.h"
23
24 namespace OHOS {
25 namespace Media {
26 namespace Test {
27 using namespace Plugin;
28
29 class TestBitReader : public ::testing::Test {
30 public:
SetUp()31 void SetUp() override
32 {
33 }
34
TearDown()35 void TearDown() override
36 {
37 }
38 Ffmpeg::BitReader bitReader;
39 };
40
TEST_F(TestBitReader,test_read_bits)41 TEST_F(TestBitReader, test_read_bits)
42 {
43 const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
44 bitReader.Reset(data, sizeof(data));
45 uint32_t val = 0;
46 EXPECT_EQ(bitReader.ReadBits(4, val), true);
47 EXPECT_EQ(val, 0xf);
48
49 EXPECT_EQ(bitReader.ReadBits(8, val), true);
50 EXPECT_EQ(val, 0xf1);
51
52 EXPECT_EQ(bitReader.GetAvailableBits(), 20);
53 EXPECT_EQ(bitReader.ReadBits(20, val), true);
54 EXPECT_EQ(val, 0x12233);
55
56 EXPECT_EQ(bitReader.ReadBits(4, val), false);
57 }
58
TEST_F(TestBitReader,test_skip_bits)59 TEST_F(TestBitReader, test_skip_bits)
60 {
61 const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
62 bitReader.Reset(data, sizeof(data));
63 uint32_t val = 0;
64 EXPECT_EQ(bitReader.ReadBits(4, val), true);
65 EXPECT_EQ(val, 0xf);
66 bitReader.SkipBits(4);
67 EXPECT_EQ(bitReader.ReadBits(8, val), true);
68 EXPECT_EQ(val, 0x11);
69 bitReader.SkipBits(8);
70 EXPECT_EQ(bitReader.ReadBits(8, val), true);
71 EXPECT_EQ(val, 0x33);
72 }
73
TEST_F(TestBitReader,test_show_bits)74 TEST_F(TestBitReader, test_show_bits)
75 {
76 const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
77 bitReader.Reset(data, sizeof(data));
78 uint32_t val = 0;
79 EXPECT_EQ(bitReader.ReadBits(4, val), true);
80 EXPECT_EQ(val, 0xf);
81 EXPECT_EQ(bitReader.PeekBits(4, val), true);
82 EXPECT_EQ(val, 0xf);
83 bitReader.SkipBits(4);
84 EXPECT_EQ(bitReader.PeekBits(8, val), true);
85 EXPECT_EQ(val, 0x11);
86 EXPECT_EQ(bitReader.ReadBits(8, val), true);
87 EXPECT_EQ(val, 0x11);
88 }
89
TEST_F(TestBitReader,test_seek_bits)90 TEST_F(TestBitReader, test_seek_bits)
91 {
92 const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
93 bitReader.Reset(data, sizeof(data));
94 uint32_t val = 0;
95 EXPECT_EQ(bitReader.ReadBits(4, val), true);
96 EXPECT_EQ(val, 0xf);
97 bitReader.SkipBits(20);
98 EXPECT_EQ(bitReader.ReadBits(8, val), true);
99 EXPECT_EQ(val, 0x33);
100 EXPECT_EQ(bitReader.GetAvailableBits(), 0);
101 EXPECT_EQ(bitReader.SeekTo(0), true);
102 EXPECT_EQ(bitReader.ReadBits(4, val), true);
103 EXPECT_EQ(val, 0xf);
104 }
105 } // namespace Test
106 } // namespace Media
107 } // namespace OHOS
108