Debian Codegraph @

libcxx/test/std/time/time.syn/formatter.year.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: no-localization
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11
12 // TODO FMT This test should not require std::to_chars(floating-point)
13 // XFAIL: availability-fp_to_chars-missing
14
15 // REQUIRES: locale.fr_FR.UTF-8
16 // REQUIRES: locale.ja_JP.UTF-8
17
18 // <chrono>
19
20 // template<class charT> struct formatter<chrono::year, charT>;
21
22 #include <chrono>
23 #include <format>
24
25 #include <cassert>
26 #include <concepts>
27 #include <locale>
28 #include <iostream>
29 #include <type_traits>
30
31 #include "formatter_tests.h"
32 #include "make_string.h"
33 #include "platform_support.h" // locale name macros
34 #include "string_literal.h"
35 #include "test_macros.h"
36
37 template <class CharT>
38 static void test_no_chrono_specs() {
39 check(SV("-32767"), SV("{}"), std::chrono::year{-32'767});
40 check(SV("-1000"), SV("{}"), std::chrono::year{-1000});
41 check(SV("-0100"), SV("{}"), std::chrono::year{-100});
42 check(SV("-0010"), SV("{}"), std::chrono::year{-10});
43 check(SV("-0001"), SV("{}"), std::chrono::year{-1});
44 check(SV("0000"), SV("{}"), std::chrono::year{0});
45 check(SV("0001"), SV("{}"), std::chrono::year{1});
46 check(SV("0010"), SV("{}"), std::chrono::year{10});
47 check(SV("0100"), SV("{}"), std::chrono::year{100});
48 check(SV("1000"), SV("{}"), std::chrono::year{1000});
49 check(SV("32727"), SV("{}"), std::chrono::year{32'727});
50
51 // Invalid year
52 check(SV("-32768 is not a valid year"), SV("{}"), std::chrono::year{-32'768});
53 check(SV("-32768 is not a valid year"), SV("{}"), std::chrono::year{32'768});
54 }
55
56 template <class CharT>
57 static void test_valid_values() {
58 constexpr std::basic_string_view<CharT> fmt = SV(
59 "{:"
60 "%%C='%C'%t"
61 "%%EC='%EC'%t"
62 "%%y='%y'%t"
63 "%%Ey='%Ey'%t"
64 "%%Oy='%Oy'%t"
65 "%%Y='%Y'%t"
66 "%%EY='%EY'%t"
67 "%n}");
68 constexpr std::basic_string_view<CharT> lfmt = SV(
69 "{:L"
70 "%%C='%C'%t"
71 "%%EC='%EC'%t"
72 "%%y='%y'%t"
73 "%%Ey='%Ey'%t"
74 "%%Oy='%Oy'%t"
75 "%%Y='%Y'%t"
76 "%%EY='%EY'%t"
77 "%n}");
78
79 const std::locale loc(LOCALE_ja_JP_UTF_8);
80 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
81
82 // Non localized output using C-locale
83 check(SV("%C='00'\t"
84 #if defined(__APPLE__) || defined(_WIN32) || defined(__FreeBSD__)
85 "%EC='00'\t"
86 #else
87 "%EC='0'\t"
88 #endif
89 "%y='00'\t"
90 "%Ey='00'\t"
91 "%Oy='00'\t"
92 "%Y='0000'\t"
93 #if defined(__APPLE__) || defined(_WIN32) || defined(__FreeBSD__)
94 "%EY='0000'\t"
95 #elif defined(_AIX)
96 "%EY=''\t"
97 #else
98 "%EY='0'\t"
99 #endif
100 "\n"),
101 fmt,
102 std::chrono::year{0});
103
104 check(SV("%C='19'\t"
105 "%EC='19'\t"
106 "%y='70'\t"
107 "%Ey='70'\t"
108 "%Oy='70'\t"
109 "%Y='1970'\t"
110 "%EY='1970'\t"
111 "\n"),
112 fmt,
113 std::chrono::year{1970});
114
115 check(SV("%C='20'\t"
116 "%EC='20'\t"
117 "%y='38'\t"
118 "%Ey='38'\t"
119 "%Oy='38'\t"
120 "%Y='2038'\t"
121 "%EY='2038'\t"
122 "\n"),
123 fmt,
124 std::chrono::year{2038});
125
126 // Use the global locale (fr_FR)
127 check(SV("%C='00'\t"
128 #if defined(__APPLE__) || defined(_WIN32) || defined(__FreeBSD__)
129 "%EC='00'\t"
130 #else
131 "%EC='0'\t"
132 #endif
133 "%y='00'\t"
134 "%Ey='00'\t"
135 "%Oy='00'\t"
136 "%Y='0000'\t"
137 #if defined(__APPLE__) || defined(_WIN32) || defined(__FreeBSD__)
138 "%EY='0000'\t"
139 #elif defined(_AIX)
140 "%EY=''\t"
141 #else
142 "%EY='0'\t"
143 #endif
144 "\n"),
145 lfmt,
146 std::chrono::year{0});
147
148 check(SV("%C='19'\t"
149 "%EC='19'\t"
150 "%y='70'\t"
151 "%Ey='70'\t"
152 "%Oy='70'\t"
153 "%Y='1970'\t"
154 "%EY='1970'\t"
155 "\n"),
156 lfmt,
157 std::chrono::year{1970});
158
159 check(SV("%C='20'\t"
160 "%EC='20'\t"
161 "%y='38'\t"
162 "%Ey='38'\t"
163 "%Oy='38'\t"
164 "%Y='2038'\t"
165 "%EY='2038'\t"
166 "\n"),
167 lfmt,
168 std::chrono::year{2038});
169
170 // Use supplied locale (ja_JP). This locale has a different alternate.
171 #if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
172
173 check(SV("%C='00'\t"
174 # if defined(__APPLE__) || defined(_WIN32) || defined(__FreeBSD__)
175 "%EC='00'\t"
176 # else
177 "%EC='0'\t"
178 # endif
179 "%y='00'\t"
180 "%Ey='00'\t"
181 "%Oy='00'\t"
182 "%Y='0000'\t"
183 # if defined(_AIX)
184 "%EY=''\t"
185 # else
186 "%EY='0000'\t"
187 # endif
188 "\n"),
189 lfmt,
190 std::chrono::year{0});
191
192 check(SV("%C='19'\t"
193 "%EC='19'\t"
194 "%y='70'\t"
195 "%Ey='70'\t"
196 "%Oy='70'\t"
197 "%Y='1970'\t"
198 "%EY='1970'\t"
199 "\n"),
200 lfmt,
201 std::chrono::year{1970});
202
203 check(SV("%C='20'\t"
204 "%EC='20'\t"
205 "%y='38'\t"
206 "%Ey='38'\t"
207 "%Oy='38'\t"
208 "%Y='2038'\t"
209 "%EY='2038'\t"
210 "\n"),
211 lfmt,
212 std::chrono::year{2038});
213
214 #else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
215 check(loc,
216 SV("%C='00'\t"
217 "%EC='紀元前'\t"
218 "%y='00'\t"
219 // https://sourceware.org/bugzilla/show_bug.cgi?id=23758
220 # if defined(__GLIBC__) && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 29
221 "%Ey='1'\t"
222 # else
223 "%Ey='01'\t"
224 # endif
225 "%Oy='〇'\t"
226 "%Y='0000'\t"
227 // https://sourceware.org/bugzilla/show_bug.cgi?id=23758
228 # if defined(__GLIBC__) && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 29
229 "%EY='紀元前1年'\t"
230 # else
231 "%EY='紀元前01年'\t"
232 # endif
233 "\n"),
234 lfmt,
235 std::chrono::year{0});
236
237 check(loc,
238 SV("%C='19'\t"
239 "%EC='昭和'\t"
240 "%y='70'\t"
241 "%Ey='45'\t"
242 "%Oy='七十'\t"
243 "%Y='1970'\t"
244 "%EY='昭和45年'\t"
245 "\n"),
246 lfmt,
247 std::chrono::year{1970});
248
249 // Note this test will fail if the Reiwa era ends before 2038.
250 check(loc,
251 SV("%C='20'\t"
252 "%EC='令和'\t"
253 "%y='38'\t"
254 "%Ey='20'\t"
255 "%Oy='三十八'\t"
256 "%Y='2038'\t"
257 "%EY='令和20年'\t"
258 "\n"),
259 lfmt,
260 std::chrono::year{2038});
261 #endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
262
263 std::locale::global(std::locale::classic());
264 }
265
266 template <class CharT>
267 static void test_padding() {
268 constexpr std::basic_string_view<CharT> fmt = SV("{:%%C='%C'%t%%y='%y'%t%%Y='%Y'%t%n}");
269 check(SV("%C='-100'\t%y='99'\t%Y='-9999'\t\n"), fmt, std::chrono::year{-9'999});
270 check(SV("%C='-10'\t%y='99'\t%Y='-0999'\t\n"), fmt, std::chrono::year{-999});
271 check(SV("%C='-1'\t%y='99'\t%Y='-0099'\t\n"), fmt, std::chrono::year{-99});
272 check(SV("%C='-1'\t%y='09'\t%Y='-0009'\t\n"), fmt, std::chrono::year{-9});
273 check(SV("%C='00'\t%y='00'\t%Y='0000'\t\n"), fmt, std::chrono::year{0});
274 check(SV("%C='00'\t%y='09'\t%Y='0009'\t\n"), fmt, std::chrono::year{9});
275 check(SV("%C='00'\t%y='99'\t%Y='0099'\t\n"), fmt, std::chrono::year{99});
276 check(SV("%C='09'\t%y='99'\t%Y='0999'\t\n"), fmt, std::chrono::year{999});
277 check(SV("%C='99'\t%y='99'\t%Y='9999'\t\n"), fmt, std::chrono::year{9'999});
278 check(SV("%C='100'\t%y='00'\t%Y='10000'\t\n"), fmt, std::chrono::year{10'000});
279 }
280
281 template <class CharT>
282 static void test() {
283 test_no_chrono_specs<CharT>();
284 test_valid_values<CharT>();
285 test_padding<CharT>();
286 check_invalid_types<CharT>(
287 {SV("C"), SV("y"), SV("Y"), SV("EC"), SV("Ey"), SV("EY"), SV("Oy")}, std::chrono::year{1970});
288
289 check_exception("The format specifier expects a '%' or a '}'", SV("{:A"), std::chrono::year{1970});
290 check_exception("The chrono specifiers contain a '{'", SV("{:%%{"), std::chrono::year{1970});
291 check_exception("End of input while parsing a conversion specifier", SV("{:%"), std::chrono::year{1970});
292 check_exception("End of input while parsing the modifier E", SV("{:%E"), std::chrono::year{1970});
293 check_exception("End of input while parsing the modifier O", SV("{:%O"), std::chrono::year{1970});
294
295 // Precision not allowed
296 check_exception("The format specifier expects a '%' or a '}'", SV("{:.3}"), std::chrono::year{1970});
297 }
298
299 int main(int, char**) {
300 test<char>();
301
302 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
303 test<wchar_t>();
304 #endif
305
306 return 0;
307 }