Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / stdlib / tst-bsearch.c
1 /* Copyright (C) 2000-2013 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
4
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
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
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.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <tst-stack-align.h>
22
23 struct entry
24 {
25   int val;
26   const char *str;
27 } arr[] =
28 {
29   { 0, "zero" },
30   { 1, "one" },
31   { 2, "two" },
32   { 3, "three" },
33   { 4, "four" },
34   { 5, "five" },
35   { 6, "six" },
36   { 7, "seven" },
37   { 8, "eight" },
38   { 9, "nine" },
39   { 10, "ten" }
40 };
41 #define narr (sizeof (arr) / sizeof (arr[0]))
42
43 static int align_check;
44
45 static int
46 comp (const void *p1, const void *p2)
47 {
48   struct entry *e1 = (struct entry *) p1;
49   struct entry *e2 = (struct entry *) p2;
50
51   if (!align_check)
52     align_check = TEST_STACK_ALIGN () ? -1 : 1;
53
54   return e1->val - e2->val;
55 }
56
57
58 int
59 main (void)
60 {
61   size_t cnt;
62   int result = 0;
63   struct entry key;
64   struct entry *res;
65
66   for (cnt = 0; cnt < narr; ++cnt)
67     {
68
69       key.val = arr[cnt].val;
70
71       res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
72       if (res == NULL)
73         {
74           printf ("entry %zd not found\n", cnt);
75           result = 1;
76         }
77       else if (res != &arr[cnt])
78         {
79           puts ("wrong entry returned");
80           result = 1;
81         }
82     }
83
84   /* And some special tests that shouldn't find any entry.  */
85   key.val = -1;
86   res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
87   if (res != NULL)
88     {
89       puts ("found an entry that's not there");
90       result = 1;
91     }
92
93   key.val = 11;
94   res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
95   if (res != NULL)
96     {
97       puts ("found an entry that's not there");
98       result = 1;
99     }
100
101   key.val = 11;
102   res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
103   if (res != NULL)
104     {
105       puts ("found an entry that's not there");
106       result = 1;
107     }
108
109   /* Now the array contains only one element - no entry should be found.  */
110   for (cnt = 0; cnt < narr; ++cnt)
111     {
112       key.val = arr[cnt].val;
113
114       res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
115       if (cnt == 5)
116         {
117           if (res == NULL)
118             {
119               printf ("entry %zd not found\n", cnt);
120               result = 1;
121             }
122           else if (res != &arr[cnt])
123             {
124               puts ("wrong entry returned");
125               result = 1;
126             }
127         }
128       else if (res != NULL)
129         {
130           puts ("found an entry that's not there");
131           result = 1;
132         }
133     }
134
135   if (align_check == 0)
136     {
137       puts ("alignment not checked");
138       result = 1;
139     }
140   else if (align_check == -1)
141     {
142       puts ("stack not sufficiently aligned");
143       result = 1;
144     }
145
146   if (result == 0)
147     puts ("all OK");
148
149   return result;
150 }