1 /* Test and measure strcasestr functions.
2 Copyright (C) 2010-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2010.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
21 #define TEST_NAME "strcasestr"
22 #include "test-string.h"
25 #define STRCASESTR simple_strcasestr
27 #define __strncasecmp strncasecmp
28 #include "strcasestr.c"
32 stupid_strcasestr (const char *s1
, const char *s2
)
34 ssize_t s1len
= strlen (s1
);
35 ssize_t s2len
= strlen (s2
);
40 for (ssize_t i
= 0; i
<= s1len
- s2len
; ++i
)
43 for (j
= 0; j
< s2len
; ++j
)
44 if (tolower (s1
[i
+ j
]) != tolower (s2
[j
]))
47 return (char *) s1
+ i
;
54 typedef char *(*proto_t
) (const char *, const char *);
56 IMPL (stupid_strcasestr
, 0)
57 IMPL (simple_strcasestr
, 0)
62 check_result (impl_t
*impl
, const char *s1
, const char *s2
,
65 char *result
= CALL (impl
, s1
, s2
);
66 if (result
!= exp_result
)
68 error (0, 0, "Wrong result in function %s %s %s", impl
->name
,
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)
84 hp_timing_t start
__attribute ((unused
));
85 hp_timing_t stop
__attribute ((unused
));
86 hp_timing_t best_time
= ~(hp_timing_t
) 0;
89 for (i
= 0; i
< 32; ++i
)
91 HP_TIMING_NOW (start
);
94 HP_TIMING_BEST (best_time
, start
, stop
);
97 printf ("\t%zd", (size_t) best_time
);
103 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
106 char *s1
= (char *) (buf1
+ align1
);
107 char *s2
= (char *) (buf2
+ align2
);
109 static const char d
[] = "1234567890abcxyz";
110 #define dl (sizeof (d) - 1)
112 for (size_t l
= len2
; l
> 0; l
= l
> dl
? l
- dl
: 0)
114 size_t t
= l
> dl
? dl
: l
;
115 ss2
= mempcpy (ss2
, d
, t
);
122 for (size_t l
= len1
; l
> 0; l
= l
> dl
? l
- dl
: 0)
124 size_t t
= l
> dl
? dl
: l
;
126 ++ss1
[len2
> 7 ? 7 : len2
- 1];
132 memset (s1
, '0', len1
);
133 for (size_t i
= 0; i
< len2
; ++i
)
134 s1
[len1
- len2
+ i
] = toupper (s2
[i
]);
139 printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
140 len1
, len2
, align1
, align2
, fail
? "fail" : "found");
142 FOR_EACH_IMPL (impl
, 0)
143 do_one_test (impl
, s1
, s2
, fail
? NULL
: s1
+ len1
- len2
);
152 const char s1
[] = "AOKB";
153 const char s2
[] = "OK";
156 exp_result
= stupid_strcasestr (s1
, s2
);
157 FOR_EACH_IMPL (impl
, 0)
158 check_result (impl
, s1
, s2
, exp_result
);
169 FOR_EACH_IMPL (impl
, 0)
170 printf ("\t%s", impl
->name
);
173 for (size_t klen
= 2; klen
< 32; ++klen
)
174 for (size_t hlen
= 2 * klen
; hlen
< 16 * klen
; hlen
+= klen
)
176 do_test (0, 0, hlen
, klen
, 0);
177 do_test (0, 0, hlen
, klen
, 1);
178 do_test (0, 3, hlen
, klen
, 0);
179 do_test (0, 3, hlen
, klen
, 1);
180 do_test (0, 9, hlen
, klen
, 0);
181 do_test (0, 9, hlen
, klen
, 1);
182 do_test (0, 15, hlen
, klen
, 0);
183 do_test (0, 15, hlen
, klen
, 1);
185 do_test (3, 0, hlen
, klen
, 0);
186 do_test (3, 0, hlen
, klen
, 1);
187 do_test (3, 3, hlen
, klen
, 0);
188 do_test (3, 3, hlen
, klen
, 1);
189 do_test (3, 9, hlen
, klen
, 0);
190 do_test (3, 9, hlen
, klen
, 1);
191 do_test (3, 15, hlen
, klen
, 0);
192 do_test (3, 15, hlen
, klen
, 1);
194 do_test (9, 0, hlen
, klen
, 0);
195 do_test (9, 0, hlen
, klen
, 1);
196 do_test (9, 3, hlen
, klen
, 0);
197 do_test (9, 3, hlen
, klen
, 1);
198 do_test (9, 9, hlen
, klen
, 0);
199 do_test (9, 9, hlen
, klen
, 1);
200 do_test (9, 15, hlen
, klen
, 0);
201 do_test (9, 15, hlen
, klen
, 1);
203 do_test (15, 0, hlen
, klen
, 0);
204 do_test (15, 0, hlen
, klen
, 1);
205 do_test (15, 3, hlen
, klen
, 0);
206 do_test (15, 3, hlen
, klen
, 1);
207 do_test (15, 9, hlen
, klen
, 0);
208 do_test (15, 9, hlen
, klen
, 1);
209 do_test (15, 15, hlen
, klen
, 0);
210 do_test (15, 15, hlen
, klen
, 1);
213 do_test (0, 0, page_size
- 1, 16, 0);
214 do_test (0, 0, page_size
- 1, 16, 1);
219 #include "../test-skeleton.c"