Back to index

glibc  2.9
tst-bsearch.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <tst-stack-align.h>
00023 
00024 struct entry
00025 {
00026   int val;
00027   const char *str;
00028 } arr[] =
00029 {
00030   { 0, "zero" },
00031   { 1, "one" },
00032   { 2, "two" },
00033   { 3, "three" },
00034   { 4, "four" },
00035   { 5, "five" },
00036   { 6, "six" },
00037   { 7, "seven" },
00038   { 8, "eight" },
00039   { 9, "nine" },
00040   { 10, "ten" }
00041 };
00042 #define narr (sizeof (arr) / sizeof (arr[0]))
00043 
00044 static int align_check;
00045 
00046 static int
00047 comp (const void *p1, const void *p2)
00048 {
00049   struct entry *e1 = (struct entry *) p1;
00050   struct entry *e2 = (struct entry *) p2;
00051 
00052   if (!align_check)
00053     align_check = TEST_STACK_ALIGN () ? -1 : 1;
00054 
00055   return e1->val - e2->val;
00056 }
00057 
00058 
00059 int
00060 main (void)
00061 {
00062   size_t cnt;
00063   int result = 0;
00064   struct entry key;
00065   struct entry *res;
00066 
00067   for (cnt = 0; cnt < narr; ++cnt)
00068     {
00069 
00070       key.val = arr[cnt].val;
00071 
00072       res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
00073       if (res == NULL)
00074        {
00075          printf ("entry %zd not found\n", cnt);
00076          result = 1;
00077        }
00078       else if (res != &arr[cnt])
00079        {
00080          puts ("wrong entry returned");
00081          result = 1;
00082        }
00083     }
00084 
00085   /* And some special tests that shouldn't find any entry.  */
00086   key.val = -1;
00087   res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
00088   if (res != NULL)
00089     {
00090       puts ("found an entry that's not there");
00091       result = 1;
00092     }
00093 
00094   key.val = 11;
00095   res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
00096   if (res != NULL)
00097     {
00098       puts ("found an entry that's not there");
00099       result = 1;
00100     }
00101 
00102   key.val = 11;
00103   res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
00104   if (res != NULL)
00105     {
00106       puts ("found an entry that's not there");
00107       result = 1;
00108     }
00109 
00110   /* Now the array contains only one element - no entry should be found.  */
00111   for (cnt = 0; cnt < narr; ++cnt)
00112     {
00113       key.val = arr[cnt].val;
00114 
00115       res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
00116       if (cnt == 5)
00117        {
00118          if (res == NULL)
00119            {
00120              printf ("entry %zd not found\n", cnt);
00121              result = 1;
00122            }
00123          else if (res != &arr[cnt])
00124            {
00125              puts ("wrong entry returned");
00126              result = 1;
00127            }
00128        }
00129       else if (res != NULL)
00130        {
00131          puts ("found an entry that's not there");
00132          result = 1;
00133        }
00134     }
00135 
00136   if (align_check == 0)
00137     {
00138       puts ("alignment not checked");
00139       result = 1;
00140     }
00141   else if (align_check == -1)
00142     {
00143       puts ("stack not sufficiently aligned");
00144       result = 1;
00145     }
00146 
00147   if (result == 0)
00148     puts ("all OK");
00149 
00150   return result;
00151 }