2 * Copyright (C) 2004, 2007 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/>. */
25 #define ASSERT(expr) \
30 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37 main (int argc, char *argv[])
40 const char input[] = "foo";
41 const char *result = memmem (input, strlen (input), "", 0);
42 ASSERT (result == input);
46 const char input[] = "foo";
47 const char *result = memmem (input, strlen (input), "o", 1);
48 ASSERT (result == input + 1);
52 const char input[] = "ABC ABCDAB ABCDABCDABDE";
53 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
54 ASSERT (result == input + 15);
58 const char input[] = "ABC ABCDAB ABCDABCDABDE";
59 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
60 ASSERT (result == NULL);
63 /* Check that length 0 does not dereference NULL. */
65 const char *result = memmem (NULL, 0, "foo", 3);
66 ASSERT (result == NULL);
70 const char input[] = "foo";
71 const char *result = memmem (input, strlen (input), NULL, 0);
72 ASSERT (result == input);
75 /* Check that a very long haystack is handled quickly if the needle is
76 short and occurs near the beginning. */
78 size_t repeat = 10000;
81 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
82 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
83 size_t n = strlen (needle);
84 char *haystack = (char *) malloc (m + 1);
87 memset (haystack, 'A', m);
90 for (; repeat > 0; repeat--)
92 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
99 /* Check that a very long needle is discarded quickly if the haystack is
102 size_t repeat = 10000;
105 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
106 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
107 size_t n = strlen (haystack);
108 char *needle = (char *) malloc (m + 1);
111 memset (needle, 'A', m);
113 for (; repeat > 0; repeat--)
115 ASSERT (memmem (haystack, n, needle, m) == NULL);
122 /* Check that the asymptotic worst-case complexity is not quadratic. */
125 char *haystack = (char *) malloc (2 * m + 1);
126 char *needle = (char *) malloc (m + 1);
127 if (haystack != NULL && needle != NULL)
131 memset (haystack, 'A', 2 * m);
132 haystack[2 * m] = 'B';
134 memset (needle, 'A', m);
137 result = memmem (haystack, 2 * m + 1, needle, m + 1);
138 ASSERT (result == haystack + m);
142 if (haystack != NULL)