1 /* Test program for tsearch et al.
2 Copyright (C) 1997, 2000-2001, 2007-2008 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 License as
7 published by the Free Software Foundation; either version 3 of the
8 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 License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 /* The system's tsearch() is not expected to keep the tree balanced. */
32 /* The gnulib replacement tsearch() should keep the tree balanced. */
45 #define random() (int) ((unsigned int) rand () >> 3)
62 /* Set to 1 if a test is flunked. */
65 /* The keys we add to the tree. */
68 /* Pointers into the key array, possibly permutated, to define an order
69 for insertion/removal. */
72 /* Flags set for each element visited during a tree walk. */
75 /* Depths for all the elements, to check that the depth is constant for
77 static int depths[SIZE];
79 /* Maximum depth during a tree walk. */
82 /* Compare two keys. */
84 cmp_fn (const void *a, const void *b)
86 return *(const int *) a - *(const int *) b;
89 /* Permute an array of integers. */
95 for (i = 0; i < SIZE; ++i)
100 j = random () % SIZE;
103 string[i] = string[j];
109 walk_action (const void *nodep, const VISIT which, const int depth)
111 int key = **(int **) nodep;
113 if (depth > max_depth)
115 if (which == leaf || which == preorder)
122 if (depths[key] != depth)
124 fputs ("Depth for one element is not constant during tree walk.\n",
131 walk_tree (void *root, int expected_count)
135 memset (z, 0, sizeof z);
138 twalk (root, walk_action);
139 for (i = 0; i < expected_count; ++i)
142 fputs ("Node was not visited.\n", stdout);
147 if (max_depth > log (expected_count) * 2 + 2)
149 if (max_depth > expected_count)
152 fputs ("Depth too large during tree walk.\n", stdout);
157 /* Perform an operation on a tree. */
159 mangle_tree (enum order how, enum action what, void **root, int lag)
163 if (how == randomorder)
165 for (i = 0; i < SIZE; ++i)
170 for (i = 0; i < SIZE + lag; ++i)
181 /* Ensure that the array index is within bounds. */
182 k = y[(SIZE - i - 1 + lag) % SIZE];
192 k = SIZE - i - 1 + lag;
197 /* This never should happen, but gcc isn't smart enough to
208 if (tfind (x + j, (void *const *) root, cmp_fn) != NULL)
210 fputs ("Found element which is not in tree yet.\n", stdout);
213 elem = tsearch (x + j, root, cmp_fn);
215 || tfind (x + j, (void *const *) root, cmp_fn) == NULL)
217 fputs ("Couldn't find element after it was added.\n",
223 if (what == build || i < lag)
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);
239 if (tfind (x + j, (void *const *) root, cmp_fn) == NULL)
241 fputs ("Couldn't find element after it was added.\n", stdout);
252 main (int argc, char **argv)
255 static char state[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
260 initstate (SEED, state, 8);
263 for (i = 0; i < SIZE; ++i)
266 /* Do this loop several times to get different permutations for the
268 fputs ("Series I\n", stdout);
269 for (i = 0; i < PASSES; ++i)
271 fprintf (stdout, "Pass %d... ", i + 1);
275 mangle_tree (ascending, build, &root, 0);
276 mangle_tree (ascending, find, &root, 0);
277 mangle_tree (descending, find, &root, 0);
278 mangle_tree (randomorder, find, &root, 0);
279 walk_tree (root, SIZE);
280 mangle_tree (ascending, delete, &root, 0);
282 mangle_tree (ascending, build, &root, 0);
283 walk_tree (root, SIZE);
284 mangle_tree (descending, delete, &root, 0);
286 mangle_tree (ascending, build, &root, 0);
287 walk_tree (root, SIZE);
288 mangle_tree (randomorder, delete, &root, 0);
290 mangle_tree (descending, build, &root, 0);
291 mangle_tree (ascending, find, &root, 0);
292 mangle_tree (descending, find, &root, 0);
293 mangle_tree (randomorder, find, &root, 0);
294 walk_tree (root, SIZE);
295 mangle_tree (descending, delete, &root, 0);
297 mangle_tree (descending, build, &root, 0);
298 walk_tree (root, SIZE);
299 mangle_tree (descending, delete, &root, 0);
301 mangle_tree (descending, build, &root, 0);
302 walk_tree (root, SIZE);
303 mangle_tree (randomorder, delete, &root, 0);
305 mangle_tree (randomorder, build, &root, 0);
306 mangle_tree (ascending, find, &root, 0);
307 mangle_tree (descending, find, &root, 0);
308 mangle_tree (randomorder, find, &root, 0);
309 walk_tree (root, SIZE);
310 mangle_tree (randomorder, delete, &root, 0);
312 for (j = 1; j < SIZE; j *= 2)
314 mangle_tree (randomorder, build_and_del, &root, j);
317 fputs (error ? " failed!\n" : " ok.\n", stdout);
318 total_error |= error;
321 fputs ("Series II\n", stdout);
322 for (i = 1; i < SIZE; i *= 2)
324 fprintf (stdout, "For size %d... ", 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);
334 mangle_tree (ascending, build_and_del, &root, i);
335 mangle_tree (descending, build_and_del, &root, i);
337 fputs (error ? " failed!\n" : " ok.\n", stdout);
338 total_error |= error;