Daily bump.
[gcc.git] / libstdc++-v3 / testsuite / 22_locale / codecvt / codecvt_utf16 / 79980.cc
blob6cf0a66ef1a3884ac4dc935f3b5094bdadecaf2e
1 // Copyright (C) 2017-2025 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
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 } }
21 #include <locale>
22 #include <codecvt>
23 #include <testsuite_hooks.h>
25 // PR libstdc++/79980
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>
32 using Conv
33 = std::wstring_convert<std::codecvt_utf16<WCh, Max, mode(Mode)>, WCh>;
35 void
36 test01()
38 const char src[] = "\xFE\xFF\xAB\xCD";
39 Conv<char16_t> conv;
40 auto dst = conv.from_bytes(src, src+4);
41 VERIFY( dst[0] == 0xabcd );
44 void
45 test02()
47 const char src[] = "\xFF\xFE\xAB\xCD";
48 Conv<char16_t> conv;
49 auto dst = conv.from_bytes(src, src+4);
50 VERIFY( dst[0] == 0xcdab );
53 void
54 test03()
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 );
62 void
63 test04()
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 );
71 void
72 test05()
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 );
81 void
82 test06()
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 );
91 void
92 test07()
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 );
107 void
108 test08()
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) );
120 void
121 test09()
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) );
132 int main()
134 test01();
135 test02();
136 test03();
137 test04();
138 test05();
139 test06();
140 test07();
141 test08();
142 test09();