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