Fix ldbl-128 built-in function use
[glibc.git] / stdlib / tst-bsearch.c
blob82789e70c3e6d30dd73b637806534678f8778ed3
1 /* Copyright (C) 2000-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <tst-stack-align.h>
22 struct item
24 int val;
25 const char *str;
26 } arr[] =
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" }
40 #define narr (sizeof (arr) / sizeof (arr[0]))
42 static int align_check;
44 static int
45 comp (const void *p1, const void *p2)
47 struct item *e1 = (struct item *) p1;
48 struct item *e2 = (struct item *) p2;
50 if (!align_check)
51 align_check = TEST_STACK_ALIGN () ? -1 : 1;
53 return e1->val - e2->val;
57 static int
58 do_test (void)
60 size_t cnt;
61 int result = 0;
62 struct item key;
63 struct item *res;
65 for (cnt = 0; cnt < narr; ++cnt)
68 key.val = arr[cnt].val;
70 res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
71 if (res == NULL)
73 printf ("entry %zd not found\n", cnt);
74 result = 1;
76 else if (res != &arr[cnt])
78 puts ("wrong entry returned");
79 result = 1;
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)
88 puts ("found an entry that's not there");
89 result = 1;
92 key.val = 11;
93 res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
94 if (res != NULL)
96 puts ("found an entry that's not there");
97 result = 1;
100 key.val = 11;
101 res = (struct item *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
102 if (res != NULL)
104 puts ("found an entry that's not there");
105 result = 1;
108 /* Now the array contains only one element - no entry should be found. */
109 for (cnt = 0; cnt < narr; ++cnt)
111 key.val = arr[cnt].val;
113 res = (struct item *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
114 if (cnt == 5)
116 if (res == NULL)
118 printf ("entry %zd not found\n", cnt);
119 result = 1;
121 else if (res != &arr[cnt])
123 puts ("wrong entry returned");
124 result = 1;
127 else if (res != NULL)
129 puts ("found an entry that's not there");
130 result = 1;
134 if (align_check == 0)
136 puts ("alignment not checked");
137 result = 1;
139 else if (align_check == -1)
141 puts ("stack not sufficiently aligned");
142 result = 1;
145 if (result == 0)
146 puts ("all OK");
148 return result;
151 #define TEST_FUNCTION do_test ()
152 #include "../test-skeleton.c"