| 1 | // { dg-options "-fchar8_t" } |
| 2 | // { dg-do run { target c++17 } } |
| 3 | |
| 4 | // Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | #include <string_view> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | void |
| 25 | test01() |
| 26 | { |
| 27 | using namespace std::literals::string_view_literals; |
| 28 | |
| 29 | std::string_view planet = "Mercury"sv; |
| 30 | std::wstring_view wplanet = L"Venus"sv; |
| 31 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 32 | std::u8string_view u8planet = u8"Mars"sv; |
| 33 | #else |
| 34 | std::string_view u8planet = u8"Mars"sv; |
| 35 | #endif |
| 36 | std::u16string_view u16planet = u"Jupiter"sv; |
| 37 | std::u32string_view u32planet = U"Saturn"sv; |
| 38 | |
| 39 | VERIFY( planet == std::string_view("Mercury") ); |
| 40 | VERIFY( wplanet == std::wstring_view(L"Venus") ); |
| 41 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 42 | VERIFY( u8planet == std::u8string_view(u8"Mars") ); |
| 43 | #else |
| 44 | VERIFY( u8planet == std::string_view(u8"Mars") ); |
| 45 | #endif |
| 46 | VERIFY( u16planet == std::u16string_view(u"Jupiter") ); |
| 47 | VERIFY( u32planet == std::u32string_view(U"Saturn") ); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | test02() |
| 52 | { |
| 53 | using namespace std::literals::string_view_literals; |
| 54 | |
| 55 | std::string_view planet_cratered = "Mercury\0cratered"sv; |
| 56 | std::wstring_view wplanet_cratered = L"Venus\0cratered"sv; |
| 57 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 58 | std::u8string_view u8planet_cratered = u8"Mars\0cratered"sv; |
| 59 | #else |
| 60 | std::string_view u8planet_cratered = u8"Mars\0cratered"sv; |
| 61 | #endif |
| 62 | std::u16string_view u16planet_cratered = u"Jupiter\0cratered"sv; |
| 63 | std::u32string_view u32planet_cratered = U"Saturn\0cratered"sv; |
| 64 | |
| 65 | VERIFY( planet_cratered == std::string_view("Mercury\0cratered", 16) ); |
| 66 | VERIFY( wplanet_cratered == std::wstring_view(L"Venus\0cratered", 14) ); |
| 67 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 68 | VERIFY( u8planet_cratered == std::u8string_view(u8"Mars\0cratered", 13) ); |
| 69 | #else |
| 70 | VERIFY( u8planet_cratered == std::string_view(u8"Mars\0cratered", 13) ); |
| 71 | #endif |
| 72 | VERIFY( u16planet_cratered == std::u16string_view(u"Jupiter\0cratered", 16) ); |
| 73 | VERIFY( u32planet_cratered == std::u32string_view(U"Saturn\0cratered", 15) ); |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | main() |
| 78 | { |
| 79 | test01(); |
| 80 | test02(); |
| 81 | } |