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