2 * Copyright (C) 2004, 2007-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 #include "zerosize-ptr.h"
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 main (int argc, char *argv[])
51 /* Declare failure if test takes too long, by using default abort
52 caused by SIGALRM. All known platforms that lack alarm also lack
53 memmem, and the replacement memmem is known to not take too
55 signal (SIGALRM, SIG_DFL);
60 const char input[] = "foo";
61 const char *result = memmem (input, strlen (input), "", 0);
62 ASSERT (result == input);
66 const char input[] = "foo";
67 const char *result = memmem (input, strlen (input), "o", 1);
68 ASSERT (result == input + 1);
72 const char input[] = "ABC ABCDAB ABCDABCDABDE";
73 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
74 ASSERT (result == input + 15);
78 const char input[] = "ABC ABCDAB ABCDABCDABDE";
79 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
80 ASSERT (result == NULL);
84 const char input[] = "ABC ABCDAB ABCDABCDABDE";
85 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
86 ASSERT (result == input + 11);
89 /* Check that length 0 does not dereference the pointer. */
91 const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
92 ASSERT (result == NULL);
96 const char input[] = "foo";
97 const char *result = memmem (input, strlen (input), null_ptr (), 0);
98 ASSERT (result == input);
101 /* Check that a very long haystack is handled quickly if the needle is
102 short and occurs near the beginning. */
104 size_t repeat = 10000;
107 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
108 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
109 size_t n = strlen (needle);
110 char *haystack = (char *) malloc (m + 1);
111 if (haystack != NULL)
113 memset (haystack, 'A', m);
116 for (; repeat > 0; repeat--)
118 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
125 /* Check that a very long needle is discarded quickly if the haystack is
128 size_t repeat = 10000;
131 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
132 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
133 size_t n = strlen (haystack);
134 char *needle = (char *) malloc (m + 1);
137 memset (needle, 'A', m);
139 for (; repeat > 0; repeat--)
141 ASSERT (memmem (haystack, n, needle, m) == NULL);
148 /* Check that the asymptotic worst-case complexity is not quadratic. */
151 char *haystack = (char *) malloc (2 * m + 1);
152 char *needle = (char *) malloc (m + 1);
153 if (haystack != NULL && needle != NULL)
157 memset (haystack, 'A', 2 * m);
158 haystack[2 * m] = 'B';
160 memset (needle, 'A', m);
163 result = memmem (haystack, 2 * m + 1, needle, m + 1);
164 ASSERT (result == haystack + m);
170 /* Check that long needles not present in a haystack can be handled
171 with sublinear speed. */
173 size_t repeat = 10000;
176 char *haystack = (char *) malloc (m);
177 char *needle = (char *) malloc (n);
178 if (haystack != NULL && needle != NULL)
182 memset (haystack, 'A', m);
183 memset (needle, 'B', n);
185 for (; repeat > 0; repeat--)
187 result = memmem (haystack, m, needle, n);
188 ASSERT (result == NULL);