| 1 |
// { dg-do run { target c++11 } } |
| 2 |
|
| 3 |
// Copyright (C) 2015-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 |
// 22.3.3.2.2 String conversions |
| 21 |
|
| 22 |
#include <locale> |
| 23 |
#include <string> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
#include <testsuite_common_types.h> |
| 26 |
|
| 27 |
template<typename Elem> |
| 28 |
struct cvt : std::codecvt<Elem, char, std::mbstate_t> { }; |
| 29 |
|
| 30 |
template<typename Elem> |
| 31 |
using str_conv = std::wstring_convert<cvt<Elem>, Elem>; |
| 32 |
// { dg-warning "deprecated" "" { target c++17 } 31 } |
| 33 |
|
| 34 |
using std::string; |
| 35 |
using std::wstring; |
| 36 |
|
| 37 |
void test01() |
| 38 |
{ |
| 39 |
typedef str_conv<char> sc; // noconv |
| 40 |
sc c; |
| 41 |
string input = "King for a day..."; |
| 42 |
string output = c.from_bytes(input); |
| 43 |
VERIFY( input == output ); |
| 44 |
VERIFY( c.converted() == output.length() ); |
| 45 |
string roundtrip = c.to_bytes(output); |
| 46 |
VERIFY( input == roundtrip ); |
| 47 |
VERIFY( c.converted() == roundtrip.length() ); |
| 48 |
|
| 49 |
__gnu_test::implicitly_default_constructible test; |
| 50 |
test.operator()<sc>(); // P0935R0 |
| 51 |
} |
| 52 |
|
| 53 |
void test02() |
| 54 |
{ |
| 55 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 56 |
typedef str_conv<wchar_t> wsc; |
| 57 |
wsc c; |
| 58 |
string input = "Fool for a lifetime"; |
| 59 |
wstring output = c.from_bytes(input); |
| 60 |
VERIFY( c.converted() == output.length() ); |
| 61 |
VERIFY( L"Fool for a lifetime" == output ); |
| 62 |
string roundtrip = c.to_bytes(output); |
| 63 |
VERIFY( input == roundtrip ); |
| 64 |
VERIFY( c.converted() == roundtrip.length() ); |
| 65 |
|
| 66 |
VERIFY( c.from_bytes(input[0]) == output.substr(0, 1) ); |
| 67 |
VERIFY( c.from_bytes(input.c_str()) == output ); |
| 68 |
VERIFY( c.from_bytes(input.data(), input.data()+input.size()) == output ); |
| 69 |
|
| 70 |
VERIFY( c.to_bytes(output[0]) == input.substr(0, 1) ); |
| 71 |
VERIFY( c.to_bytes(output.c_str()) == input ); |
| 72 |
VERIFY( c.to_bytes(output.data(), output.data()+output.size()) == input ); |
| 73 |
|
| 74 |
__gnu_test::implicitly_default_constructible test; |
| 75 |
test.operator()<wsc>(); // P0935R0 |
| 76 |
#endif |
| 77 |
} |
| 78 |
|
| 79 |
int main() |
| 80 |
{ |
| 81 |
test01(); |
| 82 |
test02(); |
| 83 |
} |