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. */
27 #define ASSERT(expr) \
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 const char input[] = "foo";
43 const char *result = strcasestr (input, "");
44 ASSERT (result == input);
48 const char input[] = "foo";
49 const char *result = strcasestr (input, "O");
50 ASSERT (result == input + 1);
54 const char input[] = "ABC ABCDAB ABCDABCDABDE";
55 const char *result = strcasestr (input, "ABCDaBD");
56 ASSERT (result == input + 15);
60 const char input[] = "ABC ABCDAB ABCDABCDABDE";
61 const char *result = strcasestr (input, "ABCDaBE");
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 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
74 char *haystack = (char *) malloc (m + 1);
77 memset (haystack, 'A', m);
81 for (; repeat > 0; repeat--)
83 ASSERT (strcasestr (haystack, needle) == haystack + 1);
90 /* Check that a very long needle is discarded quickly if the haystack is
93 size_t repeat = 10000;
96 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
97 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
98 char *needle = (char *) malloc (m + 1);
101 memset (needle, 'A', m);
104 for (; repeat > 0; repeat--)
106 ASSERT (strcasestr (haystack, needle) == NULL);
113 /* Check that the asymptotic worst-case complexity is not quadratic. */
114 #if !HAVE_STRCASESTR /* The system's strcasestr() function fails this test. */
117 char *haystack = (char *) malloc (2 * m + 2);
118 char *needle = (char *) malloc (m + 2);
119 if (haystack != NULL && needle != NULL)
123 memset (haystack, 'A', 2 * m);
124 haystack[2 * m] = 'B';
125 haystack[2 * m + 1] = '\0';
127 memset (needle, 'a', m);
129 needle[m + 1] = '\0';
131 result = strcasestr (haystack, needle);
132 ASSERT (result == haystack + m);
136 if (haystack != NULL)