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__); \
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
50 const char input[] = "foo";
51 const char *result = memmem (input, strlen (input), "", 0);
52 ASSERT (result == input);
56 const char input[] = "foo";
57 const char *result = memmem (input, strlen (input), "o", 1);
58 ASSERT (result == input + 1);
62 const char input[] = "ABC ABCDAB ABCDABCDABDE";
63 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
64 ASSERT (result == input + 15);
68 const char input[] = "ABC ABCDAB ABCDABCDABDE";
69 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
70 ASSERT (result == NULL);
74 const char input[] = "ABC ABCDAB ABCDABCDABDE";
75 const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
76 ASSERT (result == input + 11);
79 /* Check that length 0 does not dereference NULL. */
81 const char *result = memmem (NULL, 0, "foo", 3);
82 ASSERT (result == NULL);
86 const char input[] = "foo";
87 const char *result = memmem (input, strlen (input), NULL, 0);
88 ASSERT (result == input);
91 /* Check that a very long haystack is handled quickly if the needle is
92 short and occurs near the beginning. */
94 size_t repeat = 10000;
97 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
98 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
99 size_t n = strlen (needle);
100 char *haystack = (char *) malloc (m + 1);
101 if (haystack != NULL)
103 memset (haystack, 'A', m);
106 for (; repeat > 0; repeat--)
108 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
115 /* Check that a very long needle is discarded quickly if the haystack is
118 size_t repeat = 10000;
121 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
122 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
123 size_t n = strlen (haystack);
124 char *needle = (char *) malloc (m + 1);
127 memset (needle, 'A', m);
129 for (; repeat > 0; repeat--)
131 ASSERT (memmem (haystack, n, needle, m) == NULL);
138 /* Check that the asymptotic worst-case complexity is not quadratic. */
141 char *haystack = (char *) malloc (2 * m + 1);
142 char *needle = (char *) malloc (m + 1);
143 if (haystack != NULL && needle != NULL)
147 memset (haystack, 'A', 2 * m);
148 haystack[2 * m] = 'B';
150 memset (needle, 'A', m);
153 result = memmem (haystack, 2 * m + 1, needle, m + 1);
154 ASSERT (result == haystack + m);
160 /* Check that long needles not present in a haystack can be handled
161 with sublinear speed. */
163 size_t repeat = 10000;
166 char *haystack = (char *) malloc (m);
167 char *needle = (char *) malloc (n);
168 if (haystack != NULL && needle != NULL)
172 memset (haystack, 'A', m);
173 memset (needle, 'B', n);
175 for (; repeat > 0; repeat--)
177 result = memmem (haystack, m, needle, n);
178 ASSERT (result == NULL);