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 externT* from, const externT* from_end, const externT*& from_next,
22 // internT* to, internT* to_end, internT*& 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
, 0x1000> C
;
46 char n
[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
49 const char* np
= nullptr;
50 std::codecvt_base::result r
= c
.in(m
, n
, n
+ 4, np
, w
, w
+ 2, wp
);
51 assert(r
== std::codecvt_base::error
);
58 r
= c
.in(m
, n
, n
+ 3, np
, w
, w
+ 2, wp
);
59 assert(r
== std::codecvt_base::error
);
65 r
= c
.in(m
, n
, n
+ 2, np
, w
, w
+ 2, wp
);
66 assert(r
== std::codecvt_base::ok
);
69 assert(w
[0] == 0x0453);
72 r
= c
.in(m
, n
, n
+ 1, np
, w
, w
+ 2, wp
);
73 assert(r
== std::codecvt_base::ok
);
76 assert(w
[0] == 0x0056);
79 typedef std::codecvt_utf8_utf16
<CharT
, 0x10ffff, std::consume_header
> C
;
82 char n
[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1),
83 char(0x80), char(0x80), char(0x83)};
86 const char* np
= nullptr;
87 std::codecvt_base::result r
= c
.in(m
, n
, n
+ 7, np
, w
, w
+ 2, wp
);
88 assert(r
== std::codecvt_base::ok
);
91 assert(w
[0] == 0xD8C0);
92 assert(w
[1] == 0xDC03);
97 r
= c
.in(m
, n
, n
+ 3, np
, w
, w
+ 2, wp
);
98 assert(r
== std::codecvt_base::ok
);
101 assert(w
[0] == 0x1005);
105 r
= c
.in(m
, n
, n
+ 2, np
, w
, w
+ 2, wp
);
106 assert(r
== std::codecvt_base::ok
);
109 assert(w
[0] == 0x0453);
112 r
= c
.in(m
, n
, n
+ 1, np
, w
, w
+ 2, wp
);
113 assert(r
== std::codecvt_base::ok
);
116 assert(w
[0] == 0x0056);
120 template <class CharT
>
121 void TestHelper
<CharT
, 4>::test() {
123 typedef std::codecvt_utf8_utf16
<CharT
> C
;
126 char n
[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
129 const char* np
= nullptr;
130 std::codecvt_base::result r
= c
.in(m
, n
, n
+ 4, np
, w
, w
+ 2, wp
);
131 assert(r
== std::codecvt_base::ok
);
134 assert(w
[0] == 0xD8C0);
135 assert(w
[1] == 0xDC03);
140 r
= c
.in(m
, n
, n
+ 3, np
, w
, w
+ 2, wp
);
141 assert(r
== std::codecvt_base::ok
);
144 assert(w
[0] == 0x1005);
148 r
= c
.in(m
, n
, n
+ 2, np
, w
, w
+ 2, wp
);
149 assert(r
== std::codecvt_base::ok
);
152 assert(w
[0] == 0x0453);
155 r
= c
.in(m
, n
, n
+ 1, np
, w
, w
+ 2, wp
);
156 assert(r
== std::codecvt_base::ok
);
159 assert(w
[0] == 0x0056);
162 typedef std::codecvt_utf8_utf16
<CharT
, 0x1000> C
;
165 char n
[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
168 const char* np
= nullptr;
169 std::codecvt_base::result r
= c
.in(m
, n
, n
+ 4, np
, w
, w
+ 2, wp
);
170 assert(r
== std::codecvt_base::error
);
177 r
= c
.in(m
, n
, n
+ 3, np
, w
, w
+ 2, wp
);
178 assert(r
== std::codecvt_base::error
);
184 r
= c
.in(m
, n
, n
+ 2, np
, w
, w
+ 2, wp
);
185 assert(r
== std::codecvt_base::ok
);
188 assert(w
[0] == 0x0453);
191 r
= c
.in(m
, n
, n
+ 1, np
, w
, w
+ 2, wp
);
192 assert(r
== std::codecvt_base::ok
);
195 assert(w
[0] == 0x0056);
198 typedef std::codecvt_utf8_utf16
<CharT
, 0x10ffff, std::consume_header
> C
;
201 char n
[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1),
202 char(0x80), char(0x80), char(0x83)};
205 const char* np
= nullptr;
206 std::codecvt_base::result r
= c
.in(m
, n
, n
+ 7, np
, w
, w
+ 2, wp
);
207 assert(r
== std::codecvt_base::ok
);
210 assert(w
[0] == 0xD8C0);
211 assert(w
[1] == 0xDC03);
216 r
= c
.in(m
, n
, n
+ 3, np
, w
, w
+ 2, wp
);
217 assert(r
== std::codecvt_base::ok
);
220 assert(w
[0] == 0x1005);
224 r
= c
.in(m
, n
, n
+ 2, np
, w
, w
+ 2, wp
);
225 assert(r
== std::codecvt_base::ok
);
228 assert(w
[0] == 0x0453);
231 r
= c
.in(m
, n
, n
+ 1, np
, w
, w
+ 2, wp
);
232 assert(r
== std::codecvt_base::ok
);
235 assert(w
[0] == 0x0056);
239 int main(int, char**) {
241 TestHelper
<wchar_t>::test();
243 TestHelper
<char32_t
>::test();
244 TestHelper
<char16_t
>::test();