1 /* Test and measure strcasestr functions.
2    Copyright (C) 2010-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
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, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #define TEST_MAIN
20 #define TEST_NAME "strcasestr"
21 #include "test-string.h"
22 
23 
24 #define STRCASESTR c_strcasestr
25 #define NO_ALIAS
26 #define __strncasecmp strncasecmp
27 #define __strnlen strnlen
28 #include "strcasestr.c"
29 
30 /* Naive implementation to verify results.  */
31 static char *
simple_strcasestr(const char * s1,const char * s2)32 simple_strcasestr (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 (tolower (s1[i + j]) != tolower (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 (c_strcasestr, 0)
57 IMPL (strcasestr, 1)
58 
59 
60 static int
check_result(impl_t * impl,const char * s1,const char * s2,char * exp_result)61 check_result (impl_t *impl, const char *s1, const char *s2,
62 	      char *exp_result)
63 {
64   char *result = CALL (impl, s1, s2);
65   if (result != exp_result)
66     {
67       error (0, 0, "Wrong result in function %s %s %s", impl->name,
68 	     (result == NULL) ? "(null)" : result,
69 	     (exp_result == NULL) ? "(null)" : exp_result);
70       ret = 1;
71       return -1;
72     }
73   return 0;
74 }
75 
76 static void
do_one_test(impl_t * impl,const char * s1,const char * s2,char * exp_result)77 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
78 {
79   if (check_result (impl, s1, s2, exp_result) < 0)
80     return;
81 }
82 
83 
84 static void
do_test(size_t align1,size_t align2,size_t len1,size_t len2,int fail)85 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
86 	 int fail)
87 {
88   char *s1 = (char *) (buf1 + align1);
89   char *s2 = (char *) (buf2 + align2);
90 
91   static const char d[] = "1234567890abcxyz";
92 #define dl (sizeof (d) - 1)
93   char *ss2 = s2;
94   for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
95     {
96       size_t t = l > dl ? dl : l;
97       ss2 = mempcpy (ss2, d, t);
98     }
99   s2[len2] = '\0';
100 
101   if (fail)
102     {
103       char *ss1 = s1;
104       for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
105 	{
106 	  size_t t = l > dl ? dl : l;
107 	  memcpy (ss1, d, t);
108 	  ++ss1[len2 > 7 ? 7 : len2 - 1];
109 	  ss1 += t;
110 	}
111     }
112   else
113     {
114       memset (s1, '0', len1);
115       for (size_t i = 0; i < len2; ++i)
116 	s1[len1 - len2 + i] = toupper (s2[i]);
117     }
118   s1[len1] = '\0';
119 
120   FOR_EACH_IMPL (impl, 0)
121     do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
122 }
123 
124 static void
check1(void)125 check1 (void)
126 {
127   const char s1[] = "AOKB";
128   const char s2[] = "OK";
129   char *exp_result;
130 
131   exp_result = simple_strcasestr (s1, s2);
132   FOR_EACH_IMPL (impl, 0)
133     check_result (impl, s1, s2, exp_result);
134 }
135 
136 static int
test_main(void)137 test_main (void)
138 {
139   test_init ();
140 
141   check1 ();
142 
143   printf ("%23s", "");
144   FOR_EACH_IMPL (impl, 0)
145     printf ("\t%s", impl->name);
146   putchar ('\n');
147 
148   for (size_t klen = 2; klen < 32; ++klen)
149     for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
150       {
151 	do_test (0, 0, hlen, klen, 0);
152 	do_test (0, 0, hlen, klen, 1);
153 	do_test (0, 3, hlen, klen, 0);
154 	do_test (0, 3, hlen, klen, 1);
155 	do_test (0, 9, hlen, klen, 0);
156 	do_test (0, 9, hlen, klen, 1);
157 	do_test (0, 15, hlen, klen, 0);
158 	do_test (0, 15, hlen, klen, 1);
159 
160 	do_test (3, 0, hlen, klen, 0);
161 	do_test (3, 0, hlen, klen, 1);
162 	do_test (3, 3, hlen, klen, 0);
163 	do_test (3, 3, hlen, klen, 1);
164 	do_test (3, 9, hlen, klen, 0);
165 	do_test (3, 9, hlen, klen, 1);
166 	do_test (3, 15, hlen, klen, 0);
167 	do_test (3, 15, hlen, klen, 1);
168 
169 	do_test (9, 0, hlen, klen, 0);
170 	do_test (9, 0, hlen, klen, 1);
171 	do_test (9, 3, hlen, klen, 0);
172 	do_test (9, 3, hlen, klen, 1);
173 	do_test (9, 9, hlen, klen, 0);
174 	do_test (9, 9, hlen, klen, 1);
175 	do_test (9, 15, hlen, klen, 0);
176 	do_test (9, 15, hlen, klen, 1);
177 
178 	do_test (15, 0, hlen, klen, 0);
179 	do_test (15, 0, hlen, klen, 1);
180 	do_test (15, 3, hlen, klen, 0);
181 	do_test (15, 3, hlen, klen, 1);
182 	do_test (15, 9, hlen, klen, 0);
183 	do_test (15, 9, hlen, klen, 1);
184 	do_test (15, 15, hlen, klen, 0);
185 	do_test (15, 15, hlen, klen, 1);
186       }
187 
188   do_test (0, 0, page_size - 1, 16, 0);
189   do_test (0, 0, page_size - 1, 16, 1);
190 
191   return ret;
192 }
193 
194 #include <support/test-driver.c>
195