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/>. */
22 #include "signature.h"
23 SIGNATURE_CHECK (memmem, void *, (void const *, size_t, void const *, size_t));
30 #include "zerosize-ptr.h"
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
51 main (int argc, char *argv[])
54 /* Declare failure if test takes too long, by using default abort
55 caused by SIGALRM. All known platforms that lack alarm also lack
56 memmem, and the replacement memmem is known to not take too
58 signal (SIGALRM, SIG_DFL);
63 const char input[] = "foo";
64 const char *result = memmem (input, strlen (input), "", 0);
65 ASSERT (result == input);
69 const char input[] = "foo";
70 const char *result = memmem (input, strlen (input), "o", 1);
71 ASSERT (result == input + 1);
75 const char input[] = "ABC ABCDAB ABCDABCDABDE";
76 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
77 ASSERT (result == input + 15);
81 const char input[] = "ABC ABCDAB ABCDABCDABDE";
82 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
83 ASSERT (result == NULL);
87 const char input[] = "ABC ABCDAB ABCDABCDABDE";
88 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
89 ASSERT (result == input + 11);
92 /* Check that length 0 does not dereference the pointer. */
94 const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
95 ASSERT (result == NULL);
99 const char input[] = "foo";
100 const char *result = memmem (input, strlen (input), null_ptr (), 0);
101 ASSERT (result == input);
104 /* Check that a very long haystack is handled quickly if the needle is
105 short and occurs near the beginning. */
107 size_t repeat = 10000;
110 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
111 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
112 size_t n = strlen (needle);
113 char *haystack = (char *) malloc (m + 1);
114 if (haystack != NULL)
116 memset (haystack, 'A', m);
119 for (; repeat > 0; repeat--)
121 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
128 /* Check that a very long needle is discarded quickly if the haystack is
131 size_t repeat = 10000;
134 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
135 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
136 size_t n = strlen (haystack);
137 char *needle = (char *) malloc (m + 1);
140 memset (needle, 'A', m);
142 for (; repeat > 0; repeat--)
144 ASSERT (memmem (haystack, n, needle, m) == NULL);
151 /* Check that the asymptotic worst-case complexity is not quadratic. */
154 char *haystack = (char *) malloc (2 * m + 1);
155 char *needle = (char *) malloc (m + 1);
156 if (haystack != NULL && needle != NULL)
160 memset (haystack, 'A', 2 * m);
161 haystack[2 * m] = 'B';
163 memset (needle, 'A', m);
166 result = memmem (haystack, 2 * m + 1, needle, m + 1);
167 ASSERT (result == haystack + m);
173 /* Check that long needles not present in a haystack can be handled
174 with sublinear speed. */
176 size_t repeat = 10000;
179 char *haystack = (char *) malloc (m);
180 char *needle = (char *) malloc (n);
181 if (haystack != NULL && needle != NULL)
185 memset (haystack, 'A', m);
186 memset (needle, 'B', n);
188 for (; repeat > 0; repeat--)
190 result = memmem (haystack, m, needle, n);
191 ASSERT (result == NULL);