1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 /* BEGIN_HEADER */
17
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <pthread.h>
21 #include "bsl_sal.h"
22 #include "bsl_err_internal.h"
23 #include "bsl_err.h"
24 #include "avl.h"
25 #include "bsl_uio.h"
26
PthreadRWLockNew(BSL_SAL_ThreadLockHandle * lock)27 static int32_t PthreadRWLockNew(BSL_SAL_ThreadLockHandle *lock)
28 {
29 if (lock == NULL) {
30 return BSL_SAL_ERR_BAD_PARAM;
31 }
32 pthread_rwlock_t *newLock;
33 newLock = (pthread_rwlock_t *)BSL_SAL_Malloc(sizeof(pthread_rwlock_t));
34 if (newLock == NULL) {
35 return BSL_MALLOC_FAIL;
36 }
37 if (pthread_rwlock_init(newLock, NULL) != 0) {
38 return BSL_SAL_ERR_UNKNOWN;
39 }
40 *lock = newLock;
41 return BSL_SUCCESS;
42 }
43
PthreadRWLockFree(BSL_SAL_ThreadLockHandle lock)44 static void PthreadRWLockFree(BSL_SAL_ThreadLockHandle lock)
45 {
46 if (lock == NULL) {
47 return;
48 }
49 pthread_rwlock_destroy((pthread_rwlock_t *)lock);
50 BSL_SAL_FREE(lock);
51 }
52
PthreadRWLockReadLock(BSL_SAL_ThreadLockHandle lock)53 static int32_t PthreadRWLockReadLock(BSL_SAL_ThreadLockHandle lock)
54 {
55 if (lock == NULL) {
56 return BSL_SAL_ERR_BAD_PARAM;
57 }
58 if (pthread_rwlock_rdlock((pthread_rwlock_t *)lock) != 0) {
59 return BSL_SAL_ERR_UNKNOWN;
60 }
61 return BSL_SUCCESS;
62 }
63
PthreadRWLockWriteLock(BSL_SAL_ThreadLockHandle lock)64 static int32_t PthreadRWLockWriteLock(BSL_SAL_ThreadLockHandle lock)
65 {
66 if (lock == NULL) {
67 return BSL_SAL_ERR_BAD_PARAM;
68 }
69 if (pthread_rwlock_wrlock((pthread_rwlock_t *)lock) != 0) {
70 return BSL_SAL_ERR_UNKNOWN;
71 }
72 return BSL_SUCCESS;
73 }
74
PthreadRWLockUnlock(BSL_SAL_ThreadLockHandle lock)75 static int32_t PthreadRWLockUnlock(BSL_SAL_ThreadLockHandle lock)
76 {
77 if (lock == NULL) {
78 return BSL_SAL_ERR_BAD_PARAM;
79 }
80 if (pthread_rwlock_unlock((pthread_rwlock_t *)lock) != 0) {
81 return BSL_SAL_ERR_UNKNOWN;
82 }
83 return BSL_SUCCESS;
84 }
85
PthreadGetId(void)86 static uint64_t PthreadGetId(void)
87 {
88 return (uint64_t)pthread_self();
89 }
90
PushErrorFixTimes(int32_t times)91 static void PushErrorFixTimes(int32_t times)
92 {
93 while (times) {
94 BSL_ERR_PUSH_ERROR(times);
95 times--;
96 }
97 }
98
RegThreadFunc(void)99 static void RegThreadFunc(void)
100 {
101 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_NEW_CB_FUNC, PthreadRWLockNew);
102 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_FREE_CB_FUNC, PthreadRWLockFree);
103 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_READ_LOCK_CB_FUNC, PthreadRWLockReadLock);
104 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_WRITE_LOCK_CB_FUNC, PthreadRWLockWriteLock);
105 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_UNLOCK_CB_FUNC, PthreadRWLockUnlock);
106 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_GET_ID_CB_FUNC, PthreadGetId);
107 }
108
109 /* END_HEADER */
110
111 /**
112 * @test SDV_BSL_ERR_FUNC_TC001
113 * @title Error code test in single-thread mode
114 * @precon Set the memory allocation and release functions.
115 * @brief
116 * 1. Initializes BSL_ERR. Expected result 1 is obtained.
117 * 2. Invoke the interface to obtain the error when no error is pushed. Expected result 2 is obtained.
118 * 3. Push an BSL_UIO_FAIL error when no memory function is registered. Expected result 3 is obtained.
119 * 4. Push the BSL_UIO_FAIL and BSL_UIO_IO_EXCEPTION error and obtain last error. Expected result 4 is obtained.
120 * 5. Peek last error file and error line. Expected result 5 is obtained.
121 * 6. Get last error file and error line. Expected result 6 is obtained.
122 * 7. Push an error after clear the error stack, and then obtain the error. Expected result 7 is obtained.
123 * 8. Delete the error stack of the thread. Expected result 8 is obtained.
124 * @expect
125 * 1. BSL_SUCCESS
126 * 2. BSL_SUCCESS
127 * 3. BSL_UIO_FAIL
128 * 4. BSL_UIO_IO_EXCEPTION
129 * 5. BSL_UIO_FAIL
130 * 6. BSL_UIO_FAIL
131 * 7. BSL_SUCCESS
132 * 8. BSL_SUCCESS
133 */
134 /* BEGIN_CASE */
SDV_BSL_ERR_FUNC_TC001(void)135 void SDV_BSL_ERR_FUNC_TC001(void)
136 {
137 TestMemInit();
138 int32_t err;
139
140 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
141
142 /* no error is pushed */
143 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_SUCCESS);
144
145 /* If no memory function is registered, push an error and allocate an error stack. */
146 BSL_ERR_PushError(BSL_UIO_FAIL, __FILENAME__, __LINE__);
147 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_UIO_FAIL);
148
149 /* Push the BSL_UIO_FAIL and BSL_UIO_IO_EXCEPTION error to the error stack. */
150 BSL_ERR_PushError(BSL_UIO_FAIL, __FILENAME__, __LINE__);
151 BSL_ERR_PushError(BSL_UIO_IO_EXCEPTION, __FILENAME__, __LINE__);
152 err = BSL_ERR_GetLastError();
153 ASSERT_TRUE(err == BSL_UIO_IO_EXCEPTION);
154
155 const char *file = NULL;
156 uint32_t line = 0;
157 err = BSL_ERR_PeekLastErrorFileLine(&file, &line);
158 ASSERT_TRUE(err == BSL_UIO_FAIL);
159 ASSERT_TRUE(strcmp(file, __FILENAME__) == 0);
160
161 file = NULL;
162 line = 0;
163 err = BSL_ERR_GetLastErrorFileLine(&file, &line);
164 ASSERT_TRUE(err == BSL_UIO_FAIL);
165 ASSERT_TRUE(strcmp(file, __FILENAME__) == 0);
166
167 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_SUCCESS);
168
169 BSL_ERR_PushError(BSL_UIO_FAIL, __FILENAME__, __LINE__);
170 BSL_ERR_ClearError();
171 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_SUCCESS);
172
173 BSL_ERR_RemoveErrorStack(false);
174
175 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_SUCCESS);
176 EXIT:
177 BSL_ERR_DeInit();
178 return;
179 }
180 /* END_CASE */
181
182 /**
183 * @test SDV_BSL_ERR_STACK_FUNC_TC001
184 * @title After stacks are not pushed or cleared, call BSL_ERR_GetLastError to query all error stacks.
185 * @precon nan
186 * @brief
187 * 1. Call BSL_SAL_CallBack_Ctrl to initialize the memory. Expected result 1 is obtained.
188 * 2. Call BSL_SAL_CallBack_Ctrl to initialize the thread. Expected result 2 is obtained.
189 * 3. Call BSL_ERR_Init for initialization. Expected result 3 is obtained.
190 * 4. Call BSL_ERR_GetLastError to obtain stack information. Expected result 4 is obtained.
191 * 5. Call ERR_PUSH_ERROR to push stack layer 5. Expected result 5 is obtained.
192 * 6. Call BSL_ERR_ClearError to clear the stack. Expected result 6 is obtained.
193 * 7. Call BSL_ERR_GetLastError to obtain stack information. Expected result 7 is obtained.
194 * 8. Call BSL_ERR_RemoveErrorStack to delete the stack. Expected result 8 is obtained.
195 * 9. Call BSL_ERR_GetLastError to obtain stack information. Expected result 9 is obtained.
196 * 10. Call BSL_ERR_DeInit to deinitialize. Expected result 10 is obtained.
197 * @expect
198 * 1. BSL_SUCCESS
199 * 2. BSL_SUCCESS
200 * 3. BSL_SUCCESS
201 * 4. BSL_SUCCESS is returned when the error code is obtained.
202 * 5. BSL_SUCCESS
203 * 6. BSL_SUCCESS
204 * 7. BSL_SUCCESS is returned when the error code is obtained.
205 * 8. BSL_SUCCESS
206 * 9. BSL_SUCCESS is returned when the error code is obtained.
207 * 10. BSL_SUCCESS
208 */
209 /* BEGIN_CASE */
210
SDV_BSL_ERR_STACK_FUNC_TC001(int isRemoveAll)211 void SDV_BSL_ERR_STACK_FUNC_TC001(int isRemoveAll)
212 {
213 TestMemInit();
214 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_NEW_CB_FUNC, PthreadRWLockNew) == BSL_SUCCESS);
215 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_FREE_CB_FUNC, PthreadRWLockFree) == BSL_SUCCESS);
216 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_READ_LOCK_CB_FUNC, PthreadRWLockReadLock) == BSL_SUCCESS);
217 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_WRITE_LOCK_CB_FUNC, PthreadRWLockWriteLock) == BSL_SUCCESS);
218 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_UNLOCK_CB_FUNC, PthreadRWLockUnlock) == BSL_SUCCESS);
219 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_GET_ID_CB_FUNC, PthreadGetId) == BSL_SUCCESS);
220 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
221
222 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_SUCCESS);
223 PushErrorFixTimes(5);
224 ASSERT_TRUE(BSL_ERR_PeekLastErrorFileLine(NULL, NULL) == 1);
225 ASSERT_TRUE(BSL_ERR_GetLastErrorFileLine(NULL, NULL) == 1);
226 ASSERT_TRUE(BSL_ERR_GetErrorFileLine(NULL, NULL) == 5);
227 BSL_ERR_ClearError();
228 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_SUCCESS);
229 BSL_ERR_RemoveErrorStack((isRemoveAll == 1) ? true : false);
230 ASSERT_TRUE(BSL_ERR_GetLastError() == BSL_SUCCESS);
231
232 BSL_ERR_PushError(1, "2", 3);
233 uint32_t lineNo = 0;
234 char *file = NULL;
235 ASSERT_TRUE(BSL_ERR_PeekLastErrorFileLine(NULL, &lineNo) == 1);
236 ASSERT_TRUE(lineNo == 0);
237 ASSERT_TRUE(BSL_ERR_PeekErrorFileLine(NULL, &lineNo) == 1);
238 ASSERT_TRUE(lineNo == 0);
239 ASSERT_TRUE(BSL_ERR_PeekLastErrorFileLine((const char **)&file, &lineNo) == 1);
240 ASSERT_TRUE(strcmp(file, "2") == 0);
241 ASSERT_TRUE(lineNo == 3);
242 lineNo = 0;
243 ASSERT_TRUE(BSL_ERR_PeekErrorFileLine((const char **)&file, &lineNo) == 1);
244 ASSERT_TRUE(strcmp(file, "2") == 0);
245 ASSERT_TRUE(lineNo == 3);
246 ASSERT_TRUE(BSL_ERR_GetError() == 1);
247 BSL_ERR_PUSH_ERROR(BSL_SUCCESS);
248
249 lineNo = 0;
250 BSL_ERR_PushError(1, NULL, 3);
251 ASSERT_TRUE(BSL_ERR_PeekLastErrorFileLine(NULL, &lineNo) == 1);
252 ASSERT_TRUE(lineNo == 0);
253 ASSERT_TRUE(BSL_ERR_PeekErrorFileLine(NULL, &lineNo) == 1);
254 ASSERT_TRUE(lineNo == 0);
255 ASSERT_TRUE(BSL_ERR_PeekLastErrorFileLine((const char **)&file, &lineNo) == 1);
256 ASSERT_TRUE(strcmp(file, "NA") == 0);
257 ASSERT_TRUE(lineNo == 0);
258 ASSERT_TRUE(BSL_ERR_PeekErrorFileLine((const char **)&file, &lineNo) == 1);
259 ASSERT_TRUE(strcmp(file, "NA") == 0);
260 ASSERT_TRUE(lineNo == 0);
261 EXIT:
262 BSL_ERR_ClearError();
263 BSL_ERR_DeInit();
264 }
265 /* END_CASE */
266
267 /**
268 * @test SDV_BSL_ERR_COMPATIBILITY_FUNC_TC001
269 * @title Test the compatibility of the err module in different CMake versions.
270 * @precon Set the memory allocation and release functions.
271 * @brief
272 * 1. Initializes BSL_ERR. Expected result 1 is obtained.
273 * 2. Construct an exception to trigger the ERR module to push the stack. Expected result 2 is obtained.
274 * 3. Obtains the push-stack information, Expected result 3 and 4 is obtained.
275 * @expect
276 * 1. BSL_SUCCESS
277 * 2. BSL_NULL_INPUT
278 * 3. "uio_abstraction.c"
279 * 4. BSL_NULL_INPUT
280 */
281 /* BEGIN_CASE */
SDV_BSL_ERR_COMPATIBILITY_FUNC_TC001(void)282 void SDV_BSL_ERR_COMPATIBILITY_FUNC_TC001(void)
283 {
284 #ifndef HITLS_BSL_UIO_PLT
285 SKIP_TEST();
286 #else
287 TestMemInit();
288 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_NEW_CB_FUNC, PthreadRWLockNew) == BSL_SUCCESS);
289 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_FREE_CB_FUNC, PthreadRWLockFree) == BSL_SUCCESS);
290 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_READ_LOCK_CB_FUNC, PthreadRWLockReadLock) == BSL_SUCCESS);
291 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_WRITE_LOCK_CB_FUNC, PthreadRWLockWriteLock) == BSL_SUCCESS);
292 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_UNLOCK_CB_FUNC, PthreadRWLockUnlock) == BSL_SUCCESS);
293 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_GET_ID_CB_FUNC, PthreadGetId) == BSL_SUCCESS);
294 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
295 // Construct an exception to trigger the error code module to push the stack.
296 ASSERT_TRUE(BSL_UIO_SetMethodType(NULL, 1) == BSL_NULL_INPUT);
297 char *file = NULL;
298 uint32_t line = 0;
299 int32_t err = BSL_ERR_GetLastErrorFileLine((const char **)&file, &line);
300 ASSERT_TRUE(strcmp(file, "uio_abstraction.c") == 0);
301 ASSERT_TRUE(err == BSL_NULL_INPUT);
302 EXIT:
303 BSL_ERR_ClearError();
304 BSL_ERR_DeInit();
305 #endif
306 }
307 /* END_CASE */
308
309 /**
310 * @test SDV_BSL_ERR_STACK_API_TC001
311 * @title BSL_ERR_ClearError interface testing
312 * @precon nan
313 * @brief
314 * 1.Call BSL_ERR_RemoveErrorStack to delete the stack. Expected result 2 is obtained.
315 * 2.Call BSL_ERR_ClearError to clear the stack. Expected result 3 is obtained.
316 * 3.Call BSL_ERR_ClearError repeatedly to clear the stack. Expected result 4 is obtained.
317 * @expect
318 * 1.BSL_SUCCESS
319 * 2.SUCCESS
320 * 3.SUCCESS
321 */
322 /* BEGIN_CASE */
SDV_BSL_ERR_STACK_API_TC001(int isRemoveAll)323 void SDV_BSL_ERR_STACK_API_TC001(int isRemoveAll)
324 {
325 TestMemInit();
326 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_NEW_CB_FUNC, PthreadRWLockNew) == BSL_SUCCESS);
327 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_FREE_CB_FUNC, PthreadRWLockFree) == BSL_SUCCESS);
328 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_READ_LOCK_CB_FUNC, PthreadRWLockReadLock) == BSL_SUCCESS);
329 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_WRITE_LOCK_CB_FUNC, PthreadRWLockWriteLock) == BSL_SUCCESS);
330 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_UNLOCK_CB_FUNC, PthreadRWLockUnlock) == BSL_SUCCESS);
331 ASSERT_TRUE(BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_GET_ID_CB_FUNC, PthreadGetId) == BSL_SUCCESS);
332 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
333
334 BSL_ERR_RemoveErrorStack((isRemoveAll == 1) ? true : false);
335 BSL_ERR_ClearError();
336 BSL_ERR_ClearError();
337 EXIT:
338 BSL_ERR_DeInit();
339 }
340 /* END_CASE */
341
342 /**
343 * @test SDV_BSL_ERR_MARK_FUNC_TC001
344 * @title Registering and Obtaining Error Descriptions
345 * @precon nan
346 * @brief
347 * 1. Set flags. Expected result 1 is obtained.
348 * 2. Push the error code on the stack. If no flag is set, invoke the pop to mark interface
349 * and obtain the latest error code. Expected result 2 is obtained.
350 * 3. Push three error codes into the stack and set a flag for the second error code. Expected result 3 is obtained.
351 * 4. Push three error codes into the stack, set flags for the second and third error codes, clear the latest flag,
352 * and invoke pop to mark to obtain the latest error code. Expected result 4 is obtained.
353 * @expect
354 * 1. Return BSL_ERR_ERR_NO_ERROR.
355 * 2. Return BSL_ERR_ERR_NO_MARK, and the error code is 0.
356 * 3. The flag is set successfully. The second error code is returned
357 * when the latest error code is obtained for the first time.
358 * The first error code is returned when the latest error code is obtained for the second time.
359 * 4. The latest error code is the second error code.
360 */
361 /* BEGIN_CASE */
SDV_BSL_ERR_MARK_FUNC_TC001(void)362 void SDV_BSL_ERR_MARK_FUNC_TC001(void)
363 {
364 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
365 int32_t ret;
366
367 ret = BSL_ERR_SetMark();
368 ASSERT_TRUE(ret == BSL_ERR_ERR_NO_STACK);
369
370 BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL);
371 BSL_ERR_PUSH_ERROR(BSL_UIO_IO_BUSY);
372 BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION);
373 ret = BSL_ERR_PopToMark();
374 ASSERT_TRUE(ret == BSL_ERR_ERR_NO_MARK);
375 ret = BSL_ERR_GetLastError();
376 ASSERT_TRUE(ret == BSL_SUCCESS);
377
378 BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL);
379 BSL_ERR_PUSH_ERROR(BSL_UIO_IO_BUSY);
380 ret = BSL_ERR_SetMark();
381 ASSERT_TRUE(ret == BSL_SUCCESS);
382 BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION);
383 ret = BSL_ERR_PopToMark();
384 ASSERT_TRUE(ret == BSL_SUCCESS);
385 ret = BSL_ERR_GetLastError();
386 ASSERT_TRUE(ret == BSL_UIO_IO_BUSY);
387 ret = BSL_ERR_GetLastError();
388 ASSERT_TRUE(ret == BSL_UIO_FAIL);
389 ret = BSL_ERR_GetLastError();
390 ASSERT_TRUE(ret == BSL_SUCCESS);
391
392 BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL);
393 BSL_ERR_PUSH_ERROR(BSL_UIO_IO_BUSY);
394 ret = BSL_ERR_SetMark();
395 ASSERT_TRUE(ret == BSL_SUCCESS);
396 BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION);
397 ret = BSL_ERR_SetMark();
398 ASSERT_TRUE(ret == BSL_SUCCESS);
399 ret = BSL_ERR_ClearLastMark();
400 ASSERT_TRUE(ret == BSL_SUCCESS);
401 ret = BSL_ERR_PopToMark();
402 ASSERT_TRUE(ret == BSL_SUCCESS);
403 ret = BSL_ERR_GetLastError();
404 ASSERT_TRUE(ret == BSL_UIO_IO_BUSY);
405 EXIT:
406 BSL_ERR_DeInit();
407 }
408 /* END_CASE */
409
410 /**
411 * @test SDV_BSL_ERR_STRING_FUNC_TC001
412 * @title Registering and Obtaining Error Descriptions
413 * @precon nan
414 * @brief
415 * 1. The registration list is empty. Expected result 1 is obtained.
416 * 2. The registration list is not empty. The number of registrations is 0. Expected result 2 is obtained.
417 * 3. The registration list is not empty, and the number of registrations is 2. Expected result 3 is obtained.
418 * 4. Obtains the first error description. Expected result 4 is obtained.
419 * 5. Obtains the second error description. Expected result 5 is obtained.
420 * @expect
421 * 1. Return BSL_NULL_INPUT.
422 * 2. Return BSL_NULL_INPUT.
423 * 3. Return BSL_SUCCESS.
424 * 4. The result is the first error description.
425 * 5. The result is the second error description.
426 */
427 /* BEGIN_CASE */
SDV_BSL_ERR_STRING_FUNC_TC001(void)428 void SDV_BSL_ERR_STRING_FUNC_TC001(void)
429 {
430 RegThreadFunc();
431 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
432
433 ASSERT_TRUE(BSL_ERR_AddErrStringBatch(NULL, 0) == BSL_NULL_INPUT);
434 ASSERT_TRUE(BSL_ERR_AddErrStringBatch((void *)-1, 0) == BSL_NULL_INPUT);
435
436 const char *uioFail = "uio is failed";
437 const char *tlvFail = "tlv needed type";
438 const BSL_ERR_Desc descList[] = {
439 {BSL_UIO_FAIL, uioFail},
440 {BSL_TLV_ERR_NO_WANT_TYPE, tlvFail},
441 };
442 ASSERT_TRUE(BSL_ERR_AddErrStringBatch(descList, 2) == BSL_SUCCESS);
443 ASSERT_TRUE(BSL_ERR_GetString(BSL_UIO_FAIL) == uioFail);
444 ASSERT_TRUE(BSL_ERR_GetString(BSL_TLV_ERR_NO_WANT_TYPE) == tlvFail);
445 EXIT:
446 BSL_ERR_RemoveErrStringBatch();
447 BSL_ERR_DeInit();
448 }
449 /* END_CASE */
450
451 /**
452 * @test SDV_BSL_ERR_AVLLR_FUNC_TC001
453 * @title A balanced binary tree is unbalanced due to insertion of nodes into the right subtree of the left subtree, and thus rotated to balance the test.
454 * @brief
455 * 1. insert 100
456 * 2. insert 120
457 * 3. insert 80
458 * 4. insert 70
459 * 5. insert 90
460 * 6. insert 85
461 * 7. delete 90
462 * 8. delete 200
463 * 9. delete 100
464 * 10. delete 120
465 * @expect
466 * 1. root node is 100
467 * 2. root node is 100
468 * 3. root node is 100
469 * 4. root node is 100
470 * 5. root node is 100
471 * 6. root node is 90
472 * 7. root node is 85
473 * 8. root node is 85
474 * 9. root node is 85
475 * 10. root node is 80
476 */
477 /* BEGIN_CASE */
SDV_BSL_ERR_AVLLR_FUNC_TC001(void)478 void SDV_BSL_ERR_AVLLR_FUNC_TC001(void)
479 {
480 TestMemInit();
481 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
482
483 BSL_AvlTree *root = NULL;
484
485 root = BSL_AVL_InsertNode(root, 100, BSL_AVL_MakeLeafNode(NULL));
486 ASSERT_TRUE(root->nodeId == 100);
487 root = BSL_AVL_InsertNode(root, 120, BSL_AVL_MakeLeafNode(NULL));
488 ASSERT_TRUE(root->nodeId == 100);
489 root = BSL_AVL_InsertNode(root, 80, BSL_AVL_MakeLeafNode(NULL));
490 ASSERT_TRUE(root->nodeId == 100);
491 root = BSL_AVL_InsertNode(root, 70, BSL_AVL_MakeLeafNode(NULL));
492 ASSERT_TRUE(root->nodeId == 100);
493 root = BSL_AVL_InsertNode(root, 90, BSL_AVL_MakeLeafNode(NULL));
494 ASSERT_TRUE(root->nodeId == 100);
495 root = BSL_AVL_InsertNode(root, 85, BSL_AVL_MakeLeafNode(NULL));
496 ASSERT_TRUE(root->nodeId == 90);
497
498 root = BSL_AVL_DeleteNode(root, 90, NULL);
499 ASSERT_TRUE(root->nodeId == 85);
500 root = BSL_AVL_DeleteNode(root, 200, NULL);
501 ASSERT_TRUE(root->nodeId == 85);
502 root = BSL_AVL_DeleteNode(root, 100, NULL);
503 ASSERT_TRUE(root->nodeId == 85);
504 root = BSL_AVL_DeleteNode(root, 120, NULL);
505 ASSERT_TRUE(root->nodeId == 80);
506
507 BSL_AVL_DeleteTree(root, NULL);
508
509 EXIT:
510 BSL_ERR_DeInit();
511 }
512 /* END_CASE */
513
514 /**
515 * @test SDV_BSL_ERR_AVLLL_FUNC_TC001
516 * @title A balanced binary tree is unbalanced due to insertion of nodes into the left subtree of the left subtree, and thus rotated to balance the test.
517 * @brief
518 * 1. insert 100
519 * 2. insert 120
520 * 3. insert 80
521 * 4. insert 70
522 * 5. insert 90
523 * 6. insert 65
524 * 7. find 90
525 * 8. find 67
526 * 9. delete 70
527 * 10. delete 65
528 * @expect
529 * 1. root node is 100
530 * 2. root node is 100
531 * 3. root node is 100
532 * 4. root node is 100
533 * 5. root node is 100, the structure of the tree is
534 100
535 / \
536 / \
537 80 120
538 / \
539 70 90
540 * 6. root node is 80, the structure of the tree is
541 80
542 / \
543 / \
544 70 100
545 / / \
546 65 90 120
547 * 7. find
548 * 8. Couldn't find
549 * 9. root node is 80
550 * 10. root node is 100
551 */
552 /* BEGIN_CASE */
SDV_BSL_ERR_AVLLL_FUNC_TC001(void)553 void SDV_BSL_ERR_AVLLL_FUNC_TC001(void)
554 {
555 TestMemInit();
556 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
557 BSL_AvlTree *root = NULL;
558
559 root = BSL_AVL_InsertNode(root, 100, BSL_AVL_MakeLeafNode(NULL));
560 ASSERT_TRUE(root->nodeId == 100);
561 root = BSL_AVL_InsertNode(root, 120, BSL_AVL_MakeLeafNode(NULL));
562 ASSERT_TRUE(root->nodeId == 100);
563 root = BSL_AVL_InsertNode(root, 80, BSL_AVL_MakeLeafNode(NULL));
564 ASSERT_TRUE(root->nodeId == 100);
565 root = BSL_AVL_InsertNode(root, 70, BSL_AVL_MakeLeafNode(NULL));
566 ASSERT_TRUE(root->nodeId == 100);
567 root = BSL_AVL_InsertNode(root, 90, BSL_AVL_MakeLeafNode(NULL));
568 ASSERT_TRUE(root->nodeId == 100);
569 root = BSL_AVL_InsertNode(root, 65, BSL_AVL_MakeLeafNode(NULL));
570 ASSERT_TRUE(root->nodeId == 80);
571
572 ASSERT_TRUE(BSL_AVL_SearchNode(root, 90) != NULL);
573 ASSERT_TRUE(BSL_AVL_SearchNode(root, 67) == NULL);
574
575 root = BSL_AVL_DeleteNode(root, 70, NULL);
576 ASSERT_TRUE(root->nodeId == 80);
577 root = BSL_AVL_DeleteNode(root, 65, NULL);
578 ASSERT_TRUE(root->nodeId == 100);
579
580 BSL_AVL_DeleteTree(root, NULL);
581
582 EXIT:
583 BSL_ERR_DeInit();
584 }
585 /* END_CASE */
586
587 /**
588 * @test SDV_BSL_ERR_AVLRL_FUNC_TC001
589 * @title A balanced binary tree is unbalanced due to insertion of nodes into the left subtree of the right subtree, and thus rotated to balance the test.
590 * @brief
591 * 1. insert 100
592 * 2. insert 80
593 * 3. insert 120
594 * 4. insert 110
595 * 5. insert 130
596 * 6. insert 105
597 * @expect
598 * 1. root node is 100
599 * 2. root node is 100
600 * 3. root node is 100
601 * 4. root node is 100
602 * 5. root node is 100
603 * 6. root node is 110
604 */
605 /* BEGIN_CASE */
SDV_BSL_ERR_AVLRL_FUNC_TC001(void)606 void SDV_BSL_ERR_AVLRL_FUNC_TC001(void)
607 {
608 TestMemInit();
609 ASSERT_TRUE(BSL_ERR_Init() == BSL_SUCCESS);
610 BSL_AvlTree *root = NULL;
611
612 root = BSL_AVL_InsertNode(root, 100, BSL_AVL_MakeLeafNode(NULL));
613 ASSERT_TRUE(root->nodeId == 100);
614 root = BSL_AVL_InsertNode(root, 80, BSL_AVL_MakeLeafNode(NULL));
615 ASSERT_TRUE(root->nodeId == 100);
616 root = BSL_AVL_InsertNode(root, 120, BSL_AVL_MakeLeafNode(NULL));
617 ASSERT_TRUE(root->nodeId == 100);
618 root = BSL_AVL_InsertNode(root, 110, BSL_AVL_MakeLeafNode(NULL));
619 ASSERT_TRUE(root->nodeId == 100);
620 root = BSL_AVL_InsertNode(root, 130, BSL_AVL_MakeLeafNode(NULL));
621 ASSERT_TRUE(root->nodeId == 100);
622 root = BSL_AVL_InsertNode(root, 105, BSL_AVL_MakeLeafNode(NULL));
623 ASSERT_TRUE(root->nodeId == 110);
624
625 BSL_AVL_DeleteTree(root, NULL);
626
627 EXIT:
628 BSL_ERR_DeInit();
629 }
630 /* END_CASE */
631
632
633