hurd: remove non-standard siginfo symbol
[glibc.git] / misc / tst-tsearch.c
blob7d164f9a0aba2c64901b963a2eac59d087361ed8
1 /* Test program for tsearch et al.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _GNU_SOURCE
20 # define _GNU_SOURCE 1
21 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <search.h>
27 #include <tst-stack-align.h>
29 #define SEED 0
30 #define BALANCED 1
31 #define PASSES 100
33 #if BALANCED
34 #include <math.h>
35 #define SIZE 1000
36 #else
37 #define SIZE 100
38 #endif
40 enum order
42 ascending,
43 descending,
44 randomorder
47 enum action
49 build,
50 build_and_del,
51 delete,
52 find
55 /* Set to 1 if a test is flunked. */
56 static int error = 0;
58 /* The keys we add to the tree. */
59 static int x[SIZE];
61 /* Pointers into the key array, possibly permutated, to define an order
62 for insertion/removal. */
63 static int y[SIZE];
65 /* Flags set for each element visited during a tree walk. */
66 static int z[SIZE];
68 /* Depths for all the elements, to check that the depth is constant for
69 all three visits. */
70 static int depths[SIZE];
72 /* Maximum depth during a tree walk. */
73 static int max_depth;
75 static int stack_align_check[2];
77 /* Compare two keys. */
78 static int
79 cmp_fn (const void *a, const void *b)
81 if (!stack_align_check[0])
82 stack_align_check[0] = TEST_STACK_ALIGN () ? -1 : 1;
83 return *(const int *) a - *(const int *) b;
86 /* Permute an array of integers. */
87 static void
88 memfry (int *string)
90 int i;
92 for (i = 0; i < SIZE; ++i)
94 int32_t j;
95 int c;
97 j = random () % SIZE;
99 c = string[i];
100 string[i] = string[j];
101 string[j] = c;
105 static void
106 walk_action (const void *nodep, const VISIT which, const int depth)
108 int key = **(int **) nodep;
110 if (!stack_align_check[1])
111 stack_align_check[1] = TEST_STACK_ALIGN () ? -1 : 1;
113 if (depth > max_depth)
114 max_depth = depth;
115 if (which == leaf || which == preorder)
117 ++z[key];
118 depths[key] = depth;
120 else
122 if (depths[key] != depth)
124 fputs ("Depth for one element is not constant during tree walk.\n",
125 stdout);
130 static void
131 walk_tree (void *root, int expected_count)
133 int i;
135 memset (z, 0, sizeof z);
136 max_depth = 0;
138 twalk (root, walk_action);
139 for (i = 0; i < expected_count; ++i)
140 if (z[i] != 1)
142 fputs ("Node was not visited.\n", stdout);
143 error = 1;
146 #if BALANCED
147 if (max_depth > log (expected_count) * 2 + 2)
148 #else
149 if (max_depth > expected_count)
150 #endif
152 fputs ("Depth too large during tree walk.\n", stdout);
153 error = 1;
157 /* Perform an operation on a tree. */
158 static void
159 mangle_tree (enum order how, enum action what, void **root, int lag)
161 int i;
163 if (how == randomorder)
165 for (i = 0; i < SIZE; ++i)
166 y[i] = i;
167 memfry (y);
170 for (i = 0; i < SIZE + lag; ++i)
172 void *elem;
173 int j, k;
175 switch (how)
177 case randomorder:
178 if (i >= lag)
179 k = y[i - lag];
180 else
181 /* Ensure that the array index is within bounds. */
182 k = y[(SIZE - i - 1 + lag) % SIZE];
183 j = y[i % SIZE];
184 break;
186 case ascending:
187 k = i - lag;
188 j = i;
189 break;
191 case descending:
192 k = SIZE - i - 1 + lag;
193 j = SIZE - i - 1;
194 break;
196 default:
197 /* This never should happen, but gcc isn't smart enough to
198 recognize it. */
199 abort ();
202 switch (what)
204 case build_and_del:
205 case build:
206 if (i < SIZE)
208 if (tfind (x + j, (void *const *) root, cmp_fn) != NULL)
210 fputs ("Found element which is not in tree yet.\n", stdout);
211 error = 1;
213 elem = tsearch (x + j, root, cmp_fn);
214 if (elem == 0
215 || tfind (x + j, (void *const *) root, cmp_fn) == NULL)
217 fputs ("Couldn't find element after it was added.\n",
218 stdout);
219 error = 1;
223 if (what == build || i < lag)
224 break;
226 j = k;
227 /* fall through */
229 case delete:
230 elem = tfind (x + j, (void *const *) root, cmp_fn);
231 if (elem == NULL || tdelete (x + j, root, cmp_fn) == NULL)
233 fputs ("Error deleting element.\n", stdout);
234 error = 1;
236 break;
238 case find:
239 if (tfind (x + j, (void *const *) root, cmp_fn) == NULL)
241 fputs ("Couldn't find element after it was added.\n", stdout);
242 error = 1;
244 break;
251 static int
252 do_test (void)
254 int total_error = 0;
255 static char state[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
256 void *root = NULL;
257 int i, j;
259 initstate (SEED, state, 8);
261 for (i = 0; i < SIZE; ++i)
262 x[i] = i;
264 /* Do this loop several times to get different permutations for the
265 random case. */
266 fputs ("Series I\n", stdout);
267 for (i = 0; i < PASSES; ++i)
269 fprintf (stdout, "Pass %d... ", i + 1);
270 fflush (stdout);
271 error = 0;
273 mangle_tree (ascending, build, &root, 0);
274 mangle_tree (ascending, find, &root, 0);
275 mangle_tree (descending, find, &root, 0);
276 mangle_tree (randomorder, find, &root, 0);
277 walk_tree (root, SIZE);
278 mangle_tree (ascending, delete, &root, 0);
280 mangle_tree (ascending, build, &root, 0);
281 walk_tree (root, SIZE);
282 mangle_tree (descending, delete, &root, 0);
284 mangle_tree (ascending, build, &root, 0);
285 walk_tree (root, SIZE);
286 mangle_tree (randomorder, delete, &root, 0);
288 mangle_tree (descending, build, &root, 0);
289 mangle_tree (ascending, find, &root, 0);
290 mangle_tree (descending, find, &root, 0);
291 mangle_tree (randomorder, find, &root, 0);
292 walk_tree (root, SIZE);
293 mangle_tree (descending, delete, &root, 0);
295 mangle_tree (descending, build, &root, 0);
296 walk_tree (root, SIZE);
297 mangle_tree (descending, delete, &root, 0);
299 mangle_tree (descending, build, &root, 0);
300 walk_tree (root, SIZE);
301 mangle_tree (randomorder, delete, &root, 0);
303 mangle_tree (randomorder, build, &root, 0);
304 mangle_tree (ascending, find, &root, 0);
305 mangle_tree (descending, find, &root, 0);
306 mangle_tree (randomorder, find, &root, 0);
307 walk_tree (root, SIZE);
308 mangle_tree (randomorder, delete, &root, 0);
310 for (j = 1; j < SIZE; j *= 2)
312 mangle_tree (randomorder, build_and_del, &root, j);
315 fputs (error ? " failed!\n" : " ok.\n", stdout);
316 total_error |= error;
319 fputs ("Series II\n", stdout);
320 for (i = 1; i < SIZE; i *= 2)
322 fprintf (stdout, "For size %d... ", i);
323 fflush (stdout);
324 error = 0;
326 mangle_tree (ascending, build_and_del, &root, i);
327 mangle_tree (descending, build_and_del, &root, i);
328 mangle_tree (ascending, build_and_del, &root, i);
329 mangle_tree (descending, build_and_del, &root, i);
330 mangle_tree (ascending, build_and_del, &root, i);
331 mangle_tree (descending, build_and_del, &root, i);
332 mangle_tree (ascending, build_and_del, &root, i);
333 mangle_tree (descending, build_and_del, &root, i);
335 fputs (error ? " failed!\n" : " ok.\n", stdout);
336 total_error |= error;
339 for (i = 0; i < 2; ++i)
340 if (stack_align_check[i] == 0)
342 printf ("stack alignment check %d not run\n", i);
343 total_error |= 1;
345 else if (stack_align_check[i] != 1)
347 printf ("stack insufficiently aligned in check %d\n", i);
348 total_error |= 1;
351 return total_error;
354 #define TEST_FUNCTION do_test ()
355 #include "../test-skeleton.c"