Update copyright notices with scripts/update-copyrights
[glibc.git] / stdlib / tst-bsearch.c
blobe539275930b338f31b951b48401b2a5fd97ba927
1 /* Copyright (C) 2000-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <tst-stack-align.h>
23 struct entry
25 int val;
26 const char *str;
27 } arr[] =
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" }
41 #define narr (sizeof (arr) / sizeof (arr[0]))
43 static int align_check;
45 static int
46 comp (const void *p1, const void *p2)
48 struct entry *e1 = (struct entry *) p1;
49 struct entry *e2 = (struct entry *) p2;
51 if (!align_check)
52 align_check = TEST_STACK_ALIGN () ? -1 : 1;
54 return e1->val - e2->val;
58 int
59 main (void)
61 size_t cnt;
62 int result = 0;
63 struct entry key;
64 struct entry *res;
66 for (cnt = 0; cnt < narr; ++cnt)
69 key.val = arr[cnt].val;
71 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
72 if (res == NULL)
74 printf ("entry %zd not found\n", cnt);
75 result = 1;
77 else if (res != &arr[cnt])
79 puts ("wrong entry returned");
80 result = 1;
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)
89 puts ("found an entry that's not there");
90 result = 1;
93 key.val = 11;
94 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
95 if (res != NULL)
97 puts ("found an entry that's not there");
98 result = 1;
101 key.val = 11;
102 res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
103 if (res != NULL)
105 puts ("found an entry that's not there");
106 result = 1;
109 /* Now the array contains only one element - no entry should be found. */
110 for (cnt = 0; cnt < narr; ++cnt)
112 key.val = arr[cnt].val;
114 res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
115 if (cnt == 5)
117 if (res == NULL)
119 printf ("entry %zd not found\n", cnt);
120 result = 1;
122 else if (res != &arr[cnt])
124 puts ("wrong entry returned");
125 result = 1;
128 else if (res != NULL)
130 puts ("found an entry that's not there");
131 result = 1;
135 if (align_check == 0)
137 puts ("alignment not checked");
138 result = 1;
140 else if (align_check == -1)
142 puts ("stack not sufficiently aligned");
143 result = 1;
146 if (result == 0)
147 puts ("all OK");
149 return result;