1 /* Test of case-insensitive searching in a string.
2 Copyright (C) 2007, 2008 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
21 #include "c-strcasestr.h"
27 #define ASSERT(expr) \
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 const char input[] = "foo";
44 const char *result = c_strcasestr (input, "");
45 ASSERT (result == input);
49 const char input[] = "foo";
50 const char *result = c_strcasestr (input, "O");
51 ASSERT (result == input + 1);
55 const char input[] = "ABC ABCDAB ABCDABCDABDE";
56 const char *result = c_strcasestr (input, "ABCDaBD");
57 ASSERT (result == input + 15);
61 const char input[] = "ABC ABCDAB ABCDABCDABDE";
62 const char *result = c_strcasestr (input, "ABCDaBE");
63 ASSERT (result == NULL);
67 const char input[] = "ABC ABCDAB ABCDABCDABDE";
68 const char *result = c_strcasestr (input, "ABCDaBCD");
69 ASSERT (result == input + 11);
72 /* Check that a very long haystack is handled quickly if the needle is
73 short and occurs near the beginning. */
75 size_t repeat = 10000;
78 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
79 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaAAAAaaaaaaa"
80 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
81 char *haystack = (char *) malloc (m + 1);
84 memset (haystack, 'A', m);
88 for (; repeat > 0; repeat--)
90 ASSERT (c_strcasestr (haystack, needle) == haystack + 1);
97 /* Check that a very long needle is discarded quickly if the haystack is
100 size_t repeat = 10000;
103 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
104 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
105 char *needle = (char *) malloc (m + 1);
108 memset (needle, 'A', m);
111 for (; repeat > 0; repeat--)
113 ASSERT (c_strcasestr (haystack, needle) == NULL);
120 /* Check that the asymptotic worst-case complexity is not quadratic. */
123 char *haystack = (char *) malloc (2 * m + 2);
124 char *needle = (char *) malloc (m + 2);
125 if (haystack != NULL && needle != NULL)
129 memset (haystack, 'A', 2 * m);
130 haystack[2 * m] = 'B';
131 haystack[2 * m + 1] = '\0';
133 memset (needle, 'a', m);
135 needle[m + 1] = '\0';
137 result = c_strcasestr (haystack, needle);
138 ASSERT (result == haystack + m);