Better ASSERT macro.
[gnulib.git] / tests / test-mbscasestr2.c
1 /* Test of searching in a string.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <string.h>
25
26 #include <locale.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main ()
43 {
44   /* configure should already have checked that the locale is supported.  */
45   if (setlocale (LC_ALL, "") == NULL)
46     return 1;
47
48   {
49     const char input[] = "f\303\266\303\266";
50     const char *result = mbscasestr (input, "");
51     ASSERT (result == input);
52   }
53
54   {
55     const char input[] = "f\303\266\303\266";
56     const char *result = mbscasestr (input, "\303\266");
57     ASSERT (result == input + 1);
58   }
59
60   {
61     const char input[] = "f\303\266\303\266";
62     const char *result = mbscasestr (input, "\266\303");
63     ASSERT (result == NULL);
64   }
65
66   {
67     const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
68     const char *result = mbscasestr (input, "\303\244BCD\303\204BD"); /* "äBCDÄBD" */
69     ASSERT (result == input + 19);
70   }
71
72   {
73     const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
74     const char *result = mbscasestr (input, "\303\204BCD\303\204BE"); /* "ÄBCDÄBE" */
75     ASSERT (result == NULL);
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] = '\303'; haystack[1] = '\204';
91         haystack[m] = '\0';
92
93         for (; repeat > 0; repeat--)
94           {
95             ASSERT (mbscasestr (haystack, needle) == haystack + 2);
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       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
110       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
111       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
112       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
113       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207";
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 (mbscasestr (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 + 3);
133     char *needle = (char *) malloc (m + 3);
134     if (haystack != NULL && needle != NULL)
135       {
136         const char *result;
137
138         memset (haystack, 'A', 2 * m);
139         haystack[2 * m] = '\303'; haystack[2 * m + 1] = '\247';
140         haystack[2 * m + 2] = '\0';
141
142         memset (needle, 'a', m);
143         needle[m] = '\303'; needle[m + 1] = '\207';
144         needle[m + 2] = '\0';
145
146         result = mbscasestr (haystack, needle);
147         ASSERT (result == haystack + m);
148       }
149     if (needle != NULL)
150       free (needle);
151     if (haystack != NULL)
152       free (haystack);
153   }
154
155   return 0;
156 }