Work around environments that (stupidly) ignore SIGALRM.
[gnulib.git] / tests / test-strcasestr.c
1 /* Test of case-insensitive searching in a string.
2    Copyright (C) 2007, 2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <string.h>
22
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 ()
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 lack
45      memmem, and the replacement memmem is known to not take too
46      long.  */
47   signal (SIGALRM, SIG_DFL);
48   alarm (50);
49 #endif
50
51   {
52     const char input[] = "foo";
53     const char *result = strcasestr (input, "");
54     ASSERT (result == input);
55   }
56
57   {
58     const char input[] = "foo";
59     const char *result = strcasestr (input, "O");
60     ASSERT (result == input + 1);
61   }
62
63   {
64     const char input[] = "ABC ABCDAB ABCDABCDABDE";
65     const char *result = strcasestr (input, "ABCDaBD");
66     ASSERT (result == input + 15);
67   }
68
69   {
70     const char input[] = "ABC ABCDAB ABCDABCDABDE";
71     const char *result = strcasestr (input, "ABCDaBE");
72     ASSERT (result == NULL);
73   }
74
75   {
76     const char input[] = "ABC ABCDAB ABCDABCDABDE";
77     const char *result = strcasestr (input, "ABCDaBCD");
78     ASSERT (result == input + 11);
79   }
80
81   /* Check that a very long haystack is handled quickly if the needle is
82      short and occurs near the beginning.  */
83   {
84     size_t repeat = 10000;
85     size_t m = 1000000;
86     char *needle =
87       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
88       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaAAAAaaaaaaa"
89       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
90     char *haystack = (char *) malloc (m + 1);
91     if (haystack != NULL)
92       {
93         memset (haystack, 'A', m);
94         haystack[0] = 'B';
95         haystack[m] = '\0';
96
97         for (; repeat > 0; repeat--)
98           {
99             ASSERT (strcasestr (haystack, needle) == haystack + 1);
100           }
101
102         free (haystack);
103       }
104   }
105
106   /* Check that a very long needle is discarded quickly if the haystack is
107      short.  */
108   {
109     size_t repeat = 10000;
110     size_t m = 1000000;
111     char *haystack =
112       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
113       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
114     char *needle = (char *) malloc (m + 1);
115     if (needle != NULL)
116       {
117         memset (needle, 'A', m);
118         needle[m] = '\0';
119
120         for (; repeat > 0; repeat--)
121           {
122             ASSERT (strcasestr (haystack, needle) == NULL);
123           }
124
125         free (needle);
126       }
127   }
128
129   /* Check that the asymptotic worst-case complexity is not quadratic.  */
130   {
131     size_t m = 1000000;
132     char *haystack = (char *) malloc (2 * m + 2);
133     char *needle = (char *) malloc (m + 2);
134     if (haystack != NULL && needle != NULL)
135       {
136         const char *result;
137
138         memset (haystack, 'A', 2 * m);
139         haystack[2 * m] = 'B';
140         haystack[2 * m + 1] = '\0';
141
142         memset (needle, 'a', m);
143         needle[m] = 'B';
144         needle[m + 1] = '\0';
145
146         result = strcasestr (haystack, needle);
147         ASSERT (result == haystack + m);
148       }
149     free (needle);
150     free (haystack);
151   }
152
153   return 0;
154 }