1 /* Test program for tsearch et al.
2 Copyright (C) 1997, 2000-2001, 2007-2018 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 License as
7 published by the Free Software Foundation; either version 3 of the
8 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 License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
22 #include "signature.h"
23 SIGNATURE_CHECK (tdelete
, void *, (void const *, void **,
24 int (*) (void const *, void const *)));
25 SIGNATURE_CHECK (tfind
, void *, (void const *, void * const *,
26 int (*) (void const *, void const *)));
27 SIGNATURE_CHECK (tsearch
, void *, (void const *, void **,
28 int (*) (void const *, void const *)));
29 SIGNATURE_CHECK (twalk
, void, (void const *,
30 void (*) (void const *, VISIT
, int)));
41 /* The system's tsearch() is not expected to keep the tree balanced. */
44 /* The gnulib replacement tsearch() should keep the tree balanced. */
57 #define random() (int) ((unsigned int) rand () >> 3)
74 /* Set to 1 if a test is flunked. */
77 /* The keys we add to the tree. */
80 /* Pointers into the key array, possibly permuted, to define an order
81 for insertion/removal. */
84 /* Flags set for each element visited during a tree walk. */
87 /* Depths for all the elements, to check that the depth is constant for
89 static int depths
[SIZE
];
91 /* Maximum depth during a tree walk. */
94 /* Compare two keys. */
96 cmp_fn (const void *a
, const void *b
)
98 return *(const int *) a
- *(const int *) b
;
101 /* Permute an array of integers. */
107 for (i
= 0; i
< SIZE
; ++i
)
112 j
= random () % SIZE
;
115 string
[i
] = string
[j
];
121 walk_action (const void *nodep
, const VISIT which
, const int depth
)
123 int key
= **(int **) nodep
;
125 if (depth
> max_depth
)
127 if (which
== leaf
|| which
== preorder
)
134 if (depths
[key
] != depth
)
136 fputs ("Depth for one element is not constant during tree walk.\n",
143 walk_tree (void *root
, int expected_count
)
147 memset (z
, 0, sizeof z
);
150 twalk (root
, walk_action
);
151 for (i
= 0; i
< expected_count
; ++i
)
154 fputs ("Node was not visited.\n", stdout
);
159 if (max_depth
> log (expected_count
) * 2 + 2)
161 if (max_depth
> expected_count
)
164 fputs ("Depth too large during tree walk.\n", stdout
);
169 /* Perform an operation on a tree. */
171 mangle_tree (enum order how
, enum action what
, void **root
, int lag
)
175 if (how
== randomorder
)
177 for (i
= 0; i
< SIZE
; ++i
)
182 for (i
= 0; i
< SIZE
+ lag
; ++i
)
193 /* Ensure that the array index is within bounds. */
194 k
= y
[(SIZE
- i
- 1 + lag
) % SIZE
];
204 k
= SIZE
- i
- 1 + lag
;
209 /* This never should happen, but gcc isn't smart enough to
220 if (tfind (x
+ j
, (void *const *) root
, cmp_fn
) != NULL
)
222 fputs ("Found element which is not in tree yet.\n", stdout
);
225 elem
= tsearch (x
+ j
, root
, cmp_fn
);
227 || tfind (x
+ j
, (void *const *) root
, cmp_fn
) == NULL
)
229 fputs ("Couldn't find element after it was added.\n",
235 if (what
== build
|| i
< lag
)
242 elem
= tfind (x
+ j
, (void *const *) root
, cmp_fn
);
243 if (elem
== NULL
|| tdelete (x
+ j
, root
, cmp_fn
) == NULL
)
245 fputs ("Error deleting element.\n", stdout
);
251 if (tfind (x
+ j
, (void *const *) root
, cmp_fn
) == NULL
)
253 fputs ("Couldn't find element after it was added.\n", stdout
);
264 main (int argc
, char **argv
)
267 static char state
[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
272 initstate (SEED
, state
, 8);
275 for (i
= 0; i
< SIZE
; ++i
)
278 /* Do this loop several times to get different permutations for the
280 fputs ("Series I\n", stdout
);
281 for (i
= 0; i
< PASSES
; ++i
)
283 fprintf (stdout
, "Pass %d... ", i
+ 1);
287 mangle_tree (ascending
, build
, &root
, 0);
288 mangle_tree (ascending
, find
, &root
, 0);
289 mangle_tree (descending
, find
, &root
, 0);
290 mangle_tree (randomorder
, find
, &root
, 0);
291 walk_tree (root
, SIZE
);
292 mangle_tree (ascending
, delete, &root
, 0);
294 mangle_tree (ascending
, build
, &root
, 0);
295 walk_tree (root
, SIZE
);
296 mangle_tree (descending
, delete, &root
, 0);
298 mangle_tree (ascending
, build
, &root
, 0);
299 walk_tree (root
, SIZE
);
300 mangle_tree (randomorder
, delete, &root
, 0);
302 mangle_tree (descending
, build
, &root
, 0);
303 mangle_tree (ascending
, find
, &root
, 0);
304 mangle_tree (descending
, find
, &root
, 0);
305 mangle_tree (randomorder
, find
, &root
, 0);
306 walk_tree (root
, SIZE
);
307 mangle_tree (descending
, delete, &root
, 0);
309 mangle_tree (descending
, build
, &root
, 0);
310 walk_tree (root
, SIZE
);
311 mangle_tree (descending
, delete, &root
, 0);
313 mangle_tree (descending
, build
, &root
, 0);
314 walk_tree (root
, SIZE
);
315 mangle_tree (randomorder
, delete, &root
, 0);
317 mangle_tree (randomorder
, build
, &root
, 0);
318 mangle_tree (ascending
, find
, &root
, 0);
319 mangle_tree (descending
, find
, &root
, 0);
320 mangle_tree (randomorder
, find
, &root
, 0);
321 walk_tree (root
, SIZE
);
322 mangle_tree (randomorder
, delete, &root
, 0);
324 for (j
= 1; j
< SIZE
; j
*= 2)
326 mangle_tree (randomorder
, build_and_del
, &root
, j
);
329 fputs (error
? " failed!\n" : " ok.\n", stdout
);
330 total_error
|= error
;
333 fputs ("Series II\n", stdout
);
334 for (i
= 1; i
< SIZE
; i
*= 2)
336 fprintf (stdout
, "For size %d... ", i
);
340 mangle_tree (ascending
, build_and_del
, &root
, i
);
341 mangle_tree (descending
, build_and_del
, &root
, i
);
342 mangle_tree (ascending
, build_and_del
, &root
, i
);
343 mangle_tree (descending
, build_and_del
, &root
, i
);
344 mangle_tree (ascending
, build_and_del
, &root
, i
);
345 mangle_tree (descending
, build_and_del
, &root
, i
);
346 mangle_tree (ascending
, build_and_del
, &root
, i
);
347 mangle_tree (descending
, build_and_del
, &root
, i
);
349 fputs (error
? " failed!\n" : " ok.\n", stdout
);
350 total_error
|= error
;