Tests for module 'mbscasestr'.
[gnulib.git] / tests / test-mbscasestr1.c
1 /* Test of case-insensitive 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 <stdlib.h>
27
28 #define ASSERT(expr) if (!(expr)) abort ();
29
30 int
31 main ()
32 {
33   /* This test is executed in the C locale.  */
34
35   {
36     const char input[] = "foo";
37     const char *result = mbscasestr (input, "");
38     ASSERT (result == input);
39   }
40
41   {
42     const char input[] = "foo";
43     const char *result = mbscasestr (input, "O");
44     ASSERT (result == input + 1);
45   }
46
47   {
48     const char input[] = "ABC ABCDAB ABCDABCDABDE";
49     const char *result = mbscasestr (input, "ABCDaBD");
50     ASSERT (result == input + 15);
51   }
52
53   {
54     const char input[] = "ABC ABCDAB ABCDABCDABDE";
55     const char *result = mbscasestr (input, "ABCDaBE");
56     ASSERT (result == NULL);
57   }
58
59   /* Check that a very long haystack is handled quickly if the needle is
60      short and occurs near the beginning.  */
61   {
62     size_t repeat = 10000;
63     size_t m = 1000000;
64     char *needle =
65       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
66       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaAAAAaaaaaaa"
67       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
68     char *haystack = (char *) malloc (m + 1);
69     if (haystack != NULL)
70       {
71         memset (haystack, 'A', m);
72         haystack[0] = 'B';
73         haystack[m] = '\0';
74
75         for (; repeat > 0; repeat--)
76           {
77             ASSERT (mbscasestr (haystack, needle) == haystack + 1);
78           }
79
80         free (haystack);
81       }
82   }
83
84   /* Check that a very long needle is discarded quickly if the haystack is
85      short.  */
86   {
87     size_t repeat = 10000;
88     size_t m = 1000000;
89     char *haystack =
90       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
91       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
92     char *needle = (char *) malloc (m + 1);
93     if (needle != NULL)
94       {
95         memset (needle, 'A', m);
96         needle[m] = '\0';
97
98         for (; repeat > 0; repeat--)
99           {
100             ASSERT (mbscasestr (haystack, needle) == NULL);
101           }
102
103         free (needle);
104       }
105   }
106
107   /* Check that the asymptotic worst-case complexity is not quadratic.  */
108   {
109     size_t m = 1000000;
110     char *haystack = (char *) malloc (2 * m + 2);
111     char *needle = (char *) malloc (m + 2);
112     if (haystack != NULL && needle != NULL)
113       {
114         const char *result;
115
116         memset (haystack, 'A', 2 * m);
117         haystack[2 * m] = 'B';
118         haystack[2 * m + 1] = '\0';
119
120         memset (needle, 'a', m);
121         needle[m] = 'B';
122         needle[m + 1] = '\0';
123
124         result = mbscasestr (haystack, needle);
125         ASSERT (result == haystack + m);
126       }
127     if (needle != NULL)
128       free (needle);
129     if (haystack != NULL)
130       free (haystack);
131   }
132
133   return 0;
134 }