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 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
13 // template <class Elem, unsigned long Maxcode = 0x10ffff,
14 // codecvt_mode Mode = (codecvt_mode)0>
16 // : public codecvt<Elem, char, mbstate_t>
23 // const internT* from, const internT* from_end, const internT*& from_next,
24 // externT* to, externT* to_end, externT*& to_next) const;
29 #include "test_macros.h"
31 template <class CharT
, std::size_t = sizeof(CharT
)>
34 template <class CharT
>
35 struct TestHelper
<CharT
, 2> {
39 template <class CharT
>
40 struct TestHelper
<CharT
, 4> {
44 template <class CharT
>
45 void TestHelper
<CharT
, 2>::test() {
47 typedef std::codecvt_utf8
<CharT
> C
;
51 const CharT
* wp
= 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
);
58 assert(n
[0] == char(0xE1));
59 assert(n
[1] == char(0x80));
60 assert(n
[2] == char(0x85));
61 assert(n
[3] == char(0));
64 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
65 assert(r
== std::codecvt_base::ok
);
68 assert(n
[0] == char(0xD1));
69 assert(n
[1] == char(0x93));
70 assert(n
[2] == char(0x85));
71 assert(n
[3] == char(0));
74 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
75 assert(r
== std::codecvt_base::ok
);
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
;
88 const CharT
* wp
= 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
);
95 assert(n
[0] == char(0));
96 assert(n
[1] == char(0));
97 assert(n
[2] == char(0));
98 assert(n
[3] == char(0));
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);
105 assert(n
[0] == char(0xD1));
106 assert(n
[1] == char(0x93));
107 assert(n
[2] == char(0));
108 assert(n
[3] == char(0));
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);
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
;
125 const CharT
* wp
= 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);
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));
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);
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));
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);
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
;
175 const CharT
* wp
= 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);
182 assert(n
[0] == char(0xF1));
183 assert(n
[1] == char(0x80));
184 assert(n
[2] == char(0x80));
185 assert(n
[3] == char(0x83));
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);
192 assert(n
[0] == char(0xE1));
193 assert(n
[1] == char(0x80));
194 assert(n
[2] == char(0x85));
195 assert(n
[3] == char(0x83));
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);
202 assert(n
[0] == char(0xD1));
203 assert(n
[1] == char(0x93));
204 assert(n
[2] == char(0x85));
205 assert(n
[3] == char(0x83));
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);
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
;
222 const CharT
* wp
= 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
);
229 assert(n
[0] == char(0));
230 assert(n
[1] == char(0));
231 assert(n
[2] == char(0));
232 assert(n
[3] == char(0));
235 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
236 assert(r
== std::codecvt_base::error
);
239 assert(n
[0] == char(0));
240 assert(n
[1] == char(0));
241 assert(n
[2] == char(0));
242 assert(n
[3] == char(0));
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);
249 assert(n
[0] == char(0xD1));
250 assert(n
[1] == char(0x93));
251 assert(n
[2] == char(0));
252 assert(n
[3] == char(0));
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);
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
;
269 const CharT
* wp
= 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);
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));
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);
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));
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);
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));
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);
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();
329 TestHelper
<char32_t
>::test();
330 TestHelper
<char16_t
>::test();