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 "plugin/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
24using namespace testing::ext;
25
26namespace OHOS {
27namespace Media {
28namespace Test {
29using namespace Plugin;
30
31class TestBitReader : public ::testing::Test {
32public:
33    void SetUp() override
34    {
35    }
36
37    void TearDown() override
38    {
39    }
40    Ffmpeg::BitReader bitReader;
41};
42
43HWTEST_F(TestBitReader, test_read_bits, TestSize.Level1)
44{
45    const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
46    bitReader.Reset(data, sizeof(data));
47    uint32_t val = 0;
48    EXPECT_EQ(bitReader.ReadBits(4, val), true);
49    EXPECT_EQ(val, 0xf);
50
51    EXPECT_EQ(bitReader.ReadBits(8, val), true);
52    EXPECT_EQ(val, 0xf1);
53
54    EXPECT_EQ(bitReader.GetAvailableBits(), 20);
55    EXPECT_EQ(bitReader.ReadBits(20, val), true);
56    EXPECT_EQ(val, 0x12233);
57
58    EXPECT_EQ(bitReader.ReadBits(4, val), false);
59}
60
61HWTEST_F(TestBitReader, test_skip_bits, TestSize.Level1)
62{
63    const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
64    bitReader.Reset(data, sizeof(data));
65    uint32_t val = 0;
66    EXPECT_EQ(bitReader.ReadBits(4, val), true);
67    EXPECT_EQ(val, 0xf);
68    bitReader.SkipBits(4);
69    EXPECT_EQ(bitReader.ReadBits(8, val), true);
70    EXPECT_EQ(val, 0x11);
71    bitReader.SkipBits(8);
72    EXPECT_EQ(bitReader.ReadBits(8, val), true);
73    EXPECT_EQ(val, 0x33);
74}
75
76HWTEST_F(TestBitReader, test_show_bits, TestSize.Level1)
77{
78    const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
79    bitReader.Reset(data, sizeof(data));
80    uint32_t val = 0;
81    EXPECT_EQ(bitReader.ReadBits(4, val), true);
82    EXPECT_EQ(val, 0xf);
83    EXPECT_EQ(bitReader.PeekBits(4, val), true);
84    EXPECT_EQ(val, 0xf);
85    bitReader.SkipBits(4);
86    EXPECT_EQ(bitReader.PeekBits(8, val), true);
87    EXPECT_EQ(val, 0x11);
88    EXPECT_EQ(bitReader.ReadBits(8, val), true);
89    EXPECT_EQ(val, 0x11);
90}
91
92HWTEST_F(TestBitReader, test_seek_bits, TestSize.Level1)
93{
94    const uint8_t data[] = {0xff, 0x11, 0x22, 0x33};
95    bitReader.Reset(data, sizeof(data));
96    uint32_t val = 0;
97    EXPECT_EQ(bitReader.ReadBits(4, val), true);
98    EXPECT_EQ(val, 0xf);
99    bitReader.SkipBits(20);
100    EXPECT_EQ(bitReader.ReadBits(8, val), true);
101    EXPECT_EQ(val, 0x33);
102    EXPECT_EQ(bitReader.GetAvailableBits(), 0);
103    EXPECT_EQ(bitReader.SeekTo(0), true);
104    EXPECT_EQ(bitReader.ReadBits(4, val), true);
105    EXPECT_EQ(val, 0xf);
106}
107} // namespace Test
108} // namespace Media
109} // namespace OHOS
110