1 /* Test program for tsearch et al.
2 Copyright (C) 1997, 2000, 2001, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 /* The system's tsearch() is not expected to keep the tree balanced. */
34 /* The gnulib replacement tsearch() should keep the tree balanced. */
47 #define random() (int) ((unsigned int) rand () >> 3)
64 /* Set to 1 if a test is flunked. */
67 /* The keys we add to the tree. */
70 /* Pointers into the key array, possibly permutated, to define an order
71 for insertion/removal. */
74 /* Flags set for each element visited during a tree walk. */
77 /* Depths for all the elements, to check that the depth is constant for
79 static int depths[SIZE];
81 /* Maximum depth during a tree walk. */
84 /* Compare two keys. */
86 cmp_fn (const void *a, const void *b)
88 return *(const int *) a - *(const int *) b;
91 /* Permute an array of integers. */
97 for (i = 0; i < SIZE; ++i)
102 j = random () % SIZE;
105 string[i] = string[j];
111 walk_action (const void *nodep, const VISIT which, const int depth)
113 int key = **(int **) nodep;
115 if (depth > max_depth)
117 if (which == leaf || which == preorder)
124 if (depths[key] != depth)
126 fputs ("Depth for one element is not constant during tree walk.\n",
133 walk_tree (void *root, int expected_count)
137 memset (z, 0, sizeof z);
140 twalk (root, walk_action);
141 for (i = 0; i < expected_count; ++i)
144 fputs ("Node was not visited.\n", stdout);
149 if (max_depth > log (expected_count) * 2 + 2)
151 if (max_depth > expected_count)
154 fputs ("Depth too large during tree walk.\n", stdout);
159 /* Perform an operation on a tree. */
161 mangle_tree (enum order how, enum action what, void **root, int lag)
165 if (how == randomorder)
167 for (i = 0; i < SIZE; ++i)
172 for (i = 0; i < SIZE + lag; ++i)
183 /* Ensure that the array index is within bounds. */
184 k = y[(SIZE - i - 1 + lag) % SIZE];
194 k = SIZE - i - 1 + lag;
199 /* This never should happen, but gcc isn't smart enough to
210 if (tfind (x + j, (void *const *) root, cmp_fn) != NULL)
212 fputs ("Found element which is not in tree yet.\n", stdout);
215 elem = tsearch (x + j, root, cmp_fn);
217 || tfind (x + j, (void *const *) root, cmp_fn) == NULL)
219 fputs ("Couldn't find element after it was added.\n",
225 if (what == build || i < lag)
232 elem = tfind (x + j, (void *const *) root, cmp_fn);
233 if (elem == NULL || tdelete (x + j, root, cmp_fn) == NULL)
235 fputs ("Error deleting element.\n", stdout);
241 if (tfind (x + j, (void *const *) root, cmp_fn) == NULL)
243 fputs ("Couldn't find element after it was added.\n", stdout);
254 main (int argc, char **argv)
257 static char state[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
261 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;