2 * Copyright (C) 2004, 2007 Free Software Foundation
3 * Written by Simon Josefsson
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/>. */
24 #define ASSERT(expr) \
29 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36 main (int argc, char *argv[])
39 const char input[] = "foo";
40 const char *result = memmem (input, strlen (input), "", 0);
41 ASSERT (result == input);
45 const char input[] = "foo";
46 const char *result = memmem (input, strlen (input), "o", 1);
47 ASSERT (result == input + 1);
51 const char input[] = "ABC ABCDAB ABCDABCDABDE";
52 const char *result = memmem (input, strlen (input), "ABCDABD", 7);
53 ASSERT (result == input + 15);
57 const char input[] = "ABC ABCDAB ABCDABCDABDE";
58 const char *result = memmem (input, strlen (input), "ABCDABE", 7);
59 ASSERT (result == NULL);
62 /* Check that length 0 does not dereference NULL. */
64 const char *result = memmem (NULL, 0, "foo", 3);
65 ASSERT (result == NULL);
69 const char input[] = "foo";
70 const char *result = memmem (input, strlen (input), NULL, 0);
71 ASSERT (result == input);
74 /* Check that a very long haystack is handled quickly if the needle is
75 short and occurs near the beginning. */
77 size_t repeat = 10000;
80 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
81 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
82 size_t n = strlen (needle);
83 char *haystack = (char *) malloc (m + 1);
86 memset (haystack, 'A', m);
89 for (; repeat > 0; repeat--)
91 ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
98 /* Check that a very long needle is discarded quickly if the haystack is
101 size_t repeat = 10000;
104 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
105 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
106 size_t n = strlen (haystack);
107 char *needle = (char *) malloc (m + 1);
110 memset (needle, 'A', m);
112 for (; repeat > 0; repeat--)
114 ASSERT (memmem (haystack, n, needle, m) == NULL);
121 /* Check that the asymptotic worst-case complexity is not quadratic. */
124 char *haystack = (char *) malloc (2 * m + 1);
125 char *needle = (char *) malloc (m + 1);
126 if (haystack != NULL && needle != NULL)
130 memset (haystack, 'A', 2 * m);
131 haystack[2 * m] = 'B';
133 memset (needle, 'A', m);
136 result = memmem (haystack, 2 * m + 1, needle, m + 1);
137 ASSERT (result == haystack + m);
141 if (haystack != NULL)