2 * Copyright (C) 2004, 2007, 2008, 2009 Free Software Foundation
3 * Written by Bruno Haible and Eric Blake
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #define ASSERT(expr) \
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40 main (int argc, char *argv[])
43 /* Declare failure if test takes too long, by using default abort
44 caused by SIGALRM. All known platforms that lack alarm also have
45 a quadratic strstr, and the replacement strstr is known to not
47 signal (SIGALRM, SIG_DFL);
52 const char input[] = "foo";
53 const char *result = strstr (input, "");
54 ASSERT (result == input);
58 const char input[] = "foo";
59 const char *result = strstr (input, "o");
60 ASSERT (result == input + 1);
64 /* See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737 */
65 const char *fix = "aBaaaaaaaaaaax";
66 char *input = malloc (strlen (fix) + 1);
70 result = strstr (input, "B1x");
71 ASSERT (result == NULL);
76 const char input[] = "ABC ABCDAB ABCDABCDABDE";
77 const char *result = strstr (input, "ABCDABD");
78 ASSERT (result == input + 15);
82 const char input[] = "ABC ABCDAB ABCDABCDABDE";
83 const char *result = strstr (input, "ABCDABE");
84 ASSERT (result == NULL);
88 const char input[] = "ABC ABCDAB ABCDABCDABDE";
89 const char *result = strstr (input, "ABCDABCD");
90 ASSERT (result == input + 11);
93 /* Check that a very long haystack is handled quickly if the needle is
94 short and occurs near the beginning. */
96 size_t repeat = 10000;
99 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
100 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
101 char *haystack = (char *) malloc (m + 1);
102 if (haystack != NULL)
104 memset (haystack, 'A', m);
108 for (; repeat > 0; repeat--)
110 ASSERT (strstr (haystack, needle) == haystack + 1);
117 /* Check that a very long needle is discarded quickly if the haystack is
120 size_t repeat = 10000;
123 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
124 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
125 char *needle = (char *) malloc (m + 1);
128 memset (needle, 'A', m);
131 for (; repeat > 0; repeat--)
133 ASSERT (strstr (haystack, needle) == NULL);
140 /* Check that the asymptotic worst-case complexity is not quadratic. */
143 char *haystack = (char *) malloc (2 * m + 2);
144 char *needle = (char *) malloc (m + 2);
145 if (haystack != NULL && needle != NULL)
149 memset (haystack, 'A', 2 * m);
150 haystack[2 * m] = 'B';
151 haystack[2 * m + 1] = '\0';
153 memset (needle, 'A', m);
155 needle[m + 1] = '\0';
157 result = strstr (haystack, needle);
158 ASSERT (result == haystack + m);
164 /* Sublinear speed is only possible in memmem; strstr must examine
165 every character of haystack to find its length. */