Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Test and measure strstr functions.
2 Copyright (C) 2010-2019 Free Software Foundation, Inc.
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
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#define TEST_MAIN
21#define TEST_NAME "strstr"
22#include "test-string.h"
23
24
25#define STRSTR simple_strstr
26#define libc_hidden_builtin_def(arg) /* nothing */
27#define __strnlen strnlen
28#include "strstr.c"
29
30
31static char *
32stupid_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
54typedef char *(*proto_t) (const char *, const char *);
55
56IMPL (stupid_strstr, 0)
57IMPL (simple_strstr, 0)
58IMPL (strstr, 1)
59
60
61static int
62check_result (impl_t *impl, const char *s1, const char *s2,
63 char *exp_result)
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;
71 return -1;
72 }
73
74 return 0;
75}
76
77static void
78do_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;
82}
83
84
85static void
86do_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
120 FOR_EACH_IMPL (impl, 0)
121 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
122
123}
124
125static void
126check1 (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}
137
138static void
139check2 (void)
140{
141 const char s1[] = ", enable_static, \0, enable_shared, ";
142 char *exp_result;
143 char *s2 = (void *) buf1 + page_size - 18;
144
145 strcpy (s2, s1);
146 exp_result = stupid_strstr (s1, s1 + 18);
147 FOR_EACH_IMPL (impl, 0)
148 {
149 check_result (impl, s1, s1 + 18, exp_result);
150 check_result (impl, s2, s1 + 18, exp_result);
151 }
152}
153
154#define N 1024
155
156static void
157pr23637 (void)
158{
159 char *h = (char*) buf1;
160 char *n = (char*) buf2;
161
162 for (int i = 0; i < N; i++)
163 {
164 n[i] = 'x';
165 h[i] = ' ';
166 h[i + N] = 'x';
167 }
168
169 n[N] = '\0';
170 h[N * 2] = '\0';
171
172 /* Ensure we don't match at the first 'x'. */
173 h[0] = 'x';
174
175 char *exp_result = stupid_strstr (h, n);
176 FOR_EACH_IMPL (impl, 0)
177 check_result (impl, h, n, exp_result);
178}
179
180static int
181test_main (void)
182{
183 test_init ();
184
185 check1 ();
186 check2 ();
187 pr23637 ();
188
189 printf ("%23s", "");
190 FOR_EACH_IMPL (impl, 0)
191 printf ("\t%s", impl->name);
192 putchar ('\n');
193
194 for (size_t klen = 2; klen < 32; ++klen)
195 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
196 {
197 do_test (0, 0, hlen, klen, 0);
198 do_test (0, 0, hlen, klen, 1);
199 do_test (0, 3, hlen, klen, 0);
200 do_test (0, 3, hlen, klen, 1);
201 do_test (0, 9, hlen, klen, 0);
202 do_test (0, 9, hlen, klen, 1);
203 do_test (0, 15, hlen, klen, 0);
204 do_test (0, 15, hlen, klen, 1);
205
206 do_test (3, 0, hlen, klen, 0);
207 do_test (3, 0, hlen, klen, 1);
208 do_test (3, 3, hlen, klen, 0);
209 do_test (3, 3, hlen, klen, 1);
210 do_test (3, 9, hlen, klen, 0);
211 do_test (3, 9, hlen, klen, 1);
212 do_test (3, 15, hlen, klen, 0);
213 do_test (3, 15, hlen, klen, 1);
214
215 do_test (9, 0, hlen, klen, 0);
216 do_test (9, 0, hlen, klen, 1);
217 do_test (9, 3, hlen, klen, 0);
218 do_test (9, 3, hlen, klen, 1);
219 do_test (9, 9, hlen, klen, 0);
220 do_test (9, 9, hlen, klen, 1);
221 do_test (9, 15, hlen, klen, 0);
222 do_test (9, 15, hlen, klen, 1);
223
224 do_test (15, 0, hlen, klen, 0);
225 do_test (15, 0, hlen, klen, 1);
226 do_test (15, 3, hlen, klen, 0);
227 do_test (15, 3, hlen, klen, 1);
228 do_test (15, 9, hlen, klen, 0);
229 do_test (15, 9, hlen, klen, 1);
230 do_test (15, 15, hlen, klen, 0);
231 do_test (15, 15, hlen, klen, 1);
232
233 do_test (15, 15, hlen + klen * 4, klen * 4, 0);
234 do_test (15, 15, hlen + klen * 4, klen * 4, 1);
235 }
236
237 do_test (0, 0, page_size - 1, 16, 0);
238 do_test (0, 0, page_size - 1, 16, 1);
239
240 return ret;
241}
242
243#include <support/test-driver.c>
244

Warning: That file was not part of the compilation database. It may have many parsing errors.