2.9
[glibc/nacl-glibc.git] / misc / tst-tsearch.c
blob66f76ac884f0e920a30756d586178ac749dd3d7c
1 /* Test program for tsearch et al.
2 Copyright (C) 1997, 2000, 2001, 2003 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>
28 #include <tst-stack-align.h>
30 #define SEED 0
31 #define BALANCED 1
32 #define PASSES 100
34 #if BALANCED
35 #include <math.h>
36 #define SIZE 1000
37 #else
38 #define SIZE 100
39 #endif
41 enum order
43 ascending,
44 descending,
45 randomorder
48 enum action
50 build,
51 build_and_del,
52 delete,
53 find
56 /* Set to 1 if a test is flunked. */
57 static int error = 0;
59 /* The keys we add to the tree. */
60 static int x[SIZE];
62 /* Pointers into the key array, possibly permutated, to define an order
63 for insertion/removal. */
64 static int y[SIZE];
66 /* Flags set for each element visited during a tree walk. */
67 static int z[SIZE];
69 /* Depths for all the elements, to check that the depth is constant for
70 all three visits. */
71 static int depths[SIZE];
73 /* Maximum depth during a tree walk. */
74 static int max_depth;
76 static int stack_align_check[2];
78 /* Compare two keys. */
79 static int
80 cmp_fn (const void *a, const void *b)
82 if (!stack_align_check[0])
83 stack_align_check[0] = TEST_STACK_ALIGN () ? -1 : 1;
84 return *(const int *) a - *(const int *) b;
87 /* Permute an array of integers. */
88 static void
89 memfry (int *string)
91 int i;
93 for (i = 0; i < SIZE; ++i)
95 int32_t j;
96 int c;
98 j = random () % SIZE;
100 c = string[i];
101 string[i] = string[j];
102 string[j] = c;
106 static void
107 walk_action (const void *nodep, const VISIT which, const int depth)
109 int key = **(int **) nodep;
111 if (!stack_align_check[1])
112 stack_align_check[1] = TEST_STACK_ALIGN () ? -1 : 1;
114 if (depth > max_depth)
115 max_depth = depth;
116 if (which == leaf || which == preorder)
118 ++z[key];
119 depths[key] = depth;
121 else
123 if (depths[key] != depth)
125 fputs ("Depth for one element is not constant during tree walk.\n",
126 stdout);
131 static void
132 walk_tree (void *root, int expected_count)
134 int i;
136 memset (z, 0, sizeof z);
137 max_depth = 0;
139 twalk (root, walk_action);
140 for (i = 0; i < expected_count; ++i)
141 if (z[i] != 1)
143 fputs ("Node was not visited.\n", stdout);
144 error = 1;
147 #if BALANCED
148 if (max_depth > log (expected_count) * 2 + 2)
149 #else
150 if (max_depth > expected_count)
151 #endif
153 fputs ("Depth too large during tree walk.\n", stdout);
154 error = 1;
158 /* Perform an operation on a tree. */
159 static void
160 mangle_tree (enum order how, enum action what, void **root, int lag)
162 int i;
164 if (how == randomorder)
166 for (i = 0; i < SIZE; ++i)
167 y[i] = i;
168 memfry (y);
171 for (i = 0; i < SIZE + lag; ++i)
173 void *elem;
174 int j, k;
176 switch (how)
178 case randomorder:
179 if (i >= lag)
180 k = y[i - lag];
181 else
182 /* Ensure that the array index is within bounds. */
183 k = y[(SIZE - i - 1 + lag) % SIZE];
184 j = y[i % SIZE];
185 break;
187 case ascending:
188 k = i - lag;
189 j = i;
190 break;
192 case descending:
193 k = SIZE - i - 1 + lag;
194 j = SIZE - i - 1;
195 break;
197 default:
198 /* This never should happen, but gcc isn't smart enough to
199 recognize it. */
200 abort ();
203 switch (what)
205 case build_and_del:
206 case build:
207 if (i < SIZE)
209 if (tfind (x + j, (void *const *) root, cmp_fn) != NULL)
211 fputs ("Found element which is not in tree yet.\n", stdout);
212 error = 1;
214 elem = tsearch (x + j, root, cmp_fn);
215 if (elem == 0
216 || tfind (x + j, (void *const *) root, cmp_fn) == NULL)
218 fputs ("Couldn't find element after it was added.\n",
219 stdout);
220 error = 1;
224 if (what == build || i < lag)
225 break;
227 j = k;
228 /* fall through */
230 case delete:
231 elem = tfind (x + j, (void *const *) root, cmp_fn);
232 if (elem == NULL || tdelete (x + j, root, cmp_fn) == NULL)
234 fputs ("Error deleting element.\n", stdout);
235 error = 1;
237 break;
239 case find:
240 if (tfind (x + j, (void *const *) root, cmp_fn) == NULL)
242 fputs ("Couldn't find element after it was added.\n", stdout);
243 error = 1;
245 break;
253 main (int argc, char **argv)
255 int total_error = 0;
256 static char state[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
257 void *root = NULL;
258 int i, j;
260 initstate (SEED, state, 8);
262 for (i = 0; i < SIZE; ++i)
263 x[i] = i;
265 /* Do this loop several times to get different permutations for the
266 random case. */
267 fputs ("Series I\n", stdout);
268 for (i = 0; i < PASSES; ++i)
270 fprintf (stdout, "Pass %d... ", i + 1);
271 fflush (stdout);
272 error = 0;
274 mangle_tree (ascending, build, &root, 0);
275 mangle_tree (ascending, find, &root, 0);
276 mangle_tree (descending, find, &root, 0);
277 mangle_tree (randomorder, find, &root, 0);
278 walk_tree (root, SIZE);
279 mangle_tree (ascending, delete, &root, 0);
281 mangle_tree (ascending, build, &root, 0);
282 walk_tree (root, SIZE);
283 mangle_tree (descending, delete, &root, 0);
285 mangle_tree (ascending, build, &root, 0);
286 walk_tree (root, SIZE);
287 mangle_tree (randomorder, delete, &root, 0);
289 mangle_tree (descending, build, &root, 0);
290 mangle_tree (ascending, find, &root, 0);
291 mangle_tree (descending, find, &root, 0);
292 mangle_tree (randomorder, find, &root, 0);
293 walk_tree (root, SIZE);
294 mangle_tree (descending, delete, &root, 0);
296 mangle_tree (descending, build, &root, 0);
297 walk_tree (root, SIZE);
298 mangle_tree (descending, delete, &root, 0);
300 mangle_tree (descending, build, &root, 0);
301 walk_tree (root, SIZE);
302 mangle_tree (randomorder, delete, &root, 0);
304 mangle_tree (randomorder, build, &root, 0);
305 mangle_tree (ascending, find, &root, 0);
306 mangle_tree (descending, find, &root, 0);
307 mangle_tree (randomorder, find, &root, 0);
308 walk_tree (root, SIZE);
309 mangle_tree (randomorder, delete, &root, 0);
311 for (j = 1; j < SIZE; j *= 2)
313 mangle_tree (randomorder, build_and_del, &root, j);
316 fputs (error ? " failed!\n" : " ok.\n", stdout);
317 total_error |= error;
320 fputs ("Series II\n", stdout);
321 for (i = 1; i < SIZE; i *= 2)
323 fprintf (stdout, "For size %d... ", i);
324 fflush (stdout);
325 error = 0;
327 mangle_tree (ascending, build_and_del, &root, i);
328 mangle_tree (descending, build_and_del, &root, i);
329 mangle_tree (ascending, build_and_del, &root, i);
330 mangle_tree (descending, build_and_del, &root, i);
331 mangle_tree (ascending, build_and_del, &root, i);
332 mangle_tree (descending, build_and_del, &root, i);
333 mangle_tree (ascending, build_and_del, &root, i);
334 mangle_tree (descending, build_and_del, &root, i);
336 fputs (error ? " failed!\n" : " ok.\n", stdout);
337 total_error |= error;
340 for (i = 0; i < 2; ++i)
341 if (stack_align_check[i] == 0)
343 printf ("stack alignment check %d not run\n", i);
344 total_error |= 1;
346 else if (stack_align_check[i] != 1)
348 printf ("stack insufficiently aligned in check %d\n", i);
349 total_error |= 1;
352 return total_error;