1 /* Test of searching in a string.
2 Copyright (C) 2007 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44 /* configure should already have checked that the locale is supported. */
45 if (setlocale (LC_ALL, "") == NULL)
49 const char input[] = "f\303\266\303\266";
50 const char *result = mbscasestr (input, "");
51 ASSERT (result == input);
55 const char input[] = "f\303\266\303\266";
56 const char *result = mbscasestr (input, "\303\266");
57 ASSERT (result == input + 1);
61 const char input[] = "f\303\266\303\266";
62 const char *result = mbscasestr (input, "\266\303");
63 ASSERT (result == NULL);
67 const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
68 const char *result = mbscasestr (input, "\303\244BCD\303\204BD"); /* "äBCDÄBD" */
69 ASSERT (result == input + 19);
73 const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
74 const char *result = mbscasestr (input, "\303\204BCD\303\204BE"); /* "ÄBCDÄBE" */
75 ASSERT (result == NULL);
78 /* Check that a very long haystack is handled quickly if the needle is
79 short and occurs near the beginning. */
81 size_t repeat = 10000;
84 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
85 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
86 char *haystack = (char *) malloc (m + 1);
89 memset (haystack, 'a', m);
90 haystack[0] = '\303'; haystack[1] = '\204';
93 for (; repeat > 0; repeat--)
95 ASSERT (mbscasestr (haystack, needle) == haystack + 2);
102 /* Check that a very long needle is discarded quickly if the haystack is
105 size_t repeat = 10000;
108 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
109 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
110 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
111 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
112 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
113 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207";
114 char *needle = (char *) malloc (m + 1);
117 memset (needle, 'A', m);
120 for (; repeat > 0; repeat--)
122 ASSERT (mbscasestr (haystack, needle) == NULL);
129 /* Check that the asymptotic worst-case complexity is not quadratic. */
132 char *haystack = (char *) malloc (2 * m + 3);
133 char *needle = (char *) malloc (m + 3);
134 if (haystack != NULL && needle != NULL)
138 memset (haystack, 'A', 2 * m);
139 haystack[2 * m] = '\303'; haystack[2 * m + 1] = '\247';
140 haystack[2 * m + 2] = '\0';
142 memset (needle, 'a', m);
143 needle[m] = '\303'; needle[m + 1] = '\207';
144 needle[m + 2] = '\0';
146 result = mbscasestr (haystack, needle);
147 ASSERT (result == haystack + m);
151 if (haystack != NULL)