• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::IsUseVulkan()) {
52         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
53         return;
54     }
55     GTEST_LOG_(INFO) << "CacheDataTest cachedata_init_test_001 start";
56     /**
57      * @tc.steps: step1. initialize a cachedata
58      */
59     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, "TestDir");
60     /**
61      * @tc.steps: step2. test the result of initialization function
62      */
63     EXPECT_NE(nullptr, cacheData.get());
64 #endif
65 }
66 
67 /**
68  * @tc.name: serialized_size_test_001
69  * @tc.desc: Verify the function that computes the file size after serialization
70  * @tc.type: FUNC
71  * @tc.require:
72  * @tc.author:
73  */
74 HWTEST_F(CacheDataTest, serialized_size_test_001, TestSize.Level1)
75 {
76 #ifdef ACE_ENABLE_GL
77     if (RSSystemProperties::IsUseVulkan()) {
78         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
79         return;
80     }
81     GTEST_LOG_(INFO) << "CacheDataTest serialized_size_test_001 start";
82     /**
83      * @tc.steps: step1. initialize a cachedata
84      */
85     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, "TestDir");
86     struct tempHeader {
87         size_t numShaders_;
88     };
89     /**
90      * @tc.steps: step2. tests the size computation
91      */
92     size_t serSize = cacheData->SerializedSize();
93     EXPECT_EQ(sizeof(tempHeader), serSize);
94 #endif
95 }
96 
97 /**
98  * @tc.name: get_data_test_001
99  * @tc.desc: Verify the function that gets a k-v pair of data
100  * @tc.type: FUNC
101  * @tc.require:
102  * @tc.author:
103  */
104 HWTEST_F(CacheDataTest, get_data_test_001, TestSize.Level1)
105 {
106 #ifdef ACE_ENABLE_GL
107     if (RSSystemProperties::IsUseVulkan()) {
108         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
109         return;
110     }
111     GTEST_LOG_(INFO) << "CacheDataTest get_data_test_001 start";
112     /**
113      * @tc.steps: step1. initialize a cachedata
114      */
115     std::string testFileDir = "test file dir for cachedata";
116     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
117     /**
118      * @tc.steps: step2. test the data grabbing function
119      */
120     auto [errorCode, sizeGet] = cacheData->Get(nullptr, 1, nullptr, 1);
121     EXPECT_EQ(0, sizeGet);
122 #endif
123 }
124 
125 /**
126  * @tc.name: serialization_test_001
127  * @tc.desc: Verify the serialization function
128  * @tc.type: FUNC
129  * @tc.require:
130  * @tc.author:
131  */
132 HWTEST_F(CacheDataTest, serialization_test_001, TestSize.Level1)
133 {
134 #ifdef ACE_ENABLE_GL
135     if (RSSystemProperties::IsUseVulkan()) {
136         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
137         return;
138     }
139     GTEST_LOG_(INFO) << "CacheDataTest serialization_test_001 start";
140     /**
141      * @tc.steps: step1. initialize a cachedata
142      */
143     std::string testFileDir = "test file dir for cachedata";
144     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
145     /**
146      * @tc.steps: step2. test the serialization function
147      */
148     uint8_t *tempBuffer = new uint8_t[sizeof(size_t)]();
149     int retSerialized = cacheData->Serialize(tempBuffer, sizeof(size_t));
150     EXPECT_NE(retSerialized, -EINVAL);
151     delete[] tempBuffer;
152 #endif
153 }
154 
155 /**
156  * @tc.name: deserialization_test_001
157  * @tc.desc: Verify the deserialization function
158  * @tc.type: FUNC
159  * @tc.require:
160  * @tc.author:
161  */
162 HWTEST_F(CacheDataTest, deserialization_test_001, TestSize.Level1)
163 {
164 #ifdef ACE_ENABLE_GL
165     if (RSSystemProperties::IsUseVulkan()) {
166         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
167         return;
168     }
169     GTEST_LOG_(INFO) << "CacheDataTest deserialization_test_001 start";
170     /**
171      * @tc.steps: step1. initialize a cachedata
172      */
173     std::string testFileDir = "test file dir for cachedata";
174     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
175     /**
176      * @tc.steps: step2. test the deserialization function
177      */
178     uint8_t *tempBuffer = new uint8_t[sizeof(size_t)]();
179     int retDeserialized = cacheData->DeSerialize(tempBuffer, sizeof(size_t));
180     EXPECT_NE(retDeserialized, -EINVAL);
181     delete[] tempBuffer;
182 #endif
183 }
184 
185 /**
186  * @tc.name: write_data_test_001
187  * @tc.desc: Verify the rewrite and get function with overloaded data
188  * @tc.type: FUNC
189  * @tc.require:
190  * @tc.author:
191  */
192 HWTEST_F(CacheDataTest, write_data_test_001, TestSize.Level1)
193 {
194 #ifdef ACE_ENABLE_GL
195     if (RSSystemProperties::IsUseVulkan()) {
196         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
197         return;
198     }
199     GTEST_LOG_(INFO) << "CacheDataTest write_data_test_001 start";
200     /**
201      * @tc.steps: step1. initialize a cachedata
202      */
203     std::string testFileDir = "test file dir for cachedata";
204     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(2, 2, 4, testFileDir);
205     /**
206      * @tc.steps: step2. test the rewrite and get function
207      */
208     uint8_t *tempBuffer = new uint8_t[8]();
209     cacheData->Rewrite(tempBuffer, 8, tempBuffer, 8);
210     auto [errorCode, sizeGet] = cacheData->Get(tempBuffer, 8, tempBuffer, 8);
211     EXPECT_EQ(0, sizeGet);
212     delete[] tempBuffer;
213 #endif
214 }
215 
216 /**
217  * @tc.name: clean_data_test_001
218  * @tc.desc: Verify the clean function
219  * @tc.type: FUNC
220  * @tc.require:
221  * @tc.author:
222  */
223 HWTEST_F(CacheDataTest, clean_data_test_001, TestSize.Level1)
224 {
225 #ifdef ACE_ENABLE_GL
226     if (RSSystemProperties::IsUseVulkan()) {
227         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
228         return;
229     }
230     GTEST_LOG_(INFO) << "CacheDataTest clean_data_test_001 start";
231     /**
232      * @tc.steps: step1. initialize a cachedata
233      */
234     std::string testFileDir = "test file dir for cachedata";
235     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(8, 8, 15, testFileDir);
236     /**
237      * @tc.steps: step2. test the clean function
238      */
239     uint8_t *tempBuffer = new uint8_t[4]();
240     const char *testKey1 = "Key1";
241     const char *testKey2 = "Key2";
242     const char *testKey3 = "Key3";
243     const char *testKey4 = "Key4";
244     const char *testValue = "aVal";
245     cacheData->Rewrite(testKey1, 4, testValue, 4);
246     cacheData->Rewrite(testKey2, 4, testValue, 4);
247     cacheData->Rewrite(testKey3, 4, testValue, 4);
248     cacheData->Rewrite(testKey4, 4, testValue, 4);
249     auto [errorCode, sizeGet] = cacheData->Get(testKey4, 4, tempBuffer, 4);
250     EXPECT_EQ(4, sizeGet);
251     delete[] tempBuffer;
252 #endif
253 }
254 
255 /**
256  * @tc.name: clean_data_test_002
257  * @tc.desc: Verify a vain clean function
258  * @tc.type: FUNC
259  * @tc.require:
260  * @tc.author:
261  */
262 HWTEST_F(CacheDataTest, clean_data_test_002, TestSize.Level1)
263 {
264 #ifdef ACE_ENABLE_GL
265     if (RSSystemProperties::IsUseVulkan()) {
266         GTEST_LOG_(INFO) << "vulkan enable! skip opengl test case";
267         return;
268     }
269     GTEST_LOG_(INFO) << "CacheDataTest clean_data_test_002 start";
270     /**
271      * @tc.steps: step1. initialize a cachedata
272      */
273     std::string testFileDir = "test file dir for cachedata";
274     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
275     /**
276      * @tc.steps: step2. test the clean function that skips a vain clean
277      */
278     uint8_t *tempBuffer = new uint8_t[4]();
279     const char *testKey1 = "Key1";
280     const char *testKey2 = "Key2";
281     const char *testValue = "aVal";
282     cacheData->Rewrite(testKey1, 4, testValue, 4);
283     cacheData->Rewrite(testKey2, 4, testValue, 4);
284     auto [errorCode, sizeGet] = cacheData->Get(testKey2, 4, tempBuffer, 4);
285     EXPECT_EQ(0, sizeGet);
286     delete[] tempBuffer;
287 #endif
288 }
289 
290 /**
291  * @tc.name: ReadFromFileTest
292  * @tc.desc: Imporve Coverage
293  * @tc.type: FUNC
294  * @tc.require:
295  * @tc.author:
296  */
297  HWTEST_F(CacheDataTest, ReadFromFileTest, TestSize.Level1)
298 {
299     std::shared_ptr<CacheData> cacheData1 = std::make_shared<CacheData>(0, 0, 0, "TestDir");
300     EXPECT_NE(nullptr, cacheData1.get());
301     cacheData1->ReadFromFile();
302     std::shared_ptr<CacheData> cacheData2 = std::make_shared<CacheData>(0, 0, 0, "");
303     EXPECT_NE(nullptr, cacheData2.get());
304     cacheData2->ReadFromFile();
305 }
306 
307 /**
308  * @tc.name: WriteToFileTest
309  * @tc.desc: Imporve Coverage
310  * @tc.type: FUNC
311  * @tc.require:
312  * @tc.author:
313  */
314  HWTEST_F(CacheDataTest, WriteToFileTest, TestSize.Level1)
315 {
316     std::shared_ptr<CacheData> cacheData1 = std::make_shared<CacheData>(0, 0, 0, "TestDir");
317     EXPECT_NE(nullptr, cacheData1.get());
318     cacheData1->WriteToFile();
319     std::shared_ptr<CacheData> cacheData2 = std::make_shared<CacheData>(0, 0, 0, "");
320     EXPECT_NE(nullptr, cacheData2.get());
321     cacheData2->WriteToFile();
322 }
323 
324 
325 /**
326  * @tc.name: SerializeTest001
327  * @tc.desc: Imporve Coverage
328  * @tc.type: FUNC
329  * @tc.require:
330  * @tc.author:
331  */
332  HWTEST_F(CacheDataTest, SerializeTest001, TestSize.Level1)
333 {
334     std::string testFileDir = "testCachedata";
335     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
336     uint8_t *tempBuffer = new uint8_t[sizeof(size_t)]();
337     int retSerialized1 = cacheData->Serialize(tempBuffer, sizeof(size_t));
338     EXPECT_NE(retSerialized1, -EINVAL);
339     int retSerialized2 =  cacheData->Serialize(tempBuffer, 0);
340     EXPECT_EQ(retSerialized2, -EINVAL);
341     delete[] tempBuffer;
342 }
343 
344 /**
345  * @tc.name: DeSerializeTest001
346  * @tc.desc: Imporve Coverage
347  * @tc.type: FUNC
348  * @tc.require:
349  * @tc.author:
350  */
351  HWTEST_F(CacheDataTest, DeSerializeTest001, TestSize.Level1)
352 {
353     std::string testFileDir = "testCachedata";
354     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(0, 0, 0, testFileDir);
355     uint8_t *tempBuffer = new uint8_t[sizeof(size_t)]();
356     int retDeserialized1 = cacheData->DeSerialize(tempBuffer, sizeof(size_t));
357     EXPECT_NE(retDeserialized1, -EINVAL);
358     delete[] tempBuffer;
359 }
360 
361 /**
362  * @tc.name: RewriteTest001
363  * @tc.desc: Imporve Coverage
364  * @tc.type: FUNC
365  * @tc.require:
366  * @tc.author:
367  */
368  HWTEST_F(CacheDataTest, RewriteTest001, TestSize.Level1)
369 {
370     std::string testFileDir = "testCachedata";
371     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
372     uint8_t *tempBuffer = new uint8_t[4]();
373     const char *testKey1 = "key1";
374     const char *testValue = "val1";
375     cacheData->Rewrite(testKey1, 4, testValue, 4);
376     auto [errorCode, sizeGet] = cacheData->Get(testKey1, 4, tempBuffer, 4);
377     EXPECT_EQ(0, sizeGet);
378     delete[] tempBuffer;
379 }
380 
381 /**
382  * @tc.name: IfSizeValidateTest
383  * @tc.desc: Imporve Coverage
384  * @tc.type: FUNC
385  * @tc.require:
386  * @tc.author:
387  */
388  HWTEST_F(CacheDataTest, IfSizeValidateTest, TestSize.Level1)
389 {
390     std::string testFileDir = "testCachedata";
391     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
392     EXPECT_TRUE(cacheData->IfSizeValidate(2, 2));
393     EXPECT_TRUE(cacheData->IfSizeValidate(7, 0));
394     EXPECT_FALSE(cacheData->IfSizeValidate(7, 2));
395 }
396 
397 /**
398  * @tc.name: IfSkipCleanTest
399  * @tc.desc: Imporve Coverage
400  * @tc.type: FUNC
401  * @tc.require:
402  * @tc.author:
403  */
404  HWTEST_F(CacheDataTest, IfSkipCleanTest, TestSize.Level1)
405 {
406     std::string testFileDir = "testCachedata";
407     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
408     EXPECT_TRUE(cacheData->IfSkipClean(4));
409     EXPECT_FALSE(cacheData->IfSkipClean(3));
410 }
411 
412 /**
413  * @tc.name: IfSkipCleanTest
414  * @tc.desc: Imporve Coverage
415  * @tc.type: FUNC
416  * @tc.require:
417  * @tc.author:
418  */
419  HWTEST_F(CacheDataTest, IfCleanFinishedTest, TestSize.Level1)
420 {
421     std::string testFileDir = "testCachedata";
422     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
423     cacheData->cleanThreshold_ = 0;
424     EXPECT_TRUE(cacheData->IfCleanFinished());
425     cacheData->cleanThreshold_ = 1;
426     EXPECT_FALSE(cacheData->IfCleanFinished());
427 }
428 
429 /**
430  * @tc.name: RandCleanTest
431  * @tc.desc: Imporve Coverage
432  * @tc.type: FUNC
433  * @tc.require:
434  * @tc.author:
435  */
436  HWTEST_F(CacheDataTest, RandCleanTest, TestSize.Level1)
437 {
438     std::string testFileDir = "testCachedata";
439     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
440     cacheData->RandClean(0);
441     cacheData->cleanThreshold_ = 0;
442     cacheData->RandClean(1);
443     EXPECT_LE(cacheData->totalSize_, 1);
444 }
445 
446 /**
447  * @tc.name: CleanTest
448  * @tc.desc: Imporve Coverage
449  * @tc.type: FUNC
450  * @tc.require:
451  * @tc.author:
452  */
453  HWTEST_F(CacheDataTest, CleanTest, TestSize.Level1)
454 {
455     std::string testFileDir = "testCachedata";
456     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
457     cacheData->shaderPointers_ = {};
458     EXPECT_EQ(0, cacheData->Clean(2));
459 }
460 
461 /**
462  * @tc.name: GetTest
463  * @tc.desc: Imporve Coverage
464  * @tc.type: FUNC
465  * @tc.require:
466  * @tc.author:
467  */
468  HWTEST_F(CacheDataTest, GetTest, TestSize.Level1)
469 {
470     std::string testFileDir = "testCachedata";
471     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
472     auto returnData = cacheData->Get(nullptr, 5, nullptr, 1);
473     std::tuple<CacheData::ErrorCode, size_t> testData = {CacheData::ErrorCode::VALUE_SIZE_OVER_MAX_SIZE, 0};
474     EXPECT_EQ(std::get<0>(returnData), std::get<0>(testData));
475     EXPECT_EQ(std::get<1>(returnData), std::get<1>(testData));
476 }
477 
478 /**
479  * @tc.name: CleanInitTest_001
480  * @tc.desc: Purge Shader Cache at Free Time
481  * @tc.type: FUNC
482  * @tc.require:
483  * @tc.author:
484  */
485  HWTEST_F(CacheDataTest, CleanInitTest_001, TestSize.Level1)
486 {
487     std::string testFileDir = "testCachedata";
488     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
489     EXPECT_EQ(cacheData->CleanInit(), true);
490 }
491 
492 /**
493  * @tc.name: CleanInitTest_002
494  * @tc.desc: Purge Shader Cache at Free Time
495  * @tc.type: FUNC
496  * @tc.require:
497  * @tc.author:
498  */
499  HWTEST_F(CacheDataTest, CleanInitTest_002, TestSize.Level1)
500 {
501     std::string testFileDir = "testCachedata";
502     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
503     std::vector<unsigned short> originValues(cacheData->cleanInit_, cacheData->cleanInit_ + cacheData->randLength_);
504     cacheData->CleanInit();
505     bool isChanged = false;
506     for (int i = 0; i < cacheData->randLength_; ++i) {
507         if (cacheData->cleanInit_[i] != originValues[i]) {
508             isChanged = true;
509             break;
510         }
511     }
512     EXPECT_EQ(isChanged, true);
513 }
514 
515 /**
516  * @tc.name: CheckShaderCacheOverSoftLimitTest
517  * @tc.desc: Purge Shader Cache at Free Time
518  * @tc.type: FUNC
519  * @tc.require:
520  * @tc.author:
521  */
522  HWTEST_F(CacheDataTest, CheckShaderCacheOverSoftLimitTest, TestSize.Level1)
523 {
524     std::string testFileDir = "testCachedata";
525     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
526     bool check = cacheData->totalSize_ > cacheData->softLimit_;
527     EXPECT_EQ(cacheData->CheckShaderCacheOverSoftLimit(), check);
528 }
529 
530 /**
531  * @tc.name: PurgeShaderCacheAfterAnimateTest
532  * @tc.desc: Purge Shader Cache at Free Time
533  * @tc.type: FUNC
534  * @tc.require:
535  * @tc.author:
536  */
537  HWTEST_F(CacheDataTest, PurgeShaderCacheAfterAnimateTest, TestSize.Level1)
538 {
539     std::string testFileDir = "testCachedata";
540     std::shared_ptr<CacheData> cacheData = std::make_shared<CacheData>(4, 4, 6, testFileDir);
__anon320105390102() 541     cacheData->PurgeShaderCacheAfterAnimate([]() { return true; });
542     EXPECT_GE(6, cacheData->totalSize_);
543 }
544 } // namespace Rosen
545 } // namespace OHOS