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