1 /* Test program for tsearch et al.
2 Copyright (C) 1997-2015 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, see
17 <http://www.gnu.org/licenses/>. */
20 # define _GNU_SOURCE 1
27 #include <tst-stack-align.h>
55 /* Set to 1 if a test is flunked. */
58 /* The keys we add to the tree. */
61 /* Pointers into the key array, possibly permutated, to define an order
62 for insertion/removal. */
65 /* Flags set for each element visited during a tree walk. */
68 /* Depths for all the elements, to check that the depth is constant for
70 static int depths
[SIZE
];
72 /* Maximum depth during a tree walk. */
75 static int stack_align_check
[2];
77 /* Compare two keys. */
79 cmp_fn (const void *a
, const void *b
)
81 if (!stack_align_check
[0])
82 stack_align_check
[0] = TEST_STACK_ALIGN () ? -1 : 1;
83 return *(const int *) a
- *(const int *) b
;
86 /* Permute an array of integers. */
92 for (i
= 0; i
< SIZE
; ++i
)
100 string
[i
] = string
[j
];
106 walk_action (const void *nodep
, const VISIT which
, const int depth
)
108 int key
= **(int **) nodep
;
110 if (!stack_align_check
[1])
111 stack_align_check
[1] = TEST_STACK_ALIGN () ? -1 : 1;
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
);
255 static char state
[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
259 initstate (SEED
, state
, 8);
261 for (i
= 0; i
< SIZE
; ++i
)
264 /* Do this loop several times to get different permutations for the
266 fputs ("Series I\n", stdout
);
267 for (i
= 0; i
< PASSES
; ++i
)
269 fprintf (stdout
, "Pass %d... ", i
+ 1);
273 mangle_tree (ascending
, build
, &root
, 0);
274 mangle_tree (ascending
, find
, &root
, 0);
275 mangle_tree (descending
, find
, &root
, 0);
276 mangle_tree (randomorder
, find
, &root
, 0);
277 walk_tree (root
, SIZE
);
278 mangle_tree (ascending
, delete, &root
, 0);
280 mangle_tree (ascending
, build
, &root
, 0);
281 walk_tree (root
, SIZE
);
282 mangle_tree (descending
, delete, &root
, 0);
284 mangle_tree (ascending
, build
, &root
, 0);
285 walk_tree (root
, SIZE
);
286 mangle_tree (randomorder
, delete, &root
, 0);
288 mangle_tree (descending
, build
, &root
, 0);
289 mangle_tree (ascending
, find
, &root
, 0);
290 mangle_tree (descending
, find
, &root
, 0);
291 mangle_tree (randomorder
, find
, &root
, 0);
292 walk_tree (root
, SIZE
);
293 mangle_tree (descending
, delete, &root
, 0);
295 mangle_tree (descending
, build
, &root
, 0);
296 walk_tree (root
, SIZE
);
297 mangle_tree (descending
, delete, &root
, 0);
299 mangle_tree (descending
, build
, &root
, 0);
300 walk_tree (root
, SIZE
);
301 mangle_tree (randomorder
, delete, &root
, 0);
303 mangle_tree (randomorder
, build
, &root
, 0);
304 mangle_tree (ascending
, find
, &root
, 0);
305 mangle_tree (descending
, find
, &root
, 0);
306 mangle_tree (randomorder
, find
, &root
, 0);
307 walk_tree (root
, SIZE
);
308 mangle_tree (randomorder
, delete, &root
, 0);
310 for (j
= 1; j
< SIZE
; j
*= 2)
312 mangle_tree (randomorder
, build_and_del
, &root
, j
);
315 fputs (error
? " failed!\n" : " ok.\n", stdout
);
316 total_error
|= error
;
319 fputs ("Series II\n", stdout
);
320 for (i
= 1; i
< SIZE
; i
*= 2)
322 fprintf (stdout
, "For size %d... ", i
);
326 mangle_tree (ascending
, build_and_del
, &root
, i
);
327 mangle_tree (descending
, build_and_del
, &root
, 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
);
335 fputs (error
? " failed!\n" : " ok.\n", stdout
);
336 total_error
|= error
;
339 for (i
= 0; i
< 2; ++i
)
340 if (stack_align_check
[i
] == 0)
342 printf ("stack alignment check %d not run\n", i
);
345 else if (stack_align_check
[i
] != 1)
347 printf ("stack insufficiently aligned in check %d\n", i
);
354 #define TEST_FUNCTION do_test ()
355 #include "../test-skeleton.c"