1 /*
2 * Copyright (c) 2020-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 <securec.h>
17 #include "gtest/gtest.h"
18 #include "utils/SamgrTestBase.h"
19
20 const int RUN_TIMES = 10;
21
22 using namespace testing::ext;
23
24 struct Node {
25 int id;
26 char *name;
27 int value;
28 };
29
GetNode(const Node * node1)30 static const Node *GetNode(const Node *node1)
31 {
32 return node1;
33 }
34
CompareNode(const Node * node1,const Node * node2)35 static int CompareNode(const Node *node1, const Node *node2)
36 {
37 if (node1->id < node2->id) {
38 return -1;
39 } else if (node1->id == node2->id) {
40 return 0;
41 } else {
42 return 1;
43 }
44 }
45
46 class CommonTest : public testing::Test {
47 protected:
48 // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)49 static void SetUpTestCase(void)
50 {
51 printf("[hcpptest]SetUpTestCase ! \n");
52 SystemInitProxy();
53 }
54 // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)55 static void TearDownTestCase(void)
56 {
57 }
58 // Testcase setup
SetUp()59 virtual void SetUp()
60 {
61 usleep(OPER_INTERVAL * MS2US);
62 }
63 // Testcase teardown
TearDown()64 virtual void TearDown()
65 {
66 }
67 };
68
69 /**
70 * @tc.number : DMSLite_SAMGR_Common_0010
71 * @tc.name : The vector operation after initialization is correct
72 * @tc.desc : [C- SOFTWARE -0200]
73 */
74 HWTEST_F(CommonTest, testCommon0010, Function | MediumTest | Level2)
75 {
76 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
77
78 int16 vectorSize = VECTOR_Size(&nodeVector);
79 ASSERT_EQ(vectorSize == 0, TRUE);
80
81 int16 vectorNum = VECTOR_Num(&nodeVector);
82 ASSERT_EQ(vectorNum == 0, TRUE);
83
84 Node *vectorAt = (Node *)VECTOR_At(&nodeVector, (int16)0);
85 ASSERT_EQ(vectorAt == nullptr, TRUE);
86
87 Node node = {1, (char*)"node1", 1};
88 Node *vectorSwap = (Node *)VECTOR_Swap(&nodeVector, (int16)0, &node);
89 ASSERT_EQ(vectorSwap == nullptr, TRUE);
90
91 int16 indexFind = VECTOR_Find(&nodeVector, &node);
92 ASSERT_EQ(INVALID_INDEX, indexFind);
93
94 int16 indexFindByKey = VECTOR_FindByKey(&nodeVector, &node);
95 ASSERT_EQ(INVALID_INDEX, indexFindByKey);
96
97 VECTOR_Clear(&nodeVector);
98 }
99
100 /**
101 * @tc.number : DMSLite_SAMGR_Common_0020
102 * @tc.name : The vector operation is correct when only 1 element
103 * @tc.desc : [C- SOFTWARE -0200]
104 */
105 HWTEST_F(CommonTest, testCommon0020, Function | MediumTest | Level2)
106 {
107 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
108
109 Node node = {1, (char*)"node1", 1};
110 int16 resultAdd = VECTOR_Add(&nodeVector, &node);
111 ASSERT_EQ(resultAdd, 0);
112
113 int16 vectorSize = VECTOR_Size(&nodeVector);
114 ASSERT_EQ(vectorSize, 1);
115
116 int16 vectorNum = VECTOR_Num(&nodeVector);
117 ASSERT_EQ(vectorNum, 1);
118
119 Node *vectorAt = (Node *)VECTOR_At(&nodeVector, (int16)0);
120 ASSERT_EQ(vectorAt != nullptr, TRUE);
121
122 Node node2 = {2, (char*)"node2", 2};
123 Node *resultNode = (Node *)VECTOR_Swap(&nodeVector, (int16)0, &node2);
124 ASSERT_EQ(resultNode != nullptr, TRUE);
125
126 int16 indexFind = VECTOR_Find(&nodeVector, &node2);
127 ASSERT_EQ(indexFind, 0);
128
129 int16 indexFindByKey = VECTOR_FindByKey(&nodeVector, &node2);
130 ASSERT_EQ(indexFindByKey, 0);
131
132 VECTOR_Clear(&nodeVector);
133 }
134
135 /**
136 * @tc.number : DMSLite_SAMGR_Common_0030
137 * @tc.name : The vector operation is correct when multi elements
138 * @tc.desc : [C- SOFTWARE -0200]
139 */
140 HWTEST_F(CommonTest, testCommon0030, Function | MediumTest | Level2)
141 {
142 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
143
144 Node node[] = {
145 {0, (char*)"node", 0},
146 {1, (char*)"node", 1},
147 {2, (char*)"node", 2},
148 {3, (char*)"node", 3},
149 {4, (char*)"node", 4},
150 {5, (char*)"node", 5},
151 {6, (char*)"node", 6},
152 {7, (char*)"node", 7},
153 {8, (char*)"node", 8},
154 {9, (char*)"node", 9},
155 };
156
157 for (int i = 0; i < RUN_TIMES; i++) {
158 int16 resultAdd = VECTOR_Add(&nodeVector, &(node[i]));
159 ASSERT_EQ(i, resultAdd);
160 }
161
162 int16 vectorSize = VECTOR_Size(&nodeVector);
163 ASSERT_EQ(vectorSize, RUN_TIMES);
164
165 int16 vectorNum = VECTOR_Num(&nodeVector);
166 ASSERT_EQ(vectorNum, RUN_TIMES);
167
168 for (int i = 0; i < RUN_TIMES; i++) {
169 Node *vectorAt = (Node *)VECTOR_At(&nodeVector, (int16)i);
170 ASSERT_EQ(vectorAt != nullptr, TRUE);
171 ASSERT_EQ(vectorAt->id, i);
172
173 int16 indexFind = VECTOR_Find(&nodeVector, &node[i]);
174 ASSERT_EQ(indexFind, i);
175
176 int16 indexFindByKey = VECTOR_FindByKey(&nodeVector, &node[i]);
177 ASSERT_EQ(indexFindByKey, i);
178 }
179
180 Node nodeX = {99, (char*)"node", 99};
181 Node *resultNode = (Node *)VECTOR_Swap(&nodeVector, (int16)0, &nodeX);
182 ASSERT_EQ(resultNode != nullptr, TRUE);
183 ASSERT_EQ(resultNode->id, 0);
184
185 VECTOR_Clear(&nodeVector);
186 }
187
188 /**
189 * @tc.number : DMSLite_SAMGR_Common_0040
190 * @tc.name : The vector operation is correct after delete some elements
191 * @tc.desc : [C- SOFTWARE -0200]
192 */
193 HWTEST_F(CommonTest, testCommon0040, Function | MediumTest | Level2)
194 {
195 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
196 Node node[] = {
197 {0, (char*)"node", 0},
198 {1, (char*)"node", 1},
199 {2, (char*)"node", 2},
200 {3, (char*)"node", 3},
201 {4, (char*)"node", 4},
202 {5, (char*)"node", 5},
203 {6, (char*)"node", 6},
204 {7, (char*)"node", 7},
205 {8, (char*)"node", 8},
206 {9, (char*)"node", 9},
207 };
208 for (int i = 0; i < RUN_TIMES; i++) {
209 VECTOR_Add(&nodeVector, &(node[i]));
210 }
211
212 for (int i = 0; i < RUN_TIMES; i++) {
213 Node *resultNode = (Node *)VECTOR_Swap(&nodeVector, (int16)i, nullptr);
214 ASSERT_EQ(resultNode != nullptr, TRUE);
215 ASSERT_EQ(i, resultNode->id);
216
217 int16 vectorSize = VECTOR_Size(&nodeVector);
218 ASSERT_EQ(RUN_TIMES, vectorSize);
219
220 int16 vectorNum = VECTOR_Num(&nodeVector);
221 ASSERT_EQ(RUN_TIMES - (i + 1), vectorNum);
222
223 Node *vectorAt = (Node *)VECTOR_At(&nodeVector, (int16)i);
224 ASSERT_EQ(TRUE, vectorAt == nullptr);
225
226 int16 indexFind = VECTOR_Find(&nodeVector, &node[i]);
227 ASSERT_EQ(INVALID_INDEX, indexFind);
228
229 int16 indexFindByKey = VECTOR_FindByKey(&nodeVector, &node[i]);
230 ASSERT_EQ(INVALID_INDEX, indexFindByKey);
231 }
232
233 VECTOR_Clear(&nodeVector);
234 }
235
236 /**
237 * @tc.number : DMSLite_SAMGR_Common_0050
238 * @tc.name : The vector operation is correct after add and delete some elements
239 * @tc.desc : [C- SOFTWARE -0200]
240 */
241 HWTEST_F(CommonTest, testCommon0050, Function | MediumTest | Level2)
242 {
243 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
244 Node node[] = {
245 {0, (char*)"node", 0},
246 {1, (char*)"node", 1},
247 {2, (char*)"node", 2},
248 {3, (char*)"node", 3},
249 {4, (char*)"node", 4},
250 {5, (char*)"node", 5},
251 {6, (char*)"node", 6},
252 {7, (char*)"node", 7},
253 {8, (char*)"node", 8},
254 {9, (char*)"node", 9},
255 };
256 for (int i = 0; i < RUN_TIMES * RUN_TIMES; i++) {
257 for (int j = 0; j < RUN_TIMES; j++) {
258 VECTOR_Add(&nodeVector, &(node[j]));
259 }
260 for (int j = 0; j < RUN_TIMES; j++) {
261 VECTOR_Swap(&nodeVector, (int16)j, nullptr);
262 }
263 }
264
265 for (int i = 0; i < RUN_TIMES; i++) {
266 VECTOR_Add(&nodeVector, &(node[i]));
267 }
268 printf("[hctest]test start \n");
269 for (int i = 0; i < RUN_TIMES; i++) {
270 int16 indexFind = VECTOR_Find(&nodeVector, &node[i]);
271 ASSERT_EQ(indexFind >= 0, TRUE);
272
273 int16 indexFindByKey = VECTOR_FindByKey(&nodeVector, &node[i]);
274 ASSERT_EQ(indexFindByKey >= 0, TRUE);
275
276 Node *resultNode = (Node *)VECTOR_Swap(&nodeVector, indexFind, nullptr);
277 ASSERT_EQ(resultNode != nullptr, TRUE);
278 ASSERT_EQ(i, resultNode->id);
279
280 int16 vectorSize = VECTOR_Size(&nodeVector);
281 ASSERT_EQ(vectorSize > (RUN_TIMES - (i + 1)), TRUE);
282
283 int16 vectorNum = VECTOR_Num(&nodeVector);
284 ASSERT_EQ(RUN_TIMES - (i + 1), vectorNum);
285
286 Node *vectorAt = (Node *)VECTOR_At(&nodeVector, indexFind);
287 ASSERT_EQ(TRUE, vectorAt == nullptr);
288 }
289
290 VECTOR_Clear(&nodeVector);
291 }
292
293 /**
294 * @tc.number : DMSLite_SAMGR_Common_0060
295 * @tc.name : If vector or element is nullptr adding element fails
296 * @tc.desc : [C- SOFTWARE -0200]
297 */
298 HWTEST_F(CommonTest, testCommon0060, Function | MediumTest | Level2)
299 {
300 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
301
302 Node node = {1, (char*)"node1", 1};
303 int16 resultAdd = VECTOR_Add(NULL, &node);
304 ASSERT_EQ(INVALID_INDEX, resultAdd);
305
306 resultAdd = VECTOR_Add(&nodeVector, nullptr);
307 ASSERT_EQ(INVALID_INDEX, resultAdd);
308
309 VECTOR_Clear(&nodeVector);
310 }
311
312 /**
313 * @tc.number : DMSLite_SAMGR_Common_0070
314 * @tc.name : If vector is nullptr deleting element fails
315 * @tc.desc : [C- SOFTWARE -0200]
316 */
317 HWTEST_F(CommonTest, testCommon0070, Function | MediumTest | Level2)
318 {
319 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
320 Node node = {1, (char*)"node1", 1};
321 VECTOR_Add(&nodeVector, &node);
322
323 Node *vectorSwap = (Node *)VECTOR_Swap(NULL, (int16)0, nullptr);
324 ASSERT_EQ(TRUE, vectorSwap == nullptr);
325
326 VECTOR_Clear(&nodeVector);
327 }
328
329 /**
330 * @tc.number : DMSLite_SAMGR_Common_0080
331 * @tc.name : If vector is nullptr getting information from vector fails
332 * @tc.desc : [C- SOFTWARE -0200]
333 */
334 HWTEST_F(CommonTest, testCommon0080, Function | MediumTest | Level2)
335 {
336 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
337 Node node = {1, (char*)"node1", 1};
338 VECTOR_Add(&nodeVector, &node);
339
340 int16 vectorSize = VECTOR_Size(NULL);
341 ASSERT_EQ(INVALID_INDEX, vectorSize);
342
343 int16 vectorNum = VECTOR_Num(NULL);
344 ASSERT_EQ(INVALID_INDEX, vectorNum);
345
346 Node *vectorAt = (Node *)VECTOR_At(NULL, (int16)0);
347 ASSERT_EQ(TRUE, vectorAt == nullptr);
348
349 VECTOR_Clear(&nodeVector);
350 }
351
352 /**
353 * @tc.number : DMSLite_SAMGR_Common_0090
354 * @tc.name : If vector is nullptr searching from vector fails
355 * @tc.desc : [C- SOFTWARE -0200]
356 */
357 HWTEST_F(CommonTest, testCommon0090, Function | MediumTest | Level2)
358 {
359 Vector nodeVector = VECTOR_Make((VECTOR_Key)GetNode, (VECTOR_Compare)CompareNode);
360 Node node = {1, (char*)"node1", 1};
361 VECTOR_Add(&nodeVector, &node);
362
363 int16 indexFind = VECTOR_Find(NULL, &node);
364 ASSERT_EQ(INVALID_INDEX, indexFind);
365
366 int16 indexFindByKey = VECTOR_FindByKey(NULL, &node);
367 ASSERT_EQ(INVALID_INDEX, indexFindByKey);
368
369 VECTOR_Clear(&nodeVector);
370 }