(libc): Add GLIBC_2.3.1. (libpthread): Add GLIBC_2.3.1.
[glibc.git] / stdlib / tst-bsearch.c
blobad80776785b4da71c0b5c6427a4be32a240822cb
1 /* Copyright (C) 2000, 2002 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>
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]))
44 static int
45 comp (const void *p1, const void *p2)
47 struct entry *e1 = (struct entry *) p1;
48 struct entry *e2 = (struct entry *) p2;
50 return e1->val - e2->val;
54 int
55 main (void)
57 size_t cnt;
58 int result = 0;
59 struct entry key;
60 struct entry *res;
62 for (cnt = 0; cnt < narr; ++cnt)
65 key.val = arr[cnt].val;
67 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
68 if (res == NULL)
70 printf ("entry %zd not found\n", cnt);
71 result = 1;
73 else if (res != &arr[cnt])
75 puts ("wrong entry returned");
76 result = 1;
80 /* And some special tests that shouldn't find any entry. */
81 key.val = -1;
82 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
83 if (res != NULL)
85 puts ("found an entry that's not there");
86 result = 1;
89 key.val = 11;
90 res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
91 if (res != NULL)
93 puts ("found an entry that's not there");
94 result = 1;
97 key.val = 11;
98 res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
99 if (res != NULL)
101 puts ("found an entry that's not there");
102 result = 1;
105 /* Now the array contains only one element - no entry should be found. */
106 for (cnt = 0; cnt < narr; ++cnt)
108 key.val = arr[cnt].val;
110 res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
111 if (cnt == 5)
113 if (res == NULL)
115 printf ("entry %zd not found\n", cnt);
116 result = 1;
118 else if (res != &arr[cnt])
120 puts ("wrong entry returned");
121 result = 1;
124 else if (res != NULL)
126 puts ("found an entry that's not there");
127 result = 1;
131 if (result == 0)
132 puts ("all OK");
134 return result;