1/* Copyright (C) 2000-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <tst-stack-align.h>
21
22struct item
23{
24 int val;
25 const char *str;
26} arr[] =
27{
28 { 0, "zero" },
29 { 1, "one" },
30 { 2, "two" },
31 { 3, "three" },
32 { 4, "four" },
33 { 5, "five" },
34 { 6, "six" },
35 { 7, "seven" },
36 { 8, "eight" },
37 { 9, "nine" },
38 { 10, "ten" }
39};
40#define narr (sizeof (arr) / sizeof (arr[0]))
41
42static int align_check;
43
44static int
45comp (const void *p1, const void *p2)
46{
47 struct item *e1 = (struct item *) p1;
48 struct item *e2 = (struct item *) p2;
49
50 if (!align_check)
51 align_check = TEST_STACK_ALIGN () ? -1 : 1;
52
53 return e1->val - e2->val;
54}
55
56
57static int
58do_test (void)
59{
60 size_t cnt;
61 int result = 0;
62 struct item key;
63 struct item *res;
64
65 for (cnt = 0; cnt < narr; ++cnt)
66 {
67
68 key.val = arr[cnt].val;
69
70 res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
71 if (res == NULL)
72 {
73 printf (format: "entry %zd not found\n", cnt);
74 result = 1;
75 }
76 else if (res != &arr[cnt])
77 {
78 puts (s: "wrong entry returned");
79 result = 1;
80 }
81 }
82
83 /* And some special tests that shouldn't find any entry. */
84 key.val = -1;
85 res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
86 if (res != NULL)
87 {
88 puts (s: "found an entry that's not there");
89 result = 1;
90 }
91
92 key.val = 11;
93 res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
94 if (res != NULL)
95 {
96 puts (s: "found an entry that's not there");
97 result = 1;
98 }
99
100 key.val = 11;
101 res = (struct item *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
102 if (res != NULL)
103 {
104 puts (s: "found an entry that's not there");
105 result = 1;
106 }
107
108 /* Now the array contains only one element - no entry should be found. */
109 for (cnt = 0; cnt < narr; ++cnt)
110 {
111 key.val = arr[cnt].val;
112
113 res = (struct item *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
114 if (cnt == 5)
115 {
116 if (res == NULL)
117 {
118 printf (format: "entry %zd not found\n", cnt);
119 result = 1;
120 }
121 else if (res != &arr[cnt])
122 {
123 puts (s: "wrong entry returned");
124 result = 1;
125 }
126 }
127 else if (res != NULL)
128 {
129 puts (s: "found an entry that's not there");
130 result = 1;
131 }
132 }
133
134 if (align_check == 0)
135 {
136 puts (s: "alignment not checked");
137 result = 1;
138 }
139 else if (align_check == -1)
140 {
141 puts (s: "stack not sufficiently aligned");
142 result = 1;
143 }
144
145 if (result == 0)
146 puts (s: "all OK");
147
148 return result;
149}
150
151#define TEST_FUNCTION do_test ()
152#include "../test-skeleton.c"
153

source code of glibc/stdlib/tst-bsearch.c