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 // This test relies on https://wg21.link/P0482 being implemented, which isn't in
10 // older Apple dylibs
11 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0}}
12
13 // This test runs in C++20, but we have deprecated codecvt<char(16|32), char, mbstate_t> in C++20.
14 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
15
16 // <locale>
17
18 // template <> class codecvt<char32_t, char, mbstate_t>
19 // template <> class codecvt<char16_t, char, mbstate_t>
20 // template <> class codecvt<char32_t, char8_t, mbstate_t> // C++20
21 // template <> class codecvt<char16_t, char8_t, mbstate_t> // C++20
22 // template <> class codecvt<char32_t, char16_t, mbstate_t> // extension
23
24 // sanity check
25
26 #include <locale>
27 #include <codecvt>
28 #include <cassert>
29
30 #include <stdio.h>
31
32 #include "test_macros.h"
33
main(int,char **)34 int main(int, char**) {
35 typedef std::codecvt<char32_t, char, std::mbstate_t> F32_8;
36 typedef std::codecvt<char16_t, char, std::mbstate_t> F16_8;
37 typedef std::codecvt_utf16<char32_t> F32_16;
38 std::locale l = std::locale(std::locale::classic(), new F32_16);
39 const F32_8& f32_8 = std::use_facet<F32_8>(std::locale::classic());
40 const F32_16& f32_16 = std::use_facet<F32_16>(l);
41 const F16_8& f16_8 = std::use_facet<F16_8>(std::locale::classic());
42 std::mbstate_t mbs32_8 = {};
43 std::mbstate_t mbs16_8 = {};
44 std::mbstate_t mbs32_16 = {};
45 F32_8::intern_type* c32p;
46 F16_8::intern_type* c16p;
47 F32_8::extern_type* c8p;
48 const F32_8::intern_type* c_c32p;
49 const F16_8::intern_type* c_c16p;
50 const F32_8::extern_type* c_c8p;
51 F32_8::intern_type c32;
52 F16_8::intern_type c16[2];
53 char c16c[4];
54 char* c16cp;
55 F32_8::extern_type c8[4];
56
57 #if TEST_STD_VER > 17
58 typedef std::codecvt<char32_t, char8_t, std::mbstate_t> F32_8T;
59 typedef std::codecvt<char16_t, char8_t, std::mbstate_t> F16_8T;
60 const F32_8T& f32_8t = std::use_facet<F32_8T>(std::locale::classic());
61 const F16_8T& f16_8t = std::use_facet<F16_8T>(std::locale::classic());
62 std::mbstate_t mbs32_8t = {};
63 std::mbstate_t mbs16_8t = {};
64 F32_8T::extern_type* c8tp;
65 const F32_8T::extern_type* c_c8tp;
66 F32_8T::extern_type c8t[4];
67 #endif
68
69 for (F32_8::intern_type c32x = 0; c32x < 0x110003; ++c32x) {
70 if ((0xD800 <= c32x && c32x < 0xE000) || c32x >= 0x110000) {
71 #ifndef _MSVC_STL_VERSION // Don't ask; eldritch horrors.
72 assert(f32_16.out(mbs32_16, &c32x, &c32x + 1, c_c32p, c16c + 0, c16c + 4,
73 c16cp) == F32_8::error);
74 assert(f32_8.out(mbs32_8, &c32x, &c32x + 1, c_c32p, c8, c8 + 4, c8p) ==
75 F32_8::error);
76 #endif
77
78 #if TEST_STD_VER > 17
79 assert(f32_8t.out(mbs32_8t, &c32x, &c32x + 1, c_c32p, c8t, c8t + 4,
80 c8tp) == F32_8T::error);
81 #endif
82 } else {
83 assert(f32_16.out(mbs32_16, &c32x, &c32x + 1, c_c32p, c16c, c16c + 4,
84 c16cp) == F32_8::ok);
85 assert(c_c32p - &c32x == 1);
86 if (c32x < 0x10000)
87 assert(c16cp - c16c == 2);
88 else
89 assert(c16cp - c16c == 4);
90 for (int i = 0; i < (c16cp - c16c) / 2; ++i)
91 c16[i] = (char16_t)((unsigned char)c16c[2 * i] << 8 |
92 (unsigned char)c16c[2 * i + 1]);
93 c_c16p = c16 + (c16cp - c16c) / 2;
94 assert(f16_8.out(mbs16_8, c16, c_c16p, c_c16p, c8, c8 + 4, c8p) ==
95 F32_8::ok);
96 if (c32x < 0x10000)
97 assert(c_c16p - c16 == 1);
98 else
99 assert(c_c16p - c16 == 2);
100 if (c32x < 0x80)
101 assert(c8p - c8 == 1);
102 else if (c32x < 0x800)
103 assert(c8p - c8 == 2);
104 else if (c32x < 0x10000)
105 assert(c8p - c8 == 3);
106 else
107 assert(c8p - c8 == 4);
108 c_c8p = c8p;
109 assert(f32_8.in(mbs32_8, c8, c_c8p, c_c8p, &c32, &c32 + 1, c32p) ==
110 F32_8::ok);
111 if (c32x < 0x80)
112 assert(c_c8p - c8 == 1);
113 else if (c32x < 0x800)
114 assert(c_c8p - c8 == 2);
115 else if (c32x < 0x10000)
116 assert(c_c8p - c8 == 3);
117 else
118 assert(c_c8p - c8 == 4);
119 assert(c32p - &c32 == 1);
120 assert(c32 == c32x);
121 assert(f32_8.out(mbs32_8, &c32x, &c32x + 1, c_c32p, c8, c8 + 4, c8p) ==
122 F32_8::ok);
123 assert(c_c32p - &c32x == 1);
124 if (c32x < 0x80)
125 assert(c8p - c8 == 1);
126 else if (c32x < 0x800)
127 assert(c8p - c8 == 2);
128 else if (c32x < 0x10000)
129 assert(c8p - c8 == 3);
130 else
131 assert(c8p - c8 == 4);
132 c_c8p = c8p;
133 assert(f16_8.in(mbs16_8, c8, c_c8p, c_c8p, c16, c16 + 2, c16p) ==
134 F32_8::ok);
135 if (c32x < 0x80)
136 assert(c_c8p - c8 == 1);
137 else if (c32x < 0x800)
138 assert(c_c8p - c8 == 2);
139 else if (c32x < 0x10000)
140 assert(c_c8p - c8 == 3);
141 else
142 assert(c_c8p - c8 == 4);
143 if (c32x < 0x10000)
144 assert(c16p - c16 == 1);
145 else
146 assert(c16p - c16 == 2);
147 for (int i = 0; i < c16p - c16; ++i) {
148 c16c[2 * i] = static_cast<char>(c16[i] >> 8);
149 c16c[2 * i + 1] = static_cast<char>(c16[i]);
150 }
151 const char* c_c16cp = c16c + (c16p - c16) * 2;
152 assert(f32_16.in(mbs32_16, c16c, c_c16cp, c_c16cp, &c32, &c32 + 1,
153 c32p) == F32_8::ok);
154 if (c32x < 0x10000)
155 assert(c_c16cp - c16c == 2);
156 else
157 assert(c_c16cp - c16c == 4);
158 assert(c32p - &c32 == 1);
159 assert(c32 == c32x);
160
161 #if TEST_STD_VER > 17
162 assert(f32_8t.out(mbs32_8t, &c32x, &c32x + 1, c_c32p, c8t, c8t + 4,
163 c8tp) == F32_8T::ok);
164 assert(c_c32p - &c32x == 1);
165 if (c32x < 0x80)
166 assert(c8tp - c8t == 1);
167 else if (c32x < 0x800)
168 assert(c8tp - c8t == 2);
169 else if (c32x < 0x10000)
170 assert(c8tp - c8t == 3);
171 else
172 assert(c8tp - c8t == 4);
173 c_c8tp = c8tp;
174 assert(f16_8t.in(mbs16_8t, c8t, c_c8tp, c_c8tp, c16, c16 + 2, c16p) ==
175 F16_8T::ok);
176 if (c32x < 0x80)
177 assert(c_c8tp - c8t == 1);
178 else if (c32x < 0x800)
179 assert(c_c8tp - c8t == 2);
180 else if (c32x < 0x10000)
181 assert(c_c8tp - c8t == 3);
182 else
183 assert(c_c8tp - c8t == 4);
184 if (c32x < 0x10000)
185 assert(c16p - c16 == 1);
186 else
187 assert(c16p - c16 == 2);
188 c_c16p = c16p;
189 assert(f16_8t.out(mbs16_8t, c16, c_c16p, c_c16p, c8t, c8t + 4, c8tp) ==
190 F16_8T::ok);
191 if (c32x < 0x10000)
192 assert(c_c16p - c16 == 1);
193 else
194 assert(c_c16p - c16 == 2);
195 if (c32x < 0x80)
196 assert(c8tp - c8t == 1);
197 else if (c32x < 0x800)
198 assert(c8tp - c8t == 2);
199 else if (c32x < 0x10000)
200 assert(c8tp - c8t == 3);
201 else
202 assert(c8tp - c8t == 4);
203 c_c8tp = c8tp;
204 assert(f32_8t.in(mbs32_8t, c8t, c_c8tp, c_c8tp, &c32, &c32 + 1, c32p) ==
205 F32_8T::ok);
206 if (c32x < 0x80)
207 assert(c_c8tp - c8t == 1);
208 else if (c32x < 0x800)
209 assert(c_c8tp - c8t == 2);
210 else if (c32x < 0x10000)
211 assert(c_c8tp - c8t == 3);
212 else
213 assert(c_c8tp - c8t == 4);
214 assert(c32p - &c32 == 1);
215 assert(c32 == c32x);
216 #endif
217 }
218 }
219
220 return 0;
221 }
222