Hide __posix_spawn_file_actions_realloc/__spawni [BZ #18822]
[glibc.git] / string / test-strstr.c
blob33f221149aa85ed0171156006005d7d10710aa7d
1 /* Test and measure strstr functions.
2 Copyright (C) 2010-2017 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/>. */
20 #define TEST_MAIN
21 #define TEST_NAME "strstr"
22 #include "test-string.h"
25 #define STRSTR simple_strstr
26 #define libc_hidden_builtin_def(arg) /* nothing */
27 #include "strstr.c"
30 static char *
31 stupid_strstr (const char *s1, const char *s2)
33 ssize_t s1len = strlen (s1);
34 ssize_t s2len = strlen (s2);
36 if (s2len > s1len)
37 return NULL;
39 for (ssize_t i = 0; i <= s1len - s2len; ++i)
41 size_t j;
42 for (j = 0; j < s2len; ++j)
43 if (s1[i + j] != s2[j])
44 break;
45 if (j == s2len)
46 return (char *) s1 + i;
49 return NULL;
53 typedef char *(*proto_t) (const char *, const char *);
55 IMPL (stupid_strstr, 0)
56 IMPL (simple_strstr, 0)
57 IMPL (strstr, 1)
60 static int
61 check_result (impl_t *impl, const char *s1, const char *s2,
62 char *exp_result)
64 char *result = CALL (impl, s1, s2);
65 if (result != exp_result)
67 error (0, 0, "Wrong result in function %s %s %s", impl->name,
68 result, exp_result);
69 ret = 1;
70 return -1;
73 return 0;
76 static void
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)
80 return;
84 static void
85 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
86 int fail)
88 char *s1 = (char *) (buf1 + align1);
89 char *s2 = (char *) (buf2 + align2);
91 static const char d[] = "1234567890abcdef";
92 #define dl (sizeof (d) - 1)
93 char *ss2 = s2;
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);
99 s2[len2] = '\0';
101 if (fail)
103 char *ss1 = s1;
104 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
106 size_t t = l > dl ? dl : l;
107 memcpy (ss1, d, t);
108 ++ss1[len2 > 7 ? 7 : len2 - 1];
109 ss1 += t;
112 else
114 memset (s1, '0', len1);
115 memcpy (s1 + len1 - len2, s2, len2);
117 s1[len1] = '\0';
119 FOR_EACH_IMPL (impl, 0)
120 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
124 static void
125 check1 (void)
127 const char s1[] =
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";
130 char *exp_result;
132 exp_result = stupid_strstr (s1, s2);
133 FOR_EACH_IMPL (impl, 0)
134 check_result (impl, s1, s2, exp_result);
137 static void
138 check2 (void)
140 const char s1[] = ", enable_static, \0, enable_shared, ";
141 char *exp_result;
142 char *s2 = (void *) buf1 + page_size - 18;
144 strcpy (s2, s1);
145 exp_result = stupid_strstr (s1, s1 + 18);
146 FOR_EACH_IMPL (impl, 0)
148 check_result (impl, s1, s1 + 18, exp_result);
149 check_result (impl, s2, s1 + 18, exp_result);
153 static int
154 test_main (void)
156 test_init ();
158 check1 ();
159 check2 ();
161 printf ("%23s", "");
162 FOR_EACH_IMPL (impl, 0)
163 printf ("\t%s", impl->name);
164 putchar ('\n');
166 for (size_t klen = 2; klen < 32; ++klen)
167 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
169 do_test (0, 0, hlen, klen, 0);
170 do_test (0, 0, hlen, klen, 1);
171 do_test (0, 3, hlen, klen, 0);
172 do_test (0, 3, hlen, klen, 1);
173 do_test (0, 9, hlen, klen, 0);
174 do_test (0, 9, hlen, klen, 1);
175 do_test (0, 15, hlen, klen, 0);
176 do_test (0, 15, hlen, klen, 1);
178 do_test (3, 0, hlen, klen, 0);
179 do_test (3, 0, hlen, klen, 1);
180 do_test (3, 3, hlen, klen, 0);
181 do_test (3, 3, hlen, klen, 1);
182 do_test (3, 9, hlen, klen, 0);
183 do_test (3, 9, hlen, klen, 1);
184 do_test (3, 15, hlen, klen, 0);
185 do_test (3, 15, hlen, klen, 1);
187 do_test (9, 0, hlen, klen, 0);
188 do_test (9, 0, hlen, klen, 1);
189 do_test (9, 3, hlen, klen, 0);
190 do_test (9, 3, hlen, klen, 1);
191 do_test (9, 9, hlen, klen, 0);
192 do_test (9, 9, hlen, klen, 1);
193 do_test (9, 15, hlen, klen, 0);
194 do_test (9, 15, hlen, klen, 1);
196 do_test (15, 0, hlen, klen, 0);
197 do_test (15, 0, hlen, klen, 1);
198 do_test (15, 3, hlen, klen, 0);
199 do_test (15, 3, hlen, klen, 1);
200 do_test (15, 9, hlen, klen, 0);
201 do_test (15, 9, hlen, klen, 1);
202 do_test (15, 15, hlen, klen, 0);
203 do_test (15, 15, hlen, klen, 1);
206 do_test (0, 0, page_size - 1, 16, 0);
207 do_test (0, 0, page_size - 1, 16, 1);
209 return ret;
212 #include <support/test-driver.c>