glibc/misc/tst-tsearch.c
Ulrich Drepper 5929563ff2 Update.
1997-04-01 00:37  Ulrich Drepper  <drepper@cygnus.com>

	* math/Makefile: Prevent inlining for test-{float,double,longdouble}.
	* math/libm-test.c (initialize): Make strings unique and force
	values to memory.

	* posix/regex.c: Debug debugging code.

	* sysdeps/i386/asm-syntax.h: Define `L' macro to emit correct
	syntax for local label.
	* sysdeps/i386/add_n.S: Use correct local label syntax for the
	binary format.
	* sysdeps/i386/addmul_1.S: Likewise.
	* sysdeps/i386/lshift.S: Likewise.
	* sysdeps/i386/memchr.S: Likewise.
	* sysdeps/i386/memset.S: Likewise.
	* sysdeps/i386/mul_1.S: Likewise.
	* sysdeps/i386/rshift.S: Likewise.
	* sysdeps/i386/stpcpy.S: Likewise.
	* sysdeps/i386/stpncpy.S: Likewise.
	* sysdeps/i386/strchr.S: Likewise.
	* sysdeps/i386/strcspn.S: Likewise.
	* sysdeps/i386/strlen.S: Likewise.
	* sysdeps/i386/strpbrk.S: Likewise.
	* sysdeps/i386/strrchr.S: Likewise.
	* sysdeps/i386/strspn.S: Likewise.
	* sysdeps/i386/strtok.S: Likewise.
	* sysdeps/i386/sub_n.S: Likewise.
	* sysdeps/i386/submul_1.S: Likewise.
	* sysdeps/i386/i486/strcat.S: Likewise.
	* sysdeps/i386/i486/strlen.S: Likewise.
	* sysdeps/i386/i586/add_n.S: Likewise.
	* sysdeps/i386/i586/addmul_1.S: Likewise.
	* sysdeps/i386/i586/lshift.S: Likewise.
	* sysdeps/i386/i586/memset.S: Likewise.
	* sysdeps/i386/i586/mul_1.S: Likewise.
	* sysdeps/i386/i586/rshift.S: Likewise.
	* sysdeps/i386/i586/strchr.S: Likewise.
	* sysdeps/i386/i586/strlen.S: Likewise.
	* sysdeps/i386/i586/sub_n.S: Likewise.
	* sysdeps/i386/i586/submul_1.S: Likewise.

	* sysdeps/stub/s_log2l.c: Correct function name.

	* time/africa: Updated from ADO tzdata1997e.
	* time/aantarctica: Likewise.
	* time/asia: Likewise.
	* time/australia: Likewise.
	* time/europe: Likewise.
	* time/northamerica: Likewise.
	* time/southamerica: Likewise.
	* time/private.h: Updated from ADO tzcode1997d.
	* time/scheck.c: Likewise.

1997-03-31 12:42  Ulrich Drepper  <drepper@cygnus.com>

	* sysdeps/unix/sysv/linux/powerpc/Dist: Add kernel_*.h files.

1997-03-23 01:41  Geoff Keating  <geoffk@ozemail.com.au>

	* sysdeps/powerpc/strlen.s: Rewrote for more speed.
	* string/tst-strlen.c: Also test various string alignments.

1997-03-19 06:11  Geoff Keating  <geoffk@ozemail.com.au>

	* sysdeps/unix/sysv/linux/powerpc/statbuf.h: Removed.
	* sysdeps/unix/sysv/linux/powerpc/kernel_stat.h: Added.

	* sysdeps/powerpc/dl-machine.h (ELF_PREFERRED_ADDRESS): Assume
	program base address is 0x01800000 if we can't otherwise tell what
	it is.
	(elf_machine_rela): Replace `assertion failed' with real error
	messages.

	* sysdeps/powerpc/dl-machine.h: More comments, more GNU indenting.
	Put in extra parentheses to make explicit the relative precedence
	of + and >>.

	* sysdeps/powerpc/bsd-setjmp.S: Add `__setjmp' symbol so the tests
	pass.
	* sysdeps/powerpc/clone.S: Rewrite.  Probably still doesn't work.

	* sysdeps/unix/sysv/linux/powerpc/sys/sysdep.h: Use .section "text"
	instead of .text.

1997-03-31 02:28  Ulrich Drepper  <drepper@cygnus.com>

	* misc/tst-tsearch.c: Don't write to stderr, write to stdout
	instead.

	* inet/inet_ntoa.c (inet_ntoa): Complete rewrite.  Now uses
	thread-specific memory in multi-threaded programs.

	* sysdeps/stub/s_exp2.c: New file.  Stub version for exp2 function.
	* sysdeps/stub/s_exp2f.c: New file.
	* sysdeps/stub/s_exp2l.c: New file.
	Reported by a sun <asun@zoology.washington.edu>.

	* sysdeps/unix/sysv/linux/netatalk/atalk.h: Rename to...
	* sysdeps/unix/sysv/linux/netatalk/at.h: ...this.
	Suggested by a sun <asun@zoology.washington.edu>.

1997-03-30 14:53  Ulrich Drepper  <drepper@cygnus.com>

	* sysdeps/unix/sysv/linux/netinet/ip_icmp.h: Add former contents
	of <netinet/icmp.h>.
	Patch by a sun <asun@zoology.washington.edu>.

	* sysdeps/powerpc/strlen.S: Added.
1997-03-31 22:56:24 +00:00

330 lines
7.2 KiB
C

/* Test program for tsearch et al.
Copyright (C) 1997 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#define SEED 0
#define BALANCED 1
#define PASSES 100
#if BALANCED
#include <math.h>
#define SIZE 1000
#else
#define SIZE 100
#endif
enum order
{
ascending,
descending,
randomorder
};
enum action
{
build,
build_and_del,
delete,
find
};
/* Set to 1 if a test is flunked. */
static int error = 0;
/* The keys we add to the tree. */
static int x[SIZE];
/* Pointers into the key array, possibly permutated, to define an order
for insertion/removal. */
static int y[SIZE];
/* Flags set for each element visited during a tree walk. */
static int z[SIZE];
/* Depths for all the elements, to check that the depth is constant for
all three visits. */
static int depths[SIZE];
/* Maximum depth during a tree walk. */
static int max_depth;
/* Compare two keys. */
static int
cmp_fn (const void *a, const void *b)
{
return *(const int *) a - *(const int *) b;
}
/* Permute an array of integers. */
static void
memfry (int *string)
{
int i;
for (i = 0; i < SIZE; ++i)
{
int32_t j;
int c;
j = random () % SIZE;
c = string[i];
string[i] = string[j];
string[j] = c;
}
}
static void
walk_action (const void *nodep, const VISIT which, const int depth)
{
int key = **(int **) nodep;
if (depth > max_depth)
max_depth = depth;
if (which == leaf || which == preorder)
{
++z[key];
depths[key] = depth;
}
else
{
if (depths[key] != depth)
{
fputs ("Depth for one element is not constant during tree walk.\n",
stdout);
}
}
}
static void
walk_tree (void *root, int expected_count)
{
int i;
memset (z, 0, sizeof z);
max_depth = 0;
twalk (root, walk_action);
for (i = 0; i < expected_count; ++i)
if (z[i] != 1)
{
fputs ("Node was not visited.\n", stdout);
error = 1;
}
#if BALANCED
if (max_depth > log (expected_count) * 2 + 2)
#else
if (max_depth > expected_count)
#endif
{
fputs ("Depth too large during tree walk.\n", stdout);
error = 1;
}
}
/* Perform an operation on a tree. */
static void
mangle_tree (enum order how, enum action what, void **root, int lag)
{
int i;
if (how == randomorder)
{
for (i = 0; i < SIZE; ++i)
y[i] = i;
memfry (y);
}
for (i = 0; i < SIZE + lag; ++i)
{
void *elem;
int j, k;
switch (how)
{
case randomorder:
if (i >= lag)
k = y[i - lag];
else
k = y[SIZE - i - 1 + lag];
j = y[i];
break;
case ascending:
k = i - lag;
j = i;
break;
case descending:
k = SIZE - i - 1 + lag;
j = SIZE - i - 1;
break;
default:
/* This never should happen, but gcc isn't smart enough to
recognize it. */
abort ();
}
switch (what)
{
case build_and_del:
case build:
if (i < SIZE)
{
if (tfind (x + j, (const void **) root, cmp_fn) != NULL)
{
fputs ("Found element which is not in tree yet.\n", stdout);
error = 1;
}
elem = tsearch (x + j, root, cmp_fn);
if (elem == 0
|| tfind (x + j, (const void **) root, cmp_fn) == NULL)
{
fputs ("Couldn't find element after it was added.\n",
stdout);
error = 1;
}
}
if (what == build || i < lag)
break;
j = k;
/* fall through */
case delete:
elem = tfind (x + j, (const void **) root, cmp_fn);
if (elem == NULL || tdelete (x + j, root, cmp_fn) == NULL)
{
fputs ("Error deleting element.\n", stdout);
error = 1;
}
break;
case find:
if (tfind (x + j, (const void **) root, cmp_fn) == NULL)
{
fputs ("Couldn't find element after it was added.\n", stdout);
error = 1;
}
break;
}
}
}
int
main (int argc, char **argv)
{
int total_error = 0;
static int state[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
void *root = NULL;
int i, j;
initstate (SEED, state, 8);
for (i = 0; i < SIZE; ++i)
x[i] = i;
/* Do this loop several times to get different permutations for the
random case. */
fputs ("Series I\n", stdout);
for (i = 0; i < PASSES; ++i)
{
fprintf (stdout, "Pass %d... ", i + 1);
fflush (stdout);
error = 0;
mangle_tree (ascending, build, &root, 0);
mangle_tree (ascending, find, &root, 0);
mangle_tree (descending, find, &root, 0);
mangle_tree (randomorder, find, &root, 0);
walk_tree (root, SIZE);
mangle_tree (ascending, delete, &root, 0);
mangle_tree (ascending, build, &root, 0);
walk_tree (root, SIZE);
mangle_tree (descending, delete, &root, 0);
mangle_tree (ascending, build, &root, 0);
walk_tree (root, SIZE);
mangle_tree (randomorder, delete, &root, 0);
mangle_tree (descending, build, &root, 0);
mangle_tree (ascending, find, &root, 0);
mangle_tree (descending, find, &root, 0);
mangle_tree (randomorder, find, &root, 0);
walk_tree (root, SIZE);
mangle_tree (descending, delete, &root, 0);
mangle_tree (descending, build, &root, 0);
walk_tree (root, SIZE);
mangle_tree (descending, delete, &root, 0);
mangle_tree (descending, build, &root, 0);
walk_tree (root, SIZE);
mangle_tree (randomorder, delete, &root, 0);
mangle_tree (randomorder, build, &root, 0);
mangle_tree (ascending, find, &root, 0);
mangle_tree (descending, find, &root, 0);
mangle_tree (randomorder, find, &root, 0);
walk_tree (root, SIZE);
mangle_tree (randomorder, delete, &root, 0);
for (j = 1; j < SIZE; j *= 2)
{
mangle_tree (randomorder, build_and_del, &root, j);
}
fputs (error ? " failed!\n" : " ok.\n", stdout);
total_error |= error;
}
fputs ("Series II\n", stdout);
for (i = 1; i < SIZE; i *= 2)
{
fprintf (stdout, "For size %d... ", i);
fflush (stdout);
error = 0;
mangle_tree (ascending, build_and_del, &root, i);
mangle_tree (descending, build_and_del, &root, i);
mangle_tree (ascending, build_and_del, &root, i);
mangle_tree (descending, build_and_del, &root, i);
mangle_tree (ascending, build_and_del, &root, i);
mangle_tree (descending, build_and_del, &root, i);
mangle_tree (ascending, build_and_del, &root, i);
mangle_tree (descending, build_and_del, &root, i);
fputs (error ? " failed!\n" : " ok.\n", stdout);
total_error |= error;
}
return total_error;
}