Debian Codegraph @

libstdc++-v3/testsuite/std/time/month/io.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-require-namedlocale "fr_FR.ISO8859-15" }
3 // { dg-timeout-factor 2 }
4
5 #include <chrono>
6 #include <sstream>
7 #include <testsuite_hooks.h>
8
9 void
10 test_ostream()
11 {
12 using std::ostringstream;
13 using namespace std::chrono;
14
15 ostringstream ss;
16 for (int i = 1; i <= 12; ++i)
17 ss << month(i);
18 VERIFY( ss.str() == "JanFebMarAprMayJunJulAugSepOctNovDec" );
19 ss.str("");
20 ss << month(0) << '|' << month(13);
21 VERIFY( ss.str() == "0 is not a valid month|13 is not a valid month" );
22
23 ss.str("");
24 ss.imbue(std::locale(ISO_8859(15,fr_FR)));
25 ss << month(1);
26 VERIFY( ss.str() == "janv." );
27 }
28
29 void
30 test_format()
31 {
32 using std::chrono::month;
33
34 auto s = std::format("{:%b%%%B%t%m%n}", month(1));
35 VERIFY( s == "Jan%January\t01\n" );
36 auto ws = std::format(L"{:%b%%%B%t%m%n}", month(12));
37 VERIFY( ws == L"Dec%December\t12\n" );
38
39 try
40 {
41 (void) std::format("{:%b}", month(13));
42 VERIFY(false);
43 }
44 catch (const std::format_error&)
45 {
46 }
47
48 s = std::format("{} is OK, but {:L}", month(2), month(13));
49 VERIFY( s == "Feb is OK, but 13 is not a valid month" );
50
51 std::locale loc_fr(ISO_8859(15,fr_FR));
52
53 s = std::format("{:%Om}", month(1));
54 VERIFY( s == "01" );
55 s = std::format(std::locale::classic(), "{:%Om}", month(1));
56 VERIFY( s == "01" );
57 s = std::format(std::locale::classic(), "{:L%Om}", month(1));
58 VERIFY( s == "01" );
59 s = std::format(loc_fr, "{:%Om}", month(1));
60 VERIFY( s == "01" );
61 s = std::format(loc_fr, "{:L%Om}", month(1));
62 VERIFY( s == "01" );
63 // TODO test "{:L%Om}" with locale that has alternative numeric rep.
64
65 s = std::format(loc_fr, "{:%b}", month(1));
66 VERIFY( s == "Jan" );
67 s = std::format(loc_fr, "{:L%b}", month(1));
68 VERIFY( s == "janv." );
69
70 std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ";
71 std::string_view my_specs = "bBhm";
72 for (char c : specs)
73 {
74 char fmt[] = { '{', ':', '%', c, '}' };
75 try
76 {
77 month m(1);
78 (void) std::vformat(std::string_view(fmt, 5), std::make_format_args(m));
79 // The call above should throw for any conversion-spec not in my_specs:
80 VERIFY(my_specs.find(c) != my_specs.npos);
81 }
82 catch (const std::format_error& e)
83 {
84 VERIFY(my_specs.find(c) == my_specs.npos);
85 std::string_view s = e.what();
86 // Libstdc++-specific message:
87 VERIFY(s.find("format argument does not contain the information "
88 "required by the chrono-specs") != s.npos);
89 }
90 }
91 }
92
93 void
94 test_parse()
95 {
96 using namespace std::chrono;
97 std::istringstream is;
98 month m{};
99
100 is.str("JUL");
101 VERIFY( is >> parse("%B", m) );
102 VERIFY( is.eof() );
103 VERIFY( m == July );
104
105 is.clear();
106 is.str("junE bug");
107 VERIFY( is >> parse(" %b bug ", m) );
108 VERIFY( is.eof() );
109 VERIFY( m == June );
110
111 is.clear();
112 is.str("012");
113 VERIFY( is >> parse("%m", m) );
114 VERIFY( ! is.eof() );
115 VERIFY( is.peek() == '2' );
116 VERIFY( m == January );
117
118 is.clear();
119 is.str("012");
120 VERIFY( is >> parse("%4m", m) );
121 VERIFY( is.eof() );
122 VERIFY( m == December );
123
124 m = month(15);
125 is.clear();
126 is.str("Janvember");
127 VERIFY( is >> parse("%B", m) ); // Stops parsing after "Jan"
128 VERIFY( ! is.eof() );
129 VERIFY( is.peek() == 'v' );
130 VERIFY( m == January );
131
132 m = month(15);
133 is.clear();
134 is.str("Junuary");
135 VERIFY( is >> parse("%B", m) ); // Stops parsing after "Jun"
136 VERIFY( ! is.eof() );
137 VERIFY( is.peek() == 'u' );
138 VERIFY( m == June );
139
140 m = month(15);
141 is.clear();
142 is.str("Jebruary");
143 VERIFY( ! (is >> parse("%B", m)) );
144 VERIFY( is.fail() );
145 VERIFY( ! is.eof() );
146 is.clear();
147 VERIFY( is.peek() == 'e' );
148 VERIFY( m == month(15) );
149
150 m = month(13);
151 is.clear();
152 is.str("2023-6-31");
153 VERIFY( ! (is >> parse("%F", m)) ); // June only has 30 days.
154 VERIFY( ! is.eof() );
155 VERIFY( m == month(13) );
156
157 m = month(14);
158 is.clear();
159 is.str("2023-2-29");
160 VERIFY( ! (is >> parse("%Y-%m-%e", m)) ); // Feb only has 28 days in 2023.
161 VERIFY( ! is.eof() );
162 VERIFY( m == month(14) );
163
164 is.clear();
165 is.str("2-29");
166 VERIFY( is >> parse("%m-%d", m) ); // But Feb has 29 days in some years.
167 VERIFY( ! is.eof() );
168 VERIFY( m == February );
169
170 m = month(14);
171 is.clear();
172 is.str("6-31");
173 VERIFY( ! (is >> parse("%m-%d", m)) ); // June only has 30 days in all years.
174 VERIFY( ! is.eof() );
175 VERIFY( m == month(14) );
176
177 m = month(15);
178 is.clear();
179 is.str("2023-13-1");
180 VERIFY( ! (is >> parse("%F", m)) );
181 VERIFY( is.eof() );
182 VERIFY( m == month(15) );
183
184 m = month(16);
185 is.clear();
186 is.str("13/1/23");
187 VERIFY( ! (is >> parse("%D", m)) );
188 VERIFY( m == month(16) );
189
190 m = month(17);
191 is.clear();
192 is.str("13");
193 VERIFY( ! (is >> parse("%m", m)) );
194 VERIFY( ! is.eof() );
195 VERIFY( m == month(17) );
196
197 m = month(18);
198 is.clear();
199 is.str("1234");
200 VERIFY( ! (is >> parse("%3m", m)) );
201 VERIFY( ! is.eof() );
202 is.clear();
203 VERIFY( is.peek() == '4' );
204 VERIFY( m == month(18) );
205
206 is.clear();
207 is.str("2023-W32-5");
208 VERIFY( is >> parse("%G-W%V-%u", m) );
209 VERIFY( ! is.eof() );
210 VERIFY( m == August );
211 }
212
213 int main()
214 {
215 test_ostream();
216 test_format();
217 test_parse();
218 }