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

1/* Test and measure strcasestr 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 "strcasestr"
22#include "test-string.h"
23
24
25#define STRCASESTR simple_strcasestr
26#define NO_ALIAS
27#define __strncasecmp strncasecmp
28#define __strnlen strnlen
29#include "strcasestr.c"
30
31
32static char *
33stupid_strcasestr (const char *s1, const char *s2)
34{
35 ssize_t s1len = strlen (s1);
36 ssize_t s2len = strlen (s2);
37
38 if (s2len > s1len)
39 return NULL;
40
41 for (ssize_t i = 0; i <= s1len - s2len; ++i)
42 {
43 size_t j;
44 for (j = 0; j < s2len; ++j)
45 if (tolower (s1[i + j]) != tolower (s2[j]))
46 break;
47 if (j == s2len)
48 return (char *) s1 + i;
49 }
50
51 return NULL;
52}
53
54
55typedef char *(*proto_t) (const char *, const char *);
56
57IMPL (stupid_strcasestr, 0)
58IMPL (simple_strcasestr, 0)
59IMPL (strcasestr, 1)
60
61
62static int
63check_result (impl_t *impl, const char *s1, const char *s2,
64 char *exp_result)
65{
66 char *result = CALL (impl, s1, s2);
67 if (result != exp_result)
68 {
69 error (0, 0, "Wrong result in function %s %s %s", impl->name,
70 result, exp_result);
71 ret = 1;
72 return -1;
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[] = "1234567890abcxyz";
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 for (size_t i = 0; i < len2; ++i)
117 s1[len1 - len2 + i] = toupper (s2[i]);
118 }
119 s1[len1] = '\0';
120
121 FOR_EACH_IMPL (impl, 0)
122 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
123}
124
125static void
126check1 (void)
127{
128 const char s1[] = "AOKB";
129 const char s2[] = "OK";
130 char *exp_result;
131
132 exp_result = stupid_strcasestr (s1, s2);
133 FOR_EACH_IMPL (impl, 0)
134 check_result (impl, s1, s2, exp_result);
135}
136
137static int
138test_main (void)
139{
140 test_init ();
141
142 check1 ();
143
144 printf ("%23s", "");
145 FOR_EACH_IMPL (impl, 0)
146 printf ("\t%s", impl->name);
147 putchar ('\n');
148
149 for (size_t klen = 2; klen < 32; ++klen)
150 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
151 {
152 do_test (0, 0, hlen, klen, 0);
153 do_test (0, 0, hlen, klen, 1);
154 do_test (0, 3, hlen, klen, 0);
155 do_test (0, 3, hlen, klen, 1);
156 do_test (0, 9, hlen, klen, 0);
157 do_test (0, 9, hlen, klen, 1);
158 do_test (0, 15, hlen, klen, 0);
159 do_test (0, 15, hlen, klen, 1);
160
161 do_test (3, 0, hlen, klen, 0);
162 do_test (3, 0, hlen, klen, 1);
163 do_test (3, 3, hlen, klen, 0);
164 do_test (3, 3, hlen, klen, 1);
165 do_test (3, 9, hlen, klen, 0);
166 do_test (3, 9, hlen, klen, 1);
167 do_test (3, 15, hlen, klen, 0);
168 do_test (3, 15, hlen, klen, 1);
169
170 do_test (9, 0, hlen, klen, 0);
171 do_test (9, 0, hlen, klen, 1);
172 do_test (9, 3, hlen, klen, 0);
173 do_test (9, 3, hlen, klen, 1);
174 do_test (9, 9, hlen, klen, 0);
175 do_test (9, 9, hlen, klen, 1);
176 do_test (9, 15, hlen, klen, 0);
177 do_test (9, 15, hlen, klen, 1);
178
179 do_test (15, 0, hlen, klen, 0);
180 do_test (15, 0, hlen, klen, 1);
181 do_test (15, 3, hlen, klen, 0);
182 do_test (15, 3, hlen, klen, 1);
183 do_test (15, 9, hlen, klen, 0);
184 do_test (15, 9, hlen, klen, 1);
185 do_test (15, 15, hlen, klen, 0);
186 do_test (15, 15, hlen, klen, 1);
187 }
188
189 do_test (0, 0, page_size - 1, 16, 0);
190 do_test (0, 0, page_size - 1, 16, 1);
191
192 return ret;
193}
194
195#include <support/test-driver.c>
196

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