| 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 y : {-1234, -44, -1, 0, 5, 32, 325, 1066, 2022}) |
| 17 | ss << year(y) << ' '; |
| 18 | VERIFY( ss.str() == "-1234 -0044 -0001 0000 0005 0032 0325 1066 2022 " ); |
| 19 | ss.str(""); |
| 20 | ss << year::min() << ' ' << year::max() << ' ' << --year::min(); |
| 21 | VERIFY( ss.str() == "-32767 32767 -32768 is not a valid year" ); |
| 22 | |
| 23 | ss.str(""); |
| 24 | ss.imbue(std::locale(ISO_8859(15,fr_FR))); |
| 25 | ss << 1789y; |
| 26 | VERIFY( ss.str() == "1789" ); |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | test_format() |
| 31 | { |
| 32 | using std::chrono::year; |
| 33 | using namespace std::chrono_literals; |
| 34 | |
| 35 | auto s = std::format("{:%y%%%Y%t%C%n}", 2022y); |
| 36 | VERIFY( s == "22%2022\t20\n" ); |
| 37 | auto ws = std::format(L"{:%y%%%Y%t%C%n}", 2023y); |
| 38 | VERIFY( ws == L"23%2023\t20\n" ); |
| 39 | |
| 40 | s = std::format("{:%Y}", --year::min()); |
| 41 | VERIFY( s == "-32768" ); |
| 42 | |
| 43 | s = std::format("{}", --year::min()); // formatted via ostream |
| 44 | VERIFY( s == "-32768 is not a valid year" ); |
| 45 | |
| 46 | s = std::format("{:%C %y} {:%C %y}", 1976y, -1976y); |
| 47 | VERIFY( s == "19 76 -20 76" ); // LWG 3831 |
| 48 | |
| 49 | s = std::format("{:%C %y} {:%C %y} {:%C %y}", -9y, -900y, -555y); |
| 50 | VERIFY( s == "-01 09 -09 00 -06 55" ); // LWG 4022 |
| 51 | |
| 52 | s = std::format("{0:%EC}{0:%Ey} = {0:%EY}", 1642y); |
| 53 | VERIFY( s == "1642 = 1642" ); |
| 54 | s = std::format("{0:L%EC}{0:L%Ey} = {0:L%EY}", 1642y); |
| 55 | VERIFY( s == "1642 = 1642" ); |
| 56 | s = std::format(std::locale::classic(), "{0:L%EC}{0:L%Ey} = {0:L%EY}", 1642y); |
| 57 | VERIFY( s == "1642 = 1642" ); |
| 58 | |
| 59 | // TODO test "{:L%EC}" with locale that has alternative era rep. |
| 60 | // TODO test "{:L%Ey}" with locale that has alternative year rep. |
| 61 | // TODO test "{:L%EY}" with locale that has alternative year rep. |
| 62 | // TODO test "{:L%Oy}" with locale that has alternative numeric rep. |
| 63 | |
| 64 | std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ"; |
| 65 | std::string_view my_specs = "CyY"; |
| 66 | for (char c : specs) |
| 67 | { |
| 68 | char fmt[] = { '{', ':', '%', c, '}' }; |
| 69 | try |
| 70 | { |
| 71 | year y = 2022y; |
| 72 | (void) std::vformat(std::string_view(fmt, 5), std::make_format_args(y)); |
| 73 | // The call above should throw for any conversion-spec not in my_specs: |
| 74 | VERIFY(my_specs.find(c) != my_specs.npos); |
| 75 | } |
| 76 | catch (const std::format_error& e) |
| 77 | { |
| 78 | VERIFY(my_specs.find(c) == my_specs.npos); |
| 79 | std::string_view s = e.what(); |
| 80 | // Libstdc++-specific message: |
| 81 | VERIFY(s.find("format argument does not contain the information " |
| 82 | "required by the chrono-specs") != s.npos); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | test_parse() |
| 89 | { |
| 90 | using namespace std::chrono; |
| 91 | year y; |
| 92 | |
| 93 | std::istringstream is("2023"); |
| 94 | VERIFY( is >> parse("%Y", y) ); |
| 95 | VERIFY( ! is.eof() ); |
| 96 | VERIFY( y == year(2023) ); |
| 97 | |
| 98 | is.clear(); |
| 99 | is.str("2023"); |
| 100 | VERIFY( is >> parse("%5Y", y) ); |
| 101 | VERIFY( is.eof() ); |
| 102 | VERIFY( y == year(2023) ); |
| 103 | |
| 104 | is.clear(); |
| 105 | is.str("2023"); |
| 106 | VERIFY( is >> parse("%2Y", y) ); |
| 107 | VERIFY( ! is.eof() ); |
| 108 | VERIFY( y == year(20) ); |
| 109 | |
| 110 | is.clear(); |
| 111 | is.str("2023"); |
| 112 | VERIFY( is >> parse("%y", y) ); |
| 113 | VERIFY( ! is.eof() ); |
| 114 | VERIFY( y == year(2020) ); |
| 115 | |
| 116 | minutes offset; |
| 117 | std::string abbrev; |
| 118 | |
| 119 | is.clear(); |
| 120 | is.str("23 20 25:61 +1:30 WAT"); // Invalid %H:%M doesn't matter for year. |
| 121 | VERIFY( is >> parse("%y %C %H:%M %Oz %Z", y, abbrev, offset) ); |
| 122 | VERIFY( is.eof() ); |
| 123 | VERIFY( y == year(2023) ); |
| 124 | VERIFY( abbrev == "WAT" ); |
| 125 | VERIFY( offset == 90min ); |
| 126 | |
| 127 | is.clear(); |
| 128 | is.str("2022 367"); |
| 129 | VERIFY( is >> parse("%Y %j", y) ); // Invalid day-of-year doesn't matter. |
| 130 | VERIFY( ! is.eof() ); |
| 131 | VERIFY( y == 2022y ); |
| 132 | |
| 133 | y = 999y; |
| 134 | is.clear(); |
| 135 | is.str("2023"); |
| 136 | VERIFY( ! (is >> parse("%G", y)) ); // ISO year not aligned with Gregorian. |
| 137 | VERIFY( y == 999y ); |
| 138 | |
| 139 | is.clear(); |
| 140 | is.str("2023-W01-1"); // 2023-1-2 |
| 141 | is >> parse("%G-W%V-%u", y); // Can get Gregorian year from full ISO date. |
| 142 | VERIFY( ! is.eof() ); |
| 143 | VERIFY( y == 2023y ); |
| 144 | |
| 145 | is.clear(); |
| 146 | is.str("2022-W052-7"); // 2023-1-1 |
| 147 | is >> parse("%G-W%3V-%2u", y); |
| 148 | VERIFY( is.eof() ); |
| 149 | VERIFY( y == 2023y ); |
| 150 | |
| 151 | y = year(1); |
| 152 | is.clear(); |
| 153 | is.str("2023 01"); |
| 154 | VERIFY( !( is >> parse("%Y %z xx", y)) ); // Gets EOF and can't parse " xx". |
| 155 | VERIFY( is.eof() ); |
| 156 | VERIFY( y == year(1) ); |
| 157 | } |
| 158 | |
| 159 | int main() |
| 160 | { |
| 161 | test_ostream(); |
| 162 | test_format(); |
| 163 | test_parse(); |
| 164 | } |