Remove 'grp' and merge into 'nss' and 'posix'
[platform/upstream/glibc.git] / stdlib / tst-bsearch.c
index 0f3db24..f8d3b7c 100644 (file)
@@ -1,26 +1,25 @@
-/* Copyright (C) 2000 Free Software Foundation, Inc.
+/* Copyright (C) 2000-2023 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
-   Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
 
    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.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 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.
+   Lesser 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.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <tst-stack-align.h>
 
-struct entry
+struct item
 {
   int val;
   const char *str;
@@ -40,34 +39,38 @@ struct entry
 };
 #define narr (sizeof (arr) / sizeof (arr[0]))
 
+static int align_check;
 
 static int
 comp (const void *p1, const void *p2)
 {
-  struct entry *e1 = (struct entry *) p1;
-  struct entry *e2 = (struct entry *) p2;
+  struct item *e1 = (struct item *) p1;
+  struct item *e2 = (struct item *) p2;
+
+  if (!align_check)
+    align_check = TEST_STACK_ALIGN () ? -1 : 1;
 
   return e1->val - e2->val;
 }
 
 
-int
-main (void)
+static int
+do_test (void)
 {
-  int cnt;
+  size_t cnt;
   int result = 0;
-  struct entry key;
-  struct entry *res;
+  struct item key;
+  struct item *res;
 
   for (cnt = 0; cnt < narr; ++cnt)
     {
 
       key.val = arr[cnt].val;
 
-      res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
+      res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
       if (res == NULL)
        {
-         printf ("entry %d not found\n", cnt);
+         printf ("entry %zd not found\n", cnt);
          result = 1;
        }
       else if (res != &arr[cnt])
@@ -79,7 +82,7 @@ main (void)
 
   /* And some special tests that shouldn't find any entry.  */
   key.val = -1;
-  res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
+  res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
   if (res != NULL)
     {
       puts ("found an entry that's not there");
@@ -87,7 +90,7 @@ main (void)
     }
 
   key.val = 11;
-  res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
+  res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
   if (res != NULL)
     {
       puts ("found an entry that's not there");
@@ -95,24 +98,24 @@ main (void)
     }
 
   key.val = 11;
-  res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
+  res = (struct item *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
   if (res != NULL)
     {
       puts ("found an entry that's not there");
       result = 1;
     }
-  
+
   /* Now the array contains only one element - no entry should be found.  */
   for (cnt = 0; cnt < narr; ++cnt)
     {
       key.val = arr[cnt].val;
 
-      res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
+      res = (struct item *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
       if (cnt == 5)
        {
          if (res == NULL)
            {
-             printf ("entry %d not found\n", cnt);
+             printf ("entry %zd not found\n", cnt);
              result = 1;
            }
          else if (res != &arr[cnt])
@@ -128,8 +131,22 @@ main (void)
        }
     }
 
+  if (align_check == 0)
+    {
+      puts ("alignment not checked");
+      result = 1;
+    }
+  else if (align_check == -1)
+    {
+      puts ("stack not sufficiently aligned");
+      result = 1;
+    }
+
   if (result == 0)
     puts ("all OK");
 
   return result;
 }
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"