]>
Commit | Line | Data |
---|---|---|
83ffe9cd | 1 | // Copyright (C) 2014-2023 Free Software Foundation, Inc. |
f82dfb8d VV |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library 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 along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
6458742a | 18 | // { dg-do run { target c++17 } } |
f82dfb8d VV |
19 | |
20 | #include <functional> | |
fec283b6 | 21 | #include <string_view> |
f82dfb8d | 22 | #include <cstring> |
beb0086f | 23 | #include <cctype> |
f82dfb8d VV |
24 | #include <algorithm> |
25 | #include <testsuite_hooks.h> | |
26 | ||
42d3f743 AA |
27 | #if __STDC_HOSTED__ |
28 | # ifndef __cpp_lib_boyer_moore_searcher | |
29 | # error "Feature-test macro for searchers missing" | |
30 | # elif __cpp_lib_boyer_moore_searcher < 201603 | |
31 | # error "Feature-test macro for searchers has wrong value" | |
32 | # endif | |
33 | #endif // HOSTED | |
4940de30 | 34 | |
af181c91 | 35 | using std::default_searcher; |
42d3f743 | 36 | #if __STDC_HOSTED__ |
af181c91 JW |
37 | using std::boyer_moore_searcher; |
38 | using std::boyer_moore_horspool_searcher; | |
42d3f743 | 39 | #endif // HOSTED |
f82dfb8d VV |
40 | |
41 | void | |
42 | test01() | |
43 | { | |
44 | const char s[] = { 'a', (char)-97, 'a', '\0' }; | |
45 | const char* needles[] = { | |
46 | s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd" | |
47 | }; | |
48 | const char* haystacks[] = { | |
49 | s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd", | |
50 | "aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz" | |
51 | }; | |
52 | ||
53 | for (auto n : needles) | |
54 | { | |
38d68cf0 JW |
55 | auto nlen = std::strlen(n); |
56 | auto ne = n + nlen; | |
af181c91 | 57 | default_searcher d(n, ne); |
42d3f743 | 58 | #if __STDC_HOSTED__ |
af181c91 JW |
59 | boyer_moore_searcher bm(n, ne); |
60 | boyer_moore_horspool_searcher bmh(n, ne); | |
42d3f743 | 61 | #endif // HOSTED |
f82dfb8d VV |
62 | for (auto h : haystacks) |
63 | { | |
64 | auto he = h + std::strlen(h); | |
65 | auto res = std::search(h, he, n, ne); | |
66 | auto d_res = d(h, he); | |
5e8037ba | 67 | VERIFY( d_res.first == res ); |
38d68cf0 JW |
68 | if (res == he) |
69 | VERIFY( d_res.second == d_res.first ); | |
70 | else | |
71 | VERIFY( d_res.second == (d_res.first + nlen) ); | |
42d3f743 AA |
72 | |
73 | #if __STDC_HOSTED__ | |
f82dfb8d | 74 | auto bm_res = bm(h, he); |
5e8037ba | 75 | VERIFY( bm_res.first == res ); |
38d68cf0 JW |
76 | if (res == he) |
77 | VERIFY( bm_res.second == bm_res.first ); | |
78 | else | |
79 | VERIFY( bm_res.second == (bm_res.first + nlen) ); | |
f82dfb8d | 80 | auto bmh_res = bmh(h, he); |
5e8037ba | 81 | VERIFY( bmh_res.first == res ); |
38d68cf0 JW |
82 | if (res == he) |
83 | VERIFY( bmh_res.second == bmh_res.first ); | |
84 | else | |
85 | VERIFY( bmh_res.second == (bmh_res.first + nlen) ); | |
42d3f743 | 86 | #endif |
f82dfb8d VV |
87 | } |
88 | } | |
89 | } | |
90 | ||
91 | void | |
92 | test02() | |
93 | { | |
f82dfb8d VV |
94 | const wchar_t s[] = { L'a', (wchar_t)-97, L'a', L'\0' }; |
95 | const wchar_t* needles[] = { | |
96 | s, L"", L"a", L"aa", L"aaa", L"ab", L"cd", L"abcd", L"abcdabcd", L"abcabcd" | |
97 | }; | |
98 | const wchar_t* haystacks[] = { | |
99 | s, L"", L"a", L"aa", L"aaa", L"ab", L"cd", L"abcd", L"abcdabcd", L"abcabcd", | |
100 | L"aaaaaaa", L"aabaa", L"aaacab", L"cdabcdab", L"abcdabcd", L"xyzabcdxyz" | |
101 | }; | |
102 | ||
103 | for (auto n : needles) | |
104 | { | |
fec283b6 | 105 | auto nlen = std::char_traits<wchar_t>::length(n); |
38d68cf0 | 106 | auto ne = n + nlen; |
af181c91 | 107 | default_searcher d(n, ne); |
42d3f743 | 108 | #if __STDC_HOSTED__ |
af181c91 JW |
109 | boyer_moore_searcher bm(n, ne); |
110 | boyer_moore_horspool_searcher bmh(n, ne); | |
42d3f743 | 111 | #endif // HOSTED |
f82dfb8d VV |
112 | for (auto h : haystacks) |
113 | { | |
fec283b6 | 114 | auto he = h + std::char_traits<wchar_t>::length(h); |
f82dfb8d VV |
115 | auto res = std::search(h, he, n, ne); |
116 | auto d_res = d(h, he); | |
5e8037ba | 117 | VERIFY( d_res.first == res ); |
38d68cf0 JW |
118 | if (res == he) |
119 | VERIFY( d_res.second == d_res.first ); | |
120 | else | |
121 | VERIFY( d_res.second == (d_res.first + nlen) ); | |
42d3f743 | 122 | #if __STDC_HOSTED__ |
f82dfb8d | 123 | auto bm_res = bm(h, he); |
5e8037ba | 124 | VERIFY( bm_res.first == res ); |
38d68cf0 JW |
125 | if (res == he) |
126 | VERIFY( bm_res.second == bm_res.first ); | |
127 | else | |
128 | VERIFY( bm_res.second == (bm_res.first + nlen) ); | |
f82dfb8d | 129 | auto bmh_res = bmh(h, he); |
5e8037ba | 130 | VERIFY( bmh_res.first == res ); |
38d68cf0 JW |
131 | if (res == he) |
132 | VERIFY( bmh_res.second == bmh_res.first ); | |
133 | else | |
134 | VERIFY( bmh_res.second == (bmh_res.first + nlen) ); | |
42d3f743 | 135 | #endif // HOSTED |
f82dfb8d VV |
136 | } |
137 | } | |
f82dfb8d VV |
138 | } |
139 | ||
140 | void | |
141 | test03() | |
142 | { | |
143 | // custom predicate | |
144 | struct | |
145 | { | |
146 | static unsigned char | |
147 | norm(unsigned char c) { return std::isalnum(c) ? c : '#'; } | |
148 | ||
149 | // equality | |
150 | bool operator()(char l, char r) const { return norm(l) == norm(r); } | |
151 | ||
152 | // hash | |
153 | std::size_t operator()(char c) const { return std::hash<char>{}(norm(c)); } | |
154 | } eq; | |
155 | ||
156 | const char* needle = " foo 123 "; | |
157 | const char* haystack = "*****foo*123******"; | |
38d68cf0 JW |
158 | auto nlen = std::strlen(needle); |
159 | const char* ne = needle + nlen; | |
f82dfb8d VV |
160 | const char* he = haystack + std::strlen(haystack); |
161 | ||
af181c91 | 162 | default_searcher d(needle, ne, eq); |
42d3f743 | 163 | #if __STDC_HOSTED__ |
af181c91 JW |
164 | boyer_moore_searcher bm(needle, ne, eq, eq); |
165 | boyer_moore_horspool_searcher bmh(needle, ne, eq, eq); | |
42d3f743 | 166 | #endif |
f82dfb8d VV |
167 | |
168 | auto res = std::search(haystack, he, needle, ne, eq); | |
169 | auto d_res = d(haystack, he); | |
5e8037ba | 170 | VERIFY( d_res.first == res ); |
38d68cf0 JW |
171 | if (res == he) |
172 | VERIFY( d_res.second == d_res.first ); | |
173 | else | |
174 | VERIFY( d_res.second == (d_res.first + nlen) ); | |
42d3f743 | 175 | #if __STDC_HOSTED__ |
f82dfb8d | 176 | auto bm_res = bm(haystack, he); |
5e8037ba | 177 | VERIFY( bm_res.first == res ); |
38d68cf0 JW |
178 | if (res == he) |
179 | VERIFY( bm_res.second == bm_res.first ); | |
180 | else | |
181 | VERIFY( bm_res.second == (bm_res.first + nlen) ); | |
f82dfb8d | 182 | auto bmh_res = bmh(haystack, he); |
5e8037ba | 183 | VERIFY( bmh_res.first == res ); |
38d68cf0 JW |
184 | if (res == he) |
185 | VERIFY( bmh_res.second == bmh_res.first ); | |
186 | else | |
187 | VERIFY( bmh_res.second == (bmh_res.first + nlen) ); | |
42d3f743 | 188 | #endif // HOSTED |
f82dfb8d VV |
189 | } |
190 | ||
191 | int | |
192 | main() | |
193 | { | |
194 | test01(); | |
195 | test02(); | |
196 | test03(); | |
197 | } |