1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_ENABLE_CXX26_REMOVED_CODECVT
13 // template <class Elem, unsigned long Maxcode = 0x10ffff,
14 // codecvt_mode Mode = (codecvt_mode)0>
15 // class codecvt_utf8_utf16
16 // : public codecvt<Elem, char, mbstate_t>
23 // const internT* from, const internT* from_end, const internT*& from_next,
24 // externT* to, externT* to_end, externT*& to_next) const;
29 #include "test_macros.h"
31 template <class CharT
, std::size_t = sizeof(CharT
)>
33 template <class CharT
>
34 struct TestHelper
<CharT
, 2> {
37 template <class CharT
>
38 struct TestHelper
<CharT
, 4> {
42 template <class CharT
>
43 void TestHelper
<CharT
, 2>::test() {
45 typedef std::codecvt_utf8_utf16
<CharT
> C
;
47 CharT w
[2] = {0xD8C0, 0xDC03};
49 const CharT
* wp
= nullptr;
52 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
53 assert(r
== std::codecvt_base::ok
);
56 assert(n
[0] == char(0xF1));
57 assert(n
[1] == char(0x80));
58 assert(n
[2] == char(0x80));
59 assert(n
[3] == char(0x83));
62 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
63 assert(r
== std::codecvt_base::ok
);
66 assert(n
[0] == char(0xE1));
67 assert(n
[1] == char(0x80));
68 assert(n
[2] == char(0x85));
71 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
72 assert(r
== std::codecvt_base::ok
);
75 assert(n
[0] == char(0xD1));
76 assert(n
[1] == char(0x93));
79 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
80 assert(r
== std::codecvt_base::ok
);
83 assert(n
[0] == char(0x56));
86 typedef std::codecvt_utf8_utf16
<CharT
, 0x1000> C
;
88 CharT w
[2] = {0xD8C0, 0xDC03};
90 const CharT
* wp
= nullptr;
93 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
94 assert(r
== std::codecvt_base::error
);
99 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
100 assert(r
== std::codecvt_base::error
);
105 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
106 assert(r
== std::codecvt_base::ok
);
109 assert(n
[0] == char(0xD1));
110 assert(n
[1] == char(0x93));
113 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
114 assert(r
== std::codecvt_base::ok
);
117 assert(n
[0] == char(0x56));
120 typedef std::codecvt_utf8_utf16
<CharT
, 0x10ffff, std::generate_header
> C
;
122 CharT w
[2] = {0xD8C0, 0xDC03};
124 const CharT
* wp
= nullptr;
127 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 7, np
);
128 assert(r
== std::codecvt_base::ok
);
131 assert(n
[0] == char(0xEF));
132 assert(n
[1] == char(0xBB));
133 assert(n
[2] == char(0xBF));
134 assert(n
[3] == char(0xF1));
135 assert(n
[4] == char(0x80));
136 assert(n
[5] == char(0x80));
137 assert(n
[6] == char(0x83));
140 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
141 assert(r
== std::codecvt_base::ok
);
144 assert(n
[0] == char(0xEF));
145 assert(n
[1] == char(0xBB));
146 assert(n
[2] == char(0xBF));
147 assert(n
[3] == char(0xE1));
148 assert(n
[4] == char(0x80));
149 assert(n
[5] == char(0x85));
152 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
153 assert(r
== std::codecvt_base::ok
);
156 assert(n
[0] == char(0xEF));
157 assert(n
[1] == char(0xBB));
158 assert(n
[2] == char(0xBF));
159 assert(n
[3] == char(0xD1));
160 assert(n
[4] == char(0x93));
163 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
164 assert(r
== std::codecvt_base::ok
);
167 assert(n
[0] == char(0xEF));
168 assert(n
[1] == char(0xBB));
169 assert(n
[2] == char(0xBF));
170 assert(n
[3] == char(0x56));
174 template <class CharT
>
175 void TestHelper
<CharT
, 4>::test() {
177 typedef std::codecvt_utf8_utf16
<CharT
> C
;
179 CharT w
[2] = {0xD8C0, 0xDC03};
181 const CharT
* wp
= nullptr;
184 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
185 assert(r
== std::codecvt_base::ok
);
188 assert(n
[0] == char(0xF1));
189 assert(n
[1] == char(0x80));
190 assert(n
[2] == char(0x80));
191 assert(n
[3] == char(0x83));
194 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
195 assert(r
== std::codecvt_base::ok
);
198 assert(n
[0] == char(0xE1));
199 assert(n
[1] == char(0x80));
200 assert(n
[2] == char(0x85));
203 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
204 assert(r
== std::codecvt_base::ok
);
207 assert(n
[0] == char(0xD1));
208 assert(n
[1] == char(0x93));
211 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
212 assert(r
== std::codecvt_base::ok
);
215 assert(n
[0] == char(0x56));
218 typedef std::codecvt_utf8_utf16
<CharT
, 0x1000> C
;
220 CharT w
[2] = {0xD8C0, 0xDC03};
222 const CharT
* wp
= nullptr;
225 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
226 assert(r
== std::codecvt_base::error
);
231 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
232 assert(r
== std::codecvt_base::error
);
237 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
238 assert(r
== std::codecvt_base::ok
);
241 assert(n
[0] == char(0xD1));
242 assert(n
[1] == char(0x93));
245 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
246 assert(r
== std::codecvt_base::ok
);
249 assert(n
[0] == char(0x56));
252 typedef std::codecvt_utf8_utf16
<CharT
, 0x10ffff, std::generate_header
> C
;
254 CharT w
[2] = {0xD8C0, 0xDC03};
256 const CharT
* wp
= nullptr;
259 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 7, np
);
260 assert(r
== std::codecvt_base::ok
);
263 assert(n
[0] == char(0xEF));
264 assert(n
[1] == char(0xBB));
265 assert(n
[2] == char(0xBF));
266 assert(n
[3] == char(0xF1));
267 assert(n
[4] == char(0x80));
268 assert(n
[5] == char(0x80));
269 assert(n
[6] == char(0x83));
272 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
273 assert(r
== std::codecvt_base::ok
);
276 assert(n
[0] == char(0xEF));
277 assert(n
[1] == char(0xBB));
278 assert(n
[2] == char(0xBF));
279 assert(n
[3] == char(0xE1));
280 assert(n
[4] == char(0x80));
281 assert(n
[5] == char(0x85));
284 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
285 assert(r
== std::codecvt_base::ok
);
288 assert(n
[0] == char(0xEF));
289 assert(n
[1] == char(0xBB));
290 assert(n
[2] == char(0xBF));
291 assert(n
[3] == char(0xD1));
292 assert(n
[4] == char(0x93));
295 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
296 assert(r
== std::codecvt_base::ok
);
299 assert(n
[0] == char(0xEF));
300 assert(n
[1] == char(0xBB));
301 assert(n
[2] == char(0xBF));
302 assert(n
[3] == char(0x56));
306 int main(int, char**) {
307 #if !defined(_WIN32) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
308 TestHelper
<wchar_t>::test();
310 TestHelper
<char32_t
>::test();
311 TestHelper
<char16_t
>::test();