| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-timeout-factor 2 } |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <sstream> |
| 6 | #include <testsuite_hooks.h> |
| 7 | |
| 8 | #ifndef __cpp_lib_char8_t |
| 9 | using char8_t = unsigned char; // Prevent errors if -fno-char8_t is used. |
| 10 | #endif |
| 11 | |
| 12 | void |
| 13 | test01() |
| 14 | { |
| 15 | using namespace std::chrono; |
| 16 | std::stringstream ss; |
| 17 | ss << 0s << '\n'; |
| 18 | ss << 3h + 5min << '\n'; |
| 19 | ss << duration<long, std::ratio<2>>(3) << '\n'; |
| 20 | ss << duration<long, std::ratio<2, 3>>(9) << '\n'; |
| 21 | std::string s; |
| 22 | std::getline(ss, s); |
| 23 | VERIFY( s == "0s" ); |
| 24 | std::getline(ss, s); |
| 25 | VERIFY( s == "185min" ); |
| 26 | std::getline(ss, s); |
| 27 | VERIFY( s == "3[2]s" ); |
| 28 | std::getline(ss, s); |
| 29 | VERIFY( s == "9[2/3]s" ); |
| 30 | |
| 31 | // LWG 4118. How should duration formatters format custom rep types? |
| 32 | ss.str(""); |
| 33 | ss << duration<char>(121) << ' '; |
| 34 | ss << duration<wchar_t>(122) << ' '; |
| 35 | ss << duration<signed char>(123) << ' '; |
| 36 | ss << duration<unsigned char>(124) << ' '; |
| 37 | ss << duration<char8_t>(125) << ' '; |
| 38 | ss << duration<char16_t>(126) << ' '; |
| 39 | ss << duration<char32_t>(127) << ' '; |
| 40 | VERIFY( ss.str() == "121s 122s 123s 124s 125s 126s 127s " ); |
| 41 | |
| 42 | ss.str(""); |
| 43 | ss << std::hex << std::uppercase << duration<const char>(0x1A) << ' '; |
| 44 | ss << std::hex << std::uppercase << duration<const wchar_t>(0x2A) << ' '; |
| 45 | ss << std::hex << std::uppercase << duration<signed char>(0x3A) << ' '; |
| 46 | ss << std::scientific << duration<const double>(4.5) << ' '; |
| 47 | VERIFY( ss.str() == "1As 2As 3As 4.500000E+00s " ); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | test02() |
| 52 | { |
| 53 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 54 | using namespace std::chrono; |
| 55 | std::wstringstream ss; |
| 56 | ss << 0s << L'\n'; |
| 57 | ss << 3h + 5min << L'\n'; |
| 58 | ss << duration<long, std::ratio<2>>(3) << L'\n'; |
| 59 | ss << duration<long, std::ratio<2, 3>>(9) << L'\n'; |
| 60 | std::wstring s; |
| 61 | std::getline(ss, s); |
| 62 | VERIFY( s == L"0s" ); |
| 63 | std::getline(ss, s); |
| 64 | VERIFY( s == L"185min" ); |
| 65 | std::getline(ss, s); |
| 66 | VERIFY( s == L"3[2]s" ); |
| 67 | std::getline(ss, s); |
| 68 | VERIFY( s == L"9[2/3]s" ); |
| 69 | |
| 70 | // LWG 4118. How should duration formatters format custom rep types? |
| 71 | ss.str(L""); |
| 72 | ss << duration<char>(121) << ' '; |
| 73 | ss << duration<wchar_t>(122) << ' '; |
| 74 | ss << duration<signed char>(123) << ' '; |
| 75 | ss << duration<unsigned char>(124) << ' '; |
| 76 | ss << duration<char8_t>(125) << ' '; |
| 77 | ss << duration<char16_t>(126) << ' '; |
| 78 | ss << duration<char32_t>(127) << ' '; |
| 79 | VERIFY( ss.str() == L"121s 122s 123s 124s 125s 126s 127s " ); |
| 80 | |
| 81 | ss.str(L""); |
| 82 | ss << std::hex << std::uppercase << duration<const char>(0x1A) << ' '; |
| 83 | ss << std::hex << std::uppercase << duration<const wchar_t>(0x2A) << ' '; |
| 84 | ss << std::hex << std::uppercase << duration<signed char>(0x3A) << ' '; |
| 85 | ss << std::scientific << duration<const double>(4.5) << ' '; |
| 86 | VERIFY( ss.str() == L"1As 2As 3As 4.500000E+00s " ); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | test_format() |
| 92 | { |
| 93 | using namespace std::chrono_literals; |
| 94 | auto s = std::format("{} {}", 1h + 23min + 45s, -42min); |
| 95 | VERIFY( s == "5025s -42min" ); |
| 96 | s = std::format("{:%j} {:%j} {:%j}", 1h + 23min + 45s, 75h, -99h); |
| 97 | VERIFY( s == "0 3 -4" ); |
| 98 | s = std::format("{:%T = %H:%M:%S}", 1h + 23min + 45s); |
| 99 | VERIFY( s == "01:23:45 = 01:23:45" ); |
| 100 | s = std::format("{:%Q} {:%q} {:%Q%q}", 6min + 1s, 44min, -22h); |
| 101 | VERIFY( s == "361 min -22h" ); |
| 102 | |
| 103 | std::wstring ws = std::format(L"{:%Q%q}", 81s); |
| 104 | VERIFY( ws == L"81s" ); |
| 105 | |
| 106 | // Only print '-' on numeric fields for negative durations: |
| 107 | s = std::format("{:%Q} {:%q} {:%q%Q}", -21h, -20h, -19h); |
| 108 | VERIFY( s == "-21 h h-19" ); |
| 109 | s = std::format("{:%p} {:%p%H}", -2h, -13h); |
| 110 | VERIFY( s == "AM PM-13" ); |
| 111 | s = std::format("{:%t} {:%t%M}", -2h, -123s); |
| 112 | VERIFY( s == "\t \t-02" ); |
| 113 | |
| 114 | // Locale-specific formats: |
| 115 | s = std::format(std::locale::classic(), "{:%r %OH:%OM:%OS}", 123456ms); |
| 116 | VERIFY( s == "12:02:03 AM 00:02:03" ); |
| 117 | |
| 118 | std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ"; |
| 119 | std::string_view my_specs = "HIjMpqQrRSTX"; |
| 120 | for (char c : specs) |
| 121 | { |
| 122 | char fmt[] = { '{', ':', '%', c, '}' }; |
| 123 | try |
| 124 | { |
| 125 | auto s = 1s; |
| 126 | (void) std::vformat(std::string_view(fmt, 5), std::make_format_args(s)); |
| 127 | // The call above should throw for any conversion-spec not in my_specs: |
| 128 | VERIFY(my_specs.find(c) != my_specs.npos); |
| 129 | } |
| 130 | catch (const std::format_error& e) |
| 131 | { |
| 132 | VERIFY(my_specs.find(c) == my_specs.npos); |
| 133 | std::string_view s = e.what(); |
| 134 | // Libstdc++-specific message: |
| 135 | VERIFY(s.find("format argument does not contain the information " |
| 136 | "required by the chrono-specs") != s.npos); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | std::chrono::duration<float, std::milli> d{0.5}; |
| 141 | s = std::format("{}", d); |
| 142 | VERIFY( s == "0.5ms" ); |
| 143 | |
| 144 | std::chrono::duration<unsigned, std::milli> u{500}; // PR libstdc++/115668 |
| 145 | s = std::format("{}", u); |
| 146 | VERIFY( s == "500ms" ); |
| 147 | s = std::format("{:%Q %q}", u); |
| 148 | VERIFY( s == "500 ms" ); |
| 149 | |
| 150 | // PR libstdc++/116755 extra minus sign for most negative value |
| 151 | auto minsec = std::chrono::seconds::min(); |
| 152 | s = std::format("{}", minsec); |
| 153 | auto expected = std::format("{}s", minsec.count()); |
| 154 | VERIFY( s == expected ); |
| 155 | s = std::format("{:%Q%q}", minsec); |
| 156 | VERIFY( s == expected ); |
| 157 | |
| 158 | // LWG 4118. How should duration formatters format custom rep types? |
| 159 | s = std::format("{}", std::chrono::duration<char>(100)); |
| 160 | VERIFY( s == "100s" ); |
| 161 | s = std::format("{:%Q}", std::chrono::duration<char>(101)); |
| 162 | VERIFY( s == "101" ); |
| 163 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 164 | ws = std::format(L"{}", std::chrono::duration<char>(102)); |
| 165 | VERIFY( ws == L"102s" ); |
| 166 | ws = std::format(L"{}", std::chrono::duration<wchar_t>(103)); |
| 167 | VERIFY( ws == L"103s" ); |
| 168 | #endif |
| 169 | s = std::format("{}", std::chrono::duration<signed char>(50)); |
| 170 | VERIFY( s == "50s" ); |
| 171 | s = std::format("{:%Q}", std::chrono::duration<signed char>(51)); |
| 172 | VERIFY( s == "51" ); |
| 173 | s = std::format("{}", std::chrono::duration<unsigned char>(52)); |
| 174 | VERIFY( s == "52s" ); |
| 175 | s = std::format("{:%Q}", std::chrono::duration<unsigned char>(53)); |
| 176 | VERIFY( s == "53" ); |
| 177 | |
| 178 | #if __cplusplus > 202002L |
| 179 | static_assert( ! std::formattable<std::chrono::duration<wchar_t>, char> ); |
| 180 | static_assert( ! std::formattable<std::chrono::duration<char16_t>, char> ); |
| 181 | static_assert( ! std::formattable<std::chrono::duration<char32_t>, char> ); |
| 182 | static_assert( ! std::formattable<std::chrono::duration<char16_t>, wchar_t> ); |
| 183 | static_assert( ! std::formattable<std::chrono::duration<char32_t>, wchar_t> ); |
| 184 | #ifdef __cpp_lib_char8_t |
| 185 | static_assert( ! std::formattable<std::chrono::duration<char8_t>, char> ); |
| 186 | static_assert( ! std::formattable<std::chrono::duration<char8_t>, wchar_t> ); |
| 187 | #endif |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | test_parse() |
| 193 | { |
| 194 | using namespace std::chrono; |
| 195 | seconds s; |
| 196 | milliseconds ms; |
| 197 | microseconds us; |
| 198 | |
| 199 | std::istringstream is(" 2023-07-24 13:05"); |
| 200 | VERIFY( is >> parse(" %Y-%m-%d %H:%M", s) ); |
| 201 | VERIFY( is.good() ); |
| 202 | VERIFY( s == 13h + 5min ); |
| 203 | |
| 204 | s = 999s; |
| 205 | |
| 206 | is.clear(); |
| 207 | is.str("Thursday July 2023"); |
| 208 | VERIFY( !(is >> parse("%a %b %C%y", s)) ); |
| 209 | VERIFY( ! is.eof() ); |
| 210 | VERIFY( s == 999s ); |
| 211 | |
| 212 | is.clear(); |
| 213 | is.str("27"); |
| 214 | VERIFY( is >> parse("%j", s) ); |
| 215 | VERIFY( is.eof() ); |
| 216 | VERIFY( s == 24h * 27 ); |
| 217 | |
| 218 | is.clear(); |
| 219 | is.str("027"); |
| 220 | VERIFY( is >> parse("%j", s) ); |
| 221 | VERIFY( ! is.eof() ); |
| 222 | VERIFY( s == 24h * 27 ); |
| 223 | |
| 224 | is.clear(); |
| 225 | is.str("0027"); |
| 226 | VERIFY( is >> parse("%j", s) ); // defaults to %3j |
| 227 | VERIFY( is.get() == '7' ); |
| 228 | VERIFY( s == 24h * 2 ); |
| 229 | |
| 230 | is.clear(); |
| 231 | is.str("1234"); |
| 232 | VERIFY( is >> parse("%2j", s) ); |
| 233 | VERIFY( is.get() == '3' ); |
| 234 | VERIFY( s == 24h * 12 ); |
| 235 | |
| 236 | is.clear(); |
| 237 | is.str("001234"); |
| 238 | VERIFY( is >> parse("%4j", s) ); |
| 239 | VERIFY( is.get() == '3' ); |
| 240 | VERIFY( s == 24h * 12 ); |
| 241 | |
| 242 | is.clear(); |
| 243 | is.str("1234"); |
| 244 | VERIFY( is >> parse("%4j", s) ); |
| 245 | VERIFY( ! is.eof() ); |
| 246 | VERIFY( s == 24h * 1234 ); |
| 247 | |
| 248 | is.clear(); |
| 249 | is.str("125"); |
| 250 | VERIFY( is >> parse("%S", s) ); |
| 251 | VERIFY( s == 12s ); |
| 252 | VERIFY( is.get() == '5' ); |
| 253 | |
| 254 | is.clear(); |
| 255 | is.str("0.125"); |
| 256 | VERIFY( is >> parse("%S", s) ); |
| 257 | VERIFY( s == 0s ); |
| 258 | VERIFY( is.get() == '.' ); |
| 259 | |
| 260 | is.clear(); |
| 261 | is.str("0.125"); |
| 262 | VERIFY( is >> parse("%S", ms) ); |
| 263 | VERIFY( ms == 125ms ); |
| 264 | VERIFY( ! is.eof() ); |
| 265 | |
| 266 | is.clear(); |
| 267 | is.str("00.125"); |
| 268 | VERIFY( is >> parse("%S", ms) ); |
| 269 | VERIFY( ms == 125ms ); |
| 270 | VERIFY( ! is.eof() ); |
| 271 | |
| 272 | is.clear(); |
| 273 | is.str("012.345"); |
| 274 | VERIFY( is >> parse("%S", ms) ); |
| 275 | VERIFY( ms == 1000ms ); |
| 276 | VERIFY( is.get() == '2' ); |
| 277 | |
| 278 | is.clear(); |
| 279 | is.str("0.1256"); |
| 280 | VERIFY( is >> parse("%S", ms) ); |
| 281 | VERIFY( ms == 125ms ); |
| 282 | VERIFY( is.get() == '6' ); |
| 283 | |
| 284 | is.clear(); |
| 285 | is.str("0.0009765"); |
| 286 | VERIFY( is >> parse("%S", us) ); |
| 287 | VERIFY( us == 976us ); |
| 288 | VERIFY( is.get() == '5' ); |
| 289 | |
| 290 | is.clear(); |
| 291 | is.str("0.5"); |
| 292 | std::chrono::duration<double> ds; |
| 293 | VERIFY( is >> parse("%S", ds) ); |
| 294 | VERIFY( ds == 0.5s ); |
| 295 | |
| 296 | is.clear(); |
| 297 | is.str("0.125"); |
| 298 | std::chrono::duration<double, std::milli> dms; |
| 299 | VERIFY( is >> parse("%S", dms) ); |
| 300 | VERIFY( dms == 0.125s ); |
| 301 | } |
| 302 | |
| 303 | int main() |
| 304 | { |
| 305 | test01(); |
| 306 | test02(); |
| 307 | test_format(); |
| 308 | test_parse(); |
| 309 | } |