Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / gnulib-tests / test-c-strcasestr.c
1 /* Test of case-insensitive searching in a string.
2    Copyright (C) 2007-2010 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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "c-strcasestr.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "macros.h"
27
28 int
29 main ()
30 {
31   {
32     const char input[] = "foo";
33     const char *result = c_strcasestr (input, "");
34     ASSERT (result == input);
35   }
36
37   {
38     const char input[] = "foo";
39     const char *result = c_strcasestr (input, "O");
40     ASSERT (result == input + 1);
41   }
42
43   {
44     const char input[] = "ABC ABCDAB ABCDABCDABDE";
45     const char *result = c_strcasestr (input, "ABCDaBD");
46     ASSERT (result == input + 15);
47   }
48
49   {
50     const char input[] = "ABC ABCDAB ABCDABCDABDE";
51     const char *result = c_strcasestr (input, "ABCDaBE");
52     ASSERT (result == NULL);
53   }
54
55   {
56     const char input[] = "ABC ABCDAB ABCDABCDABDE";
57     const char *result = c_strcasestr (input, "ABCDaBCD");
58     ASSERT (result == input + 11);
59   }
60
61   /* Check that a very long haystack is handled quickly if the needle is
62      short and occurs near the beginning.  */
63   {
64     size_t repeat = 10000;
65     size_t m = 1000000;
66     char *needle =
67       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
68       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaAAAAaaaaaaa"
69       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
70     char *haystack = (char *) malloc (m + 1);
71     if (haystack != NULL)
72       {
73         memset (haystack, 'A', m);
74         haystack[0] = 'B';
75         haystack[m] = '\0';
76
77         for (; repeat > 0; repeat--)
78           {
79             ASSERT (c_strcasestr (haystack, needle) == haystack + 1);
80           }
81
82         free (haystack);
83       }
84   }
85
86   /* Check that a very long needle is discarded quickly if the haystack is
87      short.  */
88   {
89     size_t repeat = 10000;
90     size_t m = 1000000;
91     char *haystack =
92       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
93       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
94     char *needle = (char *) malloc (m + 1);
95     if (needle != NULL)
96       {
97         memset (needle, 'A', m);
98         needle[m] = '\0';
99
100         for (; repeat > 0; repeat--)
101           {
102             ASSERT (c_strcasestr (haystack, needle) == NULL);
103           }
104
105         free (needle);
106       }
107   }
108
109   /* Check that the asymptotic worst-case complexity is not quadratic.  */
110   {
111     size_t m = 1000000;
112     char *haystack = (char *) malloc (2 * m + 2);
113     char *needle = (char *) malloc (m + 2);
114     if (haystack != NULL && needle != NULL)
115       {
116         const char *result;
117
118         memset (haystack, 'A', 2 * m);
119         haystack[2 * m] = 'B';
120         haystack[2 * m + 1] = '\0';
121
122         memset (needle, 'a', m);
123         needle[m] = 'B';
124         needle[m + 1] = '\0';
125
126         result = c_strcasestr (haystack, needle);
127         ASSERT (result == haystack + m);
128       }
129     free (needle);
130     free (haystack);
131   }
132
133   return 0;
134 }