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);
72 /* Check that length 0 does not dereference NULL. */
74 const char *result = memmem (NULL, 0, "foo", 3);
75 ASSERT (result == NULL);
79 const char input[] = "foo";
80 const char *result = memmem (input, strlen (input), NULL, 0);
81 ASSERT (result == input);
84 /* Check that a very long haystack is handled quickly if the needle is
85 short and occurs near the beginning. */
87 size_t repeat = 10000;
90 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
91 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
92 size_t n = strlen (needle);
93 char *haystack = (char *) malloc (m + 1);
96 memset (haystack, 'A', m);
99 for (; repeat > 0; repeat--)
101 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
108 /* Check that a very long needle is discarded quickly if the haystack is
111 size_t repeat = 10000;
114 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
115 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
116 size_t n = strlen (haystack);
117 char *needle = (char *) malloc (m + 1);
120 memset (needle, 'A', m);
122 for (; repeat > 0; repeat--)
124 ASSERT (memmem (haystack, n, needle, m) == NULL);
131 /* Check that the asymptotic worst-case complexity is not quadratic. */
134 char *haystack = (char *) malloc (2 * m + 1);
135 char *needle = (char *) malloc (m + 1);
136 if (haystack != NULL && needle != NULL)
140 memset (haystack, 'A', 2 * m);
141 haystack[2 * m] = 'B';
143 memset (needle, 'A', m);
146 result = memmem (haystack, 2 * m + 1, needle, m + 1);
147 ASSERT (result == haystack + m);
151 if (haystack != NULL)