]> sourceware.org Git - glibc.git/blob - stdlib/tst-bsearch.c
Update.
[glibc.git] / stdlib / tst-bsearch.c
1 /* Copyright (C) 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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 {
57 int cnt;
58 int result = 0;
59 struct entry key;
60 struct entry *res;
61
62 for (cnt = 0; cnt < narr; ++cnt)
63 {
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 {
70 printf ("entry %d not found\n", cnt);
71 result = 1;
72 }
73 else if (res != &arr[cnt])
74 {
75 puts ("wrong entry returned");
76 result = 1;
77 }
78 }
79
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 }
104
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 {
115 printf ("entry %d not found\n", cnt);
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
131 if (result == 0)
132 puts ("all OK");
133
134 return result;
135 }
This page took 0.041264 seconds and 5 git commands to generate.