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));
29 #include "zerosize-ptr.h"
39 main (int argc, char *argv[])
42 /* Declare failure if test takes too long, by using default abort
43 caused by SIGALRM. All known platforms that lack alarm also lack
44 memmem, and the replacement memmem is known to not take too
46 signal (SIGALRM, SIG_DFL);
51 const char input[] = "foo";
52 const char *result = memmem (input, strlen (input), "", 0);
53 ASSERT (result == input);
57 const char input[] = "foo";
58 const char *result = memmem (input, strlen (input), "o", 1);
59 ASSERT (result == input + 1);
63 const char input[] = "ABC ABCDAB ABCDABCDABDE";
64 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
65 ASSERT (result == input + 15);
69 const char input[] = "ABC ABCDAB ABCDABCDABDE";
70 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
71 ASSERT (result == NULL);
75 const char input[] = "ABC ABCDAB ABCDABCDABDE";
76 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
77 ASSERT (result == input + 11);
80 /* Check that length 0 does not dereference the pointer. */
82 const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
83 ASSERT (result == NULL);
87 const char input[] = "foo";
88 const char *result = memmem (input, strlen (input), null_ptr (), 0);
89 ASSERT (result == input);
92 /* Check that a very long haystack is handled quickly if the needle is
93 short and occurs near the beginning. */
95 size_t repeat = 10000;
98 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
99 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
100 size_t n = strlen (needle);
101 char *haystack = (char *) malloc (m + 1);
102 if (haystack != NULL)
104 memset (haystack, 'A', m);
107 for (; repeat > 0; repeat--)
109 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
116 /* Check that a very long needle is discarded quickly if the haystack is
119 size_t repeat = 10000;
122 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
123 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
124 size_t n = strlen (haystack);
125 char *needle = (char *) malloc (m + 1);
128 memset (needle, 'A', m);
130 for (; repeat > 0; repeat--)
132 ASSERT (memmem (haystack, n, needle, m) == NULL);
139 /* Check that the asymptotic worst-case complexity is not quadratic. */
142 char *haystack = (char *) malloc (2 * m + 1);
143 char *needle = (char *) malloc (m + 1);
144 if (haystack != NULL && needle != NULL)
148 memset (haystack, 'A', 2 * m);
149 haystack[2 * m] = 'B';
151 memset (needle, 'A', m);
154 result = memmem (haystack, 2 * m + 1, needle, m + 1);
155 ASSERT (result == haystack + m);
161 /* Check that long needles not present in a haystack can be handled
162 with sublinear speed. */
164 size_t repeat = 10000;
167 char *haystack = (char *) malloc (m);
168 char *needle = (char *) malloc (n);
169 if (haystack != NULL && needle != NULL)
173 memset (haystack, 'A', m);
174 memset (needle, 'B', n);
176 for (; repeat > 0; repeat--)
178 result = memmem (haystack, m, needle, n);
179 ASSERT (result == NULL);