[BZ #4439]
[glibc/pb-stable.git] / stdlib / tst-bsearch.c
blob8d2fd895fe05e3e6c6cca5846820030ae9f7acf6
1 /* Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <tst-stack-align.h>
24 struct entry
26 int val;
27 const char *str;
28 } arr[] =
30 { 0, "zero" },
31 { 1, "one" },
32 { 2, "two" },
33 { 3, "three" },
34 { 4, "four" },
35 { 5, "five" },
36 { 6, "six" },
37 { 7, "seven" },
38 { 8, "eight" },
39 { 9, "nine" },
40 { 10, "ten" }
42 #define narr (sizeof (arr) / sizeof (arr[0]))
44 static int align_check;
46 static int
47 comp (const void *p1, const void *p2)
49 struct entry *e1 = (struct entry *) p1;
50 struct entry *e2 = (struct entry *) p2;
52 if (!align_check)
53 align_check = TEST_STACK_ALIGN () ? -1 : 1;
55 return e1->val - e2->val;
59 int
60 main (void)
62 size_t cnt;
63 int result = 0;
64 struct entry key;
65 struct entry *res;
67 for (cnt = 0; cnt < narr; ++cnt)
70 key.val = arr[cnt].val;
72 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
73 if (res == NULL)
75 printf ("entry %zd not found\n", cnt);
76 result = 1;
78 else if (res != &arr[cnt])
80 puts ("wrong entry returned");
81 result = 1;
85 /* And some special tests that shouldn't find any entry. */
86 key.val = -1;
87 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
88 if (res != NULL)
90 puts ("found an entry that's not there");
91 result = 1;
94 key.val = 11;
95 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
96 if (res != NULL)
98 puts ("found an entry that's not there");
99 result = 1;
102 key.val = 11;
103 res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
104 if (res != NULL)
106 puts ("found an entry that's not there");
107 result = 1;
110 /* Now the array contains only one element - no entry should be found. */
111 for (cnt = 0; cnt < narr; ++cnt)
113 key.val = arr[cnt].val;
115 res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
116 if (cnt == 5)
118 if (res == NULL)
120 printf ("entry %zd not found\n", cnt);
121 result = 1;
123 else if (res != &arr[cnt])
125 puts ("wrong entry returned");
126 result = 1;
129 else if (res != NULL)
131 puts ("found an entry that's not there");
132 result = 1;
136 if (align_check == 0)
138 puts ("alignment not checked");
139 result = 1;
141 else if (align_check == -1)
143 puts ("stack not sufficiently aligned");
144 result = 1;
147 if (result == 0)
148 puts ("all OK");
150 return result;