]>
Commit | Line | Data |
---|---|---|
57b36a0a | 1 | /* Copyright (C) 2000, 2002 Free Software Foundation, Inc. |
8cb9c650 UD |
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 | |
41bdb6e2 AJ |
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. | |
8cb9c650 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
8cb9c650 | 14 | |
41bdb6e2 AJ |
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. */ | |
8cb9c650 UD |
19 | |
20 | #include <stdio.h> | |
21 | #include <stdlib.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 | ||
44 | static int | |
45 | comp (const void *p1, const void *p2) | |
46 | { | |
47 | struct entry *e1 = (struct entry *) p1; | |
48 | struct entry *e2 = (struct entry *) p2; | |
49 | ||
50 | return e1->val - e2->val; | |
51 | } | |
52 | ||
53 | ||
54 | int | |
55 | main (void) | |
56 | { | |
57b36a0a | 57 | size_t cnt; |
8cb9c650 | 58 | int result = 0; |
9aee41de AJ |
59 | struct entry key; |
60 | struct entry *res; | |
8cb9c650 UD |
61 | |
62 | for (cnt = 0; cnt < narr; ++cnt) | |
63 | { | |
8cb9c650 UD |
64 | |
65 | key.val = arr[cnt].val; | |
66 | ||
67 | res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp); | |
68 | if (res == NULL) | |
69 | { | |
9a2d7205 | 70 | printf ("entry %zd not found\n", cnt); |
8cb9c650 UD |
71 | result = 1; |
72 | } | |
73 | else if (res != &arr[cnt]) | |
74 | { | |
75 | puts ("wrong entry returned"); | |
76 | result = 1; | |
77 | } | |
78 | } | |
79 | ||
9aee41de AJ |
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) | |
84 | { | |
85 | puts ("found an entry that's not there"); | |
86 | result = 1; | |
87 | } | |
88 | ||
89 | key.val = 11; | |
90 | res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp); | |
91 | if (res != NULL) | |
92 | { | |
93 | puts ("found an entry that's not there"); | |
94 | result = 1; | |
95 | } | |
96 | ||
97 | key.val = 11; | |
98 | res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp); | |
99 | if (res != NULL) | |
100 | { | |
101 | puts ("found an entry that's not there"); | |
102 | result = 1; | |
103 | } | |
57b36a0a | 104 | |
9aee41de AJ |
105 | /* Now the array contains only one element - no entry should be found. */ |
106 | for (cnt = 0; cnt < narr; ++cnt) | |
107 | { | |
108 | key.val = arr[cnt].val; | |
109 | ||
110 | res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp); | |
111 | if (cnt == 5) | |
112 | { | |
113 | if (res == NULL) | |
114 | { | |
9a2d7205 | 115 | printf ("entry %zd not found\n", cnt); |
9aee41de AJ |
116 | result = 1; |
117 | } | |
118 | else if (res != &arr[cnt]) | |
119 | { | |
120 | puts ("wrong entry returned"); | |
121 | result = 1; | |
122 | } | |
123 | } | |
124 | else if (res != NULL) | |
125 | { | |
126 | puts ("found an entry that's not there"); | |
127 | result = 1; | |
128 | } | |
129 | } | |
130 | ||
8cb9c650 UD |
131 | if (result == 0) |
132 | puts ("all OK"); | |
133 | ||
134 | return result; | |
135 | } |