9f2ecde1aeb85cbe97f2a0828563a742e8367b7a
[gnulib.git] / tests / test-strstr.c
1 /*
2  * Copyright (C) 2004, 2007, 2008 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 <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           fflush (stderr);                                                   \
33           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
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 have
44      a quadratic strstr, and the replacement strstr is known to not
45      take too long.  */
46   signal (SIGALRM, SIG_DFL);
47   alarm (50);
48 #endif
49
50   {
51     const char input[] = "foo";
52     const char *result = strstr (input, "");
53     ASSERT (result == input);
54   }
55
56   {
57     const char input[] = "foo";
58     const char *result = strstr (input, "o");
59     ASSERT (result == input + 1);
60   }
61
62   {
63     const char input[] = "ABC ABCDAB ABCDABCDABDE";
64     const char *result = strstr (input, "ABCDABD");
65     ASSERT (result == input + 15);
66   }
67
68   {
69     const char input[] = "ABC ABCDAB ABCDABCDABDE";
70     const char *result = strstr (input, "ABCDABE");
71     ASSERT (result == NULL);
72   }
73
74   {
75     const char input[] = "ABC ABCDAB ABCDABCDABDE";
76     const char *result = strstr (input, "ABCDABCD");
77     ASSERT (result == input + 11);
78   }
79
80   /* Check that a very long haystack is handled quickly if the needle is
81      short and occurs near the beginning.  */
82   {
83     size_t repeat = 10000;
84     size_t m = 1000000;
85     char *needle =
86       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
87       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
88     char *haystack = (char *) malloc (m + 1);
89     if (haystack != NULL)
90       {
91         memset (haystack, 'A', m);
92         haystack[0] = 'B';
93         haystack[m] = '\0';
94
95         for (; repeat > 0; repeat--)
96           {
97             ASSERT (strstr (haystack, needle) == haystack + 1);
98           }
99
100         free (haystack);
101       }
102   }
103
104   /* Check that a very long needle is discarded quickly if the haystack is
105      short.  */
106   {
107     size_t repeat = 10000;
108     size_t m = 1000000;
109     char *haystack =
110       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
111       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
112     char *needle = (char *) malloc (m + 1);
113     if (needle != NULL)
114       {
115         memset (needle, 'A', m);
116         needle[m] = '\0';
117
118         for (; repeat > 0; repeat--)
119           {
120             ASSERT (strstr (haystack, needle) == NULL);
121           }
122
123         free (needle);
124       }
125   }
126
127   /* Check that the asymptotic worst-case complexity is not quadratic.  */
128   {
129     size_t m = 1000000;
130     char *haystack = (char *) malloc (2 * m + 2);
131     char *needle = (char *) malloc (m + 2);
132     if (haystack != NULL && needle != NULL)
133       {
134         const char *result;
135
136         memset (haystack, 'A', 2 * m);
137         haystack[2 * m] = 'B';
138         haystack[2 * m + 1] = '\0';
139
140         memset (needle, 'A', m);
141         needle[m] = 'B';
142         needle[m + 1] = '\0';
143
144         result = strstr (haystack, needle);
145         ASSERT (result == haystack + m);
146       }
147     free (needle);
148     free (haystack);
149   }
150
151   /* Sublinear speed is only possible in memmem; strstr must examine
152      every character of haystack to find its length.  */
153
154   return 0;
155 }