1 /* Test of searching in a string.
2 Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
31 /* configure should already have checked that the locale is supported. */
32 if (setlocale (LC_ALL
, "") == NULL
)
36 const char input
[] = "f\303\266\303\266";
37 const char *result
= mbscasestr (input
, "");
38 ASSERT (result
== input
);
42 const char input
[] = "f\303\266\303\266";
43 const char *result
= mbscasestr (input
, "\303\266");
44 ASSERT (result
== input
+ 1);
48 const char input
[] = "f\303\266\303\266";
49 const char *result
= mbscasestr (input
, "\266\303");
50 ASSERT (result
== NULL
);
54 const char input
[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
55 const char *result
= mbscasestr (input
, "\303\244BCD\303\204BD"); /* "äBCDÄBD" */
56 ASSERT (result
== input
+ 19);
60 const char input
[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
61 const char *result
= mbscasestr (input
, "\303\204BCD\303\204BE"); /* "ÄBCDÄBE" */
62 ASSERT (result
== NULL
);
65 /* Check that a very long haystack is handled quickly if the needle is
66 short and occurs near the beginning. */
68 size_t repeat
= 10000;
71 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
72 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
73 char *haystack
= (char *) malloc (m
+ 1);
76 memset (haystack
, 'a', m
);
77 haystack
[0] = '\303'; haystack
[1] = '\204';
80 for (; repeat
> 0; repeat
--)
82 ASSERT (mbscasestr (haystack
, needle
) == haystack
+ 2);
89 /* Check that a very long needle is discarded quickly if the haystack is
92 size_t repeat
= 10000;
94 const char *haystack
=
95 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
96 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
97 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
98 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
99 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
100 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207";
101 char *needle
= (char *) malloc (m
+ 1);
104 memset (needle
, 'A', m
);
107 for (; repeat
> 0; repeat
--)
109 ASSERT (mbscasestr (haystack
, needle
) == NULL
);
116 /* Check that the asymptotic worst-case complexity is not quadratic. */
119 char *haystack
= (char *) malloc (2 * m
+ 3);
120 char *needle
= (char *) malloc (m
+ 3);
121 if (haystack
!= NULL
&& needle
!= NULL
)
125 memset (haystack
, 'A', 2 * m
);
126 haystack
[2 * m
] = '\303'; haystack
[2 * m
+ 1] = '\247';
127 haystack
[2 * m
+ 2] = '\0';
129 memset (needle
, 'a', m
);
130 needle
[m
] = '\303'; needle
[m
+ 1] = '\207';
131 needle
[m
+ 2] = '\0';
133 result
= mbscasestr (haystack
, needle
);
134 ASSERT (result
== haystack
+ m
);
140 return test_exit_status
;