Debian Codegraph @

libcxx/test/std/time/time.syn/formatter.year_month.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_month, 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 // Valid month
40 check(SV("1970/Jan"), SV("{}"), std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{1}});
41 check(SV("*1970/Jan*"), SV("{:*^10}"), std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{1}});
42 check(SV("*1970/Jan"), SV("{:*>9}"), std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{1}});
43
44 // Invalid month_day
45 check(SV("1970/0 is not a valid month"),
46 SV("{}"),
47 std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{0}});
48 check(SV("*1970/0 is not a valid month*"),
49 SV("{:*^29}"),
50 std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{0}});
51 }
52
53 template <class CharT>
54 static void test_invalid_values() {
55 // Test that %b and %B throw an exception.
56 check_exception("Formatting a month name from an invalid month number",
57 SV("{:%b}"),
58 std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{0}});
59
60 check_exception("Formatting a month name from an invalid month number",
61 SV("{:%B}"),
62 std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{0}});
63 }
64
65 template <class CharT>
66 static void test_valid_values() {
67 constexpr std::basic_string_view<CharT> fmt = SV(
68 "{:"
69 "%%b='%b'%t"
70 "%%B='%B'%t"
71 "%%C='%C'%t"
72 "%%h='%h'%t"
73 "%%y='%y'%t"
74 "%%Y='%Y'%t"
75 "%%EC='%EC'%t"
76 "%%Ey='%Ey'%t"
77 "%%EY='%EY'%t"
78 "%%Oy='%Oy'%t"
79 "%n}");
80
81 constexpr std::basic_string_view<CharT> lfmt = SV(
82 "{:L"
83 "%%b='%b'%t"
84 "%%B='%B'%t"
85 "%%C='%C'%t"
86 "%%h='%h'%t"
87 "%%y='%y'%t"
88 "%%Y='%Y'%t"
89 "%%EC='%EC'%t"
90 "%%Ey='%Ey'%t"
91 "%%EY='%EY'%t"
92 "%%Oy='%Oy'%t"
93 "%n}");
94
95 const std::locale loc(LOCALE_ja_JP_UTF_8);
96 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
97
98 // Non localized output using C-locale
99 check(SV("%b='Jan'\t"
100 "%B='January'\t"
101 "%C='19'\t"
102 "%h='Jan'\t"
103 "%y='70'\t"
104 "%Y='1970'\t"
105 "%EC='19'\t"
106 "%Ey='70'\t"
107 "%EY='1970'\t"
108 "%Oy='70'\t"
109 "\n"),
110 fmt,
111 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
112
113 check(SV("%b='May'\t"
114 "%B='May'\t"
115 "%C='20'\t"
116 "%h='May'\t"
117 "%y='04'\t"
118 "%Y='2004'\t"
119 "%EC='20'\t"
120 "%Ey='04'\t"
121 "%EY='2004'\t"
122 "%Oy='04'\t"
123 "\n"),
124 fmt,
125 std::chrono::year_month{std::chrono::year{2004}, std::chrono::May});
126
127 // Use the global locale (fr_FR)
128 check(SV(
129 #if defined(__APPLE__)
130 "%b='jan'\t"
131 #else
132 "%b='janv.'\t"
133 #endif
134 "%B='janvier'\t"
135 "%C='19'\t"
136 #if defined(__APPLE__)
137 "%h='jan'\t"
138 #else
139 "%h='janv.'\t"
140 #endif
141 "%y='70'\t"
142 "%Y='1970'\t"
143 "%EC='19'\t"
144 "%Ey='70'\t"
145 "%EY='1970'\t"
146 "%Oy='70'\t"
147 "\n"),
148 lfmt,
149 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
150
151 check(SV("%b='mai'\t"
152 "%B='mai'\t"
153 "%C='20'\t"
154 "%h='mai'\t"
155 "%y='04'\t"
156 "%Y='2004'\t"
157 "%EC='20'\t"
158 "%Ey='04'\t"
159 "%EY='2004'\t"
160 "%Oy='04'\t"
161 "\n"),
162 lfmt,
163 std::chrono::year_month{std::chrono::year{2004}, std::chrono::May});
164
165 // Use supplied locale (ja_JP)
166 check(loc,
167 SV(
168 #if defined(_WIN32)
169 "%b='1'\t"
170 #elif defined(_AIX) // defined(_WIN32)
171 "%b='1月'\t"
172 #elif defined(__APPLE__) // defined(_WIN32)
173 "%b=' 1'\t"
174 #else // defined(_WIN32)
175 "%b=' 1月'\t"
176 #endif // defined(_WIN32)
177 "%B='1月'\t"
178 "%C='19'\t"
179 #if defined(_WIN32)
180 "%h='1'\t"
181 #elif defined(_AIX) // defined(_WIN32)
182 "%h='1月'\t"
183 #elif defined(__APPLE__) // defined(_WIN32)
184 "%h=' 1'\t"
185 #else // defined(_WIN32)
186 "%h=' 1月'\t"
187 #endif // defined(_WIN32)
188 "%y='70'\t"
189 "%Y='1970'\t"
190 #if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
191 "%EC='19'\t"
192 "%Ey='70'\t"
193 "%EY='1970'\t"
194 "%Oy='70'\t"
195 #else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
196 "%EC='昭和'\t"
197 "%Ey='45'\t"
198 "%EY='昭和45年'\t"
199 "%Oy='七十'\t"
200 #endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
201 "\n"),
202 lfmt,
203 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
204
205 check(loc,
206 SV(
207
208 #if defined(_WIN32)
209 "%b='5'\t"
210 #elif defined(_AIX) // defined(_WIN32)
211 "%b='5月'\t"
212 #elif defined(__APPLE__) // defined(_WIN32)
213 "%b=' 5'\t"
214 #else // defined(_WIN32)
215 "%b=' 5月'\t"
216 #endif // defined(_WIN32)
217 "%B='5月'\t"
218 "%C='20'\t"
219 #if defined(_WIN32)
220 "%h='5'\t"
221 #elif defined(_AIX) // defined(_WIN32)
222 "%h='5月'\t"
223 #elif defined(__APPLE__) // defined(_WIN32)
224 "%h=' 5'\t"
225 #else // defined(_WIN32)
226 "%h=' 5月'\t"
227 #endif // defined(_WIN32)
228 "%y='04'\t"
229 "%Y='2004'\t"
230 #if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
231 "%EC='20'\t"
232 "%Ey='04'\t"
233 "%EY='2004'\t"
234 "%Oy='04'\t"
235 #else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
236 "%EC='平成'\t"
237 "%Ey='16'\t"
238 "%EY='平成16年'\t"
239 "%Oy='四'\t"
240 #endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
241 "\n"),
242 lfmt,
243 std::chrono::year_month{std::chrono::year{2004}, std::chrono::May});
244
245 std::locale::global(std::locale::classic());
246 }
247
248 template <class CharT>
249 static void test() {
250 test_no_chrono_specs<CharT>();
251 test_invalid_values<CharT>();
252 test_valid_values<CharT>();
253
254 check_invalid_types<CharT>(
255 {SV("b"), SV("B"), SV("C"), SV("EC"), SV("Ey"), SV("EY"), SV("h"), SV("m"), SV("Om"), SV("Oy"), SV("y"), SV("Y")},
256 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
257
258 check_exception("The format specifier expects a '%' or a '}'",
259 SV("{:A"),
260 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
261 check_exception("The chrono specifiers contain a '{'",
262 SV("{:%%{"),
263 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
264 check_exception("End of input while parsing a conversion specifier",
265 SV("{:%"),
266 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
267 check_exception("End of input while parsing the modifier E",
268 SV("{:%E"),
269 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
270 check_exception("End of input while parsing the modifier O",
271 SV("{:%O"),
272 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
273
274 // Precision not allowed
275 check_exception("The format specifier expects a '%' or a '}'",
276 SV("{:.3}"),
277 std::chrono::year_month{std::chrono::year{1970}, std::chrono::January});
278 }
279
280 int main(int, char**) {
281 test<char>();
282
283 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
284 test<wchar_t>();
285 #endif
286
287 return 0;
288 }