Add missing magic to GLIBC_PROVIDES.
[glibc.git] / string / test-strstr.c
blobd4c0efc5ba4f6cff07c16c051f7b046e7e91b717
1 /* Test and measure strstr functions.
2 Copyright (C) 2010 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 #include "test-string.h"
24 #define STRSTR simple_strstr
25 #include "strstr.c"
28 static char *
29 stupid_strstr (const char *s1, const char *s2)
31 ssize_t s1len = strlen (s1);
32 ssize_t s2len = strlen (s2);
34 if (s2len > s1len)
35 return NULL;
37 for (ssize_t i = 0; i <= s1len - s2len; ++i)
39 size_t j;
40 for (j = 0; j < s2len; ++j)
41 if (s1[i + j] != s2[j])
42 break;
43 if (j == s2len)
44 return (char *) s1 + i;
47 return NULL;
51 typedef char *(*proto_t) (const char *, const char *);
53 IMPL (stupid_strstr, 0)
54 IMPL (simple_strstr, 0)
55 IMPL (strstr, 1)
58 static int
59 check_result (impl_t *impl, const char *s1, const char *s2,
60 char *exp_result)
62 char *result = CALL (impl, s1, s2);
63 if (result != exp_result)
65 error (0, 0, "Wrong result in function %s %s %s", impl->name,
66 result, exp_result);
67 ret = 1;
68 return -1;
71 return 0;
74 static void
75 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
77 if (check_result (impl, s1, s2, exp_result) < 0)
78 return;
80 if (HP_TIMING_AVAIL)
82 hp_timing_t start __attribute ((unused));
83 hp_timing_t stop __attribute ((unused));
84 hp_timing_t best_time = ~(hp_timing_t) 0;
85 size_t i;
87 for (i = 0; i < 32; ++i)
89 HP_TIMING_NOW (start);
90 CALL (impl, s1, s2);
91 HP_TIMING_NOW (stop);
92 HP_TIMING_BEST (best_time, start, stop);
95 printf ("\t%zd", (size_t) best_time);
100 static void
101 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
102 int fail)
104 char *s1 = (char *) (buf1 + align1);
105 char *s2 = (char *) (buf2 + align2);
107 static const char d[] = "1234567890abcdef";
108 #define dl (sizeof (d) - 1)
109 char *ss2 = s2;
110 for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
112 size_t t = l > dl ? dl : l;
113 ss2 = mempcpy (ss2, d, t);
115 s2[len2] = '\0';
117 if (fail)
119 char *ss1 = s1;
120 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
122 size_t t = l > dl ? dl : l;
123 memcpy (ss1, d, t);
124 ++ss1[len2 > 7 ? 7 : len2 - 1];
125 ss1 += t;
128 else
130 memset (s1, '0', len1);
131 memcpy (s1 + len1 - len2, s2, len2);
133 s1[len1] = '\0';
135 if (HP_TIMING_AVAIL)
136 printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
137 len1, len2, align1, align2, fail ? "fail" : "found");
139 FOR_EACH_IMPL (impl, 0)
140 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
142 if (HP_TIMING_AVAIL)
143 putchar ('\n');
146 static void
147 check1 (void)
149 const char s1[] =
150 "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";
151 const char s2[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
152 char *exp_result;
154 exp_result = stupid_strstr (s1, s2);
155 FOR_EACH_IMPL (impl, 0)
156 check_result (impl, s1, s2, exp_result);
159 static void
160 check2 (void)
162 const char s1[] = ", enable_static, \0, enable_shared, ";
163 char *exp_result;
164 char *s2 = (void *) buf1 + page_size - 18;
166 strcpy (s2, s1);
167 exp_result = stupid_strstr (s1, s1 + 18);
168 FOR_EACH_IMPL (impl, 0)
170 check_result (impl, s1, s1 + 18, exp_result);
171 check_result (impl, s2, s1 + 18, exp_result);
175 static int
176 test_main (void)
178 test_init ();
180 check1 ();
181 check2 ();
183 printf ("%23s", "");
184 FOR_EACH_IMPL (impl, 0)
185 printf ("\t%s", impl->name);
186 putchar ('\n');
188 for (size_t klen = 2; klen < 32; ++klen)
189 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
191 do_test (0, 0, hlen, klen, 0);
192 do_test (0, 0, hlen, klen, 1);
193 do_test (0, 3, hlen, klen, 0);
194 do_test (0, 3, hlen, klen, 1);
195 do_test (0, 9, hlen, klen, 0);
196 do_test (0, 9, hlen, klen, 1);
197 do_test (0, 15, hlen, klen, 0);
198 do_test (0, 15, hlen, klen, 1);
200 do_test (3, 0, hlen, klen, 0);
201 do_test (3, 0, hlen, klen, 1);
202 do_test (3, 3, hlen, klen, 0);
203 do_test (3, 3, hlen, klen, 1);
204 do_test (3, 9, hlen, klen, 0);
205 do_test (3, 9, hlen, klen, 1);
206 do_test (3, 15, hlen, klen, 0);
207 do_test (3, 15, hlen, klen, 1);
209 do_test (9, 0, hlen, klen, 0);
210 do_test (9, 0, hlen, klen, 1);
211 do_test (9, 3, hlen, klen, 0);
212 do_test (9, 3, hlen, klen, 1);
213 do_test (9, 9, hlen, klen, 0);
214 do_test (9, 9, hlen, klen, 1);
215 do_test (9, 15, hlen, klen, 0);
216 do_test (9, 15, hlen, klen, 1);
218 do_test (15, 0, hlen, klen, 0);
219 do_test (15, 0, hlen, klen, 1);
220 do_test (15, 3, hlen, klen, 0);
221 do_test (15, 3, hlen, klen, 1);
222 do_test (15, 9, hlen, klen, 0);
223 do_test (15, 9, hlen, klen, 1);
224 do_test (15, 15, hlen, klen, 0);
225 do_test (15, 15, hlen, klen, 1);
228 do_test (0, 0, page_size - 1, 16, 0);
229 do_test (0, 0, page_size - 1, 16, 1);
231 return ret;
234 #include "../test-skeleton.c"