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 // template <class Elem, unsigned long Maxcode = 0x10ffff,
12 // codecvt_mode Mode = (codecvt_mode)0>
13 // class codecvt_utf16
14 // : public codecvt<Elem, char, mbstate_t>
21 // const internT* from, const internT* from_end, const internT*& from_next,
22 // externT* to, externT* to_end, externT*& to_next) const;
27 #include "test_macros.h"
29 template <class CharT
, size_t = sizeof(CharT
)>
31 template <class CharT
>
32 struct TestHelper
<CharT
, 2> {
35 template <class CharT
>
36 struct TestHelper
<CharT
, 4> {
40 template <class CharT
>
41 void TestHelper
<CharT
, 2>::test() {
42 // Nothing to do, the conversion in unsupported
45 template <class CharT
>
46 void TestHelper
<CharT
, 4>::test() {
48 typedef std::codecvt_utf16
<CharT
> C
;
52 const CharT
* wp
= nullptr;
55 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
56 assert(r
== std::codecvt_base::ok
);
59 assert(n
[0] == char(0xD8));
60 assert(n
[1] == char(0xC0));
61 assert(n
[2] == char(0xDC));
62 assert(n
[3] == char(0x03));
65 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
66 assert(r
== std::codecvt_base::ok
);
69 assert(n
[0] == char(0x10));
70 assert(n
[1] == char(0x05));
71 assert(n
[2] == char(0xDC));
72 assert(n
[3] == char(0x03));
75 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
76 assert(r
== std::codecvt_base::ok
);
79 assert(n
[0] == char(0x04));
80 assert(n
[1] == char(0x53));
81 assert(n
[2] == char(0xDC));
82 assert(n
[3] == char(0x03));
85 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
86 assert(r
== std::codecvt_base::ok
);
89 assert(n
[0] == char(0x00));
90 assert(n
[1] == char(0x56));
91 assert(n
[2] == char(0xDC));
92 assert(n
[3] == char(0x03));
95 typedef std::codecvt_utf16
<CharT
, 0x1000> C
;
99 const CharT
* wp
= nullptr;
102 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
103 assert(r
== std::codecvt_base::error
);
106 assert(n
[0] == char(0));
107 assert(n
[1] == char(0));
108 assert(n
[2] == char(0));
109 assert(n
[3] == char(0));
112 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
113 assert(r
== std::codecvt_base::error
);
116 assert(n
[0] == char(0));
117 assert(n
[1] == char(0));
118 assert(n
[2] == char(0));
119 assert(n
[3] == char(0));
122 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
123 assert(r
== std::codecvt_base::ok
);
124 assert(wp
== &w
+ 1);
126 assert(n
[0] == char(0x04));
127 assert(n
[1] == char(0x53));
128 assert(n
[2] == char(0));
129 assert(n
[3] == char(0));
132 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
133 assert(r
== std::codecvt_base::ok
);
134 assert(wp
== &w
+ 1);
136 assert(n
[0] == char(0x00));
137 assert(n
[1] == char(0x56));
138 assert(n
[2] == char(0));
139 assert(n
[3] == char(0));
142 typedef std::codecvt_utf16
<CharT
, 0x10ffff, std::generate_header
> C
;
146 const CharT
* wp
= nullptr;
149 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
150 assert(r
== std::codecvt_base::ok
);
151 assert(wp
== &w
+ 1);
153 assert(n
[0] == char(0xFE));
154 assert(n
[1] == char(0xFF));
155 assert(n
[2] == char(0xD8));
156 assert(n
[3] == char(0xC0));
157 assert(n
[4] == char(0xDC));
158 assert(n
[5] == char(0x03));
161 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
162 assert(r
== std::codecvt_base::ok
);
163 assert(wp
== &w
+ 1);
165 assert(n
[0] == char(0xFE));
166 assert(n
[1] == char(0xFF));
167 assert(n
[2] == char(0x10));
168 assert(n
[3] == char(0x05));
169 assert(n
[4] == char(0xDC));
170 assert(n
[5] == char(0x03));
173 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
174 assert(r
== std::codecvt_base::ok
);
175 assert(wp
== &w
+ 1);
177 assert(n
[0] == char(0xFE));
178 assert(n
[1] == char(0xFF));
179 assert(n
[2] == char(0x04));
180 assert(n
[3] == char(0x53));
181 assert(n
[4] == char(0xDC));
182 assert(n
[5] == char(0x03));
185 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
186 assert(r
== std::codecvt_base::ok
);
187 assert(wp
== &w
+ 1);
189 assert(n
[0] == char(0xFE));
190 assert(n
[1] == char(0xFF));
191 assert(n
[2] == char(0x00));
192 assert(n
[3] == char(0x56));
193 assert(n
[4] == char(0xDC));
194 assert(n
[5] == char(0x03));
198 typedef std::codecvt_utf16
<CharT
, 0x10FFFF, std::little_endian
> C
;
202 const CharT
* wp
= nullptr;
205 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
206 assert(r
== std::codecvt_base::ok
);
207 assert(wp
== &w
+ 1);
209 assert(n
[1] == char(0xD8));
210 assert(n
[0] == char(0xC0));
211 assert(n
[3] == char(0xDC));
212 assert(n
[2] == char(0x03));
215 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
216 assert(r
== std::codecvt_base::ok
);
217 assert(wp
== &w
+ 1);
219 assert(n
[1] == char(0x10));
220 assert(n
[0] == char(0x05));
221 assert(n
[3] == char(0xDC));
222 assert(n
[2] == char(0x03));
225 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
226 assert(r
== std::codecvt_base::ok
);
227 assert(wp
== &w
+ 1);
229 assert(n
[1] == char(0x04));
230 assert(n
[0] == char(0x53));
231 assert(n
[3] == char(0xDC));
232 assert(n
[2] == char(0x03));
235 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
236 assert(r
== std::codecvt_base::ok
);
237 assert(wp
== &w
+ 1);
239 assert(n
[1] == char(0x00));
240 assert(n
[0] == char(0x56));
241 assert(n
[3] == char(0xDC));
242 assert(n
[2] == char(0x03));
245 typedef std::codecvt_utf16
<CharT
, 0x1000, std::little_endian
> C
;
249 const CharT
* wp
= nullptr;
252 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
253 assert(r
== std::codecvt_base::error
);
256 assert(n
[1] == char(0));
257 assert(n
[0] == char(0));
258 assert(n
[3] == char(0));
259 assert(n
[2] == char(0));
262 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
263 assert(r
== std::codecvt_base::error
);
266 assert(n
[1] == char(0));
267 assert(n
[0] == char(0));
268 assert(n
[3] == char(0));
269 assert(n
[2] == char(0));
272 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
273 assert(r
== std::codecvt_base::ok
);
274 assert(wp
== &w
+ 1);
276 assert(n
[1] == char(0x04));
277 assert(n
[0] == char(0x53));
278 assert(n
[3] == char(0));
279 assert(n
[2] == char(0));
282 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
283 assert(r
== std::codecvt_base::ok
);
284 assert(wp
== &w
+ 1);
286 assert(n
[1] == char(0x00));
287 assert(n
[0] == char(0x56));
288 assert(n
[3] == char(0));
289 assert(n
[2] == char(0));
292 typedef std::codecvt_utf16
<CharT
, 0x10ffff,
293 std::codecvt_mode(std::generate_header
|
299 const CharT
* wp
= nullptr;
302 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
303 assert(r
== std::codecvt_base::ok
);
304 assert(wp
== &w
+ 1);
306 assert(n
[1] == char(0xFE));
307 assert(n
[0] == char(0xFF));
308 assert(n
[3] == char(0xD8));
309 assert(n
[2] == char(0xC0));
310 assert(n
[5] == char(0xDC));
311 assert(n
[4] == char(0x03));
314 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
315 assert(r
== std::codecvt_base::ok
);
316 assert(wp
== &w
+ 1);
318 assert(n
[1] == char(0xFE));
319 assert(n
[0] == char(0xFF));
320 assert(n
[3] == char(0x10));
321 assert(n
[2] == char(0x05));
322 assert(n
[5] == char(0xDC));
323 assert(n
[4] == char(0x03));
326 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
327 assert(r
== std::codecvt_base::ok
);
328 assert(wp
== &w
+ 1);
330 assert(n
[1] == char(0xFE));
331 assert(n
[0] == char(0xFF));
332 assert(n
[3] == char(0x04));
333 assert(n
[2] == char(0x53));
334 assert(n
[5] == char(0xDC));
335 assert(n
[4] == char(0x03));
338 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 6, np
);
339 assert(r
== std::codecvt_base::ok
);
340 assert(wp
== &w
+ 1);
342 assert(n
[1] == char(0xFE));
343 assert(n
[0] == char(0xFF));
344 assert(n
[3] == char(0x00));
345 assert(n
[2] == char(0x56));
346 assert(n
[5] == char(0xDC));
347 assert(n
[4] == char(0x03));
351 int main(int, char**) {
352 TestHelper
<char32_t
>::test();
353 TestHelper
<wchar_t>::test();