• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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, Hardware
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 <memory>
19 #include "egl_blob_cache.h"
20 
21 #include "egl_defs.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Rosen {
27 class EglBlobTest : public testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase() {}
TearDownTestCase()30     static void TearDownTestCase() {}
SetUp()31     void SetUp() {}
TearDown()32     void TearDown() {}
33 };
34 
35 /**
36  * @tc.name: EglBlobInit001
37  * @tc.desc:
38  * @tc.type: FUNC
39  */
HWTEST_F(EglBlobTest,EglBlobInit001,Level1)40 HWTEST_F(EglBlobTest, EglBlobInit001, Level1)
41 {
42     BlobCache* ret = BlobCache::Get();
43     ASSERT_NE(ret, nullptr);
44 }
45 
46 
47 /**
48  * @tc.name: EglBlobInit002
49  * @tc.desc:
50  * @tc.type: FUNC
51  */
HWTEST_F(EglBlobTest,EglBlobInit002,Level1)52 HWTEST_F(EglBlobTest, EglBlobInit002, Level1)
53 {
54     BlobCache* ret = BlobCache::Get();
55     ASSERT_NE(ret, nullptr);
56 
57     void *a = malloc(4);
58     void *b = malloc(4);
59     int* intPtr = static_cast<int*>(a);
60     int* intPtr2 = static_cast<int*>(b);
61     *intPtr = 1;
62     *intPtr2 = 2;
63     const void* key = static_cast<const void*>(a);
64     void *value = static_cast<void*>(b);
65     int d = *static_cast<int *>(value);
66     ASSERT_EQ(d, 2);
67     EGLsizeiANDROID keysize = 4;
68     EGLsizeiANDROID valuesize = 4;
69     void *value2 = malloc(4);
70     BlobCache::SetBlobFunc(key, keysize, value, valuesize);
71     BlobCache::GetBlobFunc(key, keysize, value2, valuesize);
72     int c = *static_cast<int *>(value2);
73     ASSERT_EQ(c, 2);
74 }
75 
76 /**
77  * @tc.name: EglBlobInit003
78  * @tc.desc:
79  * @tc.type: FUNC
80  */
HWTEST_F(EglBlobTest,EglBlobInit003,Level1)81 HWTEST_F(EglBlobTest, EglBlobInit003, Level1)
82 {
83     BlobCache* ret = BlobCache::Get();
84     ASSERT_NE(ret, nullptr);
85 
86     void *a = malloc(4);
87     void *b = malloc(4);
88     int* intPtr = static_cast<int*>(a);
89     int* intPtr2 = static_cast<int*>(b);
90     *intPtr = 1;
91     *intPtr2 = 2;
92     const void* key = static_cast<const void*>(a);
93     void *value = static_cast<void*>(b);
94     int d = *static_cast<int *>(value);
95     ASSERT_EQ(d, 2);
96     EGLsizeiANDROID keysize = 4;
97     EGLsizeiANDROID valuesize = 4;
98     EGLsizeiANDROID valuesize2 = 3;
99     void *value2 = malloc(4);
100     BlobCache::SetBlobFunc(key, keysize, value, valuesize);
101     BlobCache::GetBlobFunc(key, keysize, value2, valuesize2);
102     int c = *static_cast<int *>(value2);
103     ASSERT_EQ(c, 0);
104 }
105 
106 /**
107  * @tc.name: EglBlobInit004
108  * @tc.desc:
109  * @tc.type: FUNC
110  */
HWTEST_F(EglBlobTest,EglBlobInit004,Level1)111 HWTEST_F(EglBlobTest, EglBlobInit004, Level1)
112 {
113     BlobCache* ret = BlobCache::Get();
114     ASSERT_NE(ret, nullptr);
115 
116     void *a = malloc(4);
117     void *b = malloc(4);
118     int* intPtr = static_cast<int*>(a);
119     int* intPtr2 = static_cast<int*>(b);
120     *intPtr = 1;
121     *intPtr2 = 2;
122     const void* key = static_cast<const void*>(a);
123     void *value = static_cast<void*>(b);
124     int d = *static_cast<int *>(value);
125     ASSERT_EQ(d, 2);
126     EGLsizeiANDROID keysize = -1;
127     EGLsizeiANDROID valuesize = -1;
128     void *value2 = malloc(4);
129     BlobCache::SetBlobFunc(key, keysize, value, valuesize);
130     BlobCache::GetBlobFunc(key, keysize, value2, valuesize);
131     *static_cast<int *>(value2) = 0;
132     int c = *static_cast<int *>(value2);
133     ASSERT_EQ(c, 0);
134 }
135 
136 /**
137  * @tc.name: EglBlobInit005
138  * @tc.desc:
139  * @tc.type: FUNC
140  */
HWTEST_F(EglBlobTest,EglBlobInit005,Level1)141 HWTEST_F(EglBlobTest, EglBlobInit005, Level1)
142 {
143     BlobCache* ret = BlobCache::Get();
144     ASSERT_NE(ret, nullptr);
145 
146     void *a = malloc(4);
147     void *b = malloc(4);
148     int* intPtr = static_cast<int*>(a);
149     int* intPtr2 = static_cast<int*>(b);
150     *intPtr = 1;
151     *intPtr2 = 2;
152     const void* key = static_cast<const void*>(a);
153     void *value = static_cast<void*>(b);
154     int d = *static_cast<int *>(value);
155     ASSERT_EQ(d, 2);
156     EGLsizeiANDROID keysize = 4;
157     EGLsizeiANDROID valuesize = 4;
158     void *value2 = malloc(4);
159     BlobCache::SetBlobFunc(key, keysize, value, valuesize);
160     EGLsizeiANDROID retnum = BlobCache::GetBlobFunc(key, keysize, value2, valuesize);
161     ASSERT_EQ(retnum, 4);
162 }
163 
164 /**
165  * @tc.name: destructBlobCacheTest001
166  * @tc.desc:
167  * @tc.type: FUNC
168  */
HWTEST_F(EglBlobTest,destructBlobCacheTest001,Level1)169  HWTEST_F(EglBlobTest, destructBlobCacheTest001, Level1)
170 {
171     auto cache = new BlobCache();
172     EXPECT_NE(cache, nullptr);
173     cache->blobCache_ = nullptr;
174     delete cache;
175     cache = nullptr;
176 }
177 
178 /**
179  * @tc.name: destructBlobCacheTest002
180  * @tc.desc:
181  * @tc.type: FUNC
182  */
HWTEST_F(EglBlobTest,destructBlobCacheTest002,Level1)183  HWTEST_F(EglBlobTest, destructBlobCacheTest002, Level1)
184 {
185     auto cache = new BlobCache();
186     cache->blobCache_ = nullptr;
187     BlobCache::Get();
188     EXPECT_NE(cache->blobCache_, nullptr);
189     delete cache;
190     cache = nullptr;
191 }
192 
193 /**
194  * @tc.name: GetMapSizeTest
195  * @tc.desc:
196  * @tc.type: FUNC
197  */
HWTEST_F(EglBlobTest,GetMapSizeTest,Level1)198  HWTEST_F(EglBlobTest, GetMapSizeTest, Level1)
199 {
200     BlobCache* ret = BlobCache::Get();
201     EXPECT_EQ(0, ret->GetMapSize());
202 }
203 
204 /**
205  * @tc.name: InitTest
206  * @tc.desc:
207  * @tc.type: FUNC
208  */
HWTEST_F(EglBlobTest,InitTest,Level1)209  HWTEST_F(EglBlobTest, InitTest, Level1)
210 {
211     BlobCache* ret = BlobCache::Get();
212     ret->initStatus_ = true;
213     ret->Init(nullptr);
214     ret->initStatus_ = false;
215     ret->Init(nullptr);
216     EXPECT_FALSE(ret->initStatus_);
217 }
218 
219 /**
220  * @tc.name: SetBlobTest
221  * @tc.desc:
222  * @tc.type: FUNC
223  */
HWTEST_F(EglBlobTest,SetBlobTest,Level1)224  HWTEST_F(EglBlobTest, SetBlobTest, Level1)
225 {
226     BlobCache* ret = BlobCache::Get();
227     ret->SetBlob(nullptr, 0, nullptr, 0);
228     EXPECT_EQ(ret->GetBlob(nullptr, 0, nullptr, 0), 0);
229 }
230 
231 /**
232  * @tc.name: SetCacheDirTest
233  * @tc.desc:
234  * @tc.type: FUNC
235  */
HWTEST_F(EglBlobTest,SetCacheDirTest,Level1)236  HWTEST_F(EglBlobTest, SetCacheDirTest, Level1)
237 {
238     BlobCache* ret = BlobCache::Get();
239     EXPECT_NE(ret, nullptr);
240     ret->SetCacheDir("SetCacheDirTest");
241 }
242 
243 /**
244  * @tc.name: SetCacheShaderSizeTest
245  * @tc.desc:
246  * @tc.type: FUNC
247  */
HWTEST_F(EglBlobTest,SetCacheShaderSizeTest,Level1)248  HWTEST_F(EglBlobTest, SetCacheShaderSizeTest, Level1)
249 {
250     BlobCache* ret = BlobCache::Get();
251     ret->SetCacheShaderSize(0);
252     ret->SetCacheShaderSize(MAX_SHADER + 1);
253     ret->SetCacheShaderSize(1);
254     EXPECT_EQ(ret->blobSizeMax_, 1);
255 }
256 } // OHOS::Rosen
257