]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/79980.cc
1 // Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++11 } }
23 #include <testsuite_hooks.h>
25 using std::wstring_convert
;
26 using std::codecvt_utf8
;
31 std::string src
= (const char*)u8
"1234\U00001111\U0001ffff";
32 wstring_convert
<codecvt_utf8
<char16_t
>, char16_t
> c("bad", u
"BAD");
34 // utf-8 to ucs2 conversion should fail on character outside BMP
35 auto ucs2
= c
.from_bytes(src
);
36 VERIFY( ucs2
== u
"BAD" );
37 VERIFY( c
.converted() == 7 );
39 // ucs2 to utf-8 conversion should fail on invalid ucs2 input:
40 std::u16string utf16
= u
"1234\U00001111\U0001ffff";
41 auto out
= c
.to_bytes(utf16
);
42 VERIFY( out
== "bad" );
43 VERIFY( c
.converted() == 5 );
45 // And should also fail on incomplete surrogate pair (not return partial):
46 out
= c
.to_bytes(utf16
.substr(0, utf16
.size()-1));
47 VERIFY( out
== "bad" );
48 VERIFY( c
.converted() == 5 );
54 std::string src
= (const char*)u8
"1234\U00001111\U0001ffff";
55 wstring_convert
<codecvt_utf8
<char16_t
, 0x1000>, char16_t
> c("bad", u
"BAD");
57 // utf-8 to ucs2 conversion should fail on character above Maxcode=0x1000
58 auto ucs2
= c
.from_bytes(src
);
59 VERIFY( ucs2
== u
"BAD" );
60 VERIFY( c
.converted() == 4 );
66 std::string src
= (const char*)u8
"1234\U00001111\U0001ffff";
67 wstring_convert
<codecvt_utf8
<char32_t
, 0x10000>, char32_t
> c("bad", U
"BAD");
69 // utf-8 to ucs4 conversion should fail on character above Maxcode=0x10000
70 auto ucs4
= c
.from_bytes(src
);
71 VERIFY( ucs4
== U
"BAD" );
72 VERIFY( c
.converted() == 7 );
78 std::string src
= (const char*)u8
"1234\U00001111\U0001ffff";
79 wstring_convert
<codecvt_utf8
<char32_t
, 0x1000>, char32_t
> c("bad", U
"BAD");
81 // utf-8 to ucs4 conversion should fail on character above Maxcode=0x1000
82 auto ucs4
= c
.from_bytes(src
);
83 VERIFY( ucs4
== U
"BAD" );
84 VERIFY( c
.converted() == 4 );