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 /* On some platforms, the memchr() functions reads past the first
65 occurrence of the byte to be searched, leading to an out-of-bounds
66 read access for strstr().
67 See <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737>.
68 This is a bug in memchr(), see the Austin Group's clarification
69 <http://www.opengroup.org/austin/docs/austin_454.txt>. */
70 const char *fix = "aBaaaaaaaaaaax";
71 char *input = malloc (strlen (fix) + 1);
75 result = strstr (input, "B1x");
76 ASSERT (result == NULL);
81 const char input[] = "ABC ABCDAB ABCDABCDABDE";
82 const char *result = strstr (input, "ABCDABD");
83 ASSERT (result == input + 15);
87 const char input[] = "ABC ABCDAB ABCDABCDABDE";
88 const char *result = strstr (input, "ABCDABE");
89 ASSERT (result == NULL);
93 const char input[] = "ABC ABCDAB ABCDABCDABDE";
94 const char *result = strstr (input, "ABCDABCD");
95 ASSERT (result == input + 11);
98 /* Check that a very long haystack is handled quickly if the needle is
99 short and occurs near the beginning. */
101 size_t repeat = 10000;
104 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
105 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
106 char *haystack = (char *) malloc (m + 1);
107 if (haystack != NULL)
109 memset (haystack, 'A', m);
113 for (; repeat > 0; repeat--)
115 ASSERT (strstr (haystack, needle) == haystack + 1);
122 /* Check that a very long needle is discarded quickly if the haystack is
125 size_t repeat = 10000;
128 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
129 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
130 char *needle = (char *) malloc (m + 1);
133 memset (needle, 'A', m);
136 for (; repeat > 0; repeat--)
138 ASSERT (strstr (haystack, needle) == NULL);
145 /* Check that the asymptotic worst-case complexity is not quadratic. */
148 char *haystack = (char *) malloc (2 * m + 2);
149 char *needle = (char *) malloc (m + 2);
150 if (haystack != NULL && needle != NULL)
154 memset (haystack, 'A', 2 * m);
155 haystack[2 * m] = 'B';
156 haystack[2 * m + 1] = '\0';
158 memset (needle, 'A', m);
160 needle[m + 1] = '\0';
162 result = strstr (haystack, needle);
163 ASSERT (result == haystack + m);
169 /* Sublinear speed is only possible in memmem; strstr must examine
170 every character of haystack to find its length. */