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 //===----------------------------------------------------------------------===//
8 
9 // <codecvt>
10 
11 // template <class Elem, unsigned long Maxcode = 0x10ffff,
12 //           codecvt_mode Mode = (codecvt_mode)0>
13 // class codecvt_utf8
14 //     : public codecvt<Elem, char, mbstate_t>
15 // {
16 //     // unspecified
17 // };
18 
19 // int length(stateT& state, const externT* from, const externT* from_end,
20 //            size_t max) const;
21 
22 #include <codecvt>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 
main(int,char **)27 int main(int, char**)
28 {
29     {
30         typedef std::codecvt_utf8<wchar_t> C;
31         C c;
32         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
33         std::mbstate_t m;
34         int r = c.length(m, n, n+4, 1);
35         assert(r == 4);
36 
37         n[0] = char(0xE1);
38         n[1] = char(0x80);
39         n[2] = char(0x85);
40         r = c.length(m, n, n+3, 2);
41         assert(r == 3);
42 
43         n[0] = char(0xD1);
44         n[1] = char(0x93);
45         r = c.length(m, n, n+2, 3);
46         assert(r == 2);
47 
48         n[0] = char(0x56);
49         r = c.length(m, n, n+1, 3);
50         assert(r == 1);
51     }
52     {
53         typedef std::codecvt_utf8<wchar_t, 0x1000> C;
54         C c;
55         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
56         std::mbstate_t m;
57         int r = c.length(m, n, n+4, 1);
58         assert(r == 0);
59 
60         n[0] = char(0xE1);
61         n[1] = char(0x80);
62         n[2] = char(0x85);
63         r = c.length(m, n, n+3, 2);
64         assert(r == 0);
65 
66         n[0] = char(0xD1);
67         n[1] = char(0x93);
68         r = c.length(m, n, n+2, 3);
69         assert(r == 2);
70 
71         n[0] = char(0x56);
72         r = c.length(m, n, n+1, 3);
73         assert(r == 1);
74     }
75     {
76         typedef std::codecvt_utf8<wchar_t, 0xFFFFFFFF, std::consume_header> C;
77         C c;
78         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
79         std::mbstate_t m;
80         int r = c.length(m, n, n+7, 1);
81         assert(r == 7);
82 
83         n[0] = char(0xE1);
84         n[1] = char(0x80);
85         n[2] = char(0x85);
86         r = c.length(m, n, n+3, 2);
87         assert(r == 3);
88 
89         n[0] = char(0xEF);
90         n[1] = char(0xBB);
91         n[2] = char(0xBF);
92         n[3] = char(0xD1);
93         n[4] = char(0x93);
94         r = c.length(m, n, n+5, 3);
95         assert(r == 5);
96 
97         n[0] = char(0x56);
98         r = c.length(m, n, n+1, 3);
99         assert(r == 1);
100     }
101     {
102         typedef std::codecvt_utf8<char32_t> C;
103         C c;
104         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
105         std::mbstate_t m;
106         int r = c.length(m, n, n+4, 1);
107         assert(r == 4);
108 
109         n[0] = char(0xE1);
110         n[1] = char(0x80);
111         n[2] = char(0x85);
112         r = c.length(m, n, n+3, 2);
113         assert(r == 3);
114 
115         n[0] = char(0xD1);
116         n[1] = char(0x93);
117         r = c.length(m, n, n+2, 3);
118         assert(r == 2);
119 
120         n[0] = char(0x56);
121         r = c.length(m, n, n+1, 3);
122         assert(r == 1);
123     }
124     {
125         typedef std::codecvt_utf8<char32_t, 0x1000> C;
126         C c;
127         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
128         std::mbstate_t m;
129         int r = c.length(m, n, n+4, 1);
130         assert(r == 0);
131 
132         n[0] = char(0xE1);
133         n[1] = char(0x80);
134         n[2] = char(0x85);
135         r = c.length(m, n, n+3, 2);
136         assert(r == 0);
137 
138         n[0] = char(0xD1);
139         n[1] = char(0x93);
140         r = c.length(m, n, n+2, 3);
141         assert(r == 2);
142 
143         n[0] = char(0x56);
144         r = c.length(m, n, n+1, 3);
145         assert(r == 1);
146     }
147     {
148         typedef std::codecvt_utf8<char32_t, 0xFFFFFFFF, std::consume_header> C;
149         C c;
150         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
151         std::mbstate_t m;
152         int r = c.length(m, n, n+7, 1);
153         assert(r == 7);
154 
155         n[0] = char(0xE1);
156         n[1] = char(0x80);
157         n[2] = char(0x85);
158         r = c.length(m, n, n+3, 2);
159         assert(r == 3);
160 
161         n[0] = char(0xEF);
162         n[1] = char(0xBB);
163         n[2] = char(0xBF);
164         n[3] = char(0xD1);
165         n[4] = char(0x93);
166         r = c.length(m, n, n+5, 3);
167         assert(r == 5);
168 
169         n[0] = char(0x56);
170         r = c.length(m, n, n+1, 3);
171         assert(r == 1);
172     }
173     {
174         typedef std::codecvt_utf8<char16_t> C;
175         C c;
176         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
177         std::mbstate_t m;
178         int r = c.length(m, n, n+4, 1);
179         assert(r == 0);
180 
181         n[0] = char(0xE1);
182         n[1] = char(0x80);
183         n[2] = char(0x85);
184         r = c.length(m, n, n+3, 2);
185         assert(r == 3);
186 
187         n[0] = char(0xD1);
188         n[1] = char(0x93);
189         r = c.length(m, n, n+2, 3);
190         assert(r == 2);
191 
192         n[0] = char(0x56);
193         r = c.length(m, n, n+1, 3);
194         assert(r == 1);
195     }
196     {
197         typedef std::codecvt_utf8<char16_t, 0x1000> C;
198         C c;
199         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
200         std::mbstate_t m;
201         int r = c.length(m, n, n+4, 1);
202         assert(r == 0);
203 
204         n[0] = char(0xE1);
205         n[1] = char(0x80);
206         n[2] = char(0x85);
207         r = c.length(m, n, n+3, 2);
208         assert(r == 0);
209 
210         n[0] = char(0xD1);
211         n[1] = char(0x93);
212         r = c.length(m, n, n+2, 3);
213         assert(r == 2);
214 
215         n[0] = char(0x56);
216         r = c.length(m, n, n+1, 3);
217         assert(r == 1);
218     }
219     {
220         typedef std::codecvt_utf8<char16_t, 0xFFFFFFFF, std::consume_header> C;
221         C c;
222         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
223         std::mbstate_t m;
224         int r = c.length(m, n, n+7, 1);
225         assert(r == 3);
226 
227         n[0] = char(0xE1);
228         n[1] = char(0x80);
229         n[2] = char(0x85);
230         r = c.length(m, n, n+3, 2);
231         assert(r == 3);
232 
233         n[0] = char(0xEF);
234         n[1] = char(0xBB);
235         n[2] = char(0xBF);
236         n[3] = char(0xD1);
237         n[4] = char(0x93);
238         r = c.length(m, n, n+5, 3);
239         assert(r == 5);
240 
241         n[0] = char(0x56);
242         r = c.length(m, n, n+1, 3);
243         assert(r == 1);
244     }
245 
246   return 0;
247 }
248