| 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 = 0; i <= 7; ++i) |
| 17 | ss << weekday(i); |
| 18 | VERIFY( ss.str() == "SunMonTueWedThuFriSatSun" ); |
| 19 | ss.str(""); |
| 20 | ss << weekday(8) << '|' << weekday(99); |
| 21 | VERIFY( ss.str() == "8 is not a valid weekday|99 is not a valid weekday" ); |
| 22 | |
| 23 | ss.str(""); |
| 24 | ss.imbue(std::locale(ISO_8859(15,fr_FR))); |
| 25 | ss << weekday(6); |
| 26 | VERIFY( ss.str() == "sam." ); |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | test_format() |
| 31 | { |
| 32 | using std::chrono::weekday; |
| 33 | |
| 34 | auto s = std::format("{:%a%%%A%t%u%n%w}", std::chrono::Monday); |
| 35 | VERIFY( s == "Mon%Monday\t1\n1" ); |
| 36 | auto ws = std::format(L"{:%a%%%A%t%u%n%w}", weekday(7)); |
| 37 | VERIFY( ws == L"Sun%Sunday\t7\n0" ); |
| 38 | |
| 39 | s = std::format("{:%w}", weekday(8)); |
| 40 | VERIFY( s == "8" ); |
| 41 | |
| 42 | try |
| 43 | { |
| 44 | (void) std::format("{:%a}", weekday(8)); |
| 45 | VERIFY(false); |
| 46 | } |
| 47 | catch (const std::format_error&) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | s = std::format("{} is OK, but {:L}", weekday(2), weekday(13)); |
| 52 | VERIFY( s == "Tue is OK, but 13 is not a valid weekday" ); |
| 53 | |
| 54 | std::locale loc_fr(ISO_8859(15,fr_FR)); |
| 55 | |
| 56 | s = std::format("{:%Ow}", weekday(1)); |
| 57 | VERIFY( s == "1" ); |
| 58 | s = std::format(std::locale::classic(), "{:%Ow}", weekday(1)); |
| 59 | VERIFY( s == "1" ); |
| 60 | s = std::format(std::locale::classic(), "{:L%Ow}", weekday(1)); |
| 61 | VERIFY( s == "1" ); |
| 62 | s = std::format(loc_fr, "{:%Ow}", weekday(1)); |
| 63 | VERIFY( s == "1" ); |
| 64 | s = std::format(loc_fr, "{:L%Ow}", weekday(1)); |
| 65 | VERIFY( s == "1" ); |
| 66 | // TODO test "{:L%Ow}" with locale that has alternative numeric rep. |
| 67 | |
| 68 | s = std::format(loc_fr, "{:%a}", weekday(1)); |
| 69 | VERIFY( s == "Mon" ); |
| 70 | s = std::format(loc_fr, "{:L%a}", weekday(1)); |
| 71 | VERIFY( s == "lun." ); |
| 72 | |
| 73 | std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ"; |
| 74 | std::string_view my_specs = "aAuw"; |
| 75 | for (char c : specs) |
| 76 | { |
| 77 | char fmt[] = { '{', ':', '%', c, '}' }; |
| 78 | try |
| 79 | { |
| 80 | weekday wd(1); |
| 81 | (void) std::vformat(std::string_view(fmt, 5), std::make_format_args(wd)); |
| 82 | // The call above should throw for any conversion-spec not in my_specs: |
| 83 | VERIFY(my_specs.find(c) != my_specs.npos); |
| 84 | } |
| 85 | catch (const std::format_error& e) |
| 86 | { |
| 87 | VERIFY(my_specs.find(c) == my_specs.npos); |
| 88 | std::string_view s = e.what(); |
| 89 | // Libstdc++-specific message: |
| 90 | VERIFY(s.find("format argument does not contain the information " |
| 91 | "required by the chrono-specs") != s.npos); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | test_parse() |
| 98 | { |
| 99 | using namespace std::chrono; |
| 100 | std::istringstream is; |
| 101 | weekday wd{}; |
| 102 | |
| 103 | is.str("fRi funday"); |
| 104 | VERIFY( is >> std::chrono::parse(" %A funday", wd) ); |
| 105 | VERIFY( wd == Friday ); |
| 106 | |
| 107 | is.str("MONDAY xxx"); |
| 108 | VERIFY( is >> std::chrono::parse(" %a xxx ", wd) ); |
| 109 | VERIFY( wd == Monday ); |
| 110 | |
| 111 | is.clear(); |
| 112 | is.str("1"); |
| 113 | VERIFY( is >> std::chrono::parse("%u", wd) ); |
| 114 | VERIFY( wd == Monday ); |
| 115 | is.clear(); |
| 116 | is.str("7"); |
| 117 | VERIFY( is >> std::chrono::parse("%u", wd) ); |
| 118 | VERIFY( wd == Sunday ); |
| 119 | wd = weekday(99); |
| 120 | is.clear(); |
| 121 | is.str("0"); |
| 122 | VERIFY( ! (is >> std::chrono::parse("%u", wd)) ); |
| 123 | VERIFY( wd == weekday(99) ); |
| 124 | is.clear(); |
| 125 | is.str("8"); |
| 126 | VERIFY( ! (is >> std::chrono::parse("%u", wd)) ); |
| 127 | VERIFY( wd == weekday(99) ); |
| 128 | |
| 129 | is.clear(); |
| 130 | is.str("003"); |
| 131 | VERIFY( is >> std::chrono::parse("%3u", wd) ); |
| 132 | VERIFY( wd == Wednesday ); |
| 133 | wd = weekday(99); |
| 134 | is.clear(); |
| 135 | is.str("004"); |
| 136 | VERIFY( ! (is >> std::chrono::parse("%2u", wd)) ); |
| 137 | VERIFY( wd == weekday(99) ); |
| 138 | |
| 139 | is.clear(); |
| 140 | is.str("1"); |
| 141 | VERIFY( is >> std::chrono::parse("%w", wd) ); |
| 142 | VERIFY( wd == Monday ); |
| 143 | is.clear(); |
| 144 | is.str("0"); |
| 145 | VERIFY( is >> std::chrono::parse("%w", wd) ); |
| 146 | VERIFY( wd == Sunday ); |
| 147 | wd = weekday(99); |
| 148 | is.clear(); |
| 149 | is.str("7"); |
| 150 | VERIFY( ! (is >> std::chrono::parse("%w", wd)) ); |
| 151 | VERIFY( wd == weekday(99) ); |
| 152 | is.clear(); |
| 153 | is.str("8"); |
| 154 | VERIFY( ! (is >> std::chrono::parse("%w", wd)) ); |
| 155 | VERIFY( wd == weekday(99) ); |
| 156 | |
| 157 | is.clear(); |
| 158 | is.str("003"); |
| 159 | VERIFY( is >> std::chrono::parse("%3w", wd) ); |
| 160 | VERIFY( wd == Wednesday ); |
| 161 | is.clear(); |
| 162 | is.str("004"); |
| 163 | VERIFY( is >> std::chrono::parse("%2w", wd) ); |
| 164 | VERIFY( wd == Sunday ); |
| 165 | |
| 166 | is.clear(); |
| 167 | is.str("2023-8-11"); |
| 168 | VERIFY( is >> std::chrono::parse("%F", wd) ); |
| 169 | VERIFY( wd == Friday ); |
| 170 | } |
| 171 | |
| 172 | int main() |
| 173 | { |
| 174 | test_ostream(); |
| 175 | test_format(); |
| 176 | test_parse(); |
| 177 | } |