[NFC][Py Reformat] Reformat python files in libcxx/libcxxabi
[llvm-project.git] / libcxx / test / std / localization / locale.stdcvt / codecvt_utf8_out.pass.cpp
blob22636ca08980c2d4f3a3142dfaf50c0bf89ec1b8
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 //===----------------------------------------------------------------------===//
9 // <codecvt>
11 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
13 // template <class Elem, unsigned long Maxcode = 0x10ffff,
14 // codecvt_mode Mode = (codecvt_mode)0>
15 // class codecvt_utf8
16 // : public codecvt<Elem, char, mbstate_t>
17 // {
18 // // unspecified
19 // };
21 // result
22 // out(stateT& state,
23 // const internT* from, const internT* from_end, const internT*& from_next,
24 // externT* to, externT* to_end, externT*& to_next) const;
26 #include <codecvt>
27 #include <cassert>
29 #include "test_macros.h"
31 template <class CharT, std::size_t = sizeof(CharT)>
32 struct TestHelper;
34 template <class CharT>
35 struct TestHelper<CharT, 2> {
36 static void test();
39 template <class CharT>
40 struct TestHelper<CharT, 4> {
41 static void test();
44 template <class CharT>
45 void TestHelper<CharT, 2>::test() {
47 typedef std::codecvt_utf8<CharT> C;
48 C c;
49 CharT w = 0x1005;
50 char n[4] = {0};
51 const CharT* wp = nullptr;
52 std::mbstate_t m;
53 char* np = nullptr;
54 std::codecvt_base::result r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
55 assert(r == std::codecvt_base::ok);
56 assert(wp == &w + 1);
57 assert(np == n + 3);
58 assert(n[0] == char(0xE1));
59 assert(n[1] == char(0x80));
60 assert(n[2] == char(0x85));
61 assert(n[3] == char(0));
63 w = 0x453;
64 r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
65 assert(r == std::codecvt_base::ok);
66 assert(wp == &w + 1);
67 assert(np == n + 2);
68 assert(n[0] == char(0xD1));
69 assert(n[1] == char(0x93));
70 assert(n[2] == char(0x85));
71 assert(n[3] == char(0));
73 w = 0x56;
74 r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
75 assert(r == std::codecvt_base::ok);
76 assert(wp == &w + 1);
77 assert(np == n + 1);
78 assert(n[0] == char(0x56));
79 assert(n[1] == char(0x93));
80 assert(n[2] == char(0x85));
81 assert(n[3] == char(0));
84 typedef std::codecvt_utf8<CharT, 0x1000> C;
85 C c;
86 CharT w = 0x1005;
87 char n[4] = {0};
88 const CharT* wp = nullptr;
89 std::mbstate_t m;
90 char* np = nullptr;
91 std::codecvt_base::result r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
92 assert(r == std::codecvt_base::error);
93 assert(wp == &w);
94 assert(np == n);
95 assert(n[0] == char(0));
96 assert(n[1] == char(0));
97 assert(n[2] == char(0));
98 assert(n[3] == char(0));
100 w = 0x453;
101 r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
102 assert(r == std::codecvt_base::ok);
103 assert(wp == &w + 1);
104 assert(np == n + 2);
105 assert(n[0] == char(0xD1));
106 assert(n[1] == char(0x93));
107 assert(n[2] == char(0));
108 assert(n[3] == char(0));
110 w = 0x56;
111 r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
112 assert(r == std::codecvt_base::ok);
113 assert(wp == &w + 1);
114 assert(np == n + 1);
115 assert(n[0] == char(0x56));
116 assert(n[1] == char(0x93));
117 assert(n[2] == char(0));
118 assert(n[3] == char(0));
121 typedef std::codecvt_utf8<CharT, 0xFFFFFFFF, std::generate_header> C;
122 C c;
123 CharT w = 0x1005;
124 char n[7] = {0};
125 const CharT* wp = nullptr;
126 std::mbstate_t m;
127 char* np = nullptr;
128 std::codecvt_base::result r = c.out(m, &w, &w + 1, wp, n, n + 7, np);
129 assert(r == std::codecvt_base::ok);
130 assert(wp == &w + 1);
131 assert(np == n + 6);
132 assert(n[0] == char(0xEF));
133 assert(n[1] == char(0xBB));
134 assert(n[2] == char(0xBF));
135 assert(n[3] == char(0xE1));
136 assert(n[4] == char(0x80));
137 assert(n[5] == char(0x85));
138 assert(n[6] == char(0));
140 w = 0x453;
141 r = c.out(m, &w, &w + 1, wp, n, n + 7, np);
142 assert(r == std::codecvt_base::ok);
143 assert(wp == &w + 1);
144 assert(np == n + 5);
145 assert(n[0] == char(0xEF));
146 assert(n[1] == char(0xBB));
147 assert(n[2] == char(0xBF));
148 assert(n[3] == char(0xD1));
149 assert(n[4] == char(0x93));
150 assert(n[5] == char(0x85));
151 assert(n[6] == char(0));
153 w = 0x56;
154 r = c.out(m, &w, &w + 1, wp, n, n + 7, np);
155 assert(r == std::codecvt_base::ok);
156 assert(wp == &w + 1);
157 assert(np == n + 4);
158 assert(n[0] == char(0xEF));
159 assert(n[1] == char(0xBB));
160 assert(n[2] == char(0xBF));
161 assert(n[3] == char(0x56));
162 assert(n[4] == char(0x93));
163 assert(n[5] == char(0x85));
164 assert(n[6] == char(0));
168 template <class CharT>
169 void TestHelper<CharT, 4>::test() {
171 typedef std::codecvt_utf8<CharT> C;
172 C c;
173 CharT w = 0x40003;
174 char n[4] = {0};
175 const CharT* wp = nullptr;
176 std::mbstate_t m;
177 char* np = nullptr;
178 std::codecvt_base::result r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
179 assert(r == std::codecvt_base::ok);
180 assert(wp == &w + 1);
181 assert(np == n + 4);
182 assert(n[0] == char(0xF1));
183 assert(n[1] == char(0x80));
184 assert(n[2] == char(0x80));
185 assert(n[3] == char(0x83));
187 w = 0x1005;
188 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 + 3);
192 assert(n[0] == char(0xE1));
193 assert(n[1] == char(0x80));
194 assert(n[2] == char(0x85));
195 assert(n[3] == char(0x83));
197 w = 0x453;
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[0] == char(0xD1));
203 assert(n[1] == char(0x93));
204 assert(n[2] == char(0x85));
205 assert(n[3] == char(0x83));
207 w = 0x56;
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 + 1);
212 assert(n[0] == char(0x56));
213 assert(n[1] == char(0x93));
214 assert(n[2] == char(0x85));
215 assert(n[3] == char(0x83));
218 typedef std::codecvt_utf8<CharT, 0x1000> C;
219 C c;
220 CharT w = 0x40003;
221 char n[4] = {0};
222 const CharT* wp = nullptr;
223 std::mbstate_t m;
224 char* np = nullptr;
225 std::codecvt_base::result r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
226 assert(r == std::codecvt_base::error);
227 assert(wp == &w);
228 assert(np == n);
229 assert(n[0] == char(0));
230 assert(n[1] == char(0));
231 assert(n[2] == char(0));
232 assert(n[3] == char(0));
234 w = 0x1005;
235 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[0] == char(0));
240 assert(n[1] == char(0));
241 assert(n[2] == char(0));
242 assert(n[3] == char(0));
244 w = 0x453;
245 r = c.out(m, &w, &w + 1, wp, n, n + 4, np);
246 assert(r == std::codecvt_base::ok);
247 assert(wp == &w + 1);
248 assert(np == n + 2);
249 assert(n[0] == char(0xD1));
250 assert(n[1] == char(0x93));
251 assert(n[2] == char(0));
252 assert(n[3] == char(0));
254 w = 0x56;
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 + 1);
259 assert(n[0] == char(0x56));
260 assert(n[1] == char(0x93));
261 assert(n[2] == char(0));
262 assert(n[3] == char(0));
265 typedef std::codecvt_utf8<CharT, 0xFFFFFFFF, std::generate_header> C;
266 C c;
267 CharT w = 0x40003;
268 char n[7] = {0};
269 const CharT* wp = nullptr;
270 std::mbstate_t m;
271 char* np = nullptr;
272 std::codecvt_base::result r = c.out(m, &w, &w + 1, wp, n, n + 7, np);
273 assert(r == std::codecvt_base::ok);
274 assert(wp == &w + 1);
275 assert(np == n + 7);
276 assert(n[0] == char(0xEF));
277 assert(n[1] == char(0xBB));
278 assert(n[2] == char(0xBF));
279 assert(n[3] == char(0xF1));
280 assert(n[4] == char(0x80));
281 assert(n[5] == char(0x80));
282 assert(n[6] == char(0x83));
284 w = 0x1005;
285 r = c.out(m, &w, &w + 1, wp, n, n + 7, np);
286 assert(r == std::codecvt_base::ok);
287 assert(wp == &w + 1);
288 assert(np == n + 6);
289 assert(n[0] == char(0xEF));
290 assert(n[1] == char(0xBB));
291 assert(n[2] == char(0xBF));
292 assert(n[3] == char(0xE1));
293 assert(n[4] == char(0x80));
294 assert(n[5] == char(0x85));
295 assert(n[6] == char(0x83));
297 w = 0x453;
298 r = c.out(m, &w, &w + 1, wp, n, n + 7, np);
299 assert(r == std::codecvt_base::ok);
300 assert(wp == &w + 1);
301 assert(np == n + 5);
302 assert(n[0] == char(0xEF));
303 assert(n[1] == char(0xBB));
304 assert(n[2] == char(0xBF));
305 assert(n[3] == char(0xD1));
306 assert(n[4] == char(0x93));
307 assert(n[5] == char(0x85));
308 assert(n[6] == char(0x83));
310 w = 0x56;
311 r = c.out(m, &w, &w + 1, wp, n, n + 7, np);
312 assert(r == std::codecvt_base::ok);
313 assert(wp == &w + 1);
314 assert(np == n + 4);
315 assert(n[0] == char(0xEF));
316 assert(n[1] == char(0xBB));
317 assert(n[2] == char(0xBF));
318 assert(n[3] == char(0x56));
319 assert(n[4] == char(0x93));
320 assert(n[5] == char(0x85));
321 assert(n[6] == char(0x83));
325 int main(int, char**) {
326 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
327 TestHelper<wchar_t>::test();
328 #endif
329 TestHelper<char32_t>::test();
330 TestHelper<char16_t>::test();
332 return 0;