| 1 | // { dg-do run { target c++17 } } |
| 2 | |
| 3 | // Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // basic_string_view find |
| 21 | |
| 22 | #include <string_view> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | void |
| 26 | test01() |
| 27 | { |
| 28 | typedef std::wstring_view::size_type csize_type; |
| 29 | typedef std::wstring_view::const_reference cref; |
| 30 | typedef std::wstring_view::reference ref; |
| 31 | csize_type npos = std::wstring_view::npos; |
| 32 | csize_type csz01, csz02; |
| 33 | |
| 34 | const wchar_t str_lit01[] = L"mave"; |
| 35 | const std::wstring_view str01(L"mavericks, santa cruz"); |
| 36 | std::wstring_view str02(str_lit01); |
| 37 | std::wstring_view str03(L"s, s"); |
| 38 | std::wstring_view str04; |
| 39 | |
| 40 | // size_type find(const wstring_view&, size_type pos = 0) const; |
| 41 | csz01 = str01.find(str01); |
| 42 | VERIFY( csz01 == 0 ); |
| 43 | csz01 = str01.find(str01, 4); |
| 44 | VERIFY( csz01 == npos ); |
| 45 | csz01 = str01.find(str02, 0); |
| 46 | VERIFY( csz01 == 0 ); |
| 47 | csz01 = str01.find(str02, 3); |
| 48 | VERIFY( csz01 == npos ); |
| 49 | csz01 = str01.find(str03, 0); |
| 50 | VERIFY( csz01 == 8 ); |
| 51 | csz01 = str01.find(str03, 3); |
| 52 | VERIFY( csz01 == 8 ); |
| 53 | csz01 = str01.find(str03, 12); |
| 54 | VERIFY( csz01 == npos ); |
| 55 | |
| 56 | // An empty string_view consists of no characters |
| 57 | // therefore it should be found at every point in a string_view, |
| 58 | // except beyond the end |
| 59 | csz01 = str01.find(str04, 0); |
| 60 | VERIFY( csz01 == 0 ); |
| 61 | csz01 = str01.find(str04, 5); |
| 62 | VERIFY( csz01 == 5 ); |
| 63 | csz01 = str01.find(str04, str01.size()); |
| 64 | VERIFY( csz01 == str01.size() ); |
| 65 | csz01 = str01.find(str04, str01.size()+1); |
| 66 | VERIFY( csz01 == npos ); |
| 67 | |
| 68 | // size_type find(const wchar_t* s, size_type pos, size_type n) const; |
| 69 | csz01 = str01.find(str_lit01, 0, 3); |
| 70 | VERIFY( csz01 == 0 ); |
| 71 | csz01 = str01.find(str_lit01, 3, 0); |
| 72 | VERIFY( csz01 == 3 ); |
| 73 | |
| 74 | // size_type find(const wchar_t* s, size_type pos = 0) const; |
| 75 | csz01 = str01.find(str_lit01); |
| 76 | VERIFY( csz01 == 0 ); |
| 77 | csz01 = str01.find(str_lit01, 3); |
| 78 | VERIFY( csz01 == npos ); |
| 79 | |
| 80 | // size_type find(wchar_t c, size_type pos = 0) const; |
| 81 | csz01 = str01.find(L'z'); |
| 82 | csz02 = str01.size() - 1; |
| 83 | VERIFY( csz01 == csz02 ); |
| 84 | csz01 = str01.find(L'/'); |
| 85 | VERIFY( csz01 == npos ); |
| 86 | } |
| 87 | |
| 88 | constexpr bool |
| 89 | test02() |
| 90 | { |
| 91 | typedef std::wstring_view::size_type csize_type; |
| 92 | typedef std::wstring_view::const_reference cref; |
| 93 | typedef std::wstring_view::reference ref; |
| 94 | csize_type npos = std::wstring_view::npos; |
| 95 | csize_type csz01 = 0, csz02 = 0; |
| 96 | |
| 97 | const wchar_t str_lit01[] = L"mave"; |
| 98 | const std::wstring_view str01(L"mavericks, santa cruz"); |
| 99 | std::wstring_view str02(str_lit01); |
| 100 | std::wstring_view str03(L"s, s"); |
| 101 | std::wstring_view str04; |
| 102 | |
| 103 | #undef VERIFY |
| 104 | #define VERIFY(x) if(!(x)) return false |
| 105 | |
| 106 | // size_type find(const wstring_view&, size_type pos = 0) const; |
| 107 | csz01 = str01.find(str01); |
| 108 | VERIFY( csz01 == 0 ); |
| 109 | csz01 = str01.find(str01, 4); |
| 110 | VERIFY( csz01 == npos ); |
| 111 | csz01 = str01.find(str02, 0); |
| 112 | VERIFY( csz01 == 0 ); |
| 113 | csz01 = str01.find(str02, 3); |
| 114 | VERIFY( csz01 == npos ); |
| 115 | csz01 = str01.find(str03, 0); |
| 116 | VERIFY( csz01 == 8 ); |
| 117 | csz01 = str01.find(str03, 3); |
| 118 | VERIFY( csz01 == 8 ); |
| 119 | csz01 = str01.find(str03, 12); |
| 120 | VERIFY( csz01 == npos ); |
| 121 | |
| 122 | // An empty string_view consists of no characters |
| 123 | // therefore it should be found at every point in a string_view, |
| 124 | // except beyond the end |
| 125 | csz01 = str01.find(str04, 0); |
| 126 | VERIFY( csz01 == 0 ); |
| 127 | csz01 = str01.find(str04, 5); |
| 128 | VERIFY( csz01 == 5 ); |
| 129 | csz01 = str01.find(str04, str01.size()); |
| 130 | VERIFY( csz01 == str01.size() ); |
| 131 | csz01 = str01.find(str04, str01.size()+1); |
| 132 | VERIFY( csz01 == npos ); |
| 133 | |
| 134 | // size_type find(const wchar_t* s, size_type pos, size_type n) const; |
| 135 | csz01 = str01.find(str_lit01, 0, 3); |
| 136 | VERIFY( csz01 == 0 ); |
| 137 | csz01 = str01.find(str_lit01, 3, 0); |
| 138 | VERIFY( csz01 == 3 ); |
| 139 | |
| 140 | // size_type find(const wchar_t* s, size_type pos = 0) const; |
| 141 | csz01 = str01.find(str_lit01); |
| 142 | VERIFY( csz01 == 0 ); |
| 143 | csz01 = str01.find(str_lit01, 3); |
| 144 | VERIFY( csz01 == npos ); |
| 145 | |
| 146 | // size_type find(wchar_t c, size_type pos = 0) const; |
| 147 | csz01 = str01.find(L'z'); |
| 148 | csz02 = str01.size() - 1; |
| 149 | VERIFY( csz01 == csz02 ); |
| 150 | csz01 = str01.find(L'/'); |
| 151 | VERIFY( csz01 == npos ); |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | int |
| 157 | main() |
| 158 | { |
| 159 | test01(); |
| 160 | static_assert( test02() ); |
| 161 | |
| 162 | return 0; |
| 163 | } |