Use standard idioms.
[pspp] / tests / test-memmem.c
1 /*
2  * Copyright (C) 2004, 2007 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
25 #define ASSERT(expr) \
26   do                                                                         \
27     {                                                                        \
28       if (!(expr))                                                           \
29         {                                                                    \
30           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
31           abort ();                                                          \
32         }                                                                    \
33     }                                                                        \
34   while (0)
35
36 int
37 main (int argc, char *argv[])
38 {
39   {
40     const char input[] = "foo";
41     const char *result = memmem (input, strlen (input), "", 0);
42     ASSERT (result == input);
43   }
44
45   {
46     const char input[] = "foo";
47     const char *result = memmem (input, strlen (input), "o", 1);
48     ASSERT (result == input + 1);
49   }
50
51   {
52     const char input[] = "ABC ABCDAB ABCDABCDABDE";
53     const char *result = memmem (input, strlen (input), "ABCDABD", 7);
54     ASSERT (result == input + 15);
55   }
56
57   {
58     const char input[] = "ABC ABCDAB ABCDABCDABDE";
59     const char *result = memmem (input, strlen (input), "ABCDABE", 7);
60     ASSERT (result == NULL);
61   }
62
63   /* Check that length 0 does not dereference NULL.  */
64   {
65     const char *result = memmem (NULL, 0, "foo", 3);
66     ASSERT (result == NULL);
67   }
68
69   {
70     const char input[] = "foo";
71     const char *result = memmem (input, strlen (input), NULL, 0);
72     ASSERT (result == input);
73   }
74
75   /* Check that a very long haystack is handled quickly if the needle is
76      short and occurs near the beginning.  */
77   {
78     size_t repeat = 10000;
79     size_t m = 1000000;
80     char *needle =
81       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
82       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
83     size_t n = strlen (needle);
84     char *haystack = (char *) malloc (m + 1);
85     if (haystack != NULL)
86       {
87         memset (haystack, 'A', m);
88         haystack[0] = 'B';
89
90         for (; repeat > 0; repeat--)
91           {
92             ASSERT (memmem (haystack, m, needle, n) == haystack + 1);
93           }
94
95         free (haystack);
96       }
97   }
98
99   /* Check that a very long needle is discarded quickly if the haystack is
100      short.  */
101   {
102     size_t repeat = 10000;
103     size_t m = 1000000;
104     char *haystack =
105       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
106       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
107     size_t n = strlen (haystack);
108     char *needle = (char *) malloc (m + 1);
109     if (needle != NULL)
110       {
111         memset (needle, 'A', m);
112
113         for (; repeat > 0; repeat--)
114           {
115             ASSERT (memmem (haystack, n, needle, m) == NULL);
116           }
117
118         free (needle);
119       }
120   }
121
122   /* Check that the asymptotic worst-case complexity is not quadratic.  */
123   {
124     size_t m = 1000000;
125     char *haystack = (char *) malloc (2 * m + 1);
126     char *needle = (char *) malloc (m + 1);
127     if (haystack != NULL && needle != NULL)
128       {
129         const char *result;
130
131         memset (haystack, 'A', 2 * m);
132         haystack[2 * m] = 'B';
133
134         memset (needle, 'A', m);
135         needle[m] = 'B';
136
137         result = memmem (haystack, 2 * m + 1, needle, m + 1);
138         ASSERT (result == haystack + m);
139       }
140     if (needle != NULL)
141       free (needle);
142     if (haystack != NULL)
143       free (haystack);
144   }
145
146   return 0;
147 }