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 // template <class Elem, unsigned long Maxcode = 0x10ffff,
12 // codecvt_mode Mode = (codecvt_mode)0>
14 // : public codecvt<Elem, char, mbstate_t>
21 // const internT* from, const internT* from_end, const internT*& from_next,
22 // externT* to, externT* to_end, externT*& to_next) const;
27 #include "test_macros.h"
29 template <class CharT
, size_t = sizeof(CharT
)>
32 template <class CharT
>
33 struct TestHelper
<CharT
, 2> {
37 template <class CharT
>
38 struct TestHelper
<CharT
, 4> {
42 template <class CharT
>
43 void TestHelper
<CharT
, 2>::test() {
45 typedef std::codecvt_utf8
<CharT
> C
;
49 const CharT
* wp
= nullptr;
52 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
53 assert(r
== std::codecvt_base::ok
);
56 assert(n
[0] == char(0xE1));
57 assert(n
[1] == char(0x80));
58 assert(n
[2] == char(0x85));
59 assert(n
[3] == char(0));
62 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
63 assert(r
== std::codecvt_base::ok
);
66 assert(n
[0] == char(0xD1));
67 assert(n
[1] == char(0x93));
68 assert(n
[2] == char(0x85));
69 assert(n
[3] == char(0));
72 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
73 assert(r
== std::codecvt_base::ok
);
76 assert(n
[0] == char(0x56));
77 assert(n
[1] == char(0x93));
78 assert(n
[2] == char(0x85));
79 assert(n
[3] == char(0));
82 typedef std::codecvt_utf8
<CharT
, 0x1000> C
;
86 const CharT
* wp
= nullptr;
89 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
90 assert(r
== std::codecvt_base::error
);
93 assert(n
[0] == char(0));
94 assert(n
[1] == char(0));
95 assert(n
[2] == char(0));
96 assert(n
[3] == char(0));
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);
103 assert(n
[0] == char(0xD1));
104 assert(n
[1] == char(0x93));
105 assert(n
[2] == char(0));
106 assert(n
[3] == char(0));
109 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
110 assert(r
== std::codecvt_base::ok
);
111 assert(wp
== &w
+ 1);
113 assert(n
[0] == char(0x56));
114 assert(n
[1] == char(0x93));
115 assert(n
[2] == char(0));
116 assert(n
[3] == char(0));
119 typedef std::codecvt_utf8
<CharT
, 0xFFFFFFFF, std::generate_header
> C
;
123 const CharT
* wp
= nullptr;
126 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 7, np
);
127 assert(r
== std::codecvt_base::ok
);
128 assert(wp
== &w
+ 1);
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 assert(n
[6] == char(0));
139 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 7, np
);
140 assert(r
== std::codecvt_base::ok
);
141 assert(wp
== &w
+ 1);
143 assert(n
[0] == char(0xEF));
144 assert(n
[1] == char(0xBB));
145 assert(n
[2] == char(0xBF));
146 assert(n
[3] == char(0xD1));
147 assert(n
[4] == char(0x93));
148 assert(n
[5] == char(0x85));
149 assert(n
[6] == char(0));
152 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 7, np
);
153 assert(r
== std::codecvt_base::ok
);
154 assert(wp
== &w
+ 1);
156 assert(n
[0] == char(0xEF));
157 assert(n
[1] == char(0xBB));
158 assert(n
[2] == char(0xBF));
159 assert(n
[3] == char(0x56));
160 assert(n
[4] == char(0x93));
161 assert(n
[5] == char(0x85));
162 assert(n
[6] == char(0));
166 template <class CharT
>
167 void TestHelper
<CharT
, 4>::test() {
169 typedef std::codecvt_utf8
<CharT
> C
;
173 const CharT
* wp
= nullptr;
176 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
177 assert(r
== std::codecvt_base::ok
);
178 assert(wp
== &w
+ 1);
180 assert(n
[0] == char(0xF1));
181 assert(n
[1] == char(0x80));
182 assert(n
[2] == char(0x80));
183 assert(n
[3] == char(0x83));
186 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
187 assert(r
== std::codecvt_base::ok
);
188 assert(wp
== &w
+ 1);
190 assert(n
[0] == char(0xE1));
191 assert(n
[1] == char(0x80));
192 assert(n
[2] == char(0x85));
193 assert(n
[3] == char(0x83));
196 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
197 assert(r
== std::codecvt_base::ok
);
198 assert(wp
== &w
+ 1);
200 assert(n
[0] == char(0xD1));
201 assert(n
[1] == char(0x93));
202 assert(n
[2] == char(0x85));
203 assert(n
[3] == char(0x83));
206 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
207 assert(r
== std::codecvt_base::ok
);
208 assert(wp
== &w
+ 1);
210 assert(n
[0] == char(0x56));
211 assert(n
[1] == char(0x93));
212 assert(n
[2] == char(0x85));
213 assert(n
[3] == char(0x83));
216 typedef std::codecvt_utf8
<CharT
, 0x1000> C
;
220 const CharT
* wp
= nullptr;
223 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
224 assert(r
== std::codecvt_base::error
);
227 assert(n
[0] == char(0));
228 assert(n
[1] == char(0));
229 assert(n
[2] == char(0));
230 assert(n
[3] == char(0));
233 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
234 assert(r
== std::codecvt_base::error
);
237 assert(n
[0] == char(0));
238 assert(n
[1] == char(0));
239 assert(n
[2] == char(0));
240 assert(n
[3] == char(0));
243 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
244 assert(r
== std::codecvt_base::ok
);
245 assert(wp
== &w
+ 1);
247 assert(n
[0] == char(0xD1));
248 assert(n
[1] == char(0x93));
249 assert(n
[2] == char(0));
250 assert(n
[3] == char(0));
253 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 4, np
);
254 assert(r
== std::codecvt_base::ok
);
255 assert(wp
== &w
+ 1);
257 assert(n
[0] == char(0x56));
258 assert(n
[1] == char(0x93));
259 assert(n
[2] == char(0));
260 assert(n
[3] == char(0));
263 typedef std::codecvt_utf8
<CharT
, 0xFFFFFFFF, std::generate_header
> C
;
267 const CharT
* wp
= nullptr;
270 std::codecvt_base::result r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 7, np
);
271 assert(r
== std::codecvt_base::ok
);
272 assert(wp
== &w
+ 1);
274 assert(n
[0] == char(0xEF));
275 assert(n
[1] == char(0xBB));
276 assert(n
[2] == char(0xBF));
277 assert(n
[3] == char(0xF1));
278 assert(n
[4] == char(0x80));
279 assert(n
[5] == char(0x80));
280 assert(n
[6] == char(0x83));
283 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 7, np
);
284 assert(r
== std::codecvt_base::ok
);
285 assert(wp
== &w
+ 1);
287 assert(n
[0] == char(0xEF));
288 assert(n
[1] == char(0xBB));
289 assert(n
[2] == char(0xBF));
290 assert(n
[3] == char(0xE1));
291 assert(n
[4] == char(0x80));
292 assert(n
[5] == char(0x85));
293 assert(n
[6] == char(0x83));
296 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 7, np
);
297 assert(r
== std::codecvt_base::ok
);
298 assert(wp
== &w
+ 1);
300 assert(n
[0] == char(0xEF));
301 assert(n
[1] == char(0xBB));
302 assert(n
[2] == char(0xBF));
303 assert(n
[3] == char(0xD1));
304 assert(n
[4] == char(0x93));
305 assert(n
[5] == char(0x85));
306 assert(n
[6] == char(0x83));
309 r
= c
.out(m
, &w
, &w
+ 1, wp
, n
, n
+ 7, np
);
310 assert(r
== std::codecvt_base::ok
);
311 assert(wp
== &w
+ 1);
313 assert(n
[0] == char(0xEF));
314 assert(n
[1] == char(0xBB));
315 assert(n
[2] == char(0xBF));
316 assert(n
[3] == char(0x56));
317 assert(n
[4] == char(0x93));
318 assert(n
[5] == char(0x85));
319 assert(n
[6] == char(0x83));
323 int main(int, char**) {
324 TestHelper
<wchar_t>::test();
325 TestHelper
<char32_t
>::test();
326 TestHelper
<char16_t
>::test();