]>
Commit | Line | Data |
---|---|---|
6d7e8eda | 1 | /* Copyright (C) 2000-2023 Free Software Foundation, Inc. |
8cb9c650 | 2 | This file is part of the GNU C Library. |
8cb9c650 UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
8cb9c650 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
8cb9c650 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
8cb9c650 UD |
17 | |
18 | #include <stdio.h> | |
19 | #include <stdlib.h> | |
a4db3439 | 20 | #include <tst-stack-align.h> |
8cb9c650 | 21 | |
fa13e15b | 22 | struct item |
8cb9c650 UD |
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 | ||
a4db3439 | 42 | static int align_check; |
8cb9c650 UD |
43 | |
44 | static int | |
45 | comp (const void *p1, const void *p2) | |
46 | { | |
fa13e15b AS |
47 | struct item *e1 = (struct item *) p1; |
48 | struct item *e2 = (struct item *) p2; | |
8cb9c650 | 49 | |
a4db3439 UD |
50 | if (!align_check) |
51 | align_check = TEST_STACK_ALIGN () ? -1 : 1; | |
52 | ||
8cb9c650 UD |
53 | return e1->val - e2->val; |
54 | } | |
55 | ||
56 | ||
fa13e15b AS |
57 | static int |
58 | do_test (void) | |
8cb9c650 | 59 | { |
57b36a0a | 60 | size_t cnt; |
8cb9c650 | 61 | int result = 0; |
fa13e15b AS |
62 | struct item key; |
63 | struct item *res; | |
8cb9c650 UD |
64 | |
65 | for (cnt = 0; cnt < narr; ++cnt) | |
66 | { | |
8cb9c650 UD |
67 | |
68 | key.val = arr[cnt].val; | |
69 | ||
fa13e15b | 70 | res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp); |
8cb9c650 UD |
71 | if (res == NULL) |
72 | { | |
9a2d7205 | 73 | printf ("entry %zd not found\n", cnt); |
8cb9c650 UD |
74 | result = 1; |
75 | } | |
76 | else if (res != &arr[cnt]) | |
77 | { | |
78 | puts ("wrong entry returned"); | |
79 | result = 1; | |
80 | } | |
81 | } | |
82 | ||
9aee41de AJ |
83 | /* And some special tests that shouldn't find any entry. */ |
84 | key.val = -1; | |
fa13e15b | 85 | res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp); |
9aee41de AJ |
86 | if (res != NULL) |
87 | { | |
88 | puts ("found an entry that's not there"); | |
89 | result = 1; | |
90 | } | |
91 | ||
92 | key.val = 11; | |
fa13e15b | 93 | res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp); |
9aee41de AJ |
94 | if (res != NULL) |
95 | { | |
96 | puts ("found an entry that's not there"); | |
97 | result = 1; | |
98 | } | |
99 | ||
100 | key.val = 11; | |
fa13e15b | 101 | res = (struct item *) bsearch (&key, arr, 0, sizeof (arr[0]), comp); |
9aee41de AJ |
102 | if (res != NULL) |
103 | { | |
104 | puts ("found an entry that's not there"); | |
105 | result = 1; | |
106 | } | |
57b36a0a | 107 | |
9aee41de AJ |
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 | ||
fa13e15b | 113 | res = (struct item *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp); |
9aee41de AJ |
114 | if (cnt == 5) |
115 | { | |
116 | if (res == NULL) | |
117 | { | |
9a2d7205 | 118 | printf ("entry %zd not found\n", cnt); |
9aee41de AJ |
119 | result = 1; |
120 | } | |
121 | else if (res != &arr[cnt]) | |
122 | { | |
123 | puts ("wrong entry returned"); | |
124 | result = 1; | |
125 | } | |
126 | } | |
127 | else if (res != NULL) | |
128 | { | |
129 | puts ("found an entry that's not there"); | |
130 | result = 1; | |
131 | } | |
132 | } | |
133 | ||
a4db3439 UD |
134 | if (align_check == 0) |
135 | { | |
136 | puts ("alignment not checked"); | |
137 | result = 1; | |
138 | } | |
139 | else if (align_check == -1) | |
140 | { | |
141 | puts ("stack not sufficiently aligned"); | |
142 | result = 1; | |
143 | } | |
144 | ||
8cb9c650 UD |
145 | if (result == 0) |
146 | puts ("all OK"); | |
147 | ||
148 | return result; | |
149 | } | |
fa13e15b AS |
150 | |
151 | #define TEST_FUNCTION do_test () | |
152 | #include "../test-skeleton.c" |