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_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_utf16<wchar_t> C;
32         C c;
33         wchar_t w = 0x40003;
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+1, wp, n, n+4, np);
39         assert(r == std::codecvt_base::ok);
40         assert(wp == &w+1);
41         assert(np == n+4);
42         assert(n[0] == char(0xD8));
43         assert(n[1] == char(0xC0));
44         assert(n[2] == char(0xDC));
45         assert(n[3] == char(0x03));
46 
47         w = 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+2);
52         assert(n[0] == char(0x10));
53         assert(n[1] == char(0x05));
54         assert(n[2] == char(0xDC));
55         assert(n[3] == char(0x03));
56 
57         w = 0x453;
58         r = c.out(m, &w, &w+1, wp, n, n+4, np);
59         assert(r == std::codecvt_base::ok);
60         assert(wp == &w+1);
61         assert(np == n+2);
62         assert(n[0] == char(0x04));
63         assert(n[1] == char(0x53));
64         assert(n[2] == char(0xDC));
65         assert(n[3] == char(0x03));
66 
67         w = 0x56;
68         r = c.out(m, &w, &w+1, wp, n, n+4, np);
69         assert(r == std::codecvt_base::ok);
70         assert(wp == &w+1);
71         assert(np == n+2);
72         assert(n[0] == char(0x00));
73         assert(n[1] == char(0x56));
74         assert(n[2] == char(0xDC));
75         assert(n[3] == char(0x03));
76     }
77     {
78         typedef std::codecvt_utf16<wchar_t, 0x1000> C;
79         C c;
80         wchar_t w = 0x40003;
81         char n[4] = {0};
82         const wchar_t* wp = nullptr;
83         std::mbstate_t m;
84         char* np = nullptr;
85         std::codecvt_base::result 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         assert(n[0] == char(0));
90         assert(n[1] == char(0));
91         assert(n[2] == char(0));
92         assert(n[3] == char(0));
93 
94         w = 0x1005;
95         r = c.out(m, &w, &w+1, wp, n, n+4, np);
96         assert(r == std::codecvt_base::error);
97         assert(wp == &w);
98         assert(np == n);
99         assert(n[0] == char(0));
100         assert(n[1] == char(0));
101         assert(n[2] == char(0));
102         assert(n[3] == char(0));
103 
104         w = 0x453;
105         r = c.out(m, &w, &w+1, wp, n, n+4, np);
106         assert(r == std::codecvt_base::ok);
107         assert(wp == &w+1);
108         assert(np == n+2);
109         assert(n[0] == char(0x04));
110         assert(n[1] == char(0x53));
111         assert(n[2] == char(0));
112         assert(n[3] == char(0));
113 
114         w = 0x56;
115         r = c.out(m, &w, &w+1, wp, n, n+4, np);
116         assert(r == std::codecvt_base::ok);
117         assert(wp == &w+1);
118         assert(np == n+2);
119         assert(n[0] == char(0x00));
120         assert(n[1] == char(0x56));
121         assert(n[2] == char(0));
122         assert(n[3] == char(0));
123     }
124     {
125         typedef std::codecvt_utf16<wchar_t, 0x10ffff, std::generate_header> C;
126         C c;
127         wchar_t w = 0x40003;
128         char n[6] = {0};
129         const wchar_t* wp = nullptr;
130         std::mbstate_t m;
131         char* np = nullptr;
132         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+6, np);
133         assert(r == std::codecvt_base::ok);
134         assert(wp == &w+1);
135         assert(np == n+6);
136         assert(n[0] == char(0xFE));
137         assert(n[1] == char(0xFF));
138         assert(n[2] == char(0xD8));
139         assert(n[3] == char(0xC0));
140         assert(n[4] == char(0xDC));
141         assert(n[5] == char(0x03));
142 
143         w = 0x1005;
144         r = c.out(m, &w, &w+1, wp, n, n+6, np);
145         assert(r == std::codecvt_base::ok);
146         assert(wp == &w+1);
147         assert(np == n+4);
148         assert(n[0] == char(0xFE));
149         assert(n[1] == char(0xFF));
150         assert(n[2] == char(0x10));
151         assert(n[3] == char(0x05));
152         assert(n[4] == char(0xDC));
153         assert(n[5] == char(0x03));
154 
155         w = 0x453;
156         r = c.out(m, &w, &w+1, wp, n, n+6, np);
157         assert(r == std::codecvt_base::ok);
158         assert(wp == &w+1);
159         assert(np == n+4);
160         assert(n[0] == char(0xFE));
161         assert(n[1] == char(0xFF));
162         assert(n[2] == char(0x04));
163         assert(n[3] == char(0x53));
164         assert(n[4] == char(0xDC));
165         assert(n[5] == char(0x03));
166 
167         w = 0x56;
168         r = c.out(m, &w, &w+1, wp, n, n+6, np);
169         assert(r == std::codecvt_base::ok);
170         assert(wp == &w+1);
171         assert(np == n+4);
172         assert(n[0] == char(0xFE));
173         assert(n[1] == char(0xFF));
174         assert(n[2] == char(0x00));
175         assert(n[3] == char(0x56));
176         assert(n[4] == char(0xDC));
177         assert(n[5] == char(0x03));
178     }
179 
180     {
181         typedef std::codecvt_utf16<wchar_t, 0x10FFFF, std::little_endian> C;
182         C c;
183         wchar_t w = 0x40003;
184         char n[4] = {0};
185         const wchar_t* wp = nullptr;
186         std::mbstate_t m;
187         char* np = nullptr;
188         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
189         assert(r == std::codecvt_base::ok);
190         assert(wp == &w+1);
191         assert(np == n+4);
192         assert(n[1] == char(0xD8));
193         assert(n[0] == char(0xC0));
194         assert(n[3] == char(0xDC));
195         assert(n[2] == char(0x03));
196 
197         w = 0x1005;
198         r = c.out(m, &w, &w+1, wp, n, n+4, np);
199         assert(r == std::codecvt_base::ok);
200         assert(wp == &w+1);
201         assert(np == n+2);
202         assert(n[1] == char(0x10));
203         assert(n[0] == char(0x05));
204         assert(n[3] == char(0xDC));
205         assert(n[2] == char(0x03));
206 
207         w = 0x453;
208         r = c.out(m, &w, &w+1, wp, n, n+4, np);
209         assert(r == std::codecvt_base::ok);
210         assert(wp == &w+1);
211         assert(np == n+2);
212         assert(n[1] == char(0x04));
213         assert(n[0] == char(0x53));
214         assert(n[3] == char(0xDC));
215         assert(n[2] == char(0x03));
216 
217         w = 0x56;
218         r = c.out(m, &w, &w+1, wp, n, n+4, np);
219         assert(r == std::codecvt_base::ok);
220         assert(wp == &w+1);
221         assert(np == n+2);
222         assert(n[1] == char(0x00));
223         assert(n[0] == char(0x56));
224         assert(n[3] == char(0xDC));
225         assert(n[2] == char(0x03));
226     }
227     {
228         typedef std::codecvt_utf16<wchar_t, 0x1000, std::little_endian> C;
229         C c;
230         wchar_t w = 0x40003;
231         char n[4] = {0};
232         const wchar_t* wp = nullptr;
233         std::mbstate_t m;
234         char* np = nullptr;
235         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+4, np);
236         assert(r == std::codecvt_base::error);
237         assert(wp == &w);
238         assert(np == n);
239         assert(n[1] == char(0));
240         assert(n[0] == char(0));
241         assert(n[3] == char(0));
242         assert(n[2] == char(0));
243 
244         w = 0x1005;
245         r = c.out(m, &w, &w+1, wp, n, n+4, np);
246         assert(r == std::codecvt_base::error);
247         assert(wp == &w);
248         assert(np == n);
249         assert(n[1] == char(0));
250         assert(n[0] == char(0));
251         assert(n[3] == char(0));
252         assert(n[2] == char(0));
253 
254         w = 0x453;
255         r = c.out(m, &w, &w+1, wp, n, n+4, np);
256         assert(r == std::codecvt_base::ok);
257         assert(wp == &w+1);
258         assert(np == n+2);
259         assert(n[1] == char(0x04));
260         assert(n[0] == char(0x53));
261         assert(n[3] == char(0));
262         assert(n[2] == char(0));
263 
264         w = 0x56;
265         r = c.out(m, &w, &w+1, wp, n, n+4, np);
266         assert(r == std::codecvt_base::ok);
267         assert(wp == &w+1);
268         assert(np == n+2);
269         assert(n[1] == char(0x00));
270         assert(n[0] == char(0x56));
271         assert(n[3] == char(0));
272         assert(n[2] == char(0));
273     }
274     {
275         typedef std::codecvt_utf16<wchar_t, 0x10ffff, std::codecvt_mode(
276                                                          std::generate_header |
277                                                          std::little_endian)> C;
278         C c;
279         wchar_t w = 0x40003;
280         char n[6] = {0};
281         const wchar_t* wp = nullptr;
282         std::mbstate_t m;
283         char* np = nullptr;
284         std::codecvt_base::result r = c.out(m, &w, &w+1, wp, n, n+6, np);
285         assert(r == std::codecvt_base::ok);
286         assert(wp == &w+1);
287         assert(np == n+6);
288         assert(n[1] == char(0xFE));
289         assert(n[0] == char(0xFF));
290         assert(n[3] == char(0xD8));
291         assert(n[2] == char(0xC0));
292         assert(n[5] == char(0xDC));
293         assert(n[4] == char(0x03));
294 
295         w = 0x1005;
296         r = c.out(m, &w, &w+1, wp, n, n+6, np);
297         assert(r == std::codecvt_base::ok);
298         assert(wp == &w+1);
299         assert(np == n+4);
300         assert(n[1] == char(0xFE));
301         assert(n[0] == char(0xFF));
302         assert(n[3] == char(0x10));
303         assert(n[2] == char(0x05));
304         assert(n[5] == char(0xDC));
305         assert(n[4] == char(0x03));
306 
307         w = 0x453;
308         r = c.out(m, &w, &w+1, wp, n, n+6, np);
309         assert(r == std::codecvt_base::ok);
310         assert(wp == &w+1);
311         assert(np == n+4);
312         assert(n[1] == char(0xFE));
313         assert(n[0] == char(0xFF));
314         assert(n[3] == char(0x04));
315         assert(n[2] == char(0x53));
316         assert(n[5] == char(0xDC));
317         assert(n[4] == char(0x03));
318 
319         w = 0x56;
320         r = c.out(m, &w, &w+1, wp, n, n+6, np);
321         assert(r == std::codecvt_base::ok);
322         assert(wp == &w+1);
323         assert(np == n+4);
324         assert(n[1] == char(0xFE));
325         assert(n[0] == char(0xFF));
326         assert(n[3] == char(0x00));
327         assert(n[2] == char(0x56));
328         assert(n[5] == char(0xDC));
329         assert(n[4] == char(0x03));
330     }
331 }
332