]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/duration/io.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / duration / io.cc
1 // { dg-options "-std=gnu++20" }
2 // { dg-do run { target c++20 } }
3
4 #include <chrono>
5 #include <sstream>
6 #include <testsuite_hooks.h>
7
8 void
9 test01()
10 {
11 using namespace std::chrono;
12 std::stringstream ss;
13 ss << 0s << '\n';
14 ss << 3h + 5min << '\n';
15 ss << duration<long, std::ratio<2>>(3) << '\n';
16 ss << duration<long, std::ratio<2, 3>>(9) << '\n';
17 std::string s;
18 std::getline(ss, s);
19 VERIFY( s == "0s" );
20 std::getline(ss, s);
21 VERIFY( s == "185min" );
22 std::getline(ss, s);
23 VERIFY( s == "3[2]s" );
24 std::getline(ss, s);
25 VERIFY( s == "9[2/3]s" );
26 }
27
28 void
29 test02()
30 {
31 #ifdef _GLIBCXX_USE_WCHAR_T
32 using namespace std::chrono;
33 std::wstringstream ss;
34 ss << 0s << L'\n';
35 ss << 3h + 5min << L'\n';
36 ss << duration<long, std::ratio<2>>(3) << L'\n';
37 ss << duration<long, std::ratio<2, 3>>(9) << L'\n';
38 std::wstring s;
39 std::getline(ss, s);
40 VERIFY( s == L"0s" );
41 std::getline(ss, s);
42 VERIFY( s == L"185min" );
43 std::getline(ss, s);
44 VERIFY( s == L"3[2]s" );
45 std::getline(ss, s);
46 VERIFY( s == L"9[2/3]s" );
47 #endif
48 }
49
50 void
51 test_format()
52 {
53 using namespace std::chrono_literals;
54 auto s = std::format("{} {}", 1h + 23min + 45s, -42min);
55 VERIFY( s == "5025s -42min" );
56 s = std::format("{:%j} {:%j} {:%j}", 1h + 23min + 45s, 75h, -99h);
57 VERIFY( s == "0 3 -4" );
58 s = std::format("{:%T = %H:%M:%S}", 1h + 23min + 45s);
59 VERIFY( s == "01:23:45 = 01:23:45" );
60 s = std::format("{:%Q} {:%q} {:%Q%q}", 6min + 1s, 44min, -22h);
61 VERIFY( s == "361 min -22h" );
62
63 std::wstring ws = std::format(L"{:%Q%q}", 81s);
64 VERIFY( ws == L"81s" );
65
66 // Only print '-' on numeric fields for negative durations:
67 s = std::format("{:%Q} {:%q} {:%q%Q}", -21h, -20h, -19h);
68 VERIFY( s == "-21 h h-19" );
69 s = std::format("{:%p} {:%p%H}", -2h, -13h);
70 VERIFY( s == "AM PM-13" );
71 s = std::format("{:%t} {:%t%M}", -2h, -123s);
72 VERIFY( s == "\t \t-02" );
73
74 std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ";
75 std::string_view my_specs = "HIjMpqQrRSTX";
76 for (char c : specs)
77 {
78 char fmt[] = { '{', ':', '%', c, '}' };
79 try
80 {
81 (void) std::vformat(std::string_view(fmt, 5), std::make_format_args(1s));
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 int main()
97 {
98 test01();
99 test02();
100 test_format();
101 // TODO: test_parse();
102 }