1 // Copyright (C) 2014-2021 Free Software Foundation, Inc.
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 
18 // { dg-do run { target c++14 } }
19 
20 #include <experimental/functional>
21 #include <cstring>
22 #ifdef _GLIBCXX_USE_WCHAR_T
23 # include <cwchar>
24 #endif
25 #include <algorithm>
26 #include <testsuite_hooks.h>
27 
28 using std::experimental::make_default_searcher;
29 using std::experimental::make_boyer_moore_searcher;
30 using std::experimental::make_boyer_moore_horspool_searcher;
31 
32 void
test01()33 test01()
34 {
35   const char s[] = { 'a', (char)-97, 'a', '\0' };
36   const char* needles[] = {
37     s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd"
38   };
39   const char* haystacks[] = {
40     s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd",
41     "aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz"
42   };
43 
44   for (auto n : needles)
45   {
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)
51     {
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 );
60     }
61   }
62 }
63 
64 void
test02()65 test02()
66 {
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"
71   };
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"
75   };
76 
77   for (auto n : needles)
78   {
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)
84     {
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 );
93     }
94   }
95 #endif
96 }
97 
98 void
test03()99 test03()
100 {
101   // custom predicate
102   struct
103   {
104     static unsigned char
105     norm(unsigned char c) { return std::isalnum(c) ? c : '#'; }
106 
107     // equality
108     bool operator()(char l, char r) const { return norm(l) == norm(r); }
109 
110     // hash
111     std::size_t operator()(char c) const { return std::hash<char>{}(norm(c)); }
112   } eq;
113 
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);
118 
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);
122 
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 );
130 }
131 
132 int
main()133 main()
134 {
135   test01();
136   test02();
137   test03();
138 }
139