| 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 | ss << day(1) << ' ' << day(11) << ' ' << day(21) << ' ' << day(31) |
| 17 | << ' ' << day(41); |
| 18 | auto s = ss.str(); |
| 19 | VERIFY( s == "01 11 21 31 41 is not a valid day" ); |
| 20 | |
| 21 | ss.str(""); |
| 22 | ss.imbue(std::locale(ISO_8859(15,fr_FR))); |
| 23 | ss << day(1); |
| 24 | VERIFY( ss.str() == "01" ); |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | test_format() |
| 29 | { |
| 30 | using std::chrono::day; |
| 31 | |
| 32 | auto s = std::format("{:%d%%%e%t}{:%d%%%e%n}", day(1), day(11)); |
| 33 | VERIFY( s == "01% 1\t11%11\n" ); |
| 34 | auto ws = std::format(L"{:%d%%%e%t}{:%d%%%e%n}", day(1), day(11)); |
| 35 | VERIFY( ws == L"01% 1\t11%11\n" ); |
| 36 | |
| 37 | VERIFY( std::format("{} {}", day(8), day(0)) == "08 00 is not a valid day" ); |
| 38 | |
| 39 | s = std::format("{:%Od}", day(1)); |
| 40 | VERIFY( s == "01" ); |
| 41 | s = std::format(std::locale::classic(), "{:%Od}", day(1)); |
| 42 | VERIFY( s == "01" ); |
| 43 | s = std::format(std::locale::classic(), "{:L%Od}", day(1)); |
| 44 | VERIFY( s == "01" ); |
| 45 | // TODO test "{:L%Od}" with locale that has alternative numeric rep. |
| 46 | |
| 47 | std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ"; |
| 48 | std::string_view my_specs = "de"; |
| 49 | for (char c : specs) |
| 50 | { |
| 51 | char fmt[] = { '{', ':', '%', c, '}' }; |
| 52 | try |
| 53 | { |
| 54 | day d(1); |
| 55 | (void) std::vformat(std::string_view(fmt, 5), std::make_format_args(d)); |
| 56 | // The call above should throw for any conversion-spec not in my_specs: |
| 57 | VERIFY(my_specs.find(c) != my_specs.npos); |
| 58 | } |
| 59 | catch (const std::format_error& e) |
| 60 | { |
| 61 | VERIFY(my_specs.find(c) == my_specs.npos); |
| 62 | std::string_view s = e.what(); |
| 63 | // Libstdc++-specific message: |
| 64 | VERIFY(s.find("format argument does not contain the information " |
| 65 | "required by the chrono-specs") != s.npos); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | test_parse() |
| 72 | { |
| 73 | using namespace std::chrono; |
| 74 | day d(0); |
| 75 | minutes offset; |
| 76 | std::string abbrev; |
| 77 | std::istringstream is("2023-08-10 12:46 +01 BST<"); |
| 78 | VERIFY( is >> parse("%F %R %z %Z", d, abbrev, offset) ); |
| 79 | VERIFY( ! is.eof() ); |
| 80 | VERIFY( d == 10d ); |
| 81 | VERIFY( abbrev == "BST" ); |
| 82 | VERIFY( offset == 60min ); |
| 83 | |
| 84 | abbrev = "nope"; |
| 85 | offset = 999min; |
| 86 | is.clear(); |
| 87 | is.str("30"); |
| 88 | VERIFY( is >> parse("%d", d, abbrev, offset) ); |
| 89 | VERIFY( ! is.eof() ); |
| 90 | VERIFY( d == 30d ); |
| 91 | VERIFY( abbrev == "nope" ); |
| 92 | VERIFY( offset == 999min ); |
| 93 | |
| 94 | d = day(255); |
| 95 | is.clear(); |
| 96 | is.str("2023-02-30"); |
| 97 | is >> parse("%F", d); // Feb 30 is not a valid day |
| 98 | VERIFY( is.fail() ); |
| 99 | VERIFY( d == day(255) ); |
| 100 | |
| 101 | is.clear(); |
| 102 | is.str("February 30"); |
| 103 | is >> parse("%B %d", d); // Feb 30 is not a valid day |
| 104 | VERIFY( is.fail() ); |
| 105 | VERIFY( d == day(255) ); |
| 106 | |
| 107 | is.clear(); |
| 108 | is.str("February 29"); |
| 109 | is >> parse("%B %d", d); // But Feb 29 could be valid. |
| 110 | VERIFY( is.good() ); |
| 111 | VERIFY( d == 29d ); |
| 112 | |
| 113 | d = day(255); |
| 114 | is.clear(); |
| 115 | is.str("2023 Feb 29"); |
| 116 | is >> parse("%Y %B %d", d); // But 2023 is not a leap year. |
| 117 | VERIFY( is.fail() ); |
| 118 | VERIFY( d == day(255) ); |
| 119 | |
| 120 | d = day(255); |
| 121 | is.clear(); |
| 122 | is.str("20 Feb 29"); |
| 123 | is >> parse("%y %B %d", d); // But 2020 is a leap year. |
| 124 | VERIFY( is.good() ); |
| 125 | VERIFY( d == 29d ); |
| 126 | } |
| 127 | |
| 128 | int main() |
| 129 | { |
| 130 | test_ostream(); |
| 131 | test_format(); |
| 132 | test_parse(); |
| 133 | } |