107163879Schristos // { dg-options "-std=gnu++17" }
207163879Schristos 
3*1424dfb3Schristos // Copyright (C) 2013-2020 Free Software Foundation, Inc.
407163879Schristos //
507163879Schristos // This file is part of the GNU ISO C++ Library.  This library is free
607163879Schristos // software; you can redistribute it and/or modify it under the
707163879Schristos // terms of the GNU General Public License as published by the
807163879Schristos // Free Software Foundation; either version 3, or (at your option)
907163879Schristos // any later version.
1007163879Schristos 
1107163879Schristos // This library is distributed in the hope that it will be useful,
1207163879Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
1307163879Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1407163879Schristos // GNU General Public License for more details.
1507163879Schristos 
1607163879Schristos // You should have received a copy of the GNU General Public License along
1707163879Schristos // with this library; see the file COPYING3.  If not see
1807163879Schristos // <http://www.gnu.org/licenses/>.
1907163879Schristos 
2007163879Schristos // basic_string_view find
2107163879Schristos 
2207163879Schristos #include <string_view>
2307163879Schristos #include <testsuite_hooks.h>
2407163879Schristos 
2507163879Schristos void
test01()2607163879Schristos test01()
2707163879Schristos {
2807163879Schristos   typedef std::wstring_view::size_type csize_type;
2907163879Schristos   typedef std::wstring_view::const_reference cref;
3007163879Schristos   typedef std::wstring_view::reference ref;
3107163879Schristos   csize_type npos = std::wstring_view::npos;
3207163879Schristos   csize_type csz01, csz02;
3307163879Schristos 
3407163879Schristos   const wchar_t str_lit01[] = L"mave";
3507163879Schristos   const std::wstring_view str01(L"mavericks, santa cruz");
3607163879Schristos   std::wstring_view str02(str_lit01);
3707163879Schristos   std::wstring_view str03(L"s, s");
3807163879Schristos   std::wstring_view str04;
3907163879Schristos 
4007163879Schristos   // size_type find(const wstring_view&, size_type pos = 0) const;
4107163879Schristos   csz01 = str01.find(str01);
4207163879Schristos   VERIFY( csz01 == 0 );
4307163879Schristos   csz01 = str01.find(str01, 4);
4407163879Schristos   VERIFY( csz01 == npos );
4507163879Schristos   csz01 = str01.find(str02, 0);
4607163879Schristos   VERIFY( csz01 == 0 );
4707163879Schristos   csz01 = str01.find(str02, 3);
4807163879Schristos   VERIFY( csz01 == npos );
4907163879Schristos   csz01 = str01.find(str03, 0);
5007163879Schristos   VERIFY( csz01 == 8 );
5107163879Schristos   csz01 = str01.find(str03, 3);
5207163879Schristos   VERIFY( csz01 == 8 );
5307163879Schristos   csz01 = str01.find(str03, 12);
5407163879Schristos   VERIFY( csz01 == npos );
5507163879Schristos 
5607163879Schristos   // An empty string_view consists of no characters
5707163879Schristos   // therefore it should be found at every point in a string_view,
5807163879Schristos   // except beyond the end
5907163879Schristos   csz01 = str01.find(str04, 0);
6007163879Schristos   VERIFY( csz01 == 0 );
6107163879Schristos   csz01 = str01.find(str04, 5);
6207163879Schristos   VERIFY( csz01 == 5 );
6307163879Schristos   csz01 = str01.find(str04, str01.size());
6407163879Schristos   VERIFY( csz01 == str01.size() );
6507163879Schristos   csz01 = str01.find(str04, str01.size()+1);
6607163879Schristos   VERIFY( csz01 == npos );
6707163879Schristos 
6807163879Schristos   // size_type find(const wchar_t* s, size_type pos, size_type n) const;
6907163879Schristos   csz01 = str01.find(str_lit01, 0, 3);
7007163879Schristos   VERIFY( csz01 == 0 );
7107163879Schristos   csz01 = str01.find(str_lit01, 3, 0);
7207163879Schristos   VERIFY( csz01 == 3 );
7307163879Schristos 
7407163879Schristos   // size_type find(const wchar_t* s, size_type pos = 0) const;
7507163879Schristos   csz01 = str01.find(str_lit01);
7607163879Schristos   VERIFY( csz01 == 0 );
7707163879Schristos   csz01 = str01.find(str_lit01, 3);
7807163879Schristos   VERIFY( csz01 == npos );
7907163879Schristos 
8007163879Schristos   // size_type find(wchar_t c, size_type pos = 0) const;
8107163879Schristos   csz01 = str01.find(L'z');
8207163879Schristos   csz02 = str01.size() - 1;
8307163879Schristos   VERIFY( csz01 == csz02 );
8407163879Schristos   csz01 = str01.find(L'/');
8507163879Schristos   VERIFY( csz01 == npos );
8607163879Schristos }
8707163879Schristos 
8807163879Schristos constexpr bool
test02()8907163879Schristos test02()
9007163879Schristos {
9107163879Schristos   typedef std::wstring_view::size_type csize_type;
9207163879Schristos   typedef std::wstring_view::const_reference cref;
9307163879Schristos   typedef std::wstring_view::reference ref;
9407163879Schristos   csize_type npos = std::wstring_view::npos;
9507163879Schristos   csize_type csz01 = 0, csz02 = 0;
9607163879Schristos 
9707163879Schristos   const wchar_t str_lit01[] = L"mave";
9807163879Schristos   const std::wstring_view str01(L"mavericks, santa cruz");
9907163879Schristos   std::wstring_view str02(str_lit01);
10007163879Schristos   std::wstring_view str03(L"s, s");
10107163879Schristos   std::wstring_view str04;
10207163879Schristos 
10307163879Schristos #undef VERIFY
10407163879Schristos #define VERIFY(x) if(!(x)) return false
10507163879Schristos 
10607163879Schristos   // size_type find(const wstring_view&, size_type pos = 0) const;
10707163879Schristos   csz01 = str01.find(str01);
10807163879Schristos   VERIFY( csz01 == 0 );
10907163879Schristos   csz01 = str01.find(str01, 4);
11007163879Schristos   VERIFY( csz01 == npos );
11107163879Schristos   csz01 = str01.find(str02, 0);
11207163879Schristos   VERIFY( csz01 == 0 );
11307163879Schristos   csz01 = str01.find(str02, 3);
11407163879Schristos   VERIFY( csz01 == npos );
11507163879Schristos   csz01 = str01.find(str03, 0);
11607163879Schristos   VERIFY( csz01 == 8 );
11707163879Schristos   csz01 = str01.find(str03, 3);
11807163879Schristos   VERIFY( csz01 == 8 );
11907163879Schristos   csz01 = str01.find(str03, 12);
12007163879Schristos   VERIFY( csz01 == npos );
12107163879Schristos 
12207163879Schristos   // An empty string_view consists of no characters
12307163879Schristos   // therefore it should be found at every point in a string_view,
12407163879Schristos   // except beyond the end
12507163879Schristos   csz01 = str01.find(str04, 0);
12607163879Schristos   VERIFY( csz01 == 0 );
12707163879Schristos   csz01 = str01.find(str04, 5);
12807163879Schristos   VERIFY( csz01 == 5 );
12907163879Schristos   csz01 = str01.find(str04, str01.size());
13007163879Schristos   VERIFY( csz01 == str01.size() );
13107163879Schristos   csz01 = str01.find(str04, str01.size()+1);
13207163879Schristos   VERIFY( csz01 == npos );
13307163879Schristos 
13407163879Schristos   // size_type find(const wchar_t* s, size_type pos, size_type n) const;
13507163879Schristos   csz01 = str01.find(str_lit01, 0, 3);
13607163879Schristos   VERIFY( csz01 == 0 );
13707163879Schristos   csz01 = str01.find(str_lit01, 3, 0);
13807163879Schristos   VERIFY( csz01 == 3 );
13907163879Schristos 
14007163879Schristos   // size_type find(const wchar_t* s, size_type pos = 0) const;
14107163879Schristos   csz01 = str01.find(str_lit01);
14207163879Schristos   VERIFY( csz01 == 0 );
14307163879Schristos   csz01 = str01.find(str_lit01, 3);
14407163879Schristos   VERIFY( csz01 == npos );
14507163879Schristos 
14607163879Schristos   // size_type find(wchar_t c, size_type pos = 0) const;
14707163879Schristos   csz01 = str01.find(L'z');
14807163879Schristos   csz02 = str01.size() - 1;
14907163879Schristos   VERIFY( csz01 == csz02 );
15007163879Schristos   csz01 = str01.find(L'/');
15107163879Schristos   VERIFY( csz01 == npos );
15207163879Schristos 
15307163879Schristos   return true;
15407163879Schristos }
15507163879Schristos 
15607163879Schristos int
main()15707163879Schristos main()
15807163879Schristos {
15907163879Schristos   test01();
16007163879Schristos   static_assert( test02() );
16107163879Schristos 
16207163879Schristos   return 0;
16307163879Schristos }
164