1 // Copyright (C) 2014-2021 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>
21 #include <experimental/string_view>
24 #include <testsuite_hooks.h>
26 using std::experimental::make_default_searcher
;
27 using std::experimental::make_boyer_moore_searcher
;
28 using std::experimental::make_boyer_moore_horspool_searcher
;
33 const char s
[] = { 'a', (char)-97, 'a', '\0' };
34 const char* needles
[] = {
35 s
, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd"
37 const char* haystacks
[] = {
38 s
, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd",
39 "aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz"
42 for (auto n
: needles
)
44 auto ne
= n
+ std::strlen(n
);
45 auto d
= make_default_searcher(n
, ne
);
46 auto bm
= make_boyer_moore_searcher(n
, ne
);
47 auto bmh
= make_boyer_moore_horspool_searcher(n
, ne
);
48 for (auto h
: haystacks
)
50 auto he
= h
+ std::strlen(h
);
51 auto res
= std::search(h
, he
, n
, ne
);
52 auto d_res
= d(h
, he
);
53 VERIFY( d_res
== res
);
54 auto bm_res
= bm(h
, he
);
55 VERIFY( bm_res
== res
);
56 auto bmh_res
= bmh(h
, he
);
57 VERIFY( bmh_res
== res
);
65 const wchar_t s
[] = { L
'a', (wchar_t)-97, L
'a', L
'\0' };
66 const wchar_t* needles
[] = {
67 s
, L
"", L
"a", L
"aa", L
"aaa", L
"ab", L
"cd", L
"abcd", L
"abcdabcd", L
"abcabcd"
69 const wchar_t* haystacks
[] = {
70 s
, L
"", L
"a", L
"aa", L
"aaa", L
"ab", L
"cd", L
"abcd", L
"abcdabcd", L
"abcabcd",
71 L
"aaaaaaa", L
"aabaa", L
"aaacab", L
"cdabcdab", L
"abcdabcd", L
"xyzabcdxyz"
74 for (auto n
: needles
)
76 auto ne
= n
+ std::char_traits
<wchar_t>::length(n
);
77 auto d
= make_default_searcher(n
, ne
);
78 auto bm
= make_boyer_moore_searcher(n
, ne
);
79 auto bmh
= make_boyer_moore_horspool_searcher(n
, ne
);
80 for (auto h
: haystacks
)
82 auto he
= h
+ std::char_traits
<wchar_t>::length(h
);
83 auto res
= std::search(h
, he
, n
, ne
);
84 auto d_res
= d(h
, he
);
85 VERIFY( d_res
== res
);
86 auto bm_res
= bm(h
, he
);
87 VERIFY( bm_res
== res
);
88 auto bmh_res
= bmh(h
, he
);
89 VERIFY( bmh_res
== res
);
101 norm(unsigned char c
) { return std::isalnum(c
) ? c
: '#'; }
104 bool operator()(char l
, char r
) const { return norm(l
) == norm(r
); }
107 std::size_t operator()(char c
) const { return std::hash
<char>{}(norm(c
)); }
110 const char* needle
= " foo 123 ";
111 const char* haystack
= "*****foo*123******";
112 const char* ne
= needle
+ std::strlen(needle
);
113 const char* he
= haystack
+ std::strlen(haystack
);
115 auto d
= make_default_searcher(needle
, ne
, eq
);
116 auto bm
= make_boyer_moore_searcher(needle
, ne
, eq
, eq
);
117 auto bmh
= make_boyer_moore_horspool_searcher(needle
, ne
, eq
, eq
);
119 auto res
= std::search(haystack
, he
, needle
, ne
, eq
);
120 auto d_res
= d(haystack
, he
);
121 VERIFY( d_res
== res
);
122 auto bm_res
= bm(haystack
, he
);
123 VERIFY( bm_res
== res
);
124 auto bmh_res
= bmh(haystack
, he
);
125 VERIFY( bmh_res
== res
);