Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / localization / locale.stdcvt / codecvt_utf8_length.pass.cpp
blobbea2ba3d15768aa092245b9f154ebdeb4f607398
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
16 // : public codecvt<Elem, char, mbstate_t>
17 // {
18 // // unspecified
19 // };
21 // int length(stateT& state, const externT* from, const externT* from_end,
22 // size_t max) const;
24 #include <codecvt>
25 #include <cassert>
27 #include "test_macros.h"
29 template <class CharT, std::size_t = sizeof(CharT)>
30 struct TestHelper;
32 template <class CharT>
33 struct TestHelper<CharT, 2> {
34 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<CharT> C;
46 C c;
47 char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
48 std::mbstate_t m;
49 int r = c.length(m, n, n + 4, 1);
50 assert(r == 0);
52 n[0] = char(0xE1);
53 n[1] = char(0x80);
54 n[2] = char(0x85);
55 r = c.length(m, n, n + 3, 2);
56 assert(r == 3);
58 n[0] = char(0xD1);
59 n[1] = char(0x93);
60 r = c.length(m, n, n + 2, 3);
61 assert(r == 2);
63 n[0] = char(0x56);
64 r = c.length(m, n, n + 1, 3);
65 assert(r == 1);
68 typedef std::codecvt_utf8<CharT, 0x1000> C;
69 C c;
70 char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
71 std::mbstate_t m;
72 int r = c.length(m, n, n + 4, 1);
73 assert(r == 0);
75 n[0] = char(0xE1);
76 n[1] = char(0x80);
77 n[2] = char(0x85);
78 r = c.length(m, n, n + 3, 2);
79 assert(r == 0);
81 n[0] = char(0xD1);
82 n[1] = char(0x93);
83 r = c.length(m, n, n + 2, 3);
84 assert(r == 2);
86 n[0] = char(0x56);
87 r = c.length(m, n, n + 1, 3);
88 assert(r == 1);
91 typedef std::codecvt_utf8<CharT, 0xFFFFFFFF, std::consume_header> C;
92 C c;
93 char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
94 std::mbstate_t m;
95 int r = c.length(m, n, n + 7, 1);
96 assert(r == 3);
98 n[0] = char(0xE1);
99 n[1] = char(0x80);
100 n[2] = char(0x85);
101 r = c.length(m, n, n + 3, 2);
102 assert(r == 3);
104 n[0] = char(0xEF);
105 n[1] = char(0xBB);
106 n[2] = char(0xBF);
107 n[3] = char(0xD1);
108 n[4] = char(0x93);
109 r = c.length(m, n, n + 5, 3);
110 assert(r == 5);
112 n[0] = char(0x56);
113 r = c.length(m, n, n + 1, 3);
114 assert(r == 1);
118 template <class CharT>
119 void TestHelper<CharT, 4>::test() {
121 typedef std::codecvt_utf8<CharT> C;
122 C c;
123 char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
124 std::mbstate_t m;
125 int r = c.length(m, n, n + 4, 1);
126 assert(r == 4);
128 n[0] = char(0xE1);
129 n[1] = char(0x80);
130 n[2] = char(0x85);
131 r = c.length(m, n, n + 3, 2);
132 assert(r == 3);
134 n[0] = char(0xD1);
135 n[1] = char(0x93);
136 r = c.length(m, n, n + 2, 3);
137 assert(r == 2);
139 n[0] = char(0x56);
140 r = c.length(m, n, n + 1, 3);
141 assert(r == 1);
144 typedef std::codecvt_utf8<CharT, 0x1000> C;
145 C c;
146 char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
147 std::mbstate_t m;
148 int r = c.length(m, n, n + 4, 1);
149 assert(r == 0);
151 n[0] = char(0xE1);
152 n[1] = char(0x80);
153 n[2] = char(0x85);
154 r = c.length(m, n, n + 3, 2);
155 assert(r == 0);
157 n[0] = char(0xD1);
158 n[1] = char(0x93);
159 r = c.length(m, n, n + 2, 3);
160 assert(r == 2);
162 n[0] = char(0x56);
163 r = c.length(m, n, n + 1, 3);
164 assert(r == 1);
167 typedef std::codecvt_utf8<CharT, 0xFFFFFFFF, std::consume_header> C;
168 C c;
169 char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
170 std::mbstate_t m;
171 int r = c.length(m, n, n + 7, 1);
172 assert(r == 7);
174 n[0] = char(0xE1);
175 n[1] = char(0x80);
176 n[2] = char(0x85);
177 r = c.length(m, n, n + 3, 2);
178 assert(r == 3);
180 n[0] = char(0xEF);
181 n[1] = char(0xBB);
182 n[2] = char(0xBF);
183 n[3] = char(0xD1);
184 n[4] = char(0x93);
185 r = c.length(m, n, n + 5, 3);
186 assert(r == 5);
188 n[0] = char(0x56);
189 r = c.length(m, n, n + 1, 3);
190 assert(r == 1);
194 int main(int, char**) {
195 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
196 TestHelper<wchar_t>::test();
197 #endif
198 TestHelper<char32_t>::test();
199 TestHelper<char16_t>::test();
200 return 0;