tests/test-getaddrinfo.c: Don't print debug messages by default.
[pspp] / tests / test-memmem.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 lack
44      memmem, and the replacement memmem is known to not take too
45      long.  */
46   alarm (100);
47 #endif
48
49   {
50     const char input[] = "foo";
51     const char *result = memmem (input, strlen (input), "", 0);
52     ASSERT (result == input);
53   }
54
55   {
56     const char input[] = "foo";
57     const char *result = memmem (input, strlen (input), "o", 1);
58     ASSERT (result == input + 1);
59   }
60
61   {
62     const char input[] = "ABC ABCDAB ABCDABCDABDE";
63     const char *result = memmem (input, strlen (input), "ABCDABD", 7);
64     ASSERT (result == input + 15);
65   }
66
67   {
68     const char input[] = "ABC ABCDAB ABCDABCDABDE";
69     const char *result = memmem (input, strlen (input), "ABCDABE", 7);
70     ASSERT (result == NULL);
71   }
72
73   {
74     const char input[] = "ABC ABCDAB ABCDABCDABDE";
75     const char *result = memmem (input, strlen (input), "ABCDABCD", 8);
76     ASSERT (result == input + 11);
77   }
78
79   /* Check that length 0 does not dereference NULL.  */
80   {
81     const char *result = memmem (NULL, 0, "foo", 3);
82     ASSERT (result == NULL);
83   }
84
85   {
86     const char input[] = "foo";
87     const char *result = memmem (input, strlen (input), NULL, 0);
88     ASSERT (result == input);
89   }
90
91   /* Check that a very long haystack is handled quickly if the needle is
92      short and occurs near the beginning.  */
93   {
94     size_t repeat = 10000;
95     size_t m = 1000000;
96     char *needle =
97       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
98       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
99     size_t n = strlen (needle);
100     char *haystack = (char *) malloc (m + 1);
101     if (haystack != NULL)
102       {
103         memset (haystack, 'A', m);
104         haystack[0] = 'B';
105
106         for (; repeat > 0; repeat--)
107           {
108             ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
109           }
110
111         free (haystack);
112       }
113   }
114
115   /* Check that a very long needle is discarded quickly if the haystack is
116      short.  */
117   {
118     size_t repeat = 10000;
119     size_t m = 1000000;
120     char *haystack =
121       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
122       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
123     size_t n = strlen (haystack);
124     char *needle = (char *) malloc (m + 1);
125     if (needle != NULL)
126       {
127         memset (needle, 'A', m);
128
129         for (; repeat > 0; repeat--)
130           {
131             ASSERT (memmem (haystack, n, needle, m) == NULL);
132           }
133
134         free (needle);
135       }
136   }
137
138   /* Check that the asymptotic worst-case complexity is not quadratic.  */
139   {
140     size_t m = 1000000;
141     char *haystack = (char *) malloc (2 * m + 1);
142     char *needle = (char *) malloc (m + 1);
143     if (haystack != NULL && needle != NULL)
144       {
145         const char *result;
146
147         memset (haystack, 'A', 2 * m);
148         haystack[2 * m] = 'B';
149
150         memset (needle, 'A', m);
151         needle[m] = 'B';
152
153         result = memmem (haystack, 2 * m + 1, needle, m + 1);
154         ASSERT (result == haystack + m);
155       }
156     free (needle);
157     free (haystack);
158   }
159
160   /* Check that long needles not present in a haystack can be handled
161      with sublinear speed.  */
162   {
163     size_t repeat = 10000;
164     size_t m = 1000000;
165     size_t n = 1000;
166     char *haystack = (char *) malloc (m);
167     char *needle = (char *) malloc (n);
168     if (haystack != NULL && needle != NULL)
169       {
170         const char *result;
171
172         memset (haystack, 'A', m);
173         memset (needle, 'B', n);
174
175         for (; repeat > 0; repeat--)
176           {
177             result = memmem (haystack, m, needle, n);
178             ASSERT (result == NULL);
179           }
180       }
181     free (haystack);
182     free (needle);
183   }
184
185   return 0;
186 }