1 /* Test of case-insensitive 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. */
28 #define ASSERT(expr) if (!(expr)) abort ();
33 /* This test is executed in the C locale. */
36 const char input[] = "foo";
37 const char *result = mbscasestr (input, "");
38 ASSERT (result == input);
42 const char input[] = "foo";
43 const char *result = mbscasestr (input, "O");
44 ASSERT (result == input + 1);
48 const char input[] = "ABC ABCDAB ABCDABCDABDE";
49 const char *result = mbscasestr (input, "ABCDaBD");
50 ASSERT (result == input + 15);
54 const char input[] = "ABC ABCDAB ABCDABCDABDE";
55 const char *result = mbscasestr (input, "ABCDaBE");
56 ASSERT (result == NULL);
59 /* Check that a very long haystack is handled quickly if the needle is
60 short and occurs near the beginning. */
62 size_t repeat = 10000;
65 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
66 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaAAAAaaaaaaa"
67 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
68 char *haystack = (char *) malloc (m + 1);
71 memset (haystack, 'A', m);
75 for (; repeat > 0; repeat--)
77 ASSERT (mbscasestr (haystack, needle) == haystack + 1);
84 /* Check that a very long needle is discarded quickly if the haystack is
87 size_t repeat = 10000;
90 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
91 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
92 char *needle = (char *) malloc (m + 1);
95 memset (needle, 'A', m);
98 for (; repeat > 0; repeat--)
100 ASSERT (mbscasestr (haystack, needle) == NULL);
107 /* Check that the asymptotic worst-case complexity is not quadratic. */
110 char *haystack = (char *) malloc (2 * m + 2);
111 char *needle = (char *) malloc (m + 2);
112 if (haystack != NULL && needle != NULL)
116 memset (haystack, 'A', 2 * m);
117 haystack[2 * m] = 'B';
118 haystack[2 * m + 1] = '\0';
120 memset (needle, 'a', m);
122 needle[m + 1] = '\0';
124 result = mbscasestr (haystack, needle);
125 ASSERT (result == haystack + m);
129 if (haystack != NULL)