1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "It_test_misc.h"
32
33 static void *g_root = NULL;
34
Xmalloc(unsigned n)35 static void *Xmalloc(unsigned n)
36 {
37 void *p = NULL;
38 if (n <= 0) {
39 printf("err: malloc size invaild\n");
40 exit(EXIT_FAILURE);
41 }
42 p = malloc(n);
43 if (p) {
44 return p;
45 }
46 fprintf(stderr, "insufficient memory\n");
47 exit(EXIT_FAILURE);
48 }
49
Compare(const void * pa,const void * pb)50 static int Compare(const void *pa, const void *pb)
51 {
52 if (*(int *)pa < *(int *)pb) {
53 return -1;
54 }
55 if (*(int *)pa > *(int *)pb) {
56 return 1;
57 }
58 return 0;
59 }
60
Action(const void * nodep,VISIT which,int depth)61 static void Action(const void *nodep, VISIT which, int depth)
62 {
63 int *datap = NULL;
64
65 switch (which) {
66 case preorder:
67 break;
68 case postorder:
69 datap = *(int **)nodep;
70 break;
71 case endorder:
72 break;
73 case leaf:
74 datap = *(int **)nodep;
75 break;
76 }
77 }
78
TestCase(VOID)79 static UINT32 TestCase(VOID)
80 {
81 int i;
82 int *ptr = NULL;
83 void *val = NULL;
84
85 srand(time(NULL));
86 for (i = 0; i < 12; i++) {
87 ptr = (int *)Xmalloc(sizeof(int));
88 *ptr = rand() & 0xff;
89 val = tsearch((void *)ptr, &g_root, Compare);
90 if (val == NULL)
91 exit(EXIT_FAILURE);
92 else if ((*(int **)val) != ptr)
93 free(ptr);
94 }
95 twalk(g_root, Action);
96 tdestroy(g_root, free);
97
98 return 0;
99 }
100
ItTestUtil007(VOID)101 VOID ItTestUtil007(VOID)
102 {
103 TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
104 }
105