| 1 | // { dg-do run { target c++20 } } |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <sstream> |
| 5 | #include <testsuite_hooks.h> |
| 6 | #include <testsuite_allocator.h> |
| 7 | |
| 8 | template<typename T, typename CharT> |
| 9 | concept stream_extractable |
| 10 | = requires (std::basic_istream<CharT>& is) { is >> std::declval<T>(); }; |
| 11 | |
| 12 | void |
| 13 | test_recommended_practice() |
| 14 | { |
| 15 | std::chrono::seconds s; |
| 16 | using parse_manip = decltype(std::chrono::parse("", s)); |
| 17 | static_assert( stream_extractable<parse_manip, char> ); |
| 18 | static_assert( not stream_extractable<parse_manip, wchar_t> ); |
| 19 | using wparse_manip = decltype(std::chrono::parse(L"", s)); |
| 20 | static_assert( stream_extractable<wparse_manip, wchar_t> ); |
| 21 | static_assert( not stream_extractable<wparse_manip, char> ); |
| 22 | |
| 23 | // These properties are recommended by the standard, to avoid using a |
| 24 | // parse manipulator that has a dangling reference to a format string. |
| 25 | static_assert( not std::is_move_constructible_v<parse_manip> ); |
| 26 | static_assert( not std::is_move_assignable_v<parse_manip> ); |
| 27 | static_assert( not stream_extractable<parse_manip&, char> ); |
| 28 | static_assert( not stream_extractable<const parse_manip&, char> ); |
| 29 | static_assert( not stream_extractable<wparse_manip&, wchar_t> ); |
| 30 | static_assert( not stream_extractable<const wparse_manip&, wchar_t> ); |
| 31 | } |
| 32 | |
| 33 | template<typename... Args> |
| 34 | concept parsable = requires(Args... args) { std::chrono::parse(args...); }; |
| 35 | |
| 36 | const std::string f = "format string"; |
| 37 | |
| 38 | namespace N |
| 39 | { |
| 40 | struct A { }; |
| 41 | |
| 42 | void |
| 43 | from_stream(std::istream&, const char* fmt, A&) |
| 44 | { |
| 45 | VERIFY( fmt == f.c_str() ); |
| 46 | } |
| 47 | |
| 48 | template<typename... Args> |
| 49 | void |
| 50 | from_stream(std::istream&, const char*, A&, void*, void*) = delete; |
| 51 | |
| 52 | struct B { }; |
| 53 | |
| 54 | void |
| 55 | from_stream(std::istream&, const char* fmt, B&, std::string* abbrev) |
| 56 | { |
| 57 | VERIFY( fmt == f.c_str() ); |
| 58 | VERIFY( abbrev != nullptr ); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | from_stream(std::istream&, const char*, B&, std::string*, void*) = delete; |
| 63 | |
| 64 | struct C { }; |
| 65 | |
| 66 | void |
| 67 | from_stream(std::istream&, const char* fmt, C&, std::string* abbrev, |
| 68 | std::chrono::minutes* offset) |
| 69 | { |
| 70 | VERIFY( fmt == f.c_str() ); |
| 71 | VERIFY( abbrev == nullptr ); |
| 72 | VERIFY( offset != nullptr ); |
| 73 | } |
| 74 | |
| 75 | struct D { }; |
| 76 | |
| 77 | void |
| 78 | from_stream(std::istream&, const char* fmt, D&, std::string* abbrev, |
| 79 | std::chrono::minutes* offset) |
| 80 | { |
| 81 | VERIFY( fmt == f.c_str() ); |
| 82 | VERIFY( abbrev != nullptr ); |
| 83 | VERIFY( offset != nullptr ); |
| 84 | } |
| 85 | |
| 86 | struct E { }; |
| 87 | |
| 88 | void |
| 89 | from_stream(std::wistream&, const wchar_t*, E&, std::wstring* = nullptr, |
| 90 | std::chrono::minutes* = nullptr) |
| 91 | { } |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | test_adl() |
| 96 | { |
| 97 | using std::string; |
| 98 | using std::wstring; |
| 99 | using std::chrono::minutes; |
| 100 | |
| 101 | string abbrev; |
| 102 | minutes offset; |
| 103 | |
| 104 | // Check that valid calls are well-formed. |
| 105 | N::A a; |
| 106 | (void) std::chrono::parse(f, a); |
| 107 | N::B b; |
| 108 | (void) std::chrono::parse(f, b, abbrev); |
| 109 | N::C c; |
| 110 | (void) std::chrono::parse(f, c, offset); |
| 111 | // This satisfies the concept, but would fail the VERIFY assertion: |
| 112 | static_assert( parsable<const char*, N::C, string, minutes> ); |
| 113 | N::D d; |
| 114 | (void) std::chrono::parse(f, d, abbrev, offset); |
| 115 | // This satisfies the concept, but would fail the VERIFY assertion: |
| 116 | static_assert( parsable<const char*, N::D, minutes> ); |
| 117 | |
| 118 | // Wide strings. |
| 119 | static_assert( parsable<const wchar_t*, N::E, wstring> ); |
| 120 | static_assert( parsable<const wchar_t*, N::E, wstring> ); |
| 121 | static_assert( parsable<const wchar_t*, N::E, minutes> ); |
| 122 | static_assert( parsable<const wchar_t*, N::E, wstring, minutes> ); |
| 123 | |
| 124 | // Check that invalid calls are properly constrained. |
| 125 | |
| 126 | // from_stream is only overloaded for N::A without abbrev or offset. |
| 127 | static_assert( not parsable<const char*, N::A, std::string> ); |
| 128 | static_assert( not parsable<const char*, N::A, minutes> ); |
| 129 | static_assert( not parsable<const char*, N::A, string, minutes> ); |
| 130 | // from_stream is only overloaded for N::B with abbrev. |
| 131 | static_assert( not parsable<const char*, N::B> ); |
| 132 | static_assert( not parsable<const char*, N::B, minutes> ); |
| 133 | static_assert( not parsable<const char*, N::B, string, minutes> ); |
| 134 | // from_stream is only overloaded for N::C with abbrev and minutes. |
| 135 | static_assert( not parsable<const char*, N::C> ); |
| 136 | static_assert( not parsable<const char*, N::C, string> ); |
| 137 | // from_stream is only overloaded for N::D with abbrev and minutes. |
| 138 | static_assert( not parsable<const char*, N::D> ); |
| 139 | static_assert( not parsable<const char*, N::D, string> ); |
| 140 | |
| 141 | // Mismatched strings |
| 142 | static_assert( not parsable<string, std::chrono::year, wstring> ); |
| 143 | static_assert( not parsable<string, std::chrono::year, wstring, minutes> ); |
| 144 | |
| 145 | using Alloc = __gnu_test::SimpleAllocator<char>; |
| 146 | using String = std::basic_string<char, std::char_traits<char>, Alloc>; |
| 147 | // Custom allocator |
| 148 | static_assert( parsable<String, std::chrono::year> ); |
| 149 | static_assert( parsable<String, std::chrono::year, String> ); |
| 150 | static_assert( parsable<String, std::chrono::year, minutes> ); |
| 151 | static_assert( parsable<String, std::chrono::year, String, minutes> ); |
| 152 | static_assert( parsable<const char*, std::chrono::year, String> ); |
| 153 | static_assert( parsable<const char*, std::chrono::year, String, minutes> ); |
| 154 | // Mismatched allocators |
| 155 | static_assert( not parsable<string, std::chrono::year, String> ); |
| 156 | static_assert( not parsable<string, std::chrono::year, String, minutes> ); |
| 157 | static_assert( not parsable<String, std::chrono::year, string> ); |
| 158 | static_assert( not parsable<String, std::chrono::year, string, minutes> ); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | test_whitespace() |
| 163 | { |
| 164 | using namespace std::chrono_literals; |
| 165 | std::chrono::minutes min; |
| 166 | std::istringstream is; |
| 167 | is.str(" a b 1 "); |
| 168 | is >> parse(" a b %M", min); |
| 169 | VERIFY( is.good() ); |
| 170 | VERIFY( min == 1min ); |
| 171 | is.str(" a b 1 "); |
| 172 | is >> parse(" a b %M ", min); |
| 173 | VERIFY( is.eof() && !is.fail() ); |
| 174 | VERIFY( min == 1min ); |
| 175 | is.clear(); |
| 176 | is.str(" 1"); |
| 177 | is >> parse(" %n%M%n", min); |
| 178 | VERIFY( is.fail() ); |
| 179 | is.clear(); |
| 180 | is.str(" a b 1 "); |
| 181 | is >> parse("%n a%n%nb %t%M%n", min); |
| 182 | VERIFY( is.good() ); |
| 183 | VERIFY( min == 1min ); |
| 184 | is.str("a b 1 "); |
| 185 | is >> parse("%ta b %M%n%t", min); |
| 186 | VERIFY( is.good() ); |
| 187 | VERIFY( min == 1min ); |
| 188 | is.str("1 "); |
| 189 | is >> parse("%M%n%t%t", min); |
| 190 | VERIFY( is.eof() && !is.fail() ); |
| 191 | VERIFY( min == 1min ); |
| 192 | is.clear(); |
| 193 | is.str("1 "); |
| 194 | is >> parse("%M%n%t%t", min); |
| 195 | VERIFY( is.eof() && !is.fail() ); |
| 196 | VERIFY( min == 1min ); |
| 197 | is.clear(); |
| 198 | is.str("1 "); |
| 199 | is >> parse("%M%n%t%n", min); |
| 200 | VERIFY( is.eof() && is.fail() ); |
| 201 | VERIFY( min == 1min ); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | test_errors() |
| 206 | { |
| 207 | using namespace std::chrono_literals; |
| 208 | std::chrono::minutes min(999); |
| 209 | std::chrono::year y(-1); |
| 210 | std::istringstream is; |
| 211 | |
| 212 | is.str("x"); |
| 213 | is >> parse("x", min); // Matches expected pattern, but no minutes present. |
| 214 | VERIFY( !is.eof() && is.fail() ); |
| 215 | VERIFY( min == 999min ); |
| 216 | |
| 217 | is.clear(); |
| 218 | is.str("x"); |
| 219 | is >> parse("%M", min); // Doesn't match expected pattern. |
| 220 | VERIFY( !is.eof() && is.fail() ); |
| 221 | VERIFY( min == 999min ); |
| 222 | |
| 223 | is.clear(); |
| 224 | is.str("001:002"); |
| 225 | is >> parse("%H:%M", min); // Extracts "00" then fails to find ':' next. |
| 226 | VERIFY( !is.eof() && is.fail() ); |
| 227 | VERIFY( min == 999min ); |
| 228 | |
| 229 | is.clear(); |
| 230 | is.str("12:61"); |
| 231 | is >> parse("%H:%M", min); // 61min is out of range. |
| 232 | VERIFY( !is.eof() && is.fail() ); |
| 233 | VERIFY( min == 999min ); |
| 234 | |
| 235 | is.clear(); |
| 236 | is.str("12:15 100"); |
| 237 | is >> parse("%H:%M %3y", min); // 100y is out of range for %y but not needed |
| 238 | VERIFY( is.good() ); |
| 239 | VERIFY( min == (12h + 15min) ); |
| 240 | |
| 241 | min = 999min; |
| 242 | is.clear(); |
| 243 | is.str("12:15 100"); |
| 244 | is >> parse("%H:%M %3y", y); // 100y is out of range for %y and needed |
| 245 | VERIFY( is.fail() ); |
| 246 | VERIFY( y == -1y ); |
| 247 | |
| 248 | is.clear(); |
| 249 | is.str("23:61 10"); |
| 250 | is >> parse("%H:%M %3y", y); // 61min is out of range but not needed |
| 251 | VERIFY( is.eof() && ! is.fail() ); |
| 252 | VERIFY( y == 2010y ); |
| 253 | |
| 254 | min = -1min; |
| 255 | is.clear(); |
| 256 | is.str("25:59"); |
| 257 | is >> parse("%H:%M", min); // 25h is out of range and needed |
| 258 | VERIFY( is.fail() ); |
| 259 | VERIFY( min == -1min ); |
| 260 | |
| 261 | is.clear(); |
| 262 | is.str("328 00"); |
| 263 | is >> parse("%3C %y", y); // 328 is out of range for %C (PR libstdc++/111162) |
| 264 | VERIFY( is.fail() ); |
| 265 | VERIFY( y == 2010y ); |
| 266 | |
| 267 | is.clear(); |
| 268 | is.str("-328 00"); |
| 269 | is >> parse("%3C %y", y); // -328 is out of range for %C |
| 270 | VERIFY( is.fail() ); |
| 271 | VERIFY( y == 2010y ); |
| 272 | } |
| 273 | |
| 274 | void |
| 275 | test_modifiers() |
| 276 | { |
| 277 | using namespace std::chrono_literals; |
| 278 | std::chrono::minutes min; |
| 279 | std::istringstream is; |
| 280 | |
| 281 | is.str("0001:000002"); |
| 282 | is >> parse("%4H:%5M", min); |
| 283 | VERIFY( is.good() ); |
| 284 | VERIFY( min == 60min ); |
| 285 | |
| 286 | is.str("0001:000002"); |
| 287 | is >> parse("%6H:%6M", min); |
| 288 | VERIFY( is.good() ); |
| 289 | VERIFY( min == 62min ); |
| 290 | |
| 291 | is.str("002"); |
| 292 | is >> parse("%4M", min); |
| 293 | VERIFY( is.eof() && !is.fail() ); |
| 294 | VERIFY( min == 2min ); |
| 295 | |
| 296 | is.clear(); |
| 297 | is.str("0061"); |
| 298 | is >> parse("%3M", min); |
| 299 | VERIFY( is.good() ); |
| 300 | VERIFY( min == 6min ); |
| 301 | |
| 302 | is.clear(); |
| 303 | is.str("0061"); |
| 304 | is >> parse("%4M", min); |
| 305 | VERIFY( !is.eof() && is.fail() ); |
| 306 | |
| 307 | is.clear(); |
| 308 | is.str("0061"); |
| 309 | is >> parse("%5M", min); |
| 310 | VERIFY( is.eof() && is.fail() ); |
| 311 | |
| 312 | std::chrono::seconds s; |
| 313 | is.clear(); |
| 314 | is.str("000000000012345"); |
| 315 | is >> parse("%12S", s); // Read more than 10 digits to check overflow logic. |
| 316 | VERIFY( is.good() ); |
| 317 | VERIFY( s == 12s ); |
| 318 | } |
| 319 | |
| 320 | void |
| 321 | test_wchar() |
| 322 | { |
| 323 | std::wistringstream is; |
| 324 | std::chrono::duration<double, std::milli> ms; |
| 325 | |
| 326 | is.clear(); |
| 327 | is.str(L"0.125"); |
| 328 | is >> parse(L"%S", ms); |
| 329 | VERIFY( is.good() ); |
| 330 | VERIFY( ms.count() == 125 ); |
| 331 | } |
| 332 | |
| 333 | int main() |
| 334 | { |
| 335 | test_recommended_practice(); |
| 336 | test_adl(); |
| 337 | test_whitespace(); |
| 338 | test_errors(); |
| 339 | test_modifiers(); |
| 340 | test_wchar(); |
| 341 | } |