Fix memmem to avoid O(n^2) worst-case complexity.
[gnulib.git] / tests / test-memmem.c
1 /*
2  * Copyright (C) 2004, 2007 Free Software Foundation
3  * Written by Simon Josefsson
4  *
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.
9  *
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.
14  *
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/>.  */
17
18 #include <config.h>
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #define ASSERT(expr) \
25   do                                                                         \
26     {                                                                        \
27       if (!(expr))                                                           \
28         {                                                                    \
29           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
30           abort ();                                                          \
31         }                                                                    \
32     }                                                                        \
33   while (0)
34
35 int
36 main (int argc, char *argv[])
37 {
38   {
39     const char input[] = "foo";
40     const char *result = memmem (input, strlen (input), "", 0);
41     ASSERT (result == input);
42   }
43
44   {
45     const char input[] = "foo";
46     const char *result = memmem (input, strlen (input), "o", 1);
47     ASSERT (result == input + 1);
48   }
49
50   {
51     const char input[] = "ABC ABCDAB ABCDABCDABDE";
52     const char *result = memmem (input, strlen (input), "ABCDABD", 7);
53     ASSERT (result == input + 15);
54   }
55
56   {
57     const char input[] = "ABC ABCDAB ABCDABCDABDE";
58     const char *result = memmem (input, strlen (input), "ABCDABE", 7);
59     ASSERT (result == NULL);
60   }
61
62   /* Check that length 0 does not dereference NULL.  */
63   {
64     const char *result = memmem (NULL, 0, "foo", 3);
65     ASSERT (result == NULL);
66   }
67
68   {
69     const char input[] = "foo";
70     const char *result = memmem (input, strlen (input), NULL, 0);
71     ASSERT (result == input);
72   }
73
74   /* Check that a very long haystack is handled quickly if the needle is
75      short and occurs near the beginning.  */
76   {
77     size_t repeat = 10000;
78     size_t m = 1000000;
79     char *needle =
80       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
81       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
82     size_t n = strlen (needle);
83     char *haystack = (char *) malloc (m + 1);
84     if (haystack != NULL)
85       {
86         memset (haystack, 'A', m);
87         haystack[0] = 'B';
88
89         for (; repeat > 0; repeat--)
90           {
91             ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
92           }
93
94         free (haystack);
95       }
96   }
97
98   /* Check that a very long needle is discarded quickly if the haystack is
99      short.  */
100   {
101     size_t repeat = 10000;
102     size_t m = 1000000;
103     char *haystack =
104       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
105       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
106     size_t n = strlen (haystack);
107     char *needle = (char *) malloc (m + 1);
108     if (needle != NULL)
109       {
110         memset (needle, 'A', m);
111
112         for (; repeat > 0; repeat--)
113           {
114             ASSERT (memmem (haystack, n, needle, m) == NULL);
115           }
116
117         free (needle);
118       }
119   }
120
121   /* Check that the asymptotic worst-case complexity is not quadratic.  */
122   {
123     size_t m = 1000000;
124     char *haystack = (char *) malloc (2 * m + 1);
125     char *needle = (char *) malloc (m + 1);
126     if (haystack != NULL && needle != NULL)
127       {
128         const char *result;
129
130         memset (haystack, 'A', 2 * m);
131         haystack[2 * m] = 'B';
132
133         memset (needle, 'A', m);
134         needle[m] = 'B';
135
136         result = memmem (haystack, 2 * m + 1, needle, m + 1);
137         ASSERT (result == haystack + m);
138       }
139     if (needle != NULL)
140       free (needle);
141     if (haystack != NULL)
142       free (haystack);
143   }
144
145   return 0;
146 }