1 // Copyright (C) 2014-2016 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-options "-std=gnu++14" }
20 #include <experimental/functional>
22 #ifdef _GLIBCXX_USE_WCHAR_T
25 #include <testsuite_hooks.h>
27 using std::experimental::make_default_searcher
;
28 using std::experimental::make_boyer_moore_searcher
;
29 using std::experimental::make_boyer_moore_horspool_searcher
;
34 const char s
[] = { 'a', (char)-97, 'a', '\0' };
35 const char* needles
[] = {
36 s
, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd"
38 const char* haystacks
[] = {
39 s
, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd",
40 "aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz"
43 for (auto n
: needles
)
45 auto ne
= n
+ std::strlen(n
);
46 auto d
= make_default_searcher(n
, ne
);
47 auto bm
= make_boyer_moore_searcher(n
, ne
);
48 auto bmh
= make_boyer_moore_horspool_searcher(n
, ne
);
49 for (auto h
: haystacks
)
51 auto he
= h
+ std::strlen(h
);
52 auto res
= std::search(h
, he
, n
, ne
);
53 auto d_res
= d(h
, he
);
54 VERIFY( d_res
== res
);
55 auto bm_res
= bm(h
, he
);
56 VERIFY( bm_res
== res
);
57 auto bmh_res
= bmh(h
, he
);
58 VERIFY( bmh_res
== res
);
66 #ifdef _GLIBCXX_USE_WCHAR_T
67 const wchar_t s
[] = { L
'a', (wchar_t)-97, L
'a', L
'\0' };
68 const wchar_t* needles
[] = {
69 s
, L
"", L
"a", L
"aa", L
"aaa", L
"ab", L
"cd", L
"abcd", L
"abcdabcd", L
"abcabcd"
71 const wchar_t* haystacks
[] = {
72 s
, L
"", L
"a", L
"aa", L
"aaa", L
"ab", L
"cd", L
"abcd", L
"abcdabcd", L
"abcabcd",
73 L
"aaaaaaa", L
"aabaa", L
"aaacab", L
"cdabcdab", L
"abcdabcd", L
"xyzabcdxyz"
76 for (auto n
: needles
)
78 auto ne
= n
+ std::wcslen(n
);
79 auto d
= make_default_searcher(n
, ne
);
80 auto bm
= make_boyer_moore_searcher(n
, ne
);
81 auto bmh
= make_boyer_moore_horspool_searcher(n
, ne
);
82 for (auto h
: haystacks
)
84 auto he
= h
+ std::wcslen(h
);
85 auto res
= std::search(h
, he
, n
, ne
);
86 auto d_res
= d(h
, he
);
87 VERIFY( d_res
== res
);
88 auto bm_res
= bm(h
, he
);
89 VERIFY( bm_res
== res
);
90 auto bmh_res
= bmh(h
, he
);
91 VERIFY( bmh_res
== res
);
104 norm(unsigned char c
) { return std::isalnum(c
) ? c
: '#'; }
107 bool operator()(char l
, char r
) const { return norm(l
) == norm(r
); }
110 std::size_t operator()(char c
) const { return std::hash
<char>{}(norm(c
)); }
113 const char* needle
= " foo 123 ";
114 const char* haystack
= "*****foo*123******";
115 const char* ne
= needle
+ std::strlen(needle
);
116 const char* he
= haystack
+ std::strlen(haystack
);
118 auto d
= make_default_searcher(needle
, ne
, eq
);
119 auto bm
= make_boyer_moore_searcher(needle
, ne
, eq
, eq
);
120 auto bmh
= make_boyer_moore_horspool_searcher(needle
, ne
, eq
, eq
);
122 auto res
= std::search(haystack
, he
, needle
, ne
, eq
);
123 auto d_res
= d(haystack
, he
);
124 VERIFY( d_res
== res
);
125 auto bm_res
= bm(haystack
, he
);
126 VERIFY( bm_res
== res
);
127 auto bmh_res
= bmh(haystack
, he
);
128 VERIFY( bmh_res
== res
);