1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <codecvt>
11 
12 // template <class Elem, unsigned long Maxcode = 0x10ffff,
13 //           codecvt_mode Mode = (codecvt_mode)0>
14 // class codecvt_utf8_utf16
15 //     : public codecvt<Elem, char, mbstate_t>
16 // {
17 //     // unspecified
18 // };
19 
20 // result
21 // out(stateT& state,
22 //     const internT* from, const internT* from_end, const internT*& from_next,
23 //     externT* to, externT* to_end, externT*& to_next) const;
24 
25 #include <codecvt>
26 #include <cassert>
27 
main()28 int main()
29 {
30     {
31         typedef std::codecvt_utf8_utf16<wchar_t> C;
32         C c;
33         wchar_t w[2] = {0xD8C0, 0xDC03};
34         char n[4] = {0};
35         const wchar_t* wp = nullptr;
36         std::mbstate_t m;
37         char* np = nullptr;
38         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+4, np);
39         assert(r == std::codecvt_base::ok);
40         assert(wp == w+2);
41         assert(np == n+4);
42         assert(n[0] == char(0xF1));
43         assert(n[1] == char(0x80));
44         assert(n[2] == char(0x80));
45         assert(n[3] == char(0x83));
46 
47         w[0] = 0x1005;
48         r = c.out(m, w, w+1, wp, n, n+4, np);
49         assert(r == std::codecvt_base::ok);
50         assert(wp == w+1);
51         assert(np == n+3);
52         assert(n[0] == char(0xE1));
53         assert(n[1] == char(0x80));
54         assert(n[2] == char(0x85));
55 
56         w[0] = 0x453;
57         r = c.out(m, w, w+1, wp, n, n+4, np);
58         assert(r == std::codecvt_base::ok);
59         assert(wp == w+1);
60         assert(np == n+2);
61         assert(n[0] == char(0xD1));
62         assert(n[1] == char(0x93));
63 
64         w[0] = 0x56;
65         r = c.out(m, w, w+1, wp, n, n+4, np);
66         assert(r == std::codecvt_base::ok);
67         assert(wp == w+1);
68         assert(np == n+1);
69         assert(n[0] == char(0x56));
70     }
71     {
72         typedef std::codecvt_utf8_utf16<wchar_t, 0x1000> C;
73         C c;
74         wchar_t w[2] = {0xD8C0, 0xDC03};
75         char n[4] = {0};
76         const wchar_t* wp = nullptr;
77         std::mbstate_t m;
78         char* np = nullptr;
79         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+4, np);
80         assert(r == std::codecvt_base::error);
81         assert(wp == w);
82         assert(np == n);
83 
84         w[0] = 0x1005;
85         r = c.out(m, w, w+1, wp, n, n+4, np);
86         assert(r == std::codecvt_base::error);
87         assert(wp == w);
88         assert(np == n);
89 
90         w[0] = 0x453;
91         r = c.out(m, w, w+1, wp, n, n+4, np);
92         assert(r == std::codecvt_base::ok);
93         assert(wp == w+1);
94         assert(np == n+2);
95         assert(n[0] == char(0xD1));
96         assert(n[1] == char(0x93));
97 
98         w[0] = 0x56;
99         r = c.out(m, w, w+1, wp, n, n+4, np);
100         assert(r == std::codecvt_base::ok);
101         assert(wp == w+1);
102         assert(np == n+1);
103         assert(n[0] == char(0x56));
104     }
105     {
106         typedef std::codecvt_utf8_utf16<wchar_t, 0x10ffff, std::generate_header> C;
107         C c;
108         wchar_t w[2] = {0xD8C0, 0xDC03};
109         char n[7] = {0};
110         const wchar_t* wp = nullptr;
111         std::mbstate_t m;
112         char* np = nullptr;
113         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+7, np);
114         assert(r == std::codecvt_base::ok);
115         assert(wp == w+2);
116         assert(np == n+7);
117         assert(n[0] == char(0xEF));
118         assert(n[1] == char(0xBB));
119         assert(n[2] == char(0xBF));
120         assert(n[3] == char(0xF1));
121         assert(n[4] == char(0x80));
122         assert(n[5] == char(0x80));
123         assert(n[6] == char(0x83));
124 
125         w[0] = 0x1005;
126         r = c.out(m, w, w+1, wp, n, n+7, np);
127         assert(r == std::codecvt_base::ok);
128         assert(wp == w+1);
129         assert(np == n+6);
130         assert(n[0] == char(0xEF));
131         assert(n[1] == char(0xBB));
132         assert(n[2] == char(0xBF));
133         assert(n[3] == char(0xE1));
134         assert(n[4] == char(0x80));
135         assert(n[5] == char(0x85));
136 
137         w[0] = 0x453;
138         r = c.out(m, w, w+1, wp, n, n+7, np);
139         assert(r == std::codecvt_base::ok);
140         assert(wp == w+1);
141         assert(np == n+5);
142         assert(n[0] == char(0xEF));
143         assert(n[1] == char(0xBB));
144         assert(n[2] == char(0xBF));
145         assert(n[3] == char(0xD1));
146         assert(n[4] == char(0x93));
147 
148         w[0] = 0x56;
149         r = c.out(m, w, w+1, wp, n, n+7, np);
150         assert(r == std::codecvt_base::ok);
151         assert(wp == w+1);
152         assert(np == n+4);
153         assert(n[0] == char(0xEF));
154         assert(n[1] == char(0xBB));
155         assert(n[2] == char(0xBF));
156         assert(n[3] == char(0x56));
157     }
158     {
159         typedef std::codecvt_utf8_utf16<char32_t> C;
160         C c;
161         char32_t w[2] = {0xD8C0, 0xDC03};
162         char n[4] = {0};
163         const char32_t* wp = nullptr;
164         std::mbstate_t m;
165         char* np = nullptr;
166         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+4, np);
167         assert(r == std::codecvt_base::ok);
168         assert(wp == w+2);
169         assert(np == n+4);
170         assert(n[0] == char(0xF1));
171         assert(n[1] == char(0x80));
172         assert(n[2] == char(0x80));
173         assert(n[3] == char(0x83));
174 
175         w[0] = 0x1005;
176         r = c.out(m, w, w+1, wp, n, n+4, np);
177         assert(r == std::codecvt_base::ok);
178         assert(wp == w+1);
179         assert(np == n+3);
180         assert(n[0] == char(0xE1));
181         assert(n[1] == char(0x80));
182         assert(n[2] == char(0x85));
183 
184         w[0] = 0x453;
185         r = c.out(m, w, w+1, wp, n, n+4, np);
186         assert(r == std::codecvt_base::ok);
187         assert(wp == w+1);
188         assert(np == n+2);
189         assert(n[0] == char(0xD1));
190         assert(n[1] == char(0x93));
191 
192         w[0] = 0x56;
193         r = c.out(m, w, w+1, wp, n, n+4, np);
194         assert(r == std::codecvt_base::ok);
195         assert(wp == w+1);
196         assert(np == n+1);
197         assert(n[0] == char(0x56));
198     }
199     {
200         typedef std::codecvt_utf8_utf16<char32_t, 0x1000> C;
201         C c;
202         char32_t w[2] = {0xD8C0, 0xDC03};
203         char n[4] = {0};
204         const char32_t* wp = nullptr;
205         std::mbstate_t m;
206         char* np = nullptr;
207         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+4, np);
208         assert(r == std::codecvt_base::error);
209         assert(wp == w);
210         assert(np == n);
211 
212         w[0] = 0x1005;
213         r = c.out(m, w, w+1, wp, n, n+4, np);
214         assert(r == std::codecvt_base::error);
215         assert(wp == w);
216         assert(np == n);
217 
218         w[0] = 0x453;
219         r = c.out(m, w, w+1, wp, n, n+4, np);
220         assert(r == std::codecvt_base::ok);
221         assert(wp == w+1);
222         assert(np == n+2);
223         assert(n[0] == char(0xD1));
224         assert(n[1] == char(0x93));
225 
226         w[0] = 0x56;
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);
230         assert(np == n+1);
231         assert(n[0] == char(0x56));
232     }
233     {
234         typedef std::codecvt_utf8_utf16<char32_t, 0x10ffff, std::generate_header> C;
235         C c;
236         char32_t w[2] = {0xD8C0, 0xDC03};
237         char n[7] = {0};
238         const char32_t* wp = nullptr;
239         std::mbstate_t m;
240         char* np = nullptr;
241         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+7, np);
242         assert(r == std::codecvt_base::ok);
243         assert(wp == w+2);
244         assert(np == n+7);
245         assert(n[0] == char(0xEF));
246         assert(n[1] == char(0xBB));
247         assert(n[2] == char(0xBF));
248         assert(n[3] == char(0xF1));
249         assert(n[4] == char(0x80));
250         assert(n[5] == char(0x80));
251         assert(n[6] == char(0x83));
252 
253         w[0] = 0x1005;
254         r = c.out(m, w, w+1, wp, n, n+7, np);
255         assert(r == std::codecvt_base::ok);
256         assert(wp == w+1);
257         assert(np == n+6);
258         assert(n[0] == char(0xEF));
259         assert(n[1] == char(0xBB));
260         assert(n[2] == char(0xBF));
261         assert(n[3] == char(0xE1));
262         assert(n[4] == char(0x80));
263         assert(n[5] == char(0x85));
264 
265         w[0] = 0x453;
266         r = c.out(m, w, w+1, wp, n, n+7, np);
267         assert(r == std::codecvt_base::ok);
268         assert(wp == w+1);
269         assert(np == n+5);
270         assert(n[0] == char(0xEF));
271         assert(n[1] == char(0xBB));
272         assert(n[2] == char(0xBF));
273         assert(n[3] == char(0xD1));
274         assert(n[4] == char(0x93));
275 
276         w[0] = 0x56;
277         r = c.out(m, w, w+1, wp, n, n+7, np);
278         assert(r == std::codecvt_base::ok);
279         assert(wp == w+1);
280         assert(np == n+4);
281         assert(n[0] == char(0xEF));
282         assert(n[1] == char(0xBB));
283         assert(n[2] == char(0xBF));
284         assert(n[3] == char(0x56));
285     }
286 
287     {
288         typedef std::codecvt_utf8_utf16<char16_t> C;
289         C c;
290         char16_t w[2] = {0xD8C0, 0xDC03};
291         char n[4] = {0};
292         const char16_t* wp = nullptr;
293         std::mbstate_t m;
294         char* np = nullptr;
295         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+4, np);
296         assert(r == std::codecvt_base::ok);
297         assert(wp == w+2);
298         assert(np == n+4);
299         assert(n[0] == char(0xF1));
300         assert(n[1] == char(0x80));
301         assert(n[2] == char(0x80));
302         assert(n[3] == char(0x83));
303 
304         w[0] = 0x1005;
305         r = c.out(m, w, w+1, wp, n, n+4, np);
306         assert(r == std::codecvt_base::ok);
307         assert(wp == w+1);
308         assert(np == n+3);
309         assert(n[0] == char(0xE1));
310         assert(n[1] == char(0x80));
311         assert(n[2] == char(0x85));
312 
313         w[0] = 0x453;
314         r = c.out(m, w, w+1, wp, n, n+4, np);
315         assert(r == std::codecvt_base::ok);
316         assert(wp == w+1);
317         assert(np == n+2);
318         assert(n[0] == char(0xD1));
319         assert(n[1] == char(0x93));
320 
321         w[0] = 0x56;
322         r = c.out(m, w, w+1, wp, n, n+4, np);
323         assert(r == std::codecvt_base::ok);
324         assert(wp == w+1);
325         assert(np == n+1);
326         assert(n[0] == char(0x56));
327     }
328     {
329         typedef std::codecvt_utf8_utf16<char16_t, 0x1000> C;
330         C c;
331         char16_t w[2] = {0xD8C0, 0xDC03};
332         char n[4] = {0};
333         const char16_t* wp = nullptr;
334         std::mbstate_t m;
335         char* np = nullptr;
336         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+4, np);
337         assert(r == std::codecvt_base::error);
338         assert(wp == w);
339         assert(np == n);
340 
341         w[0] = 0x1005;
342         r = c.out(m, w, w+1, wp, n, n+4, np);
343         assert(r == std::codecvt_base::error);
344         assert(wp == w);
345         assert(np == n);
346 
347         w[0] = 0x453;
348         r = c.out(m, w, w+1, wp, n, n+4, np);
349         assert(r == std::codecvt_base::ok);
350         assert(wp == w+1);
351         assert(np == n+2);
352         assert(n[0] == char(0xD1));
353         assert(n[1] == char(0x93));
354 
355         w[0] = 0x56;
356         r = c.out(m, w, w+1, wp, n, n+4, np);
357         assert(r == std::codecvt_base::ok);
358         assert(wp == w+1);
359         assert(np == n+1);
360         assert(n[0] == char(0x56));
361     }
362     {
363         typedef std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::generate_header> C;
364         C c;
365         char16_t w[2] = {0xD8C0, 0xDC03};
366         char n[7] = {0};
367         const char16_t* wp = nullptr;
368         std::mbstate_t m;
369         char* np = nullptr;
370         std::codecvt_base::result r = c.out(m, w, w+2, wp, n, n+7, np);
371         assert(r == std::codecvt_base::ok);
372         assert(wp == w+2);
373         assert(np == n+7);
374         assert(n[0] == char(0xEF));
375         assert(n[1] == char(0xBB));
376         assert(n[2] == char(0xBF));
377         assert(n[3] == char(0xF1));
378         assert(n[4] == char(0x80));
379         assert(n[5] == char(0x80));
380         assert(n[6] == char(0x83));
381 
382         w[0] = 0x1005;
383         r = c.out(m, w, w+1, wp, n, n+7, np);
384         assert(r == std::codecvt_base::ok);
385         assert(wp == w+1);
386         assert(np == n+6);
387         assert(n[0] == char(0xEF));
388         assert(n[1] == char(0xBB));
389         assert(n[2] == char(0xBF));
390         assert(n[3] == char(0xE1));
391         assert(n[4] == char(0x80));
392         assert(n[5] == char(0x85));
393 
394         w[0] = 0x453;
395         r = c.out(m, w, w+1, wp, n, n+7, np);
396         assert(r == std::codecvt_base::ok);
397         assert(wp == w+1);
398         assert(np == n+5);
399         assert(n[0] == char(0xEF));
400         assert(n[1] == char(0xBB));
401         assert(n[2] == char(0xBF));
402         assert(n[3] == char(0xD1));
403         assert(n[4] == char(0x93));
404 
405         w[0] = 0x56;
406         r = c.out(m, w, w+1, wp, n, n+7, np);
407         assert(r == std::codecvt_base::ok);
408         assert(wp == w+1);
409         assert(np == n+4);
410         assert(n[0] == char(0xEF));
411         assert(n[1] == char(0xBB));
412         assert(n[2] == char(0xBF));
413         assert(n[3] == char(0x56));
414     }
415 }
416