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_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() {
44 // Nothing to do, the conversion in unsupported
47 template <class CharT
>
48 void TestHelper
<CharT
, 4>::test() {
50 typedef std::codecvt_utf16
<CharT
> C
;
54 const CharT
* wp
= nullptr;
57 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
58 assert(r
== std::codecvt_base::ok
);
61 assert(n
[0] == char(0xD8));
62 assert(n
[1] == char(0xC0));
63 assert(n
[2] == char(0xDC));
64 assert(n
[3] == char(0x03));
67 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
68 assert(r
== std::codecvt_base::ok
);
71 assert(n
[0] == char(0x10));
72 assert(n
[1] == char(0x05));
73 assert(n
[2] == char(0xDC));
74 assert(n
[3] == char(0x03));
77 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
78 assert(r
== std::codecvt_base::ok
);
81 assert(n
[0] == char(0x04));
82 assert(n
[1] == char(0x53));
83 assert(n
[2] == char(0xDC));
84 assert(n
[3] == char(0x03));
87 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
88 assert(r
== std::codecvt_base::ok
);
91 assert(n
[0] == char(0x00));
92 assert(n
[1] == char(0x56));
93 assert(n
[2] == char(0xDC));
94 assert(n
[3] == char(0x03));
97 typedef std::codecvt_utf16
<CharT
, 0x1000> C
;
101 const CharT
* wp
= nullptr;
104 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
105 assert(r
== std::codecvt_base::error
);
108 assert(n
[0] == char(0));
109 assert(n
[1] == char(0));
110 assert(n
[2] == char(0));
111 assert(n
[3] == char(0));
114 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
115 assert(r
== std::codecvt_base::error
);
118 assert(n
[0] == char(0));
119 assert(n
[1] == char(0));
120 assert(n
[2] == char(0));
121 assert(n
[3] == char(0));
124 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
125 assert(r
== std::codecvt_base::ok
);
126 assert(wp
== &w
+ 1);
128 assert(n
[0] == char(0x04));
129 assert(n
[1] == char(0x53));
130 assert(n
[2] == char(0));
131 assert(n
[3] == char(0));
134 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
135 assert(r
== std::codecvt_base::ok
);
136 assert(wp
== &w
+ 1);
138 assert(n
[0] == char(0x00));
139 assert(n
[1] == char(0x56));
140 assert(n
[2] == char(0));
141 assert(n
[3] == char(0));
144 typedef std::codecvt_utf16
<CharT
, 0x10ffff, std::generate_header
> C
;
148 const CharT
* wp
= nullptr;
151 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
152 assert(r
== std::codecvt_base::ok
);
153 assert(wp
== &w
+ 1);
155 assert(n
[0] == char(0xFE));
156 assert(n
[1] == char(0xFF));
157 assert(n
[2] == char(0xD8));
158 assert(n
[3] == char(0xC0));
159 assert(n
[4] == char(0xDC));
160 assert(n
[5] == char(0x03));
163 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
164 assert(r
== std::codecvt_base::ok
);
165 assert(wp
== &w
+ 1);
167 assert(n
[0] == char(0xFE));
168 assert(n
[1] == char(0xFF));
169 assert(n
[2] == char(0x10));
170 assert(n
[3] == char(0x05));
171 assert(n
[4] == char(0xDC));
172 assert(n
[5] == char(0x03));
175 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
176 assert(r
== std::codecvt_base::ok
);
177 assert(wp
== &w
+ 1);
179 assert(n
[0] == char(0xFE));
180 assert(n
[1] == char(0xFF));
181 assert(n
[2] == char(0x04));
182 assert(n
[3] == char(0x53));
183 assert(n
[4] == char(0xDC));
184 assert(n
[5] == char(0x03));
187 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
188 assert(r
== std::codecvt_base::ok
);
189 assert(wp
== &w
+ 1);
191 assert(n
[0] == char(0xFE));
192 assert(n
[1] == char(0xFF));
193 assert(n
[2] == char(0x00));
194 assert(n
[3] == char(0x56));
195 assert(n
[4] == char(0xDC));
196 assert(n
[5] == char(0x03));
200 typedef std::codecvt_utf16
<CharT
, 0x10FFFF, std::little_endian
> C
;
204 const CharT
* wp
= nullptr;
207 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
208 assert(r
== std::codecvt_base::ok
);
209 assert(wp
== &w
+ 1);
211 assert(n
[1] == char(0xD8));
212 assert(n
[0] == char(0xC0));
213 assert(n
[3] == char(0xDC));
214 assert(n
[2] == char(0x03));
217 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
218 assert(r
== std::codecvt_base::ok
);
219 assert(wp
== &w
+ 1);
221 assert(n
[1] == char(0x10));
222 assert(n
[0] == char(0x05));
223 assert(n
[3] == char(0xDC));
224 assert(n
[2] == char(0x03));
227 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
228 assert(r
== std::codecvt_base::ok
);
229 assert(wp
== &w
+ 1);
231 assert(n
[1] == char(0x04));
232 assert(n
[0] == char(0x53));
233 assert(n
[3] == char(0xDC));
234 assert(n
[2] == char(0x03));
237 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
238 assert(r
== std::codecvt_base::ok
);
239 assert(wp
== &w
+ 1);
241 assert(n
[1] == char(0x00));
242 assert(n
[0] == char(0x56));
243 assert(n
[3] == char(0xDC));
244 assert(n
[2] == char(0x03));
247 typedef std::codecvt_utf16
<CharT
, 0x1000, std::little_endian
> C
;
251 const CharT
* wp
= nullptr;
254 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
255 assert(r
== std::codecvt_base::error
);
258 assert(n
[1] == char(0));
259 assert(n
[0] == char(0));
260 assert(n
[3] == char(0));
261 assert(n
[2] == char(0));
264 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
265 assert(r
== std::codecvt_base::error
);
268 assert(n
[1] == char(0));
269 assert(n
[0] == char(0));
270 assert(n
[3] == char(0));
271 assert(n
[2] == char(0));
274 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
275 assert(r
== std::codecvt_base::ok
);
276 assert(wp
== &w
+ 1);
278 assert(n
[1] == char(0x04));
279 assert(n
[0] == char(0x53));
280 assert(n
[3] == char(0));
281 assert(n
[2] == char(0));
284 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
285 assert(r
== std::codecvt_base::ok
);
286 assert(wp
== &w
+ 1);
288 assert(n
[1] == char(0x00));
289 assert(n
[0] == char(0x56));
290 assert(n
[3] == char(0));
291 assert(n
[2] == char(0));
294 typedef std::codecvt_utf16
<CharT
, 0x10ffff,
295 std::codecvt_mode(std::generate_header
|
301 const CharT
* wp
= nullptr;
304 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
305 assert(r
== std::codecvt_base::ok
);
306 assert(wp
== &w
+ 1);
308 assert(n
[1] == char(0xFE));
309 assert(n
[0] == char(0xFF));
310 assert(n
[3] == char(0xD8));
311 assert(n
[2] == char(0xC0));
312 assert(n
[5] == char(0xDC));
313 assert(n
[4] == char(0x03));
316 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
317 assert(r
== std::codecvt_base::ok
);
318 assert(wp
== &w
+ 1);
320 assert(n
[1] == char(0xFE));
321 assert(n
[0] == char(0xFF));
322 assert(n
[3] == char(0x10));
323 assert(n
[2] == char(0x05));
324 assert(n
[5] == char(0xDC));
325 assert(n
[4] == char(0x03));
328 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
329 assert(r
== std::codecvt_base::ok
);
330 assert(wp
== &w
+ 1);
332 assert(n
[1] == char(0xFE));
333 assert(n
[0] == char(0xFF));
334 assert(n
[3] == char(0x04));
335 assert(n
[2] == char(0x53));
336 assert(n
[5] == char(0xDC));
337 assert(n
[4] == char(0x03));
340 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
341 assert(r
== std::codecvt_base::ok
);
342 assert(wp
== &w
+ 1);
344 assert(n
[1] == char(0xFE));
345 assert(n
[0] == char(0xFF));
346 assert(n
[3] == char(0x00));
347 assert(n
[2] == char(0x56));
348 assert(n
[5] == char(0xDC));
349 assert(n
[4] == char(0x03));
353 int main(int, char**) {
354 TestHelper
<char32_t
>::test();
355 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
356 TestHelper
<wchar_t>::test();