1 /*
2  * Copyright (c) 2022 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 
18 #include "data_buffer.h"
19 #include "distributed_camera_errno.h"
20 #include "distributed_hardware_log.h"
21 
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 class DataBufferTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32 
33     size_t capacity_ = 0;
34     std::shared_ptr<DataBuffer> dataBuffer_ = nullptr;
35 };
36 
SetUpTestCase(void)37 void DataBufferTest::SetUpTestCase(void)
38 {
39     DHLOGI("enter");
40 }
41 
TearDownTestCase(void)42 void DataBufferTest::TearDownTestCase(void)
43 {
44     DHLOGI("enter");
45 }
46 
SetUp(void)47 void DataBufferTest::SetUp(void)
48 {
49     DHLOGI("enter");
50     size_t capacity = 1;
51     dataBuffer_ = std::make_shared<DataBuffer>(capacity);
52 }
53 
TearDown(void)54 void DataBufferTest::TearDown(void)
55 {
56     DHLOGI("enter");
57     dataBuffer_ = nullptr;
58 }
59 
60 /**
61  * @tc.name: SetRange_001
62  * @tc.desc: Verify the SetRange function failed.
63  * @tc.type: FUNC
64  * @tc.require: Issue Number
65  */
66 HWTEST_F(DataBufferTest, SetRange_001, TestSize.Level1)
67 {
68     size_t offset = 0;
69     size_t size = 0;
70     int32_t ret = dataBuffer_->SetRange(offset, size);
71     EXPECT_EQ(DCAMERA_OK, ret);
72     offset = dataBuffer_->Offset() + 2;
73     size = dataBuffer_->Size();
74     ret = dataBuffer_->SetRange(offset, size);
75     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
76 }
77 
78 /**
79  * @tc.name: FindInt32_001
80  * @tc.desc: Verify the FindInt32 function failed.
81  * @tc.type: FUNC
82  * @tc.require: Issue Number
83  */
84 HWTEST_F(DataBufferTest, FindInt32_001, TestSize.Level1)
85 {
86     string name = "test";
87     int32_t value = 1;
88     dataBuffer_->SetInt32(name, value);
89     bool ret = dataBuffer_->FindInt32(name, value);
90     EXPECT_EQ(true, ret);
91     name = "test1";
92     ret = dataBuffer_->FindInt32(name, value);
93     EXPECT_EQ(false, ret);
94 }
95 
96 /**
97  * @tc.name: FindInt64_001
98  * @tc.desc: Verify the FindInt64 function failed.
99  * @tc.type: FUNC
100  * @tc.require: Issue Number
101  */
102 HWTEST_F(DataBufferTest, FindInt62_001, TestSize.Level1)
103 {
104     string name = "test";
105     int64_t value = 1;
106     dataBuffer_->SetInt64(name, value);
107     bool ret = dataBuffer_->FindInt64(name, value);
108     EXPECT_EQ(true, ret);
109     name = "test1";
110     ret = dataBuffer_->FindInt64(name, value);
111     EXPECT_EQ(false, ret);
112 }
113 
114 /**
115  * @tc.name: FindString_001
116  * @tc.desc: Verify the FindString function failed.
117  * @tc.type: FUNC
118  * @tc.require: Issue Number
119  */
120 HWTEST_F(DataBufferTest, FindString_001, TestSize.Level1)
121 {
122     string name = "test";
123     string value = "test";
124     dataBuffer_->SetString(name, value);
125     bool ret = dataBuffer_->FindString(name, value);
126     EXPECT_EQ(true, ret);
127     name = "test1";
128     ret = dataBuffer_->FindString(name, value);
129     EXPECT_EQ(false, ret);
130 }
131 } // namespace DistributedHardware
132 } // namespace OHOS