1 // Copyright (C) 2017-2025 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 } }
19 // { dg-additional-options "-Wno-deprecated-declarations" { target c++17 } }
23 #include <testsuite_hooks.h>
27 constexpr std::codecvt_mode
mode(std::codecvt_mode m
)
28 { return static_cast<std::codecvt_mode
>(m
| std::consume_header
); }
30 template<typename WCh
, unsigned long Max
= 0x10FFFF,
31 std::codecvt_mode Mode
= std::consume_header
>
33 = std::wstring_convert
<std::codecvt_utf16
<WCh
, Max
, mode(Mode
)>, WCh
>;
38 const char src
[] = "\xFE\xFF\xAB\xCD";
40 auto dst
= conv
.from_bytes(src
, src
+4);
41 VERIFY( dst
[0] == 0xabcd );
47 const char src
[] = "\xFF\xFE\xAB\xCD";
49 auto dst
= conv
.from_bytes(src
, src
+4);
50 VERIFY( dst
[0] == 0xcdab );
56 const char src
[] = "\xFE\xFF\xAB\xCD";
57 Conv
<char16_t
, 0x10FFFF, std::little_endian
> conv
;
58 auto dst
= conv
.from_bytes(src
, src
+4);
59 VERIFY( dst
[0] == 0xabcd );
65 const char src
[] = "\xFF\xFE\xAB\xCD";
66 Conv
<char16_t
, 0x10FFFF, std::little_endian
> conv
;
67 auto dst
= conv
.from_bytes(src
, src
+4);
68 VERIFY( dst
[0] == 0xcdab );
74 const char src
[] = "\0\x61\xAB\xCD"; // character greater than 0x00FF
75 Conv
<char16_t
, 0xFF> conv("to_bytes failed", u
"from_bytes failed");
76 std::u16string result
= conv
.from_bytes(src
, src
+4);
77 VERIFY( result
== u
"from_bytes failed" );
78 VERIFY( conv
.converted() == 2 );
84 const char src
[] = "\0\x61\xAB\xCD";
85 Conv
<char16_t
> conv("to_bytes failed", u
"from_bytes failed");
86 std::u16string result
= conv
.from_bytes(src
, src
+3); // incomplete character
87 VERIFY( result
== u
"\u0061" );
88 VERIFY( conv
.converted() == 2 );
94 Conv
<char16_t
> conv("to_bytes failed", u
"from_bytes failed");
95 // ucs2 to utf-16 conversion should fail on invalid ucs2 input:
96 std::u16string utf16
= u
"1234\U00001111\U0001ffff";
97 auto out
= conv
.to_bytes(utf16
);
98 VERIFY( out
== "to_bytes failed" );
99 VERIFY( conv
.converted() == 5 );
101 // And should also fail on incomplete surrogate pair (not return partial):
102 out
= conv
.to_bytes(utf16
.substr(0, utf16
.size()-1));
103 VERIFY( out
== "to_bytes failed" );
104 VERIFY( conv
.converted() == 5 );
110 // Read/write UTF-16 code units from data not correctly aligned for char16_t
111 Conv
<char16_t
, 0x10FFFF, std::generate_header
> conv
;
112 const char src
[] = "-\xFE\xFF\0\x61\xAB\xCD";
113 auto out
= conv
.from_bytes(src
+ 1, src
+ 7);
114 VERIFY( out
[0] == 0x0061 );
115 VERIFY( out
[1] == 0xabcd );
116 auto bytes
= conv
.to_bytes(out
);
117 VERIFY( bytes
== std::string(src
+ 1, 6) );
123 // Read/write UTF-16 code units from data not correctly aligned for char16_t
124 Conv
<char32_t
, 0x10FFFF, std::generate_header
> conv
;
125 const char src
[] = "-\xFE\xFF\xD8\x08\xDF\x45";
126 auto out
= conv
.from_bytes(src
+ 1, src
+ 7);
127 VERIFY( out
== U
"\U00012345" );
128 auto bytes
= conv
.to_bytes(out
);
129 VERIFY( bytes
== std::string(src
+ 1, 6) );