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. */
29 #define ASSERT(expr) if (!(expr)) abort ();
34 /* configure should already have checked that the locale is supported. */
35 if (setlocale (LC_ALL, "") == NULL)
39 const char input[] = "f\303\266\303\266";
40 const char *result = mbscasestr (input, "");
41 ASSERT (result == input);
45 const char input[] = "f\303\266\303\266";
46 const char *result = mbscasestr (input, "\303\266");
47 ASSERT (result == input + 1);
51 const char input[] = "f\303\266\303\266";
52 const char *result = mbscasestr (input, "\266\303");
53 ASSERT (result == NULL);
57 const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
58 const char *result = mbscasestr (input, "\303\244BCD\303\204BD"); /* "äBCDÄBD" */
59 ASSERT (result == input + 19);
63 const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
64 const char *result = mbscasestr (input, "\303\204BCD\303\204BE"); /* "ÄBCDÄBE" */
65 ASSERT (result == NULL);
68 /* Check that a very long haystack is handled quickly if the needle is
69 short and occurs near the beginning. */
71 size_t repeat = 10000;
74 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
75 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
76 char *haystack = (char *) malloc (m + 1);
79 memset (haystack, 'a', m);
80 haystack[0] = '\303'; haystack[1] = '\204';
83 for (; repeat > 0; repeat--)
85 ASSERT (mbscasestr (haystack, needle) == haystack + 2);
92 /* Check that a very long needle is discarded quickly if the haystack is
95 size_t repeat = 10000;
98 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
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\207A\303\207"
101 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
102 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
103 "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207";
104 char *needle = (char *) malloc (m + 1);
107 memset (needle, 'A', m);
110 for (; repeat > 0; repeat--)
112 ASSERT (mbscasestr (haystack, needle) == NULL);
119 /* Check that the asymptotic worst-case complexity is not quadratic. */
122 char *haystack = (char *) malloc (2 * m + 3);
123 char *needle = (char *) malloc (m + 3);
124 if (haystack != NULL && needle != NULL)
128 memset (haystack, 'A', 2 * m);
129 haystack[2 * m] = '\303'; haystack[2 * m + 1] = '\247';
130 haystack[2 * m + 2] = '\0';
132 memset (needle, 'a', m);
133 needle[m] = '\303'; needle[m + 1] = '\207';
134 needle[m + 2] = '\0';
136 result = mbscasestr (haystack, needle);
137 ASSERT (result == haystack + m);
141 if (haystack != NULL)