1 /* Test and measure strstr functions.
2 Copyright (C) 2010-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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/>. */
20 #define TEST_NAME "strstr"
21 #include "test-string.h"
24 #define STRSTR c_strstr
25 #define libc_hidden_builtin_def(arg) /* nothing */
26 #define __strnlen strnlen
29 /* Naive implementation to verify results. */
31 simple_strstr (const char *s1
, const char *s2
)
33 ssize_t s1len
= strlen (s1
);
34 ssize_t s2len
= strlen (s2
);
39 for (ssize_t i
= 0; i
<= s1len
- s2len
; ++i
)
42 for (j
= 0; j
< s2len
; ++j
)
43 if (s1
[i
+ j
] != s2
[j
])
46 return (char *) s1
+ i
;
53 typedef char *(*proto_t
) (const char *, const char *);
60 check_result (impl_t
*impl
, const char *s1
, const char *s2
,
63 char *result
= CALL (impl
, s1
, s2
);
64 if (result
!= exp_result
)
66 error (0, 0, "Wrong result in function %s %s %s", impl
->name
,
67 (result
== NULL
) ? "(null)" : result
,
68 (exp_result
== NULL
) ? "(null)" : exp_result
);
77 do_one_test (impl_t
*impl
, const char *s1
, const char *s2
, char *exp_result
)
79 if (check_result (impl
, s1
, s2
, exp_result
) < 0)
85 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
88 char *s1
= (char *) (buf1
+ align1
);
89 char *s2
= (char *) (buf2
+ align2
);
91 static const char d
[] = "1234567890abcdef";
92 #define dl (sizeof (d) - 1)
94 for (size_t l
= len2
; l
> 0; l
= l
> dl
? l
- dl
: 0)
96 size_t t
= l
> dl
? dl
: l
;
97 ss2
= mempcpy (ss2
, d
, t
);
104 for (size_t l
= len1
; l
> 0; l
= l
> dl
? l
- dl
: 0)
106 size_t t
= l
> dl
? dl
: l
;
108 ++ss1
[len2
> 7 ? 7 : len2
- 1];
114 memset (s1
, '0', len1
);
115 memcpy (s1
+ len1
- len2
, s2
, len2
);
119 FOR_EACH_IMPL (impl
, 0)
120 do_one_test (impl
, s1
, s2
, fail
? NULL
: s1
+ len1
- len2
);
128 "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";
129 const char s2
[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
132 exp_result
= simple_strstr (s1
, s2
);
133 FOR_EACH_IMPL (impl
, 0)
134 check_result (impl
, s1
, s2
, exp_result
);
140 const char s1_stack
[] = ", enable_static, \0, enable_shared, ";
141 const size_t s1_byte_count
= 18;
142 const char *s2_stack
= &(s1_stack
[s1_byte_count
]);
143 const size_t s2_byte_count
= 18;
145 const size_t page_size_real
= getpagesize ();
147 /* Haystack at end of page. The following page is protected. */
148 char *s1_page_end
= (void *) buf1
+ page_size
- s1_byte_count
;
149 strcpy (s1_page_end
, s1_stack
);
151 /* Haystack which crosses a page boundary.
152 Note: page_size is at least 2 * getpagesize. See test_init. */
153 char *s1_page_cross
= (void *) buf1
+ page_size_real
- 8;
154 strcpy (s1_page_cross
, s1_stack
);
156 /* Needle at end of page. The following page is protected. */
157 char *s2_page_end
= (void *) buf2
+ page_size
- s2_byte_count
;
158 strcpy (s2_page_end
, s2_stack
);
160 /* Needle which crosses a page boundary.
161 Note: page_size is at least 2 * getpagesize. See test_init. */
162 char *s2_page_cross
= (void *) buf2
+ page_size_real
- 8;
163 strcpy (s2_page_cross
, s2_stack
);
165 exp_result
= simple_strstr (s1_stack
, s2_stack
);
166 FOR_EACH_IMPL (impl
, 0)
168 check_result (impl
, s1_stack
, s2_stack
, exp_result
);
169 check_result (impl
, s1_stack
, s2_page_end
, exp_result
);
170 check_result (impl
, s1_stack
, s2_page_cross
, exp_result
);
172 check_result (impl
, s1_page_end
, s2_stack
, exp_result
);
173 check_result (impl
, s1_page_end
, s2_page_end
, exp_result
);
174 check_result (impl
, s1_page_end
, s2_page_cross
, exp_result
);
176 check_result (impl
, s1_page_cross
, s2_stack
, exp_result
);
177 check_result (impl
, s1_page_cross
, s2_page_end
, exp_result
);
178 check_result (impl
, s1_page_cross
, s2_page_cross
, exp_result
);
187 char *h
= (char*) buf1
;
188 char *n
= (char*) buf2
;
190 for (int i
= 0; i
< N
; i
++)
200 /* Ensure we don't match at the first 'x'. */
203 char *exp_result
= simple_strstr (h
, n
);
204 FOR_EACH_IMPL (impl
, 0)
205 check_result (impl
, h
, n
, exp_result
);
218 FOR_EACH_IMPL (impl
, 0)
219 printf ("\t%s", impl
->name
);
222 for (size_t klen
= 2; klen
< 32; ++klen
)
223 for (size_t hlen
= 2 * klen
; hlen
< 16 * klen
; hlen
+= klen
)
225 do_test (0, 0, hlen
, klen
, 0);
226 do_test (0, 0, hlen
, klen
, 1);
227 do_test (0, 3, hlen
, klen
, 0);
228 do_test (0, 3, hlen
, klen
, 1);
229 do_test (0, 9, hlen
, klen
, 0);
230 do_test (0, 9, hlen
, klen
, 1);
231 do_test (0, 15, hlen
, klen
, 0);
232 do_test (0, 15, hlen
, klen
, 1);
234 do_test (3, 0, hlen
, klen
, 0);
235 do_test (3, 0, hlen
, klen
, 1);
236 do_test (3, 3, hlen
, klen
, 0);
237 do_test (3, 3, hlen
, klen
, 1);
238 do_test (3, 9, hlen
, klen
, 0);
239 do_test (3, 9, hlen
, klen
, 1);
240 do_test (3, 15, hlen
, klen
, 0);
241 do_test (3, 15, hlen
, klen
, 1);
243 do_test (9, 0, hlen
, klen
, 0);
244 do_test (9, 0, hlen
, klen
, 1);
245 do_test (9, 3, hlen
, klen
, 0);
246 do_test (9, 3, hlen
, klen
, 1);
247 do_test (9, 9, hlen
, klen
, 0);
248 do_test (9, 9, hlen
, klen
, 1);
249 do_test (9, 15, hlen
, klen
, 0);
250 do_test (9, 15, hlen
, klen
, 1);
252 do_test (15, 0, hlen
, klen
, 0);
253 do_test (15, 0, hlen
, klen
, 1);
254 do_test (15, 3, hlen
, klen
, 0);
255 do_test (15, 3, hlen
, klen
, 1);
256 do_test (15, 9, hlen
, klen
, 0);
257 do_test (15, 9, hlen
, klen
, 1);
258 do_test (15, 15, hlen
, klen
, 0);
259 do_test (15, 15, hlen
, klen
, 1);
261 do_test (15, 15, hlen
+ klen
* 4, klen
* 4, 0);
262 do_test (15, 15, hlen
+ klen
* 4, klen
* 4, 1);
265 do_test (0, 0, page_size
- 1, 16, 0);
266 do_test (0, 0, page_size
- 1, 16, 1);
271 #include <support/test-driver.c>