maint: update almost all copyright ranges to include 2011
[gnulib.git] / tests / test-memmem.c
1 /*
2  * Copyright (C) 2004, 2007-2011 Free Software Foundation, Inc.
3  * Written by Bruno Haible and Eric Blake
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 <string.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (memmem, void *, (void const *, size_t, void const *, size_t));
24
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include "zerosize-ptr.h"
30 #include "macros.h"
31
32 static void *
33 null_ptr (void)
34 {
35   return NULL;
36 }
37
38 int
39 main (int argc, char *argv[])
40 {
41 #if HAVE_DECL_ALARM
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
45      long.  */
46   signal (SIGALRM, SIG_DFL);
47   alarm (100);
48 #endif
49
50   {
51     const char input[] = "foo";
52     const char *result = memmem (input, strlen (input), "", 0);
53     ASSERT (result == input);
54   }
55
56   {
57     const char input[] = "foo";
58     const char *result = memmem (input, strlen (input), "o", 1);
59     ASSERT (result == input + 1);
60   }
61
62   {
63     const char input[] = "ABC ABCDAB ABCDABCDABDE";
64     const char *result = memmem (input, strlen (input), "ABCDABD", 7);
65     ASSERT (result == input + 15);
66   }
67
68   {
69     const char input[] = "ABC ABCDAB ABCDABCDABDE";
70     const char *result = memmem (input, strlen (input), "ABCDABE", 7);
71     ASSERT (result == NULL);
72   }
73
74   {
75     const char input[] = "ABC ABCDAB ABCDABCDABDE";
76     const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
77     ASSERT (result == input + 11);
78   }
79
80   /* Check that length 0 does not dereference the pointer.  */
81   {
82     const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
83     ASSERT (result == NULL);
84   }
85
86   {
87     const char input[] = "foo";
88     const char *result = memmem (input, strlen (input), null_ptr (), 0);
89     ASSERT (result == input);
90   }
91
92   /* Check that a long periodic needle does not cause false positives.  */
93   {
94     const char input[] = ("F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"
95                           "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD"
96                           "_C3_A7_20_EF_BF_BD");
97     const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
98     const char *result = memmem (input, strlen (input), need, strlen (need));
99     ASSERT (result == NULL);
100   }
101   {
102     const char input[] = ("F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"
103                           "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD"
104                           "_C3_A7_20_EF_BF_BD_DA_B5_C2_A6_20"
105                           "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD");
106     const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
107     const char *result = memmem (input, strlen (input), need, strlen (need));
108     ASSERT (result == input + 115);
109   }
110
111   /* Check that a very long haystack is handled quickly if the needle is
112      short and occurs near the beginning.  */
113   {
114     size_t repeat = 10000;
115     size_t m = 1000000;
116     const char *needle =
117       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
118       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
119     size_t n = strlen (needle);
120     char *haystack = (char *) malloc (m + 1);
121     if (haystack != NULL)
122       {
123         memset (haystack, 'A', m);
124         haystack[0] = 'B';
125
126         for (; repeat > 0; repeat--)
127           {
128             ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
129           }
130
131         free (haystack);
132       }
133   }
134
135   /* Check that a very long needle is discarded quickly if the haystack is
136      short.  */
137   {
138     size_t repeat = 10000;
139     size_t m = 1000000;
140     const char *haystack =
141       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
142       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
143     size_t n = strlen (haystack);
144     char *needle = (char *) malloc (m + 1);
145     if (needle != NULL)
146       {
147         memset (needle, 'A', m);
148
149         for (; repeat > 0; repeat--)
150           {
151             ASSERT (memmem (haystack, n, needle, m) == NULL);
152           }
153
154         free (needle);
155       }
156   }
157
158   /* Check that the asymptotic worst-case complexity is not quadratic.  */
159   {
160     size_t m = 1000000;
161     char *haystack = (char *) malloc (2 * m + 1);
162     char *needle = (char *) malloc (m + 1);
163     if (haystack != NULL && needle != NULL)
164       {
165         const char *result;
166
167         memset (haystack, 'A', 2 * m);
168         haystack[2 * m] = 'B';
169
170         memset (needle, 'A', m);
171         needle[m] = 'B';
172
173         result = memmem (haystack, 2 * m + 1, needle, m + 1);
174         ASSERT (result == haystack + m);
175       }
176     free (needle);
177     free (haystack);
178   }
179
180   /* Check that long needles not present in a haystack can be handled
181      with sublinear speed.  */
182   {
183     size_t repeat = 10000;
184     size_t m = 1000000;
185     size_t n = 1000;
186     char *haystack = (char *) malloc (m);
187     char *needle = (char *) malloc (n);
188     if (haystack != NULL && needle != NULL)
189       {
190         const char *result;
191
192         memset (haystack, 'A', m);
193         memset (needle, 'B', n);
194
195         for (; repeat > 0; repeat--)
196           {
197             result = memmem (haystack, m, needle, n);
198             ASSERT (result == NULL);
199           }
200       }
201     free (haystack);
202     free (needle);
203   }
204
205   return 0;
206 }