Increase the maximum allowed time for the test.
[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           abort ();                                                          \
33         }                                                                    \
34     }                                                                        \
35   while (0)
36
37 int
38 main (int argc, char *argv[])
39 {
40 #if HAVE_DECL_ALARM
41   /* Declare failure if test takes too long, by using default abort
42      caused by SIGALRM.  All known platforms that lack alarm also have
43      a quadratic strstr, and the replacement strstr is known to not
44      take too long.  */
45   alarm (50);
46 #endif
47
48   {
49     const char input[] = "foo";
50     const char *result = strstr (input, "");
51     ASSERT (result == input);
52   }
53
54   {
55     const char input[] = "foo";
56     const char *result = strstr (input, "o");
57     ASSERT (result == input + 1);
58   }
59
60   {
61     const char input[] = "ABC ABCDAB ABCDABCDABDE";
62     const char *result = strstr (input, "ABCDABD");
63     ASSERT (result == input + 15);
64   }
65
66   {
67     const char input[] = "ABC ABCDAB ABCDABCDABDE";
68     const char *result = strstr (input, "ABCDABE");
69     ASSERT (result == NULL);
70   }
71
72   {
73     const char input[] = "ABC ABCDAB ABCDABCDABDE";
74     const char *result = strstr (input, "ABCDABCD");
75     ASSERT (result == input + 11);
76   }
77
78   /* Check that a very long haystack is handled quickly if the needle is
79      short and occurs near the beginning.  */
80   {
81     size_t repeat = 10000;
82     size_t m = 1000000;
83     char *needle =
84       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
85       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
86     char *haystack = (char *) malloc (m + 1);
87     if (haystack != NULL)
88       {
89         memset (haystack, 'A', m);
90         haystack[0] = 'B';
91         haystack[m] = '\0';
92
93         for (; repeat > 0; repeat--)
94           {
95             ASSERT (strstr (haystack, needle) == haystack + 1);
96           }
97
98         free (haystack);
99       }
100   }
101
102   /* Check that a very long needle is discarded quickly if the haystack is
103      short.  */
104   {
105     size_t repeat = 10000;
106     size_t m = 1000000;
107     char *haystack =
108       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
109       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
110     char *needle = (char *) malloc (m + 1);
111     if (needle != NULL)
112       {
113         memset (needle, 'A', m);
114         needle[m] = '\0';
115
116         for (; repeat > 0; repeat--)
117           {
118             ASSERT (strstr (haystack, needle) == NULL);
119           }
120
121         free (needle);
122       }
123   }
124
125   /* Check that the asymptotic worst-case complexity is not quadratic.  */
126   {
127     size_t m = 1000000;
128     char *haystack = (char *) malloc (2 * m + 2);
129     char *needle = (char *) malloc (m + 2);
130     if (haystack != NULL && needle != NULL)
131       {
132         const char *result;
133
134         memset (haystack, 'A', 2 * m);
135         haystack[2 * m] = 'B';
136         haystack[2 * m + 1] = '\0';
137
138         memset (needle, 'A', m);
139         needle[m] = 'B';
140         needle[m + 1] = '\0';
141
142         result = strstr (haystack, needle);
143         ASSERT (result == haystack + m);
144       }
145     if (needle != NULL)
146       free (needle);
147     if (haystack != NULL)
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 }