• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "avformat_unit_test.h"
17 #include <cmath>
18 #include "gtest/gtest.h"
19 #include "avcodec_errors.h"
20 #include "securec.h"
21 
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::MediaAVCodec;
25 using namespace testing::ext;
26 
27 constexpr float EPSINON_FLOAT = 0.0001;
28 constexpr double EPSINON_DOUBLE = 0.0001;
29 
SetUpTestCase(void)30 void AVFormatUnitTest::SetUpTestCase(void) {}
31 
TearDownTestCase(void)32 void AVFormatUnitTest::TearDownTestCase(void) {}
33 
SetUp(void)34 void AVFormatUnitTest::SetUp(void)
35 {
36     format_ = FormatMockFactory::CreateFormat();
37     ASSERT_NE(nullptr, format_);
38 }
39 
TearDown(void)40 void AVFormatUnitTest::TearDown(void)
41 {
42     if (format_ != nullptr) {
43         format_->Destroy();
44     }
45 }
46 
47 /**
48  * @tc.name: Format_Value_001
49  * @tc.desc: format put and get value
50  * @tc.type: FUNC
51  * @tc.require: issueI5OX06 issueI5P8N0
52  */
53 HWTEST_F(AVFormatUnitTest, Format_Value_001, TestSize.Level0)
54 {
55     const std::string_view intKey = "IntKey";
56     const std::string_view longKey = "LongKey";
57     const std::string_view floatKey = "FloatKey";
58     const std::string_view doubleKey = "DoubleKey";
59     const std::string_view stringKey = "StringKey";
60 
61     int32_t intValue = 1;
62     int64_t longValue = 1;
63     float floatValue = 1.0;
64     double doubleValue = 1.0;
65     const std::string stringValue = "StringValue";
66 
67     int32_t getIntValue = 0;
68     int64_t getLongValue = 0;
69     float getFloatValue = 0.0;
70     double getDoubleValue = 0.0;
71     std::string getStringValue = "";
72 
73     EXPECT_TRUE(format_->PutIntValue(intKey, intValue));
74     EXPECT_TRUE(format_->GetIntValue(intKey, getIntValue));
75     EXPECT_TRUE(intValue == getIntValue);
76     EXPECT_FALSE(format_->GetLongValue(intKey, getLongValue));
77 
78     EXPECT_TRUE(format_->PutLongValue(longKey, intValue));
79     EXPECT_TRUE(format_->GetLongValue(longKey, getLongValue));
80     EXPECT_TRUE(longValue == getLongValue);
81     EXPECT_FALSE(format_->GetIntValue(longKey, getIntValue));
82 
83     EXPECT_TRUE(format_->PutFloatValue(floatKey, floatValue));
84     EXPECT_TRUE(format_->GetFloatValue(floatKey, getFloatValue));
85     EXPECT_TRUE(fabs(floatValue - getFloatValue) < EPSINON_FLOAT);
86     EXPECT_FALSE(format_->GetDoubleValue(floatKey, getDoubleValue));
87 
88     EXPECT_TRUE(format_->PutDoubleValue(doubleKey, doubleValue));
89     EXPECT_TRUE(format_->GetDoubleValue(doubleKey, getDoubleValue));
90     EXPECT_TRUE(fabs(doubleValue - getDoubleValue) < EPSINON_DOUBLE);
91     EXPECT_FALSE(format_->GetFloatValue(doubleKey, getFloatValue));
92 
93     EXPECT_TRUE(format_->PutStringValue(stringKey, stringValue.c_str()));
94     EXPECT_TRUE(format_->GetStringValue(stringKey, getStringValue));
95     EXPECT_TRUE(stringValue == getStringValue);
96 }
97 
98 /**
99  * @tc.name: Format_Buffer_001
100  * @tc.desc: format put and get buffer
101  * @tc.type: FUNC
102  * @tc.require: issueI5OWXY issueI5OXCD
103  */
104 HWTEST_F(AVFormatUnitTest, Format_Buffer_001, TestSize.Level0)
105 {
106     constexpr size_t size = 3;
107     const std::string_view key = "BufferKey";
108     uint8_t buffer[size] = {'a', 'b', 'b'};
109 
110     EXPECT_TRUE(format_->PutBuffer(key, buffer, size));
111     uint8_t *getBuffer;
112     size_t getSize;
113     EXPECT_TRUE(format_->GetBuffer(key, &getBuffer, getSize));
114     EXPECT_TRUE(size == getSize);
115     for (int32_t i = 0; i < size; i++) {
116         EXPECT_TRUE(buffer[i] == getBuffer[i]);
117     }
118 
119     std::string getString;
120     EXPECT_FALSE(format_->GetStringValue(key, getString));
121 }