| 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 |
|
| 26 |
template<typename Elem> |
| 27 |
struct cvt : std::codecvt<Elem, char, std::mbstate_t> { }; |
| 28 |
|
| 29 |
template<typename Elem> |
| 30 |
using str_conv = std::wstring_convert<cvt<Elem>, Elem>; |
| 31 |
// { dg-warning "deprecated" "" { target c++17 } 30 } |
| 32 |
|
| 33 |
using std::string; |
| 34 |
using std::u16string; |
| 35 |
using std::u32string; |
| 36 |
|
| 37 |
// test conversion errors, with and without error strings |
| 38 |
|
| 39 |
void test01() |
| 40 |
{ |
| 41 |
typedef str_conv<char> sc; |
| 42 |
|
| 43 |
const sc::byte_string berr = "invalid wide string"; |
| 44 |
const sc::wide_string werr = "invalid byte string"; |
| 45 |
|
| 46 |
sc c(berr, werr); |
| 47 |
string input = "Stop"; |
| 48 |
input += char(0xFF); |
| 49 |
string woutput = c.from_bytes(input); |
| 50 |
VERIFY( input == woutput ); // noconv case doesn't detect invalid input |
| 51 |
string winput = "Stop"; |
| 52 |
winput += char(0xFF); |
| 53 |
string output = c.to_bytes(winput); |
| 54 |
VERIFY( winput == output ); // noconv case doesn't detect invalid input |
| 55 |
} |
| 56 |
|
| 57 |
void test02() |
| 58 |
{ |
| 59 |
typedef str_conv<char16_t> sc; |
| 60 |
|
| 61 |
const sc::byte_string berr = "invalid wide string"; |
| 62 |
const sc::wide_string werr = u"invalid byte string"; |
| 63 |
|
| 64 |
sc c(berr, werr); |
| 65 |
string input = "Stop"; |
| 66 |
input += char(0xFF); |
| 67 |
u16string woutput = c.from_bytes(input); |
| 68 |
VERIFY( werr == woutput ); |
| 69 |
u16string winput = u"Stop"; |
| 70 |
winput += char16_t(0xDC00); |
| 71 |
string output = c.to_bytes(winput); |
| 72 |
VERIFY( berr == output ); |
| 73 |
} |
| 74 |
|
| 75 |
void test03() |
| 76 |
{ |
| 77 |
typedef str_conv<char32_t> sc; |
| 78 |
|
| 79 |
const sc::byte_string berr = "invalid wide string"; |
| 80 |
const sc::wide_string werr = U"invalid byte string"; |
| 81 |
|
| 82 |
sc c(berr, werr); |
| 83 |
string input = "Halt"; |
| 84 |
input += char(0xff); |
| 85 |
u32string woutput = c.from_bytes(input); |
| 86 |
VERIFY( werr == woutput ); |
| 87 |
u32string winput = U"Halt"; |
| 88 |
winput += char32_t(-1); |
| 89 |
string output = c.to_bytes(winput); |
| 90 |
VERIFY( berr == output ); |
| 91 |
} |
| 92 |
|
| 93 |
int main() |
| 94 |
{ |
| 95 |
test01(); |
| 96 |
test02(); |
| 97 |
test03(); |
| 98 |
} |