]>
Commit | Line | Data |
---|---|---|
dbc676d4 | 1 | /* Test and measure strstr functions. |
688903eb | 2 | Copyright (C) 2010-2018 Free Software Foundation, Inc. |
dbc676d4 UD |
3 | This file is part of the GNU C Library. |
4 | Written by Ulrich Drepper <drepper@redhat.com>, 2010. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
dbc676d4 UD |
19 | |
20 | #define TEST_MAIN | |
69f07e5f | 21 | #define TEST_NAME "strstr" |
dbc676d4 UD |
22 | #include "test-string.h" |
23 | ||
24 | ||
25 | #define STRSTR simple_strstr | |
7c3018f9 | 26 | #define libc_hidden_builtin_def(arg) /* nothing */ |
3ae725df | 27 | #define __strnlen strnlen |
dbc676d4 UD |
28 | #include "strstr.c" |
29 | ||
30 | ||
31 | static char * | |
32 | stupid_strstr (const char *s1, const char *s2) | |
33 | { | |
34 | ssize_t s1len = strlen (s1); | |
35 | ssize_t s2len = strlen (s2); | |
36 | ||
37 | if (s2len > s1len) | |
38 | return NULL; | |
39 | ||
40 | for (ssize_t i = 0; i <= s1len - s2len; ++i) | |
41 | { | |
42 | size_t j; | |
43 | for (j = 0; j < s2len; ++j) | |
44 | if (s1[i + j] != s2[j]) | |
45 | break; | |
46 | if (j == s2len) | |
47 | return (char *) s1 + i; | |
48 | } | |
49 | ||
50 | return NULL; | |
51 | } | |
52 | ||
53 | ||
54 | typedef char *(*proto_t) (const char *, const char *); | |
55 | ||
56 | IMPL (stupid_strstr, 0) | |
57 | IMPL (simple_strstr, 0) | |
58 | IMPL (strstr, 1) | |
59 | ||
60 | ||
03759f47 L |
61 | static int |
62 | check_result (impl_t *impl, const char *s1, const char *s2, | |
63 | char *exp_result) | |
dbc676d4 UD |
64 | { |
65 | char *result = CALL (impl, s1, s2); | |
66 | if (result != exp_result) | |
67 | { | |
68 | error (0, 0, "Wrong result in function %s %s %s", impl->name, | |
69 | result, exp_result); | |
70 | ret = 1; | |
03759f47 | 71 | return -1; |
dbc676d4 UD |
72 | } |
73 | ||
03759f47 L |
74 | return 0; |
75 | } | |
76 | ||
77 | static void | |
78 | do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result) | |
79 | { | |
80 | if (check_result (impl, s1, s2, exp_result) < 0) | |
81 | return; | |
dbc676d4 UD |
82 | } |
83 | ||
84 | ||
85 | static void | |
86 | do_test (size_t align1, size_t align2, size_t len1, size_t len2, | |
87 | int fail) | |
88 | { | |
89 | char *s1 = (char *) (buf1 + align1); | |
90 | char *s2 = (char *) (buf2 + align2); | |
91 | ||
92 | static const char d[] = "1234567890abcdef"; | |
93 | #define dl (sizeof (d) - 1) | |
94 | char *ss2 = s2; | |
95 | for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0) | |
96 | { | |
97 | size_t t = l > dl ? dl : l; | |
98 | ss2 = mempcpy (ss2, d, t); | |
99 | } | |
100 | s2[len2] = '\0'; | |
101 | ||
102 | if (fail) | |
103 | { | |
104 | char *ss1 = s1; | |
105 | for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0) | |
106 | { | |
107 | size_t t = l > dl ? dl : l; | |
108 | memcpy (ss1, d, t); | |
109 | ++ss1[len2 > 7 ? 7 : len2 - 1]; | |
110 | ss1 += t; | |
111 | } | |
112 | } | |
113 | else | |
114 | { | |
115 | memset (s1, '0', len1); | |
116 | memcpy (s1 + len1 - len2, s2, len2); | |
117 | } | |
118 | s1[len1] = '\0'; | |
119 | ||
dbc676d4 UD |
120 | FOR_EACH_IMPL (impl, 0) |
121 | do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2); | |
122 | ||
dbc676d4 UD |
123 | } |
124 | ||
03759f47 L |
125 | static void |
126 | check1 (void) | |
127 | { | |
128 | const char s1[] = | |
129 | "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_A7_20_EF_BF_BD"; | |
130 | const char s2[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"; | |
131 | char *exp_result; | |
132 | ||
133 | exp_result = stupid_strstr (s1, s2); | |
134 | FOR_EACH_IMPL (impl, 0) | |
135 | check_result (impl, s1, s2, exp_result); | |
136 | } | |
dbc676d4 | 137 | |
d7e0dab9 L |
138 | static void |
139 | check2 (void) | |
140 | { | |
141 | const char s1[] = ", enable_static, \0, enable_shared, "; | |
142 | char *exp_result; | |
5d41d91a | 143 | char *s2 = (void *) buf1 + page_size - 18; |
d7e0dab9 | 144 | |
5d41d91a | 145 | strcpy (s2, s1); |
d7e0dab9 L |
146 | exp_result = stupid_strstr (s1, s1 + 18); |
147 | FOR_EACH_IMPL (impl, 0) | |
5d41d91a L |
148 | { |
149 | check_result (impl, s1, s1 + 18, exp_result); | |
150 | check_result (impl, s2, s1 + 18, exp_result); | |
151 | } | |
d7e0dab9 L |
152 | } |
153 | ||
dbc676d4 UD |
154 | static int |
155 | test_main (void) | |
156 | { | |
157 | test_init (); | |
158 | ||
03759f47 | 159 | check1 (); |
d7e0dab9 | 160 | check2 (); |
03759f47 | 161 | |
dbc676d4 UD |
162 | printf ("%23s", ""); |
163 | FOR_EACH_IMPL (impl, 0) | |
164 | printf ("\t%s", impl->name); | |
165 | putchar ('\n'); | |
166 | ||
167 | for (size_t klen = 2; klen < 32; ++klen) | |
168 | for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen) | |
169 | { | |
170 | do_test (0, 0, hlen, klen, 0); | |
171 | do_test (0, 0, hlen, klen, 1); | |
172 | do_test (0, 3, hlen, klen, 0); | |
173 | do_test (0, 3, hlen, klen, 1); | |
174 | do_test (0, 9, hlen, klen, 0); | |
175 | do_test (0, 9, hlen, klen, 1); | |
176 | do_test (0, 15, hlen, klen, 0); | |
177 | do_test (0, 15, hlen, klen, 1); | |
178 | ||
179 | do_test (3, 0, hlen, klen, 0); | |
180 | do_test (3, 0, hlen, klen, 1); | |
181 | do_test (3, 3, hlen, klen, 0); | |
182 | do_test (3, 3, hlen, klen, 1); | |
183 | do_test (3, 9, hlen, klen, 0); | |
184 | do_test (3, 9, hlen, klen, 1); | |
185 | do_test (3, 15, hlen, klen, 0); | |
186 | do_test (3, 15, hlen, klen, 1); | |
187 | ||
188 | do_test (9, 0, hlen, klen, 0); | |
189 | do_test (9, 0, hlen, klen, 1); | |
190 | do_test (9, 3, hlen, klen, 0); | |
191 | do_test (9, 3, hlen, klen, 1); | |
192 | do_test (9, 9, hlen, klen, 0); | |
193 | do_test (9, 9, hlen, klen, 1); | |
194 | do_test (9, 15, hlen, klen, 0); | |
195 | do_test (9, 15, hlen, klen, 1); | |
196 | ||
197 | do_test (15, 0, hlen, klen, 0); | |
198 | do_test (15, 0, hlen, klen, 1); | |
199 | do_test (15, 3, hlen, klen, 0); | |
200 | do_test (15, 3, hlen, klen, 1); | |
201 | do_test (15, 9, hlen, klen, 0); | |
202 | do_test (15, 9, hlen, klen, 1); | |
203 | do_test (15, 15, hlen, klen, 0); | |
204 | do_test (15, 15, hlen, klen, 1); | |
205 | } | |
206 | ||
207 | do_test (0, 0, page_size - 1, 16, 0); | |
208 | do_test (0, 0, page_size - 1, 16, 1); | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
fb82116f | 213 | #include <support/test-driver.c> |