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