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 invalid\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 (*static_cast<int *>(const_cast<void *>(pa)) < *static_cast<int *>(const_cast<void *>(pb))) {
53 return -1;
54 }
55 if (*static_cast<int *>(const_cast<void *>(pa)) > *static_cast<int *>(const_cast<void *>(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 = *static_cast<int **>(const_cast<void *>(nodep));
70 break;
71 case endorder:
72 break;
73 case leaf:
74 datap = *static_cast<int **>(const_cast<void *>(nodep));
75 break;
76 default:
77 break;
78 }
79 }
80
TestCase(VOID)81 static UINT32 TestCase(VOID)
82 {
83 int i;
84 int *ptr = NULL;
85 void *val = NULL;
86
87 srand(time(NULL));
88 for (i = 0; i < 12; i++) {
89 ptr = (int *)Xmalloc(sizeof(int));
90 *ptr = rand() & 0xff;
91 val = tsearch(static_cast<void *>(ptr), &g_root, Compare);
92 if (val == NULL) {
93 exit(EXIT_FAILURE);
94 } else if ((*(int **)val) != ptr) {
95 free(ptr);
96 }
97 }
98
99 twalk(g_root, Action);
100 tdestroy(g_root, free);
101
102 return 0;
103 }
104
ItTestUtil007(VOID)105 VOID ItTestUtil007(VOID)
106 {
107 TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
108 }
109