Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / localization / locale.stdcvt / codecvt_utf8_utf16_in.pass.cpp
blob0cf6290418daa140bb0ab1d8697c3295ea681e64
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 -D_LIBCPP_ENABLE_CXX26_REMOVED_CODECVT
13 // template <class Elem, unsigned long Maxcode = 0x10ffff,
14 // codecvt_mode Mode = (codecvt_mode)0>
15 // class codecvt_utf8_utf16
16 // : public codecvt<Elem, char, mbstate_t>
17 // {
18 // // unspecified
19 // };
21 // result
22 // in(stateT& state,
23 // const externT* from, const externT* from_end, const externT*& from_next,
24 // internT* to, internT* to_end, internT*& 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;
33 template <class CharT>
34 struct TestHelper<CharT, 2> {
35 static void test();
37 template <class CharT>
38 struct TestHelper<CharT, 4> {
39 static void test();
42 template <class CharT>
43 void TestHelper<CharT, 2>::test() {
45 typedef std::codecvt_utf8_utf16<CharT, 0x1000> C;
46 C c;
47 CharT w[2] = {0};
48 char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
49 CharT* wp = nullptr;
50 std::mbstate_t m;
51 const char* np = nullptr;
52 std::codecvt_base::result r = c.in(m, n, n + 4, np, w, w + 2, wp);
53 assert(r == std::codecvt_base::error);
54 assert(wp == w);
55 assert(np == n);
57 n[0] = char(0xE1);
58 n[1] = char(0x80);
59 n[2] = char(0x85);
60 r = c.in(m, n, n + 3, np, w, w + 2, wp);
61 assert(r == std::codecvt_base::error);
62 assert(wp == w);
63 assert(np == n);
65 n[0] = char(0xD1);
66 n[1] = char(0x93);
67 r = c.in(m, n, n + 2, np, w, w + 2, wp);
68 assert(r == std::codecvt_base::ok);
69 assert(wp == w + 1);
70 assert(np == n + 2);
71 assert(w[0] == 0x0453);
73 n[0] = char(0x56);
74 r = c.in(m, n, n + 1, np, w, w + 2, wp);
75 assert(r == std::codecvt_base::ok);
76 assert(wp == w + 1);
77 assert(np == n + 1);
78 assert(w[0] == 0x0056);
81 typedef std::codecvt_utf8_utf16<CharT, 0x10ffff, std::consume_header> C;
82 C c;
83 CharT w[2] = {0};
84 char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1),
85 char(0x80), char(0x80), char(0x83)};
86 CharT* wp = nullptr;
87 std::mbstate_t m;
88 const char* np = nullptr;
89 std::codecvt_base::result r = c.in(m, n, n + 7, np, w, w + 2, wp);
90 assert(r == std::codecvt_base::ok);
91 assert(wp == w + 2);
92 assert(np == n + 7);
93 assert(w[0] == 0xD8C0);
94 assert(w[1] == 0xDC03);
96 n[0] = char(0xE1);
97 n[1] = char(0x80);
98 n[2] = char(0x85);
99 r = c.in(m, n, n + 3, np, w, w + 2, wp);
100 assert(r == std::codecvt_base::ok);
101 assert(wp == w + 1);
102 assert(np == n + 3);
103 assert(w[0] == 0x1005);
105 n[0] = char(0xD1);
106 n[1] = char(0x93);
107 r = c.in(m, n, n + 2, np, w, w + 2, wp);
108 assert(r == std::codecvt_base::ok);
109 assert(wp == w + 1);
110 assert(np == n + 2);
111 assert(w[0] == 0x0453);
113 n[0] = char(0x56);
114 r = c.in(m, n, n + 1, np, w, w + 2, wp);
115 assert(r == std::codecvt_base::ok);
116 assert(wp == w + 1);
117 assert(np == n + 1);
118 assert(w[0] == 0x0056);
122 template <class CharT>
123 void TestHelper<CharT, 4>::test() {
125 typedef std::codecvt_utf8_utf16<CharT> C;
126 C c;
127 CharT w[2] = {0};
128 char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
129 CharT* wp = nullptr;
130 std::mbstate_t m;
131 const char* np = nullptr;
132 std::codecvt_base::result r = c.in(m, n, n + 4, np, w, w + 2, wp);
133 assert(r == std::codecvt_base::ok);
134 assert(wp == w + 2);
135 assert(np == n + 4);
136 assert(w[0] == 0xD8C0);
137 assert(w[1] == 0xDC03);
139 n[0] = char(0xE1);
140 n[1] = char(0x80);
141 n[2] = char(0x85);
142 r = c.in(m, n, n + 3, np, w, w + 2, wp);
143 assert(r == std::codecvt_base::ok);
144 assert(wp == w + 1);
145 assert(np == n + 3);
146 assert(w[0] == 0x1005);
148 n[0] = char(0xD1);
149 n[1] = char(0x93);
150 r = c.in(m, n, n + 2, np, w, w + 2, wp);
151 assert(r == std::codecvt_base::ok);
152 assert(wp == w + 1);
153 assert(np == n + 2);
154 assert(w[0] == 0x0453);
156 n[0] = char(0x56);
157 r = c.in(m, n, n + 1, np, w, w + 2, wp);
158 assert(r == std::codecvt_base::ok);
159 assert(wp == w + 1);
160 assert(np == n + 1);
161 assert(w[0] == 0x0056);
164 typedef std::codecvt_utf8_utf16<CharT, 0x1000> C;
165 C c;
166 CharT w[2] = {0};
167 char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
168 CharT* wp = nullptr;
169 std::mbstate_t m;
170 const char* np = nullptr;
171 std::codecvt_base::result r = c.in(m, n, n + 4, np, w, w + 2, wp);
172 assert(r == std::codecvt_base::error);
173 assert(wp == w);
174 assert(np == n);
176 n[0] = char(0xE1);
177 n[1] = char(0x80);
178 n[2] = char(0x85);
179 r = c.in(m, n, n + 3, np, w, w + 2, wp);
180 assert(r == std::codecvt_base::error);
181 assert(wp == w);
182 assert(np == n);
184 n[0] = char(0xD1);
185 n[1] = char(0x93);
186 r = c.in(m, n, n + 2, np, w, w + 2, wp);
187 assert(r == std::codecvt_base::ok);
188 assert(wp == w + 1);
189 assert(np == n + 2);
190 assert(w[0] == 0x0453);
192 n[0] = char(0x56);
193 r = c.in(m, n, n + 1, np, w, w + 2, wp);
194 assert(r == std::codecvt_base::ok);
195 assert(wp == w + 1);
196 assert(np == n + 1);
197 assert(w[0] == 0x0056);
200 typedef std::codecvt_utf8_utf16<CharT, 0x10ffff, std::consume_header> C;
201 C c;
202 CharT w[2] = {0};
203 char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1),
204 char(0x80), char(0x80), char(0x83)};
205 CharT* wp = nullptr;
206 std::mbstate_t m;
207 const char* np = nullptr;
208 std::codecvt_base::result r = c.in(m, n, n + 7, np, w, w + 2, wp);
209 assert(r == std::codecvt_base::ok);
210 assert(wp == w + 2);
211 assert(np == n + 7);
212 assert(w[0] == 0xD8C0);
213 assert(w[1] == 0xDC03);
215 n[0] = char(0xE1);
216 n[1] = char(0x80);
217 n[2] = char(0x85);
218 r = c.in(m, n, n + 3, np, w, w + 2, wp);
219 assert(r == std::codecvt_base::ok);
220 assert(wp == w + 1);
221 assert(np == n + 3);
222 assert(w[0] == 0x1005);
224 n[0] = char(0xD1);
225 n[1] = char(0x93);
226 r = c.in(m, n, n + 2, np, w, w + 2, wp);
227 assert(r == std::codecvt_base::ok);
228 assert(wp == w + 1);
229 assert(np == n + 2);
230 assert(w[0] == 0x0453);
232 n[0] = char(0x56);
233 r = c.in(m, n, n + 1, np, w, w + 2, wp);
234 assert(r == std::codecvt_base::ok);
235 assert(wp == w + 1);
236 assert(np == n + 1);
237 assert(w[0] == 0x0056);
241 int main(int, char**) {
242 #if !defined(_WIN32) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
243 TestHelper<wchar_t>::test();
244 #endif
245 TestHelper<char32_t>::test();
246 TestHelper<char16_t>::test();
248 return 0;