1 /*
2  * Copyright (c) 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 
18 #include "utils/data.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 class DataTest : public testing::Test {
27 public:
28     static void SetUpTestCase();
29     static void TearDownTestCase();
30     void SetUp() override;
31     void TearDown() override;
32 };
33 
SetUpTestCase()34 void DataTest::SetUpTestCase() {}
TearDownTestCase()35 void DataTest::TearDownTestCase() {}
SetUp()36 void DataTest::SetUp() {}
TearDown()37 void DataTest::TearDown() {}
38 
39 /*
40  * @tc.name: CreateAndDestory001
41  * @tc.desc:Verify the CreateAndDestory001 sub function.
42  * @tc.type: FUNC
43  * @tc.require: I71KWQ
44  */
45 HWTEST_F(DataTest, CreateAndDestory001, TestSize.Level1)
46 {
47     auto data = std::make_unique<Data>();
48     ASSERT_TRUE(data != nullptr);
49 }
50 
51 /*
52  * @tc.name: BuildFromMallocTest001
53  * @tc.desc:Verify the BuildFromMallocTest001 sub function.
54  * @tc.type: FUNC
55  * @tc.require: I71KWQ
56  */
57 HWTEST_F(DataTest, BuildFromMallocTest001, TestSize.Level1)
58 {
59     auto data = std::make_unique<Data>();
60     ASSERT_TRUE(data != nullptr);
61     auto intData = new int();
62     EXPECT_TRUE(data->BuildFromMalloc(intData, sizeof(intData)));
63 }
64 
65 /*
66  * @tc.name: BuildWithCopyTest001
67  * @tc.desc:Verify the BuildWithCopyTest001 sub function.
68  * @tc.type: FUNC
69  * @tc.require: I71KWQ
70  */
71 HWTEST_F(DataTest, BuildWithCopyTest001, TestSize.Level1)
72 {
73     auto data = std::make_unique<Data>();
74     ASSERT_TRUE(data != nullptr);
75     auto intData = std::make_unique<int>();
76     EXPECT_TRUE(data->BuildWithCopy(intData.get(), sizeof(*intData.get())));
77 }
78 
79 /*
80  * @tc.name: BuildWithoutCopyTest001
81  * @tc.desc:Verify the BuildWithoutCopyTest001 sub function.
82  * @tc.type: FUNC
83  * @tc.require: I71KWQ
84  */
85 HWTEST_F(DataTest, BuildWithoutCopyTest001, TestSize.Level1)
86 {
87     auto data = std::make_unique<Data>();
88     ASSERT_TRUE(data != nullptr);
89     auto intData = std::make_unique<int>();
90     EXPECT_TRUE(data->BuildWithoutCopy(intData.get(), sizeof(*intData.get())));
91 }
92 
93 /*
94  * @tc.name: BuildUninitializedTest001
95  * @tc.desc:Verify the BuildUninitializedTest001 sub function.
96  * @tc.type: FUNC
97  * @tc.require: I71KWQ
98  */
99 HWTEST_F(DataTest, BuildUninitializedTest001, TestSize.Level1)
100 {
101     auto data = std::make_unique<Data>();
102     ASSERT_TRUE(data != nullptr);
103     auto intData = std::make_unique<int>();
104     EXPECT_TRUE(data->BuildUninitialized(sizeof(*intData.get())));
105 }
106 
107 /*
108  * @tc.name: WritableDataTest001
109  * @tc.desc:Verify the WritableDataTest001 sub function.
110  * @tc.type: FUNC
111  * @tc.require: I71KWQ
112  */
113 HWTEST_F(DataTest, WritableDataTest001, TestSize.Level1)
114 {
115     auto data = std::make_unique<Data>();
116     ASSERT_TRUE(data != nullptr);
117     EXPECT_FALSE(data->WritableData());
118 }
119 
120 /*
121  * @tc.name: DataGetSizeTest001
122  * @tc.desc:Verify the DataGetSizeTest001 sub function.
123  * @tc.type: FUNC
124  * @tc.require: I71KWQ
125  */
126 HWTEST_F(DataTest, DataGetSizeTest001, TestSize.Level1)
127 {
128     auto data = std::make_unique<Data>();
129     ASSERT_TRUE(data != nullptr);
130     auto intData = std::make_unique<int>();
131     data->BuildWithCopy(intData.get(), sizeof(*intData.get()));
132     EXPECT_TRUE(sizeof(*intData.get()) == data->GetSize());
133 }
134 
135 /*
136  * @tc.name: DataGetDataTest001
137  * @tc.desc:Verify the DataGetDataTest001 sub function.
138  * @tc.type: FUNC
139  * @tc.require: I71KWQ
140  */
141 HWTEST_F(DataTest, DataGetDataTest001, TestSize.Level1)
142 {
143     auto data = std::make_unique<Data>();
144     ASSERT_TRUE(data != nullptr);
145     auto intData = std::make_unique<int>(10);
146     data->BuildWithCopy(intData.get(), sizeof(*intData.get()));
147     EXPECT_TRUE(*intData == *((int*)data->GetData()));
148 }
149 } // namespace Drawing
150 } // namespace Rosen
151 } // namespace OHOS
152