* sysdeps/unix/sysv/linux/m68k/getpagesize.c: Fix last change.
[glibc.git] / misc / tst-tsearch.c
blob722b6d79074d568a48cd10a3efdae85e5644c494
1 /* Test program for tsearch et al.
2 Copyright (C) 1997, 2000, 2001 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
18 02111-1307 USA. */
20 #ifndef _GNU_SOURCE
21 # define _GNU_SOURCE 1
22 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <search.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 /* Compare two keys. */
76 static int
77 cmp_fn (const void *a, const void *b)
79 return *(const int *) a - *(const int *) b;
82 /* Permute an array of integers. */
83 static void
84 memfry (int *string)
86 int i;
88 for (i = 0; i < SIZE; ++i)
90 int32_t j;
91 int c;
93 j = random () % SIZE;
95 c = string[i];
96 string[i] = string[j];
97 string[j] = c;
101 static void
102 walk_action (const void *nodep, const VISIT which, const int depth)
104 int key = **(int **) nodep;
106 if (depth > max_depth)
107 max_depth = depth;
108 if (which == leaf || which == preorder)
110 ++z[key];
111 depths[key] = depth;
113 else
115 if (depths[key] != depth)
117 fputs ("Depth for one element is not constant during tree walk.\n",
118 stdout);
123 static void
124 walk_tree (void *root, int expected_count)
126 int i;
128 memset (z, 0, sizeof z);
129 max_depth = 0;
131 twalk (root, walk_action);
132 for (i = 0; i < expected_count; ++i)
133 if (z[i] != 1)
135 fputs ("Node was not visited.\n", stdout);
136 error = 1;
139 #if BALANCED
140 if (max_depth > log (expected_count) * 2 + 2)
141 #else
142 if (max_depth > expected_count)
143 #endif
145 fputs ("Depth too large during tree walk.\n", stdout);
146 error = 1;
150 /* Perform an operation on a tree. */
151 static void
152 mangle_tree (enum order how, enum action what, void **root, int lag)
154 int i;
156 if (how == randomorder)
158 for (i = 0; i < SIZE; ++i)
159 y[i] = i;
160 memfry (y);
163 for (i = 0; i < SIZE + lag; ++i)
165 void *elem;
166 int j, k;
168 switch (how)
170 case randomorder:
171 if (i >= lag)
172 k = y[i - lag];
173 else
174 /* Ensure that the array index is within bounds. */
175 k = y[(SIZE - i - 1 + lag) % SIZE];
176 j = y[i % SIZE];
177 break;
179 case ascending:
180 k = i - lag;
181 j = i;
182 break;
184 case descending:
185 k = SIZE - i - 1 + lag;
186 j = SIZE - i - 1;
187 break;
189 default:
190 /* This never should happen, but gcc isn't smart enough to
191 recognize it. */
192 abort ();
195 switch (what)
197 case build_and_del:
198 case build:
199 if (i < SIZE)
201 if (tfind (x + j, (void *const *) root, cmp_fn) != NULL)
203 fputs ("Found element which is not in tree yet.\n", stdout);
204 error = 1;
206 elem = tsearch (x + j, root, cmp_fn);
207 if (elem == 0
208 || tfind (x + j, (void *const *) root, cmp_fn) == NULL)
210 fputs ("Couldn't find element after it was added.\n",
211 stdout);
212 error = 1;
216 if (what == build || i < lag)
217 break;
219 j = k;
220 /* fall through */
222 case delete:
223 elem = tfind (x + j, (void *const *) root, cmp_fn);
224 if (elem == NULL || tdelete (x + j, root, cmp_fn) == NULL)
226 fputs ("Error deleting element.\n", stdout);
227 error = 1;
229 break;
231 case find:
232 if (tfind (x + j, (void *const *) root, cmp_fn) == NULL)
234 fputs ("Couldn't find element after it was added.\n", stdout);
235 error = 1;
237 break;
245 main (int argc, char **argv)
247 int total_error = 0;
248 static char state[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
249 void *root = NULL;
250 int i, j;
252 initstate (SEED, state, 8);
254 for (i = 0; i < SIZE; ++i)
255 x[i] = i;
257 /* Do this loop several times to get different permutations for the
258 random case. */
259 fputs ("Series I\n", stdout);
260 for (i = 0; i < PASSES; ++i)
262 fprintf (stdout, "Pass %d... ", i + 1);
263 fflush (stdout);
264 error = 0;
266 mangle_tree (ascending, build, &root, 0);
267 mangle_tree (ascending, find, &root, 0);
268 mangle_tree (descending, find, &root, 0);
269 mangle_tree (randomorder, find, &root, 0);
270 walk_tree (root, SIZE);
271 mangle_tree (ascending, delete, &root, 0);
273 mangle_tree (ascending, build, &root, 0);
274 walk_tree (root, SIZE);
275 mangle_tree (descending, delete, &root, 0);
277 mangle_tree (ascending, build, &root, 0);
278 walk_tree (root, SIZE);
279 mangle_tree (randomorder, delete, &root, 0);
281 mangle_tree (descending, build, &root, 0);
282 mangle_tree (ascending, find, &root, 0);
283 mangle_tree (descending, find, &root, 0);
284 mangle_tree (randomorder, find, &root, 0);
285 walk_tree (root, SIZE);
286 mangle_tree (descending, delete, &root, 0);
288 mangle_tree (descending, build, &root, 0);
289 walk_tree (root, SIZE);
290 mangle_tree (descending, delete, &root, 0);
292 mangle_tree (descending, build, &root, 0);
293 walk_tree (root, SIZE);
294 mangle_tree (randomorder, delete, &root, 0);
296 mangle_tree (randomorder, build, &root, 0);
297 mangle_tree (ascending, find, &root, 0);
298 mangle_tree (descending, find, &root, 0);
299 mangle_tree (randomorder, find, &root, 0);
300 walk_tree (root, SIZE);
301 mangle_tree (randomorder, delete, &root, 0);
303 for (j = 1; j < SIZE; j *= 2)
305 mangle_tree (randomorder, build_and_del, &root, j);
308 fputs (error ? " failed!\n" : " ok.\n", stdout);
309 total_error |= error;
312 fputs ("Series II\n", stdout);
313 for (i = 1; i < SIZE; i *= 2)
315 fprintf (stdout, "For size %d... ", i);
316 fflush (stdout);
317 error = 0;
319 mangle_tree (ascending, build_and_del, &root, i);
320 mangle_tree (descending, build_and_del, &root, i);
321 mangle_tree (ascending, build_and_del, &root, i);
322 mangle_tree (descending, build_and_del, &root, i);
323 mangle_tree (ascending, build_and_del, &root, i);
324 mangle_tree (descending, build_and_del, &root, i);
325 mangle_tree (ascending, build_and_del, &root, i);
326 mangle_tree (descending, build_and_del, &root, i);
328 fputs (error ? " failed!\n" : " ok.\n", stdout);
329 total_error |= error;
332 return total_error;