1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <codecvt>
10 
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>
15 // {
16 //     // unspecified
17 // };
18 
19 // int length(stateT& state, const externT* from, const externT* from_end,
20 //            size_t max) const;
21 
22 #include <codecvt>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 
main(int,char **)27 int main(int, char**)
28 {
29     {
30         typedef std::codecvt_utf16<wchar_t> C;
31         C c;
32         char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)};
33         std::mbstate_t m;
34         int r = c.length(m, n, n+4, 2);
35         assert(r == 4);
36 
37         n[0] = char(0x10);
38         n[1] = char(0x05);
39         r = c.length(m, n, n+2, 2);
40         assert(r == 2);
41 
42         n[0] = char(0x04);
43         n[1] = char(0x53);
44         r = c.length(m, n, n+2, 2);
45         assert(r == 2);
46 
47         n[0] = char(0x00);
48         n[1] = char(0x56);
49         r = c.length(m, n, n+2, 2);
50         assert(r == 2);
51     }
52     {
53         typedef std::codecvt_utf16<wchar_t, 0x1000> C;
54         C c;
55         char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)};
56         std::mbstate_t m;
57         int r = c.length(m, n, n+4, 2);
58         assert(r == 0);
59 
60         n[0] = char(0x10);
61         n[1] = char(0x05);
62         r = c.length(m, n, n+2, 2);
63         assert(r == 0);
64 
65         n[0] = char(0x04);
66         n[1] = char(0x53);
67         r = c.length(m, n, n+2, 2);
68         assert(r == 2);
69 
70         n[0] = char(0x00);
71         n[1] = char(0x56);
72         r = c.length(m, n, n+2, 2);
73         assert(r == 2);
74     }
75     {
76         typedef std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header> C;
77         C c;
78         char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)};
79         std::mbstate_t m;
80         int r = c.length(m, n, n+6, 2);
81         assert(r == 6);
82 
83         n[0] = char(0x10);
84         n[1] = char(0x05);
85         r = c.length(m, n, n+2, 2);
86         assert(r == 2);
87 
88         n[0] = char(0x04);
89         n[1] = char(0x53);
90         r = c.length(m, n, n+2, 2);
91         assert(r == 2);
92 
93         n[0] = char(0x00);
94         n[1] = char(0x56);
95         r = c.length(m, n, n+2, 2);
96         assert(r == 2);
97     }
98     {
99         typedef std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian> C;
100         C c;
101         char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)};
102         std::mbstate_t m;
103         int r = c.length(m, n, n+4, 2);
104         assert(r == 4);
105 
106         n[1] = char(0x10);
107         n[0] = char(0x05);
108         r = c.length(m, n, n+2, 2);
109         assert(r == 2);
110 
111         n[1] = char(0x04);
112         n[0] = char(0x53);
113         r = c.length(m, n, n+2, 2);
114         assert(r == 2);
115 
116         n[1] = char(0x00);
117         n[0] = char(0x56);
118         r = c.length(m, n, n+2, 2);
119         assert(r == 2);
120     }
121     {
122         typedef std::codecvt_utf16<wchar_t, 0x1000, std::little_endian> C;
123         C c;
124         char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)};
125         std::mbstate_t m;
126         int r = c.length(m, n, n+4, 2);
127         assert(r == 0);
128 
129         n[1] = char(0x10);
130         n[0] = char(0x05);
131         r = c.length(m, n, n+2, 2);
132         assert(r == 0);
133 
134         n[1] = char(0x04);
135         n[0] = char(0x53);
136         r = c.length(m, n, n+2, 2);
137         assert(r == 2);
138 
139         n[1] = char(0x00);
140         n[0] = char(0x56);
141         r = c.length(m, n, n+2, 2);
142         assert(r == 2);
143     }
144     {
145         typedef std::codecvt_utf16<wchar_t, 0x10ffff, std::codecvt_mode(
146                                                          std::consume_header |
147                                                          std::little_endian)> C;
148         C c;
149         char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)};
150         std::mbstate_t m;
151         int r = c.length(m, n, n+6, 2);
152         assert(r == 6);
153 
154         n[1] = char(0x10);
155         n[0] = char(0x05);
156         r = c.length(m, n, n+2, 2);
157         assert(r == 2);
158 
159         n[1] = char(0x04);
160         n[0] = char(0x53);
161         r = c.length(m, n, n+2, 2);
162         assert(r == 2);
163 
164         n[1] = char(0x00);
165         n[0] = char(0x56);
166         r = c.length(m, n, n+2, 2);
167         assert(r == 2);
168     }
169     {
170         typedef std::codecvt_utf16<char32_t> C;
171         C c;
172         char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)};
173         std::mbstate_t m;
174         int r = c.length(m, n, n+4, 2);
175         assert(r == 4);
176 
177         n[0] = char(0x10);
178         n[1] = char(0x05);
179         r = c.length(m, n, n+2, 2);
180         assert(r == 2);
181 
182         n[0] = char(0x04);
183         n[1] = char(0x53);
184         r = c.length(m, n, n+2, 2);
185         assert(r == 2);
186 
187         n[0] = char(0x00);
188         n[1] = char(0x56);
189         r = c.length(m, n, n+2, 2);
190         assert(r == 2);
191     }
192     {
193         typedef std::codecvt_utf16<char32_t, 0x1000> C;
194         C c;
195         char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)};
196         std::mbstate_t m;
197         int r = c.length(m, n, n+4, 2);
198         assert(r == 0);
199 
200         n[0] = char(0x10);
201         n[1] = char(0x05);
202         r = c.length(m, n, n+2, 2);
203         assert(r == 0);
204 
205         n[0] = char(0x04);
206         n[1] = char(0x53);
207         r = c.length(m, n, n+2, 2);
208         assert(r == 2);
209 
210         n[0] = char(0x00);
211         n[1] = char(0x56);
212         r = c.length(m, n, n+2, 2);
213         assert(r == 2);
214     }
215     {
216         typedef std::codecvt_utf16<char32_t, 0x10ffff, std::consume_header> C;
217         C c;
218         char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)};
219         std::mbstate_t m;
220         int r = c.length(m, n, n+6, 2);
221         assert(r == 6);
222 
223         n[0] = char(0x10);
224         n[1] = char(0x05);
225         r = c.length(m, n, n+2, 2);
226         assert(r == 2);
227 
228         n[0] = char(0x04);
229         n[1] = char(0x53);
230         r = c.length(m, n, n+2, 2);
231         assert(r == 2);
232 
233         n[0] = char(0x00);
234         n[1] = char(0x56);
235         r = c.length(m, n, n+2, 2);
236         assert(r == 2);
237     }
238     {
239         typedef std::codecvt_utf16<char32_t, 0x10ffff, std::little_endian> C;
240         C c;
241         char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)};
242         std::mbstate_t m;
243         int r = c.length(m, n, n+4, 2);
244         assert(r == 4);
245 
246         n[1] = char(0x10);
247         n[0] = char(0x05);
248         r = c.length(m, n, n+2, 2);
249         assert(r == 2);
250 
251         n[1] = char(0x04);
252         n[0] = char(0x53);
253         r = c.length(m, n, n+2, 2);
254         assert(r == 2);
255 
256         n[1] = char(0x00);
257         n[0] = char(0x56);
258         r = c.length(m, n, n+2, 2);
259         assert(r == 2);
260     }
261     {
262         typedef std::codecvt_utf16<char32_t, 0x1000, std::little_endian> C;
263         C c;
264         char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)};
265         std::mbstate_t m;
266         int r = c.length(m, n, n+4, 2);
267         assert(r == 0);
268 
269         n[1] = char(0x10);
270         n[0] = char(0x05);
271         r = c.length(m, n, n+2, 2);
272         assert(r == 0);
273 
274         n[1] = char(0x04);
275         n[0] = char(0x53);
276         r = c.length(m, n, n+2, 2);
277         assert(r == 2);
278 
279         n[1] = char(0x00);
280         n[0] = char(0x56);
281         r = c.length(m, n, n+2, 2);
282         assert(r == 2);
283     }
284     {
285         typedef std::codecvt_utf16<char32_t, 0x10ffff, std::codecvt_mode(
286                                                          std::consume_header |
287                                                          std::little_endian)> C;
288         C c;
289         char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)};
290         std::mbstate_t m;
291         int r = c.length(m, n, n+6, 2);
292         assert(r == 6);
293 
294         n[1] = char(0x10);
295         n[0] = char(0x05);
296         r = c.length(m, n, n+2, 2);
297         assert(r == 2);
298 
299         n[1] = char(0x04);
300         n[0] = char(0x53);
301         r = c.length(m, n, n+2, 2);
302         assert(r == 2);
303 
304         n[1] = char(0x00);
305         n[0] = char(0x56);
306         r = c.length(m, n, n+2, 2);
307         assert(r == 2);
308     }
309 
310     {
311         typedef std::codecvt_utf16<char16_t> C;
312         C c;
313         char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)};
314         std::mbstate_t m;
315         int r = c.length(m, n, n+4, 2);
316         assert(r == 0);
317 
318         n[0] = char(0x10);
319         n[1] = char(0x05);
320         r = c.length(m, n, n+2, 2);
321         assert(r == 2);
322 
323         n[0] = char(0x04);
324         n[1] = char(0x53);
325         r = c.length(m, n, n+2, 2);
326         assert(r == 2);
327 
328         n[0] = char(0x00);
329         n[1] = char(0x56);
330         r = c.length(m, n, n+2, 2);
331         assert(r == 2);
332     }
333     {
334         typedef std::codecvt_utf16<char16_t, 0x1000> C;
335         C c;
336         char n[4] = {char(0xD8), char(0xC0), char(0xDC), char(0x03)};
337         std::mbstate_t m;
338         int r = c.length(m, n, n+4, 2);
339         assert(r == 0);
340 
341         n[0] = char(0x10);
342         n[1] = char(0x05);
343         r = c.length(m, n, n+2, 2);
344         assert(r == 0);
345 
346         n[0] = char(0x04);
347         n[1] = char(0x53);
348         r = c.length(m, n, n+2, 2);
349         assert(r == 2);
350 
351         n[0] = char(0x00);
352         n[1] = char(0x56);
353         r = c.length(m, n, n+2, 2);
354         assert(r == 2);
355     }
356     {
357         typedef std::codecvt_utf16<char16_t, 0x10ffff, std::consume_header> C;
358         C c;
359         char n[6] = {char(0xFE), char(0xFF), char(0xD8), char(0xC0), char(0xDC), char(0x03)};
360         std::mbstate_t m;
361         int r = c.length(m, n, n+6, 2);
362         assert(r == 2);
363 
364         n[0] = char(0x10);
365         n[1] = char(0x05);
366         r = c.length(m, n, n+2, 2);
367         assert(r == 2);
368 
369         n[0] = char(0x04);
370         n[1] = char(0x53);
371         r = c.length(m, n, n+2, 2);
372         assert(r == 2);
373 
374         n[0] = char(0x00);
375         n[1] = char(0x56);
376         r = c.length(m, n, n+2, 2);
377         assert(r == 2);
378     }
379     {
380         typedef std::codecvt_utf16<char16_t, 0x10ffff, std::little_endian> C;
381         C c;
382         char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)};
383         std::mbstate_t m;
384         int r = c.length(m, n, n+4, 2);
385         assert(r == 0);
386 
387         n[1] = char(0x10);
388         n[0] = char(0x05);
389         r = c.length(m, n, n+2, 2);
390         assert(r == 2);
391 
392         n[1] = char(0x04);
393         n[0] = char(0x53);
394         r = c.length(m, n, n+2, 2);
395         assert(r == 2);
396 
397         n[1] = char(0x00);
398         n[0] = char(0x56);
399         r = c.length(m, n, n+2, 2);
400         assert(r == 2);
401     }
402     {
403         typedef std::codecvt_utf16<char16_t, 0x1000, std::little_endian> C;
404         C c;
405         char n[4] = {char(0xC0), char(0xD8), char(0x03), char(0xDC)};
406         std::mbstate_t m;
407         int r = c.length(m, n, n+4, 2);
408         assert(r == 0);
409 
410         n[1] = char(0x10);
411         n[0] = char(0x05);
412         r = c.length(m, n, n+2, 2);
413         assert(r == 0);
414 
415         n[1] = char(0x04);
416         n[0] = char(0x53);
417         r = c.length(m, n, n+2, 2);
418         assert(r == 2);
419 
420         n[1] = char(0x00);
421         n[0] = char(0x56);
422         r = c.length(m, n, n+2, 2);
423         assert(r == 2);
424     }
425     {
426         typedef std::codecvt_utf16<char16_t, 0x10ffff, std::codecvt_mode(
427                                                          std::consume_header |
428                                                          std::little_endian)> C;
429         C c;
430         char n[6] = {char(0xFF), char(0xFE), char(0xC0), char(0xD8), char(0x03), char(0xDC)};
431         std::mbstate_t m;
432         int r = c.length(m, n, n+6, 2);
433         assert(r == 2);
434 
435         n[1] = char(0x10);
436         n[0] = char(0x05);
437         r = c.length(m, n, n+2, 2);
438         assert(r == 2);
439 
440         n[1] = char(0x04);
441         n[0] = char(0x53);
442         r = c.length(m, n, n+2, 2);
443         assert(r == 2);
444 
445         n[1] = char(0x00);
446         n[0] = char(0x56);
447         r = c.length(m, n, n+2, 2);
448         assert(r == 2);
449     }
450 
451   return 0;
452 }
453