maint: update copyright
[gnulib.git] / tests / test-tsearch.c
index b6a22fa..5eb84a6 100644 (file)
@@ -1,5 +1,5 @@
 /* Test program for tsearch et al.
-   Copyright (C) 1997, 2000, 2001, 2007 Free Software Foundation, Inc.
+   Copyright (C) 1997, 2000-2001, 2007-2014 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
 
 #include <search.h>
 
+#include "signature.h"
+SIGNATURE_CHECK (tdelete, void *, (void const *, void **,
+                                   int (*) (void const *, void const *)));
+SIGNATURE_CHECK (tfind, void *, (void const *, void * const *,
+                                 int (*) (void const *, void const *)));
+SIGNATURE_CHECK (tsearch, void *, (void const *, void **,
+                                   int (*) (void const *, void const *)));
+SIGNATURE_CHECK (twalk, void, (void const *,
+                               void (*) (void const *, VISIT, int)));
+
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -65,7 +75,7 @@ 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
+/* Pointers into the key array, possibly permuted, to define an order
    for insertion/removal.  */
 static int y[SIZE];
 
@@ -120,10 +130,10 @@ walk_action (const void *nodep, const VISIT which, const int depth)
   else
     {
       if (depths[key] != depth)
-       {
-         fputs ("Depth for one element is not constant during tree walk.\n",
-                stdout);
-       }
+        {
+          fputs ("Depth for one element is not constant during tree walk.\n",
+                 stdout);
+        }
     }
 }
 
@@ -139,8 +149,8 @@ walk_tree (void *root, int expected_count)
   for (i = 0; i < expected_count; ++i)
     if (z[i] != 1)
       {
-       fputs ("Node was not visited.\n", stdout);
-       error = 1;
+        fputs ("Node was not visited.\n", stdout);
+        error = 1;
       }
 
 #if BALANCED
@@ -163,7 +173,7 @@ mangle_tree (enum order how, enum action what, void **root, int lag)
   if (how == randomorder)
     {
       for (i = 0; i < SIZE; ++i)
-       y[i] = i;
+        y[i] = i;
       memfry (y);
     }
 
@@ -173,77 +183,77 @@ mangle_tree (enum order how, enum action what, void **root, int lag)
       int j, k;
 
       switch (how)
-       {
-       case randomorder:
-         if (i >= lag)
-           k = y[i - lag];
-         else
-           /* Ensure that the array index is within bounds.  */
-           k = y[(SIZE - i - 1 + lag) % SIZE];
-         j = y[i % SIZE];
-         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 ();
-       }
+        {
+        case randomorder:
+          if (i >= lag)
+            k = y[i - lag];
+          else
+            /* Ensure that the array index is within bounds.  */
+            k = y[(SIZE - i - 1 + lag) % SIZE];
+          j = y[i % SIZE];
+          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, (void *const *) 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, (void *const *) 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, (void *const *) 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, (void *const *) root, cmp_fn) == NULL)
-           {
-             fputs ("Couldn't find element after it was added.\n", stdout);
-             error = 1;
-           }
-         break;
-
-       }
+        {
+        case build_and_del:
+        case build:
+          if (i < SIZE)
+            {
+              if (tfind (x + j, (void *const *) 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, (void *const *) 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, (void *const *) 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, (void *const *) root, cmp_fn) == NULL)
+            {
+              fputs ("Couldn't find element after it was added.\n", stdout);
+              error = 1;
+            }
+          break;
+
+        }
     }
 }
 
@@ -256,7 +266,9 @@ main (int argc, char **argv)
   void *root = NULL;
   int i, j;
 
+#if HAVE_INITSTATE
   initstate (SEED, state, 8);
+#endif
 
   for (i = 0; i < SIZE; ++i)
     x[i] = i;
@@ -308,9 +320,9 @@ main (int argc, char **argv)
       mangle_tree (randomorder, delete, &root, 0);
 
       for (j = 1; j < SIZE; j *= 2)
-       {
-         mangle_tree (randomorder, build_and_del, &root, j);
-       }
+        {
+          mangle_tree (randomorder, build_and_del, &root, j);
+        }
 
       fputs (error ? " failed!\n" : " ok.\n", stdout);
       total_error |= error;