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 "cache_data.h"
17
18 #include <cerrno>
19
20 #include <gtest/gtest.h>
21 #include "platform/common/rs_system_properties.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 class CacheDataTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34 };
35
SetUpTestCase()36 void CacheDataTest::SetUpTestCase() {}
TearDownTestCase()37 void CacheDataTest::TearDownTestCase() {}
SetUp()38 void CacheDataTest::SetUp() {}
TearDown()39 void CacheDataTest::TearDown() {}
40
41 /**
42 * @tc.name: cachedata_init_test_001
43 * @tc.desc: Verify the initialization function of shader cache
44 * @tc.type: FUNC
45 * @tc.require:
46 * @tc.author:
47 */
48 HWTEST_F(CacheDataTest, cachedata_init_test_001, TestSize.Level1)
49 {
50 #ifdef ACE_ENABLE_GL
51 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
52 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
53 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
54 return;
55 }
56 GTEST_LOG_(INFO) << "CacheDataTest cachedata_init_test_001 start";
57 /**
58 * @tc.steps: step1. initialize a cachedata
59 */
60 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, "TestDir");
61 /**
62 * @tc.steps: step2. test the result of initialization function
63 */
64 EXPECT_NE(nullptr, cacheData.get());
65 #endif
66 }
67
68 /**
69 * @tc.name: serialized_size_test_001
70 * @tc.desc: Verify the function that computes the file size after serialization
71 * @tc.type: FUNC
72 * @tc.require:
73 * @tc.author:
74 */
75 HWTEST_F(CacheDataTest, serialized_size_test_001, TestSize.Level1)
76 {
77 #ifdef ACE_ENABLE_GL
78 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
79 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
80 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
81 return;
82 }
83 GTEST_LOG_(INFO) << "CacheDataTest serialized_size_test_001 start";
84 /**
85 * @tc.steps: step1. initialize a cachedata
86 */
87 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, "TestDir");
88 struct tempHeader {
89 size_t numShaders_;
90 };
91 /**
92 * @tc.steps: step2. tests the size computation
93 */
94 size_t serSize = cacheData->SerializedSize();
95 EXPECT_EQ(sizeof(tempHeader), serSize);
96 #endif
97 }
98
99 /**
100 * @tc.name: get_data_test_001
101 * @tc.desc: Verify the function that gets a k-v pair of data
102 * @tc.type: FUNC
103 * @tc.require:
104 * @tc.author:
105 */
106 HWTEST_F(CacheDataTest, get_data_test_001, TestSize.Level1)
107 {
108 #ifdef ACE_ENABLE_GL
109 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
110 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
111 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
112 return;
113 }
114 GTEST_LOG_(INFO) << "CacheDataTest get_data_test_001 start";
115 /**
116 * @tc.steps: step1. initialize a cachedata
117 */
118 std::string testFileDir = "test file dir for cachedata";
119 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
120 /**
121 * @tc.steps: step2. test the data grabbing function
122 */
123 auto [errorCode, sizeGet] = cacheData->Get(nullptr, 1, nullptr, 1);
124 EXPECT_EQ(0, sizeGet);
125 #endif
126 }
127
128 /**
129 * @tc.name: serialization_test_001
130 * @tc.desc: Verify the serialization function
131 * @tc.type: FUNC
132 * @tc.require:
133 * @tc.author:
134 */
135 HWTEST_F(CacheDataTest, serialization_test_001, TestSize.Level1)
136 {
137 #ifdef ACE_ENABLE_GL
138 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
139 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
140 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
141 return;
142 }
143 GTEST_LOG_(INFO) << "CacheDataTest serialization_test_001 start";
144 /**
145 * @tc.steps: step1. initialize a cachedata
146 */
147 std::string testFileDir = "test file dir for cachedata";
148 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
149 /**
150 * @tc.steps: step2. test the serialization function
151 */
152 uint8_t *tempBuffer = new uint8_t[sizeof(size_t)]();
153 int retSerialized = cacheData->Serialize(tempBuffer, sizeof(size_t));
154 EXPECT_NE(retSerialized, -EINVAL);
155 delete[] tempBuffer;
156 #endif
157 }
158
159 /**
160 * @tc.name: deserialization_test_001
161 * @tc.desc: Verify the deserialization function
162 * @tc.type: FUNC
163 * @tc.require:
164 * @tc.author:
165 */
166 HWTEST_F(CacheDataTest, deserialization_test_001, TestSize.Level1)
167 {
168 #ifdef ACE_ENABLE_GL
169 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
170 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
171 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
172 return;
173 }
174 GTEST_LOG_(INFO) << "CacheDataTest deserialization_test_001 start";
175 /**
176 * @tc.steps: step1. initialize a cachedata
177 */
178 std::string testFileDir = "test file dir for cachedata";
179 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
180 /**
181 * @tc.steps: step2. test the deserialization function
182 */
183 uint8_t *tempBuffer = new uint8_t[sizeof(size_t)]();
184 int retDeserialized = cacheData->DeSerialize(tempBuffer, sizeof(size_t));
185 EXPECT_NE(retDeserialized, -EINVAL);
186 delete[] tempBuffer;
187 #endif
188 }
189
190 /**
191 * @tc.name: write_data_test_001
192 * @tc.desc: Verify the rewrite and get function with overloaded data
193 * @tc.type: FUNC
194 * @tc.require:
195 * @tc.author:
196 */
197 HWTEST_F(CacheDataTest, write_data_test_001, TestSize.Level1)
198 {
199 #ifdef ACE_ENABLE_GL
200 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
201 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
202 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
203 return;
204 }
205 GTEST_LOG_(INFO) << "CacheDataTest write_data_test_001 start";
206 /**
207 * @tc.steps: step1. initialize a cachedata
208 */
209 std::string testFileDir = "test file dir for cachedata";
210 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(2, 2, 4, testFileDir);
211 /**
212 * @tc.steps: step2. test the rewrite and get function
213 */
214 uint8_t *tempBuffer = new uint8_t[8]();
215 cacheData->Rewrite(tempBuffer, 8, tempBuffer, 8);
216 auto [errorCode, sizeGet] = cacheData->Get(tempBuffer, 8, tempBuffer, 8);
217 EXPECT_EQ(0, sizeGet);
218 delete[] tempBuffer;
219 #endif
220 }
221
222 /**
223 * @tc.name: clean_data_test_001
224 * @tc.desc: Verify the clean function
225 * @tc.type: FUNC
226 * @tc.require:
227 * @tc.author:
228 */
229 HWTEST_F(CacheDataTest, clean_data_test_001, TestSize.Level1)
230 {
231 #ifdef ACE_ENABLE_GL
232 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
233 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
234 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
235 return;
236 }
237 GTEST_LOG_(INFO) << "CacheDataTest clean_data_test_001 start";
238 /**
239 * @tc.steps: step1. initialize a cachedata
240 */
241 std::string testFileDir = "test file dir for cachedata";
242 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(8, 8, 15, testFileDir);
243 /**
244 * @tc.steps: step2. test the clean function
245 */
246 uint8_t *tempBuffer = new uint8_t[4]();
247 const char *testKey1 = "Key1";
248 const char *testKey2 = "Key2";
249 const char *testKey3 = "Key3";
250 const char *testKey4 = "Key4";
251 const char *testValue = "aVal";
252 cacheData->Rewrite(testKey1, 4, testValue, 4);
253 cacheData->Rewrite(testKey2, 4, testValue, 4);
254 cacheData->Rewrite(testKey3, 4, testValue, 4);
255 cacheData->Rewrite(testKey4, 4, testValue, 4);
256 auto [errorCode, sizeGet] = cacheData->Get(testKey4, 4, tempBuffer, 4);
257 EXPECT_EQ(4, sizeGet);
258 delete[] tempBuffer;
259 #endif
260 }
261
262 /**
263 * @tc.name: clean_data_test_002
264 * @tc.desc: Verify a vain clean function
265 * @tc.type: FUNC
266 * @tc.require:
267 * @tc.author:
268 */
269 HWTEST_F(CacheDataTest, clean_data_test_002, TestSize.Level1)
270 {
271 #ifdef ACE_ENABLE_GL
272 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN ||
273 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) {
274 GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
275 return;
276 }
277 GTEST_LOG_(INFO) << "CacheDataTest clean_data_test_002 start";
278 /**
279 * @tc.steps: step1. initialize a cachedata
280 */
281 std::string testFileDir = "test file dir for cachedata";
282 std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
283 /**
284 * @tc.steps: step2. test the clean function that skips a vain clean
285 */
286 uint8_t *tempBuffer = new uint8_t[4]();
287 const char *testKey1 = "Key1";
288 const char *testKey2 = "Key2";
289 const char *testValue = "aVal";
290 cacheData->Rewrite(testKey1, 4, testValue, 4);
291 cacheData->Rewrite(testKey2, 4, testValue, 4);
292 auto [errorCode, sizeGet] = cacheData->Get(testKey2, 4, tempBuffer, 4);
293 EXPECT_EQ(0, sizeGet);
294 delete[] tempBuffer;
295 #endif
296 }
297 } // namespace Rosen
298 } // namespace OHOS