1 /* Copyright (C) 2000-2024 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/>. */
20 #include <tst-stack-align.h>
40 #define narr (sizeof (arr) / sizeof (arr[0]))
42 static int align_check
;
45 comp (const void *p1
, const void *p2
)
47 struct item
*e1
= (struct item
*) p1
;
48 struct item
*e2
= (struct item
*) p2
;
51 align_check
= TEST_STACK_ALIGN () ? -1 : 1;
53 return e1
->val
- e2
->val
;
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
);
73 printf ("entry %zd not found\n", cnt
);
76 else if (res
!= &arr
[cnt
])
78 puts ("wrong entry returned");
83 /* And some special tests that shouldn't find any entry. */
85 res
= (struct item
*) bsearch (&key
, arr
, narr
, sizeof (arr
[0]), comp
);
88 puts ("found an entry that's not there");
93 res
= (struct item
*) bsearch (&key
, arr
, narr
, sizeof (arr
[0]), comp
);
96 puts ("found an entry that's not there");
101 res
= (struct item
*) bsearch (&key
, arr
, 0, sizeof (arr
[0]), comp
);
104 puts ("found an entry that's not there");
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
);
118 printf ("entry %zd not found\n", cnt
);
121 else if (res
!= &arr
[cnt
])
123 puts ("wrong entry returned");
127 else if (res
!= NULL
)
129 puts ("found an entry that's not there");
134 if (align_check
== 0)
136 puts ("alignment not checked");
139 else if (align_check
== -1)
141 puts ("stack not sufficiently aligned");
151 #define TEST_FUNCTION do_test ()
152 #include "../test-skeleton.c"