1 /* Copyright (C) 2000-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 #include <tst-stack-align.h>
41 #define narr (sizeof (arr) / sizeof (arr[0]))
43 static int align_check
;
46 comp (const void *p1
, const void *p2
)
48 struct item
*e1
= (struct item
*) p1
;
49 struct item
*e2
= (struct item
*) p2
;
52 align_check
= TEST_STACK_ALIGN () ? -1 : 1;
54 return e1
->val
- e2
->val
;
66 for (cnt
= 0; cnt
< narr
; ++cnt
)
69 key
.val
= arr
[cnt
].val
;
71 res
= (struct item
*) bsearch (&key
, arr
, narr
, sizeof (arr
[0]), comp
);
74 printf ("entry %zd not found\n", cnt
);
77 else if (res
!= &arr
[cnt
])
79 puts ("wrong entry returned");
84 /* And some special tests that shouldn't find any entry. */
86 res
= (struct item
*) bsearch (&key
, arr
, narr
, sizeof (arr
[0]), comp
);
89 puts ("found an entry that's not there");
94 res
= (struct item
*) bsearch (&key
, arr
, narr
, sizeof (arr
[0]), comp
);
97 puts ("found an entry that's not there");
102 res
= (struct item
*) bsearch (&key
, arr
, 0, sizeof (arr
[0]), comp
);
105 puts ("found an entry that's not there");
109 /* Now the array contains only one element - no entry should be found. */
110 for (cnt
= 0; cnt
< narr
; ++cnt
)
112 key
.val
= arr
[cnt
].val
;
114 res
= (struct item
*) bsearch (&key
, &arr
[5], 1, sizeof (arr
[0]), comp
);
119 printf ("entry %zd not found\n", cnt
);
122 else if (res
!= &arr
[cnt
])
124 puts ("wrong entry returned");
128 else if (res
!= NULL
)
130 puts ("found an entry that's not there");
135 if (align_check
== 0)
137 puts ("alignment not checked");
140 else if (align_check
== -1)
142 puts ("stack not sufficiently aligned");
152 #define TEST_FUNCTION do_test ()
153 #include "../test-skeleton.c"