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
21 # define _GNU_SOURCE 1
28 #include <tst-stack-align.h>
56 /* Set to 1 if a test is flunked. */
59 /* The keys we add to the tree. */
62 /* Pointers into the key array, possibly permutated, to define an order
63 for insertion/removal. */
66 /* Flags set for each element visited during a tree walk. */
69 /* Depths for all the elements, to check that the depth is constant for
71 static int depths
[SIZE
];
73 /* Maximum depth during a tree walk. */
76 static int stack_align_check
[2];
78 /* Compare two keys. */
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. */
93 for (i
= 0; i
< SIZE
; ++i
)
101 string
[i
] = string
[j
];
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
)
116 if (which
== leaf
|| which
== preorder
)
123 if (depths
[key
] != depth
)
125 fputs ("Depth for one element is not constant during tree walk.\n",
132 walk_tree (void *root
, int expected_count
)
136 memset (z
, 0, sizeof z
);
139 twalk (root
, walk_action
);
140 for (i
= 0; i
< expected_count
; ++i
)
143 fputs ("Node was not visited.\n", stdout
);
148 if (max_depth
> log (expected_count
) * 2 + 2)
150 if (max_depth
> expected_count
)
153 fputs ("Depth too large during tree walk.\n", stdout
);
158 /* Perform an operation on a tree. */
160 mangle_tree (enum order how
, enum action what
, void **root
, int lag
)
164 if (how
== randomorder
)
166 for (i
= 0; i
< SIZE
; ++i
)
171 for (i
= 0; i
< SIZE
+ lag
; ++i
)
182 /* Ensure that the array index is within bounds. */
183 k
= y
[(SIZE
- i
- 1 + lag
) % SIZE
];
193 k
= SIZE
- i
- 1 + lag
;
198 /* This never should happen, but gcc isn't smart enough to
209 if (tfind (x
+ j
, (void *const *) root
, cmp_fn
) != NULL
)
211 fputs ("Found element which is not in tree yet.\n", stdout
);
214 elem
= tsearch (x
+ j
, root
, cmp_fn
);
216 || tfind (x
+ j
, (void *const *) root
, cmp_fn
) == NULL
)
218 fputs ("Couldn't find element after it was added.\n",
224 if (what
== build
|| i
< lag
)
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
);
240 if (tfind (x
+ j
, (void *const *) root
, cmp_fn
) == NULL
)
242 fputs ("Couldn't find element after it was added.\n", stdout
);
253 main (int argc
, char **argv
)
256 static char state
[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
260 initstate (SEED
, state
, 8);
262 for (i
= 0; i
< SIZE
; ++i
)
265 /* Do this loop several times to get different permutations for the
267 fputs ("Series I\n", stdout
);
268 for (i
= 0; i
< PASSES
; ++i
)
270 fprintf (stdout
, "Pass %d... ", i
+ 1);
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
);
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
);
346 else if (stack_align_check
[i
] != 1)
348 printf ("stack insufficiently aligned in check %d\n", i
);