xref: /llvm-project/libcxx/test/std/time/time.cal/time.cal.ym/time.cal.ym.nonmembers/ostream.pass.cpp (revision 8f01029b17a392cee020e5d23816c7a6fffd63ee)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 
13 // TODO FMT This test should not require std::to_chars(floating-point)
14 // XFAIL: availability-fp_to_chars-missing
15 
16 // REQUIRES: locale.fr_FR.UTF-8
17 // REQUIRES: locale.ja_JP.UTF-8
18 
19 // <chrono>
20 
21 // class year_month;
22 
23 // template<class charT, class traits>
24 //   basic_ostream<charT, traits>&
25 //     operator<<(basic_ostream<charT, traits>& os, const year_month& ym);
26 
27 #include <chrono>
28 #include <cassert>
29 #include <sstream>
30 
31 #include "make_string.h"
32 #include "platform_support.h" // locale name macros
33 #include "test_macros.h"
34 #include "assert_macros.h"
35 #include "concat_macros.h"
36 
37 #define SV(S) MAKE_STRING_VIEW(CharT, S)
38 
39 #define TEST_EQUAL(OUT, EXPECTED)                                                                                      \
40   TEST_REQUIRE(OUT == EXPECTED,                                                                                        \
41                TEST_WRITE_CONCATENATED(                                                                                \
42                    "\nExpression      ", #OUT, "\nExpected output ", EXPECTED, "\nActual output   ", OUT, '\n'));
43 
44 template <class CharT>
stream_c_locale(std::chrono::year_month ym)45 static std::basic_string<CharT> stream_c_locale(std::chrono::year_month ym) {
46   std::basic_stringstream<CharT> sstr;
47   sstr << ym;
48   return sstr.str();
49 }
50 
51 template <class CharT>
stream_fr_FR_locale(std::chrono::year_month ym)52 static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::year_month ym) {
53   std::basic_stringstream<CharT> sstr;
54   const std::locale locale(LOCALE_fr_FR_UTF_8);
55   sstr.imbue(locale);
56   sstr << ym;
57   return sstr.str();
58 }
59 
60 template <class CharT>
stream_ja_JP_locale(std::chrono::year_month ym)61 static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::year_month ym) {
62   std::basic_stringstream<CharT> sstr;
63   const std::locale locale(LOCALE_ja_JP_UTF_8);
64   sstr.imbue(locale);
65   sstr << ym;
66   return sstr.str();
67 }
68 
69 template <class CharT>
test()70 static void test() {
71   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{0}}),
72              SV("-32768 is not a valid year/0 is not a valid month"));
73   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{1}}),
74              SV("-32768 is not a valid year/Jan"));
75   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'767}, std::chrono::month{2}}),
76              SV("-32767/Feb"));
77   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{3}}),
78              SV("0000/Mar"));
79   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
80              SV("1970/Apr"));
81   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{32'767}, std::chrono::month{5}}),
82              SV("32767/May"));
83   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{6}}),
84              SV("0000/Jun"));
85   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{7}}),
86              SV("0000/Jul"));
87   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{8}}),
88              SV("0000/Aug"));
89   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{9}}),
90              SV("0000/Sep"));
91   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{10}}),
92              SV("0000/Oct"));
93   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{11}}),
94              SV("0000/Nov"));
95   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{12}}),
96              SV("0000/Dec"));
97   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{13}}),
98              SV("0000/13 is not a valid month"));
99   TEST_EQUAL(stream_c_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{255}}),
100              SV("-32768 is not a valid year/255 is not a valid month"));
101 
102   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{0}}),
103              SV("-32768 is not a valid year/0 is not a valid month"));
104 #if defined(__APPLE__)
105   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{1}}),
106              SV("-32768 is not a valid year/jan"));
107   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'767}, std::chrono::month{2}}),
108              SV("-32767/fév"));
109   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{3}}),
110              SV("0000/mar"));
111   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
112              SV("1970/avr"));
113   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{32'767}, std::chrono::month{5}}),
114              SV("32767/mai"));
115   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{6}}),
116              SV("0000/jui"));
117   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{7}}),
118              SV("0000/jul"));
119   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{8}}),
120              SV("0000/aoû"));
121   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{9}}),
122              SV("0000/sep"));
123   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{10}}),
124              SV("0000/oct"));
125   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{11}}),
126              SV("0000/nov"));
127   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{12}}),
128              SV("0000/déc"));
129 #else //  defined(__APPLE__)
130   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{1}}),
131              SV("-32768 is not a valid year/janv."));
132   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'767}, std::chrono::month{2}}),
133              SV("-32767/févr."));
134   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{3}}),
135              SV("0000/mars"));
136 #  if defined(_WIN32) || defined(_AIX) || defined(__FreeBSD__)
137   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
138              SV("1970/avr."));
139 #  else  // defined(_WIN32) || defined(_AIX) || defined(__FreeBSD__)
140   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
141              SV("1970/avril"));
142 #  endif // defined(_WIN32) || defined(_AIX) || defined(__FreeBSD__)
143   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{32'767}, std::chrono::month{5}}),
144              SV("32767/mai"));
145   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{6}}),
146              SV("0000/juin"));
147   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{7}}),
148              SV("0000/juil."));
149   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{8}}),
150              SV("0000/août"));
151   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{9}}),
152              SV("0000/sept."));
153   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{10}}),
154              SV("0000/oct."));
155   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{11}}),
156              SV("0000/nov."));
157   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{12}}),
158              SV("0000/déc."));
159 #endif   // defined(__APPLE__)
160   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{13}}),
161              SV("0000/13 is not a valid month"));
162   TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{255}}),
163              SV("-32768 is not a valid year/255 is not a valid month"));
164 
165   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{0}}),
166              SV("-32768 is not a valid year/0 is not a valid month"));
167 #if defined(__APPLE__) || defined(_WIN32)
168 #  if defined(__APPLE__)
169   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{1}}),
170              SV("-32768 is not a valid year/ 1"));
171   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'767}, std::chrono::month{2}}),
172              SV("-32767/ 2"));
173   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{3}}),
174              SV("0000/ 3"));
175   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
176              SV("1970/ 4"));
177   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{32'767}, std::chrono::month{5}}),
178              SV("32767/ 5"));
179   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{6}}),
180              SV("0000/ 6"));
181   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{7}}),
182              SV("0000/ 7"));
183   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{8}}),
184              SV("0000/ 8"));
185   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{9}}),
186              SV("0000/ 9"));
187 #  else  // defined(__APPLE__)
188   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{1}}),
189              SV("-32768 is not a valid year/1"));
190   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'767}, std::chrono::month{2}}),
191              SV("-32767/2"));
192   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{3}}),
193              SV("0000/3"));
194   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
195              SV("1970/4"));
196   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{32'767}, std::chrono::month{5}}),
197              SV("32767/5"));
198   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{6}}),
199              SV("0000/6"));
200   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{7}}),
201              SV("0000/7"));
202   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{8}}),
203              SV("0000/8"));
204   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{9}}),
205              SV("0000/9"));
206 #  endif // defined(__APPLE__)
207   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{10}}),
208              SV("0000/10"));
209   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{11}}),
210              SV("0000/11"));
211   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{12}}),
212              SV("0000/12"));
213 #else // defined(__APPLE__) || defined(_WIN32)
214 #  if defined(_AIX)
215   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{1}}),
216              SV("-32768 is not a valid year/1月"));
217   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'767}, std::chrono::month{2}}),
218              SV("-32767/2月"));
219   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{3}}),
220              SV("0000/3月"));
221   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
222              SV("1970/4月"));
223   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{32'767}, std::chrono::month{5}}),
224              SV("32767/5月"));
225   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{6}}),
226              SV("0000/6月"));
227   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{7}}),
228              SV("0000/7月"));
229   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{8}}),
230              SV("0000/8月"));
231   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{9}}),
232              SV("0000/9月"));
233 #  else  // defined(_AIX)
234   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{1}}),
235              SV("-32768 is not a valid year/ 1月"));
236   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'767}, std::chrono::month{2}}),
237              SV("-32767/ 2月"));
238   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{3}}),
239              SV("0000/ 3月"));
240   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{1970}, std::chrono::month{4}}),
241              SV("1970/ 4月"));
242   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{32'767}, std::chrono::month{5}}),
243              SV("32767/ 5月"));
244   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{6}}),
245              SV("0000/ 6月"));
246   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{7}}),
247              SV("0000/ 7月"));
248   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{8}}),
249              SV("0000/ 8月"));
250   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{9}}),
251              SV("0000/ 9月"));
252 #  endif // defined(_AIX)
253   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{10}}),
254              SV("0000/10月"));
255   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{11}}),
256              SV("0000/11月"));
257   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{12}}),
258              SV("0000/12月"));
259 #endif   // defined(__APPLE__) || defined(_WIN32)
260   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{0}, std::chrono::month{13}}),
261              SV("0000/13 is not a valid month"));
262   TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::year_month{std::chrono::year{-32'768}, std::chrono::month{255}}),
263              SV("-32768 is not a valid year/255 is not a valid month"));
264 }
265 
main(int,char **)266 int main(int, char**) {
267   test<char>();
268 
269 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
270   test<wchar_t>();
271 #endif
272 
273   return 0;
274 }
275