1 | /* -*- buffer-read-only: t -*- vi: set ro: */
|
---|
2 | /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
|
---|
3 | /* Test of searching in a string.
|
---|
4 | Copyright (C) 2007-2011 Free Software Foundation, Inc.
|
---|
5 |
|
---|
6 | This program is free software: you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
|
---|
20 |
|
---|
21 | #include <config.h>
|
---|
22 |
|
---|
23 | #include <string.h>
|
---|
24 |
|
---|
25 | #include <locale.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 |
|
---|
28 | #include "macros.h"
|
---|
29 |
|
---|
30 | int
|
---|
31 | main ()
|
---|
32 | {
|
---|
33 | /* configure should already have checked that the locale is supported. */
|
---|
34 | if (setlocale (LC_ALL, "") == NULL)
|
---|
35 | return 1;
|
---|
36 |
|
---|
37 | {
|
---|
38 | const char input[] = "f\303\266\303\266";
|
---|
39 | const char *result = mbsstr (input, "");
|
---|
40 | ASSERT (result == input);
|
---|
41 | }
|
---|
42 |
|
---|
43 | {
|
---|
44 | const char input[] = "f\303\266\303\266";
|
---|
45 | const char *result = mbsstr (input, "\303\266");
|
---|
46 | ASSERT (result == input + 1);
|
---|
47 | }
|
---|
48 |
|
---|
49 | {
|
---|
50 | const char input[] = "f\303\266\303\266";
|
---|
51 | const char *result = mbsstr (input, "\266\303");
|
---|
52 | ASSERT (result == NULL);
|
---|
53 | }
|
---|
54 |
|
---|
55 | {
|
---|
56 | const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÃBC ÃBCDÃB ÃBCDÃBCDÃBDE" */
|
---|
57 | const char *result = mbsstr (input, "\303\204BCD\303\204BD"); /* "ÃBCDÃBD" */
|
---|
58 | ASSERT (result == input + 19);
|
---|
59 | }
|
---|
60 |
|
---|
61 | {
|
---|
62 | const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÃBC ÃBCDÃB ÃBCDÃBCDÃBDE" */
|
---|
63 | const char *result = mbsstr (input, "\303\204BCD\303\204BE"); /* "ÃBCDÃBE" */
|
---|
64 | ASSERT (result == NULL);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* Check that a very long haystack is handled quickly if the needle is
|
---|
68 | short and occurs near the beginning. */
|
---|
69 | {
|
---|
70 | size_t repeat = 10000;
|
---|
71 | size_t m = 1000000;
|
---|
72 | const char *needle =
|
---|
73 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
---|
74 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
---|
75 | char *haystack = (char *) malloc (m + 1);
|
---|
76 | if (haystack != NULL)
|
---|
77 | {
|
---|
78 | memset (haystack, 'A', m);
|
---|
79 | haystack[0] = '\303'; haystack[1] = '\204';
|
---|
80 | haystack[m] = '\0';
|
---|
81 |
|
---|
82 | for (; repeat > 0; repeat--)
|
---|
83 | {
|
---|
84 | ASSERT (mbsstr (haystack, needle) == haystack + 2);
|
---|
85 | }
|
---|
86 |
|
---|
87 | free (haystack);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* Check that a very long needle is discarded quickly if the haystack is
|
---|
92 | short. */
|
---|
93 | {
|
---|
94 | size_t repeat = 10000;
|
---|
95 | size_t m = 1000000;
|
---|
96 | const char *haystack =
|
---|
97 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
---|
98 | "A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207A\303\207"
|
---|
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\207";
|
---|
103 | char *needle = (char *) malloc (m + 1);
|
---|
104 | if (needle != NULL)
|
---|
105 | {
|
---|
106 | memset (needle, 'A', m);
|
---|
107 | needle[m] = '\0';
|
---|
108 |
|
---|
109 | for (; repeat > 0; repeat--)
|
---|
110 | {
|
---|
111 | ASSERT (mbsstr (haystack, needle) == NULL);
|
---|
112 | }
|
---|
113 |
|
---|
114 | free (needle);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* Check that the asymptotic worst-case complexity is not quadratic. */
|
---|
119 | {
|
---|
120 | size_t m = 1000000;
|
---|
121 | char *haystack = (char *) malloc (2 * m + 3);
|
---|
122 | char *needle = (char *) malloc (m + 3);
|
---|
123 | if (haystack != NULL && needle != NULL)
|
---|
124 | {
|
---|
125 | const char *result;
|
---|
126 |
|
---|
127 | memset (haystack, 'A', 2 * m);
|
---|
128 | haystack[2 * m] = '\303'; haystack[2 * m + 1] = '\207';
|
---|
129 | haystack[2 * m + 2] = '\0';
|
---|
130 |
|
---|
131 | memset (needle, 'A', m);
|
---|
132 | needle[m] = '\303'; needle[m + 1] = '\207';
|
---|
133 | needle[m + 2] = '\0';
|
---|
134 |
|
---|
135 | result = mbsstr (haystack, needle);
|
---|
136 | ASSERT (result == haystack + m);
|
---|
137 | }
|
---|
138 | free (needle);
|
---|
139 | free (haystack);
|
---|
140 | }
|
---|
141 |
|
---|
142 | return 0;
|
---|
143 | }
|
---|