Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-memmem.c
1 /*
2  * Copyright (C) 2004, 2007-2009 Free Software Foundation
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 <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "zerosize-ptr.h"
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main (int argc, char *argv[])
43 {
44 #if HAVE_DECL_ALARM
45   /* Declare failure if test takes too long, by using default abort
46      caused by SIGALRM.  All known platforms that lack alarm also lack
47      memmem, and the replacement memmem is known to not take too
48      long.  */
49   signal (SIGALRM, SIG_DFL);
50   alarm (100);
51 #endif
52
53   {
54     const char input[] = "foo";
55     const char *result = memmem (input, strlen (input), "", 0);
56     ASSERT (result == input);
57   }
58
59   {
60     const char input[] = "foo";
61     const char *result = memmem (input, strlen (input), "o", 1);
62     ASSERT (result == input + 1);
63   }
64
65   {
66     const char input[] = "ABC ABCDAB ABCDABCDABDE";
67     const char *result = memmem (input, strlen (input), "ABCDABD", 7);
68     ASSERT (result == input + 15);
69   }
70
71   {
72     const char input[] = "ABC ABCDAB ABCDABCDABDE";
73     const char *result = memmem (input, strlen (input), "ABCDABE", 7);
74     ASSERT (result == NULL);
75   }
76
77   {
78     const char input[] = "ABC ABCDAB ABCDABCDABDE";
79     const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
80     ASSERT (result == input + 11);
81   }
82
83   /* Check that length 0 does not dereference the pointer.  */
84   {
85     const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
86     ASSERT (result == NULL);
87   }
88
89   {
90     const char input[] = "foo";
91     const char *result = memmem (input, strlen (input), NULL, 0);
92     ASSERT (result == input);
93   }
94
95   /* Check that a very long haystack is handled quickly if the needle is
96      short and occurs near the beginning.  */
97   {
98     size_t repeat = 10000;
99     size_t m = 1000000;
100     char *needle =
101       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
102       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
103     size_t n = strlen (needle);
104     char *haystack = (char *) malloc (m + 1);
105     if (haystack != NULL)
106       {
107         memset (haystack, 'A', m);
108         haystack[0] = 'B';
109
110         for (; repeat > 0; repeat--)
111           {
112             ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
113           }
114
115         free (haystack);
116       }
117   }
118
119   /* Check that a very long needle is discarded quickly if the haystack is
120      short.  */
121   {
122     size_t repeat = 10000;
123     size_t m = 1000000;
124     char *haystack =
125       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
126       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
127     size_t n = strlen (haystack);
128     char *needle = (char *) malloc (m + 1);
129     if (needle != NULL)
130       {
131         memset (needle, 'A', m);
132
133         for (; repeat > 0; repeat--)
134           {
135             ASSERT (memmem (haystack, n, needle, m) == NULL);
136           }
137
138         free (needle);
139       }
140   }
141
142   /* Check that the asymptotic worst-case complexity is not quadratic.  */
143   {
144     size_t m = 1000000;
145     char *haystack = (char *) malloc (2 * m + 1);
146     char *needle = (char *) malloc (m + 1);
147     if (haystack != NULL && needle != NULL)
148       {
149         const char *result;
150
151         memset (haystack, 'A', 2 * m);
152         haystack[2 * m] = 'B';
153
154         memset (needle, 'A', m);
155         needle[m] = 'B';
156
157         result = memmem (haystack, 2 * m + 1, needle, m + 1);
158         ASSERT (result == haystack + m);
159       }
160     free (needle);
161     free (haystack);
162   }
163
164   /* Check that long needles not present in a haystack can be handled
165      with sublinear speed.  */
166   {
167     size_t repeat = 10000;
168     size_t m = 1000000;
169     size_t n = 1000;
170     char *haystack = (char *) malloc (m);
171     char *needle = (char *) malloc (n);
172     if (haystack != NULL && needle != NULL)
173       {
174         const char *result;
175
176         memset (haystack, 'A', m);
177         memset (needle, 'B', n);
178
179         for (; repeat > 0; repeat--)
180           {
181             result = memmem (haystack, m, needle, n);
182             ASSERT (result == NULL);
183           }
184       }
185     free (haystack);
186     free (needle);
187   }
188
189   return 0;
190 }