Tests for module 'mbscasestr'.
[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 <stdlib.h>
28
29 #define ASSERT(expr) if (!(expr)) abort ();
30
31 int
32 main ()
33 {
34   /* configure should already have checked that the locale is supported.  */
35   if (setlocale (LC_ALL, "") == NULL)
36     return 1;
37
38   {
39     const char input[] = "f\303\266\303\266";
40     const char *result = mbscasestr (input, "");
41     ASSERT (result == input);
42   }
43
44   {
45     const char input[] = "f\303\266\303\266";
46     const char *result = mbscasestr (input, "\303\266");
47     ASSERT (result == input + 1);
48   }
49
50   {
51     const char input[] = "f\303\266\303\266";
52     const char *result = mbscasestr (input, "\266\303");
53     ASSERT (result == NULL);
54   }
55
56   {
57     const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
58     const char *result = mbscasestr (input, "\303\244BCD\303\204BD"); /* "äBCDÄBD" */
59     ASSERT (result == input + 19);
60   }
61
62   {
63     const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
64     const char *result = mbscasestr (input, "\303\204BCD\303\204BE"); /* "ÄBCDÄBE" */
65     ASSERT (result == NULL);
66   }
67
68   /* Check that a very long haystack is handled quickly if the needle is
69      short and occurs near the beginning.  */
70   {
71     size_t repeat = 10000;
72     size_t m = 1000000;
73     char *needle =
74       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
75       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
76     char *haystack = (char *) malloc (m + 1);
77     if (haystack != NULL)
78       {
79         memset (haystack, 'a', m);
80         haystack[0] = '\303'; haystack[1] = '\204';
81         haystack[m] = '\0';
82
83         for (; repeat > 0; repeat--)
84           {
85             ASSERT (mbscasestr (haystack, needle) == haystack + 2);
86           }
87
88         free (haystack);
89       }
90   }
91
92   /* Check that a very long needle is discarded quickly if the haystack is
93      short.  */
94   {
95     size_t repeat = 10000;
96     size_t m = 1000000;
97     char *haystack =
98       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
99       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
100       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
101       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
102       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
103       "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207";
104     char *needle = (char *) malloc (m + 1);
105     if (needle != NULL)
106       {
107         memset (needle, 'A', m);
108         needle[m] = '\0';
109
110         for (; repeat > 0; repeat--)
111           {
112             ASSERT (mbscasestr (haystack, needle) == NULL);
113           }
114
115         free (needle);
116       }
117   }
118
119   /* Check that the asymptotic worst-case complexity is not quadratic.  */
120   {
121     size_t m = 1000000;
122     char *haystack = (char *) malloc (2 * m + 3);
123     char *needle = (char *) malloc (m + 3);
124     if (haystack != NULL && needle != NULL)
125       {
126         const char *result;
127
128         memset (haystack, 'A', 2 * m);
129         haystack[2 * m] = '\303'; haystack[2 * m + 1] = '\247';
130         haystack[2 * m + 2] = '\0';
131
132         memset (needle, 'a', m);
133         needle[m] = '\303'; needle[m + 1] = '\207';
134         needle[m + 2] = '\0';
135
136         result = mbscasestr (haystack, needle);
137         ASSERT (result == haystack + m);
138       }
139     if (needle != NULL)
140       free (needle);
141     if (haystack != NULL)
142       free (haystack);
143   }
144
145   return 0;
146 }