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>
13 // class codecvt_utf8_utf16
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
)>
31 template <class CharT
>
32 struct TestHelper
<CharT
, 2> {
35 template <class CharT
>
36 struct TestHelper
<CharT
, 4> {
40 template <class CharT
>
41 void TestHelper
<CharT
, 2>::test() {
43 typedef std::codecvt_utf8_utf16
<CharT
> C
;
45 CharT w
[2] = {0xD8C0, 0xDC03};
47 const CharT
* wp
= nullptr;
50 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
51 assert(r
== std::codecvt_base::ok
);
54 assert(n
[0] == char(0xF1));
55 assert(n
[1] == char(0x80));
56 assert(n
[2] == char(0x80));
57 assert(n
[3] == char(0x83));
60 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
61 assert(r
== std::codecvt_base::ok
);
64 assert(n
[0] == char(0xE1));
65 assert(n
[1] == char(0x80));
66 assert(n
[2] == char(0x85));
69 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
70 assert(r
== std::codecvt_base::ok
);
73 assert(n
[0] == char(0xD1));
74 assert(n
[1] == char(0x93));
77 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
78 assert(r
== std::codecvt_base::ok
);
81 assert(n
[0] == char(0x56));
84 typedef std::codecvt_utf8_utf16
<CharT
, 0x1000> C
;
86 CharT w
[2] = {0xD8C0, 0xDC03};
88 const CharT
* wp
= nullptr;
91 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
92 assert(r
== std::codecvt_base::error
);
97 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
98 assert(r
== std::codecvt_base::error
);
103 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
104 assert(r
== std::codecvt_base::ok
);
107 assert(n
[0] == char(0xD1));
108 assert(n
[1] == char(0x93));
111 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
112 assert(r
== std::codecvt_base::ok
);
115 assert(n
[0] == char(0x56));
118 typedef std::codecvt_utf8_utf16
<CharT
, 0x10ffff, std::generate_header
> C
;
120 CharT w
[2] = {0xD8C0, 0xDC03};
122 const CharT
* wp
= nullptr;
125 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 7, np
);
126 assert(r
== std::codecvt_base::ok
);
129 assert(n
[0] == char(0xEF));
130 assert(n
[1] == char(0xBB));
131 assert(n
[2] == char(0xBF));
132 assert(n
[3] == char(0xF1));
133 assert(n
[4] == char(0x80));
134 assert(n
[5] == char(0x80));
135 assert(n
[6] == char(0x83));
138 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
139 assert(r
== std::codecvt_base::ok
);
142 assert(n
[0] == char(0xEF));
143 assert(n
[1] == char(0xBB));
144 assert(n
[2] == char(0xBF));
145 assert(n
[3] == char(0xE1));
146 assert(n
[4] == char(0x80));
147 assert(n
[5] == char(0x85));
150 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
151 assert(r
== std::codecvt_base::ok
);
154 assert(n
[0] == char(0xEF));
155 assert(n
[1] == char(0xBB));
156 assert(n
[2] == char(0xBF));
157 assert(n
[3] == char(0xD1));
158 assert(n
[4] == char(0x93));
161 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
162 assert(r
== std::codecvt_base::ok
);
165 assert(n
[0] == char(0xEF));
166 assert(n
[1] == char(0xBB));
167 assert(n
[2] == char(0xBF));
168 assert(n
[3] == char(0x56));
172 template <class CharT
>
173 void TestHelper
<CharT
, 4>::test() {
175 typedef std::codecvt_utf8_utf16
<CharT
> C
;
177 CharT w
[2] = {0xD8C0, 0xDC03};
179 const CharT
* wp
= nullptr;
182 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
183 assert(r
== std::codecvt_base::ok
);
186 assert(n
[0] == char(0xF1));
187 assert(n
[1] == char(0x80));
188 assert(n
[2] == char(0x80));
189 assert(n
[3] == char(0x83));
192 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
193 assert(r
== std::codecvt_base::ok
);
196 assert(n
[0] == char(0xE1));
197 assert(n
[1] == char(0x80));
198 assert(n
[2] == char(0x85));
201 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
202 assert(r
== std::codecvt_base::ok
);
205 assert(n
[0] == char(0xD1));
206 assert(n
[1] == char(0x93));
209 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
210 assert(r
== std::codecvt_base::ok
);
213 assert(n
[0] == char(0x56));
216 typedef std::codecvt_utf8_utf16
<CharT
, 0x1000> C
;
218 CharT w
[2] = {0xD8C0, 0xDC03};
220 const CharT
* wp
= nullptr;
223 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 4, np
);
224 assert(r
== std::codecvt_base::error
);
229 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
230 assert(r
== std::codecvt_base::error
);
235 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
236 assert(r
== std::codecvt_base::ok
);
239 assert(n
[0] == char(0xD1));
240 assert(n
[1] == char(0x93));
243 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 4, np
);
244 assert(r
== std::codecvt_base::ok
);
247 assert(n
[0] == char(0x56));
250 typedef std::codecvt_utf8_utf16
<CharT
, 0x10ffff, std::generate_header
> C
;
252 CharT w
[2] = {0xD8C0, 0xDC03};
254 const CharT
* wp
= nullptr;
257 std::codecvt_base::result r
= c
.out(m
, w
, w
+ 2, wp
, n
, n
+ 7, np
);
258 assert(r
== std::codecvt_base::ok
);
261 assert(n
[0] == char(0xEF));
262 assert(n
[1] == char(0xBB));
263 assert(n
[2] == char(0xBF));
264 assert(n
[3] == char(0xF1));
265 assert(n
[4] == char(0x80));
266 assert(n
[5] == char(0x80));
267 assert(n
[6] == char(0x83));
270 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
271 assert(r
== std::codecvt_base::ok
);
274 assert(n
[0] == char(0xEF));
275 assert(n
[1] == char(0xBB));
276 assert(n
[2] == char(0xBF));
277 assert(n
[3] == char(0xE1));
278 assert(n
[4] == char(0x80));
279 assert(n
[5] == char(0x85));
282 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
283 assert(r
== std::codecvt_base::ok
);
286 assert(n
[0] == char(0xEF));
287 assert(n
[1] == char(0xBB));
288 assert(n
[2] == char(0xBF));
289 assert(n
[3] == char(0xD1));
290 assert(n
[4] == char(0x93));
293 r
= c
.out(m
, w
, w
+ 1, wp
, n
, n
+ 7, np
);
294 assert(r
== std::codecvt_base::ok
);
297 assert(n
[0] == char(0xEF));
298 assert(n
[1] == char(0xBB));
299 assert(n
[2] == char(0xBF));
300 assert(n
[3] == char(0x56));
304 int main(int, char**) {
306 TestHelper
<wchar_t>::test();
308 TestHelper
<char32_t
>::test();
309 TestHelper
<char16_t
>::test();