1 // Copyright (C) 2014-2020 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++14 } }
20 #include <experimental/functional>
22 #ifdef _GLIBCXX_USE_WCHAR_T
26 #include <testsuite_hooks.h>
28 using std::experimental::make_default_searcher
;
29 using std::experimental::make_boyer_moore_searcher
;
30 using std::experimental::make_boyer_moore_horspool_searcher
;
35 const char s
[] = { 'a', (char)-97, 'a', '\0' };
36 const char* needles
[] = {
37 s
, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd"
39 const char* haystacks
[] = {
40 s
, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd",
41 "aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz"
44 for (auto n
: needles
)
46 auto ne
= n
+ std::strlen(n
);
47 auto d
= make_default_searcher(n
, ne
);
48 auto bm
= make_boyer_moore_searcher(n
, ne
);
49 auto bmh
= make_boyer_moore_horspool_searcher(n
, ne
);
50 for (auto h
: haystacks
)
52 auto he
= h
+ std::strlen(h
);
53 auto res
= std::search(h
, he
, n
, ne
);
54 auto d_res
= d(h
, he
);
55 VERIFY( d_res
== res
);
56 auto bm_res
= bm(h
, he
);
57 VERIFY( bm_res
== res
);
58 auto bmh_res
= bmh(h
, he
);
59 VERIFY( bmh_res
== res
);
67 #ifdef _GLIBCXX_USE_WCHAR_T
68 const wchar_t s
[] = { L
'a', (wchar_t)-97, L
'a', L
'\0' };
69 const wchar_t* needles
[] = {
70 s
, L
"", L
"a", L
"aa", L
"aaa", L
"ab", L
"cd", L
"abcd", L
"abcdabcd", L
"abcabcd"
72 const wchar_t* haystacks
[] = {
73 s
, L
"", L
"a", L
"aa", L
"aaa", L
"ab", L
"cd", L
"abcd", L
"abcdabcd", L
"abcabcd",
74 L
"aaaaaaa", L
"aabaa", L
"aaacab", L
"cdabcdab", L
"abcdabcd", L
"xyzabcdxyz"
77 for (auto n
: needles
)
79 auto ne
= n
+ std::wcslen(n
);
80 auto d
= make_default_searcher(n
, ne
);
81 auto bm
= make_boyer_moore_searcher(n
, ne
);
82 auto bmh
= make_boyer_moore_horspool_searcher(n
, ne
);
83 for (auto h
: haystacks
)
85 auto he
= h
+ std::wcslen(h
);
86 auto res
= std::search(h
, he
, n
, ne
);
87 auto d_res
= d(h
, he
);
88 VERIFY( d_res
== res
);
89 auto bm_res
= bm(h
, he
);
90 VERIFY( bm_res
== res
);
91 auto bmh_res
= bmh(h
, he
);
92 VERIFY( bmh_res
== res
);
105 norm(unsigned char c
) { return std::isalnum(c
) ? c
: '#'; }
108 bool operator()(char l
, char r
) const { return norm(l
) == norm(r
); }
111 std::size_t operator()(char c
) const { return std::hash
<char>{}(norm(c
)); }
114 const char* needle
= " foo 123 ";
115 const char* haystack
= "*****foo*123******";
116 const char* ne
= needle
+ std::strlen(needle
);
117 const char* he
= haystack
+ std::strlen(haystack
);
119 auto d
= make_default_searcher(needle
, ne
, eq
);
120 auto bm
= make_boyer_moore_searcher(needle
, ne
, eq
, eq
);
121 auto bmh
= make_boyer_moore_horspool_searcher(needle
, ne
, eq
, eq
);
123 auto res
= std::search(haystack
, he
, needle
, ne
, eq
);
124 auto d_res
= d(haystack
, he
);
125 VERIFY( d_res
== res
);
126 auto bm_res
= bm(haystack
, he
);
127 VERIFY( bm_res
== res
);
128 auto bmh_res
= bmh(haystack
, he
);
129 VERIFY( bmh_res
== res
);