2 * Copyright (C) 2004, 2007, 2008 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/>. */
26 #define ASSERT(expr) \
31 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38 main (int argc, char *argv[])
41 /* Declare failure if test takes too long, by using default abort
42 caused by SIGALRM. All known platforms that lack alarm also lack
43 memmem, and the replacement memmem is known to not take too
49 const char input[] = "foo";
50 const char *result = memmem (input, strlen (input), "", 0);
51 ASSERT (result == input);
55 const char input[] = "foo";
56 const char *result = memmem (input, strlen (input), "o", 1);
57 ASSERT (result == input + 1);
61 const char input[] = "ABC ABCDAB ABCDABCDABDE";
62 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
63 ASSERT (result == input + 15);
67 const char input[] = "ABC ABCDAB ABCDABCDABDE";
68 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
69 ASSERT (result == NULL);
73 const char input[] = "ABC ABCDAB ABCDABCDABDE";
74 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
75 ASSERT (result == input + 11);
78 /* Check that length 0 does not dereference NULL. */
80 const char *result = memmem (NULL, 0, "foo", 3);
81 ASSERT (result == NULL);
85 const char input[] = "foo";
86 const char *result = memmem (input, strlen (input), NULL, 0);
87 ASSERT (result == input);
90 /* Check that a very long haystack is handled quickly if the needle is
91 short and occurs near the beginning. */
93 size_t repeat = 10000;
96 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
97 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
98 size_t n = strlen (needle);
99 char *haystack = (char *) malloc (m + 1);
100 if (haystack != NULL)
102 memset (haystack, 'A', m);
105 for (; repeat > 0; repeat--)
107 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
114 /* Check that a very long needle is discarded quickly if the haystack is
117 size_t repeat = 10000;
120 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
121 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
122 size_t n = strlen (haystack);
123 char *needle = (char *) malloc (m + 1);
126 memset (needle, 'A', m);
128 for (; repeat > 0; repeat--)
130 ASSERT (memmem (haystack, n, needle, m) == NULL);
137 /* Check that the asymptotic worst-case complexity is not quadratic. */
140 char *haystack = (char *) malloc (2 * m + 1);
141 char *needle = (char *) malloc (m + 1);
142 if (haystack != NULL && needle != NULL)
146 memset (haystack, 'A', 2 * m);
147 haystack[2 * m] = 'B';
149 memset (needle, 'A', m);
152 result = memmem (haystack, 2 * m + 1, needle, m + 1);
153 ASSERT (result == haystack + m);
159 /* Check that long needles not present in a haystack can be handled
160 with sublinear speed. */
162 size_t repeat = 10000;
165 char *haystack = (char *) malloc (m);
166 char *needle = (char *) malloc (n);
167 if (haystack != NULL && needle != NULL)
171 memset (haystack, 'A', m);
172 memset (needle, 'B', n);
174 for (; repeat > 0; repeat--)
176 result = memmem (haystack, m, needle, n);
177 ASSERT (result == NULL);