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