| 1 | // Copyright (C) 2014-2025 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 <experimental/string_view> |
| 22 | #include <cstring> |
| 23 | #include <algorithm> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | using std::experimental::make_default_searcher; |
| 27 | using std::experimental::make_boyer_moore_searcher; |
| 28 | using std::experimental::make_boyer_moore_horspool_searcher; |
| 29 | |
| 30 | void |
| 31 | test01() |
| 32 | { |
| 33 | const char s[] = { 'a', (char)-97, 'a', '\0' }; |
| 34 | const char* needles[] = { |
| 35 | s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd" |
| 36 | }; |
| 37 | const char* haystacks[] = { |
| 38 | s, "", "a", "aa", "aaa", "ab", "cd", "abcd", "abcdabcd", "abcabcd", |
| 39 | "aaaaaaa", "aabaa", "aaacab", "cdabcdab", "abcdabcd", "xyzabcdxyz" |
| 40 | }; |
| 41 | |
| 42 | for (auto n : needles) |
| 43 | { |
| 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) |
| 49 | { |
| 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 ); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | test02() |
| 64 | { |
| 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" |
| 68 | }; |
| 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" |
| 72 | }; |
| 73 | |
| 74 | for (auto n : needles) |
| 75 | { |
| 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) |
| 81 | { |
| 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 ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | test03() |
| 96 | { |
| 97 | // custom predicate |
| 98 | struct |
| 99 | { |
| 100 | static unsigned char |
| 101 | norm(unsigned char c) { return std::isalnum(c) ? c : '#'; } |
| 102 | |
| 103 | // equality |
| 104 | bool operator()(char l, char r) const { return norm(l) == norm(r); } |
| 105 | |
| 106 | // hash |
| 107 | std::size_t operator()(char c) const { return std::hash<char>{}(norm(c)); } |
| 108 | } eq; |
| 109 | |
| 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); |
| 114 | |
| 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); |
| 118 | |
| 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 ); |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | main() |
| 130 | { |
| 131 | test01(); |
| 132 | test02(); |
| 133 | test03(); |
| 134 | } |