Debian Codegraph @

libcxx/test/std/time/time.syn/formatter.weekday_last.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 // TODO FMT Investigate Windows issues.
16 // XFAIL: msvc
17
18 // REQUIRES: locale.fr_FR.UTF-8
19 // REQUIRES: locale.ja_JP.UTF-8
20
21 // <chrono>
22
23 // template<class charT> struct formatter<chrono::weekday_last, charT>;
24
25 #include <chrono>
26 #include <format>
27
28 #include <cassert>
29 #include <concepts>
30 #include <locale>
31 #include <iostream>
32 #include <type_traits>
33
34 #include "formatter_tests.h"
35 #include "make_string.h"
36 #include "platform_support.h" // locale name macros
37 #include "test_macros.h"
38
39 template <class CharT>
40 static void test_no_chrono_specs() {
41 using namespace std::literals::chrono_literals;
42
43 // Valid day
44 check(SV("Sun[last]"), SV("{}"), std::chrono::weekday_last{std::chrono::weekday(0)});
45
46 // Invalid day
47 check(SV("8 is not a valid weekday[last]"), SV("{}"), std::chrono::weekday_last{std::chrono::weekday(8)});
48 }
49
50 template <class CharT>
51 static void test_valid_values() {
52 constexpr std::basic_string_view<CharT> fmt =
53 SV("{:%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%t%%a='%a'%t%%A='%A'%n}");
54 constexpr std::basic_string_view<CharT> lfmt =
55 SV("{:L%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%t%%a='%a'%t%%A='%A'%n}");
56
57 const std::locale loc(LOCALE_ja_JP_UTF_8);
58 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
59
60 // Non localized output using C-locale
61 check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Sun'\t%A='Sunday'\n"),
62 fmt,
63 std::chrono::weekday_last{std::chrono::weekday(0)});
64 check(SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='Mon'\t%A='Monday'\n"),
65 fmt,
66 std::chrono::weekday_last{std::chrono::weekday(1)});
67 check(SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='Tue'\t%A='Tuesday'\n"),
68 fmt,
69 std::chrono::weekday_last{std::chrono::weekday(2)});
70 check(SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='Wed'\t%A='Wednesday'\n"),
71 fmt,
72 std::chrono::weekday_last{std::chrono::weekday(3)});
73 check(SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='Thu'\t%A='Thursday'\n"),
74 fmt,
75 std::chrono::weekday_last{std::chrono::weekday(4)});
76 check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='Fri'\t%A='Friday'\n"),
77 fmt,
78 std::chrono::weekday_last{std::chrono::weekday(5)});
79 check(SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='Sat'\t%A='Saturday'\n"),
80 fmt,
81 std::chrono::weekday_last{std::chrono::weekday(6)});
82 check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Sun'\t%A='Sunday'\n"),
83 fmt,
84 std::chrono::weekday_last{std::chrono::weekday(7)});
85
86 // Use the global locale (fr_FR)
87 #if defined(__APPLE__)
88 check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Dim'\t%A='Dimanche'\n"),
89 lfmt,
90 std::chrono::weekday_last{std::chrono::weekday(0)});
91 check(SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='Lun'\t%A='Lundi'\n"),
92 lfmt,
93 std::chrono::weekday_last{std::chrono::weekday(1)});
94 check(SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='Mar'\t%A='Mardi'\n"),
95 lfmt,
96 std::chrono::weekday_last{std::chrono::weekday(2)});
97 check(SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='Mer'\t%A='Mercredi'\n"),
98 lfmt,
99 std::chrono::weekday_last{std::chrono::weekday(3)});
100 check(SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='Jeu'\t%A='Jeudi'\n"),
101 lfmt,
102 std::chrono::weekday_last{std::chrono::weekday(4)});
103 check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='Ven'\t%A='Vendredi'\n"),
104 lfmt,
105 std::chrono::weekday_last{std::chrono::weekday(5)});
106 check(SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='Sam'\t%A='Samedi'\n"),
107 lfmt,
108 std::chrono::weekday_last{std::chrono::weekday(6)});
109 check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Dim'\t%A='Dimanche'\n"),
110 lfmt,
111 std::chrono::weekday_last{std::chrono::weekday(7)});
112 #else // defined(__APPLE__)
113 check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='dim.'\t%A='dimanche'\n"),
114 lfmt,
115 std::chrono::weekday_last{std::chrono::weekday(0)});
116 check(SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='lun.'\t%A='lundi'\n"),
117 lfmt,
118 std::chrono::weekday_last{std::chrono::weekday(1)});
119 check(SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='mar.'\t%A='mardi'\n"),
120 lfmt,
121 std::chrono::weekday_last{std::chrono::weekday(2)});
122 check(SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='mer.'\t%A='mercredi'\n"),
123 lfmt,
124 std::chrono::weekday_last{std::chrono::weekday(3)});
125 check(SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='jeu.'\t%A='jeudi'\n"),
126 lfmt,
127 std::chrono::weekday_last{std::chrono::weekday(4)});
128 check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='ven.'\t%A='vendredi'\n"),
129 lfmt,
130 std::chrono::weekday_last{std::chrono::weekday(5)});
131 check(SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='sam.'\t%A='samedi'\n"),
132 lfmt,
133 std::chrono::weekday_last{std::chrono::weekday(6)});
134 check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='dim.'\t%A='dimanche'\n"),
135 lfmt,
136 std::chrono::weekday_last{std::chrono::weekday(7)});
137 #endif // defined(__APPLE__)
138
139 // Use supplied locale (ja_JP).
140 // This locale has a different alternate, but not on all platforms
141 #if defined(_WIN32) || defined(__APPLE__) || defined(_AIX) || defined(__FreeBSD__)
142 check(loc,
143 SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='日'\t%A='日曜日'\n"),
144 lfmt,
145 std::chrono::weekday_last{std::chrono::weekday(0)});
146 check(loc,
147 SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='月'\t%A='月曜日'\n"),
148 lfmt,
149 std::chrono::weekday_last{std::chrono::weekday(1)});
150 check(loc,
151 SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='火'\t%A='火曜日'\n"),
152 lfmt,
153 std::chrono::weekday_last{std::chrono::weekday(2)});
154 check(loc,
155 SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='水'\t%A='水曜日'\n"),
156 lfmt,
157 std::chrono::weekday_last{std::chrono::weekday(3)});
158 check(loc,
159 SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='木'\t%A='木曜日'\n"),
160 lfmt,
161 std::chrono::weekday_last{std::chrono::weekday(4)});
162 check(loc,
163 SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='金'\t%A='金曜日'\n"),
164 lfmt,
165 std::chrono::weekday_last{std::chrono::weekday(5)});
166 check(loc,
167 SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='土'\t%A='土曜日'\n"),
168 lfmt,
169 std::chrono::weekday_last{std::chrono::weekday(6)});
170 check(loc,
171 SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='日'\t%A='日曜日'\n"),
172 lfmt,
173 std::chrono::weekday_last{std::chrono::weekday(7)});
174 #else // defined(_WIN32) || defined(__APPLE__) || defined(_AIX) || defined(__FreeBSD__)
175 check(loc,
176 SV("%u='7'\t%Ou='七'\t%w='0'\t%Ow='〇'\t%a='日'\t%A='日曜日'\n"),
177 lfmt,
178 std::chrono::weekday_last{std::chrono::weekday(0)});
179 check(loc,
180 SV("%u='1'\t%Ou='一'\t%w='1'\t%Ow='一'\t%a='月'\t%A='月曜日'\n"),
181 lfmt,
182 std::chrono::weekday_last{std::chrono::weekday(1)});
183 check(loc,
184 SV("%u='2'\t%Ou='二'\t%w='2'\t%Ow='二'\t%a='火'\t%A='火曜日'\n"),
185 lfmt,
186 std::chrono::weekday_last{std::chrono::weekday(2)});
187 check(loc,
188 SV("%u='3'\t%Ou='三'\t%w='3'\t%Ow='三'\t%a='水'\t%A='水曜日'\n"),
189 lfmt,
190 std::chrono::weekday_last{std::chrono::weekday(3)});
191 check(loc,
192 SV("%u='4'\t%Ou='四'\t%w='4'\t%Ow='四'\t%a='木'\t%A='木曜日'\n"),
193 lfmt,
194 std::chrono::weekday_last{std::chrono::weekday(4)});
195 check(loc,
196 SV("%u='5'\t%Ou='五'\t%w='5'\t%Ow='五'\t%a='金'\t%A='金曜日'\n"),
197 lfmt,
198 std::chrono::weekday_last{std::chrono::weekday(5)});
199 check(loc,
200 SV("%u='6'\t%Ou='六'\t%w='6'\t%Ow='六'\t%a='土'\t%A='土曜日'\n"),
201 lfmt,
202 std::chrono::weekday_last{std::chrono::weekday(6)});
203 check(loc,
204 SV("%u='7'\t%Ou='七'\t%w='0'\t%Ow='〇'\t%a='日'\t%A='日曜日'\n"),
205 lfmt,
206 std::chrono::weekday_last{std::chrono::weekday(7)});
207 #endif // defined(_WIN32) || defined(__APPLE__) || defined(_AIX) || defined(__FreeBSD__)
208
209 std::locale::global(std::locale::classic());
210 }
211
212 template <class CharT>
213 static void test_invalid_values() {
214 // Test that %a and %A throw an exception.
215 check_exception("Formatting a weekday name needs a valid weekday",
216 SV("{:%a}"),
217 std::chrono::weekday_last{std::chrono::weekday(8)});
218 check_exception("Formatting a weekday name needs a valid weekday",
219 SV("{:%a}"),
220 std::chrono::weekday_last{std::chrono::weekday(255)});
221 check_exception("Formatting a weekday name needs a valid weekday",
222 SV("{:%A}"),
223 std::chrono::weekday_last{std::chrono::weekday(8)});
224 check_exception("Formatting a weekday name needs a valid weekday",
225 SV("{:%A}"),
226 std::chrono::weekday_last{std::chrono::weekday(255)});
227
228 constexpr std::basic_string_view<CharT> fmt = SV("{:%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%n}");
229 constexpr std::basic_string_view<CharT> lfmt = SV("{:L%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%n}");
230
231 const std::locale loc(LOCALE_ja_JP_UTF_8);
232 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
233
234 #if defined(__APPLE__) || defined(__FreeBSD__)
235 // Non localized output using C-locale
236 check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(8)});
237 check(SV("%u='255'\t%Ou='255'\t%w='255'\t%Ow='255'\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(255)});
238
239 // Use the global locale (fr_FR)
240 check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
241 check(SV("%u='255'\t%Ou='255'\t%w='255'\t%Ow='255'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(255)});
242
243 // Use supplied locale (ja_JP). This locale has a different alternate.
244 check(loc, SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
245 check(loc,
246 SV("%u='255'\t%Ou='255'\t%w='255'\t%Ow='255'\n"),
247 lfmt,
248 std::chrono::weekday_last{std::chrono::weekday(255)});
249 #elif defined(_WIN32) // defined(__APPLE__) || defined(__FreeBSD__)
250 // Non localized output using C-locale
251 check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(8)});
252 check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(255)});
253
254 // Use the global locale (fr_FR)
255 check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
256 check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(255)});
257
258 // Use supplied locale (ja_JP). This locale has a different alternate.
259 check(loc, SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
260 check(loc, SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(255)});
261 #elif defined(_AIX) // defined(__APPLE__) || defined(__FreeBSD__)
262 // Non localized output using C-locale
263 check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(8)});
264 check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(255)});
265
266 // Use the global locale (fr_FR)
267 check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
268 check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(255)});
269
270 // Use supplied locale (ja_JP). This locale has a different alternate.
271 check(loc, SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
272 check(loc, SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(255)});
273 #else // defined(__APPLE__) || defined(__FreeBSD__)
274 // Non localized output using C-locale
275 check(SV("%u='1'\t%Ou='1'\t%w='8'\t%Ow='8'\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(8)});
276 check(SV("%u='3'\t%Ou='3'\t%w='255'\t%Ow='255'\n"), fmt, std::chrono::weekday_last{std::chrono::weekday(255)});
277
278 // Use the global locale (fr_FR)
279 check(SV("%u='1'\t%Ou='1'\t%w='8'\t%Ow='8'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
280 check(SV("%u='3'\t%Ou='3'\t%w='255'\t%Ow='255'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(255)});
281
282 // Use supplied locale (ja_JP). This locale has a different alternate.
283 check(loc, SV("%u='1'\t%Ou='一'\t%w='8'\t%Ow='八'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(8)});
284 check(loc, SV("%u='3'\t%Ou='三'\t%w='255'\t%Ow='255'\n"), lfmt, std::chrono::weekday_last{std::chrono::weekday(255)});
285 #endif // defined(__APPLE__) || defined(__FreeBSD__)
286
287 std::locale::global(std::locale::classic());
288 }
289
290 template <class CharT>
291 static void test() {
292 test_no_chrono_specs<CharT>();
293 test_valid_values<CharT>();
294 test_invalid_values<CharT>();
295 check_invalid_types<CharT>({SV("a"), SV("A"), SV("t"), SV("u"), SV("w"), SV("Ou"), SV("Ow")},
296 std::chrono::weekday_last{std::chrono::weekday(0)});
297
298 check_exception(
299 "The format specifier expects a '%' or a '}'", SV("{:A"), std::chrono::weekday_last{std::chrono::weekday(0)});
300 check_exception(
301 "The chrono specifiers contain a '{'", SV("{:%%{"), std::chrono::weekday_last{std::chrono::weekday(0)});
302 check_exception("End of input while parsing a conversion specifier",
303 SV("{:%"),
304 std::chrono::weekday_last{std::chrono::weekday(0)});
305 check_exception(
306 "End of input while parsing the modifier E", SV("{:%E"), std::chrono::weekday_last{std::chrono::weekday(0)});
307 check_exception(
308 "End of input while parsing the modifier O", SV("{:%O"), std::chrono::weekday_last{std::chrono::weekday(0)});
309
310 // Precision not allowed
311 check_exception(
312 "The format specifier expects a '%' or a '}'", SV("{:.3}"), std::chrono::weekday_last{std::chrono::weekday(0)});
313 }
314
315 int main(int, char**) {
316 test<char>();
317
318 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
319 test<wchar_t>();
320 #endif
321
322 return 0;
323 }