Mercurial > gnulib
annotate tests/test-mbscasestr1.c @ 40186:8964917f9574
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 18 Feb 2019 08:02:49 -0800 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
8134 | 1 /* Test of case-insensitive searching in a string. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2007-2019 Free Software Foundation, Inc. |
8134 | 3 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
8134 | 5 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
7 (at your option) any later version. |
8134 | 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 | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
8134 | 16 |
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |
18 | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8754
diff
changeset
|
19 #include <config.h> |
8134 | 20 |
21 #include <string.h> | |
22 | |
23 #include <stdlib.h> | |
24 | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
25 #include "macros.h" |
8134 | 26 |
27 int | |
28 main () | |
29 { | |
30 /* This test is executed in the C locale. */ | |
31 | |
32 { | |
33 const char input[] = "foo"; | |
34 const char *result = mbscasestr (input, ""); | |
35 ASSERT (result == input); | |
36 } | |
37 | |
38 { | |
39 const char input[] = "foo"; | |
40 const char *result = mbscasestr (input, "O"); | |
41 ASSERT (result == input + 1); | |
42 } | |
43 | |
44 { | |
45 const char input[] = "ABC ABCDAB ABCDABCDABDE"; | |
46 const char *result = mbscasestr (input, "ABCDaBD"); | |
47 ASSERT (result == input + 15); | |
48 } | |
49 | |
50 { | |
51 const char input[] = "ABC ABCDAB ABCDABCDABDE"; | |
52 const char *result = mbscasestr (input, "ABCDaBE"); | |
53 ASSERT (result == NULL); | |
54 } | |
55 | |
56 /* Check that a very long haystack is handled quickly if the needle is | |
57 short and occurs near the beginning. */ | |
58 { | |
59 size_t repeat = 10000; | |
60 size_t m = 1000000; | |
13415
a884db63982b
Avoid some more warnings from "gcc -Wwrite-strings".
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
61 const char *needle = |
8134 | 62 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
63 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaAAAAaaaaaaa" | |
64 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; | |
65 char *haystack = (char *) malloc (m + 1); | |
66 if (haystack != NULL) | |
67 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
68 memset (haystack, 'A', m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
69 haystack[0] = 'B'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
70 haystack[m] = '\0'; |
8134 | 71 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
72 for (; repeat > 0; repeat--) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
73 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
74 ASSERT (mbscasestr (haystack, needle) == haystack + 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
75 } |
8134 | 76 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
77 free (haystack); |
8134 | 78 } |
79 } | |
80 | |
81 /* Check that a very long needle is discarded quickly if the haystack is | |
82 short. */ | |
83 { | |
84 size_t repeat = 10000; | |
85 size_t m = 1000000; | |
13415
a884db63982b
Avoid some more warnings from "gcc -Wwrite-strings".
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
86 const char *haystack = |
8134 | 87 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
88 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB"; | |
89 char *needle = (char *) malloc (m + 1); | |
90 if (needle != NULL) | |
91 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
92 memset (needle, 'A', m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
93 needle[m] = '\0'; |
8134 | 94 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
95 for (; repeat > 0; repeat--) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
96 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
97 ASSERT (mbscasestr (haystack, needle) == NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
98 } |
8134 | 99 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
100 free (needle); |
8134 | 101 } |
102 } | |
103 | |
104 /* Check that the asymptotic worst-case complexity is not quadratic. */ | |
105 { | |
106 size_t m = 1000000; | |
107 char *haystack = (char *) malloc (2 * m + 2); | |
108 char *needle = (char *) malloc (m + 2); | |
109 if (haystack != NULL && needle != NULL) | |
110 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
111 const char *result; |
8134 | 112 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
113 memset (haystack, 'A', 2 * m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
114 haystack[2 * m] = 'B'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
115 haystack[2 * m + 1] = '\0'; |
8134 | 116 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
117 memset (needle, 'a', m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
118 needle[m] = 'B'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
119 needle[m + 1] = '\0'; |
8134 | 120 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
121 result = mbscasestr (haystack, needle); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
122 ASSERT (result == haystack + m); |
8134 | 123 } |
9749
daf5ba9fe2d9
Remove useless "if" tests before free. Deprecate "free" module.
Jim Meyering <meyering@redhat.com>
parents:
9309
diff
changeset
|
124 free (needle); |
daf5ba9fe2d9
Remove useless "if" tests before free. Deprecate "free" module.
Jim Meyering <meyering@redhat.com>
parents:
9309
diff
changeset
|
125 free (haystack); |
8134 | 126 } |
127 | |
128 return 0; | |
129 } |