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__); \
42 main (int argc, char *argv[])
45 /* Declare failure if test takes too long, by using default abort
46 caused by SIGALRM. All known platforms that lack alarm also lack
47 memmem, and the replacement memmem is known to not take too
49 signal (SIGALRM, SIG_DFL);
54 const char input[] = "foo";
55 const char *result = memmem (input, strlen (input), "", 0);
56 ASSERT (result == input);
60 const char input[] = "foo";
61 const char *result = memmem (input, strlen (input), "o", 1);
62 ASSERT (result == input + 1);
66 const char input[] = "ABC ABCDAB ABCDABCDABDE";
67 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
68 ASSERT (result == input + 15);
72 const char input[] = "ABC ABCDAB ABCDABCDABDE";
73 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
74 ASSERT (result == NULL);
78 const char input[] = "ABC ABCDAB ABCDABCDABDE";
79 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
80 ASSERT (result == input + 11);
83 /* Check that length 0 does not dereference the pointer. */
85 const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
86 ASSERT (result == NULL);
90 const char input[] = "foo";
91 const char *result = memmem (input, strlen (input), NULL, 0);
92 ASSERT (result == input);
95 /* Check that a very long haystack is handled quickly if the needle is
96 short and occurs near the beginning. */
98 size_t repeat = 10000;
101 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
102 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
103 size_t n = strlen (needle);
104 char *haystack = (char *) malloc (m + 1);
105 if (haystack != NULL)
107 memset (haystack, 'A', m);
110 for (; repeat > 0; repeat--)
112 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
119 /* Check that a very long needle is discarded quickly if the haystack is
122 size_t repeat = 10000;
125 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
126 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
127 size_t n = strlen (haystack);
128 char *needle = (char *) malloc (m + 1);
131 memset (needle, 'A', m);
133 for (; repeat > 0; repeat--)
135 ASSERT (memmem (haystack, n, needle, m) == NULL);
142 /* Check that the asymptotic worst-case complexity is not quadratic. */
145 char *haystack = (char *) malloc (2 * m + 1);
146 char *needle = (char *) malloc (m + 1);
147 if (haystack != NULL && needle != NULL)
151 memset (haystack, 'A', 2 * m);
152 haystack[2 * m] = 'B';
154 memset (needle, 'A', m);
157 result = memmem (haystack, 2 * m + 1, needle, m + 1);
158 ASSERT (result == haystack + m);
164 /* Check that long needles not present in a haystack can be handled
165 with sublinear speed. */
167 size_t repeat = 10000;
170 char *haystack = (char *) malloc (m);
171 char *needle = (char *) malloc (n);
172 if (haystack != NULL && needle != NULL)
176 memset (haystack, 'A', m);
177 memset (needle, 'B', n);
179 for (; repeat > 0; repeat--)
181 result = memmem (haystack, m, needle, n);
182 ASSERT (result == NULL);