1 /* Test of case-insensitive searching in a string.
2 Copyright (C) 2007, 2008, 2009 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. */
23 #include "signature.h"
24 SIGNATURE_CHECK (strcasestr, char *, (char const *, char const *));
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 /* Declare failure if test takes too long, by using default abort
48 caused by SIGALRM. All known platforms that lack alarm also lack
49 strcasestr, and the replacement strcasestr is known to not take too
51 signal (SIGALRM, SIG_DFL);
56 const char input[] = "foo";
57 const char *result = strcasestr (input, "");
58 ASSERT (result == input);
62 const char input[] = "foo";
63 const char *result = strcasestr (input, "O");
64 ASSERT (result == input + 1);
68 const char input[] = "ABC ABCDAB ABCDABCDABDE";
69 const char *result = strcasestr (input, "ABCDaBD");
70 ASSERT (result == input + 15);
74 const char input[] = "ABC ABCDAB ABCDABCDABDE";
75 const char *result = strcasestr (input, "ABCDaBE");
76 ASSERT (result == NULL);
80 const char input[] = "ABC ABCDAB ABCDABCDABDE";
81 const char *result = strcasestr (input, "ABCDaBCD");
82 ASSERT (result == input + 11);
85 /* Check that a very long haystack is handled quickly if the needle is
86 short and occurs near the beginning. */
88 size_t repeat = 10000;
91 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
92 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaAAAAaaaaaaa"
93 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
94 char *haystack = (char *) malloc (m + 1);
97 memset (haystack, 'A', m);
101 for (; repeat > 0; repeat--)
103 ASSERT (strcasestr (haystack, needle) == haystack + 1);
110 /* Check that a very long needle is discarded quickly if the haystack is
113 size_t repeat = 10000;
116 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
117 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
118 char *needle = (char *) malloc (m + 1);
121 memset (needle, 'A', m);
124 for (; repeat > 0; repeat--)
126 ASSERT (strcasestr (haystack, needle) == NULL);
133 /* Check that the asymptotic worst-case complexity is not quadratic. */
136 char *haystack = (char *) malloc (2 * m + 2);
137 char *needle = (char *) malloc (m + 2);
138 if (haystack != NULL && needle != NULL)
142 memset (haystack, 'A', 2 * m);
143 haystack[2 * m] = 'B';
144 haystack[2 * m + 1] = '\0';
146 memset (needle, 'a', m);
148 needle[m + 1] = '\0';
150 result = strcasestr (haystack, needle);
151 ASSERT (result == haystack + m);