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