Debian Codegraph @

libstdc++-v3/testsuite/std/time/format/format.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-timeout-factor 2 }
3
4 #include <chrono>
5 #include <testsuite_hooks.h>
6
7 void
8 test_format_strings()
9 {
10 using namespace std::chrono_literals;
11
12 // valid format strings
13 VERIFY( std::format("{}", 1s) == "1s" );
14 VERIFY( std::format("{:}", 1s) == "1s" );
15 VERIFY( std::format("{:L}", 1s) == "1s" );
16 VERIFY( std::format("{:%%%n%t}", 1s) == "%\n\t" );
17 VERIFY( std::format("{:L%%%n%t}", 1s) == "%\n\t" );
18 VERIFY( std::format("{:4%%}", 1s) == "% " );
19 VERIFY( std::format("{:4L%%}", 1s) == "% " );
20 VERIFY( std::format("{: >4}", 1s) == " 1s" );
21 VERIFY( std::format("{: <4}", 1s) == "1s " );
22 VERIFY( std::format("{: <4L}", 1s) == "1s " );
23 VERIFY( std::format("{: >4%%}", 1s) == " %" );
24 VERIFY( std::format("{: >4L%%}", 1s) == " %" );
25 VERIFY( std::format("{: ^4%%}", 1s) == " % " );
26 }
27
28 template<typename... Args>
29 bool
30 is_format_string_for(const char* str, Args&&... args)
31 {
32 try {
33 (void) std::vformat(str, std::make_format_args(args...));
34 return true;
35 } catch (const std::format_error&) {
36 return false;
37 }
38 }
39
40 void
41 test_bad_format_strings()
42 {
43 std::chrono::sys_seconds t{};
44
45 // literal '%' must be formatted as "%%"
46 VERIFY( not is_format_string_for("{:%}", t) );
47
48 // chrono-specs must start with '%'
49 VERIFY( not is_format_string_for("{:a%}", t) );
50 VERIFY( not is_format_string_for("{:La%}", t) );
51
52 // '{' not valid in chrono-specs
53 VERIFY( not is_format_string_for("{:%%{{%%}", t) );
54
55 // padding with leading zero not valid for chrono types
56 VERIFY( not is_format_string_for("{:04%T}", t) );
57
58 // precision only valid for chrono::duration types with floating-point rep.
59 VERIFY( not is_format_string_for("{:.4}", t) );
60
61 // unfinished format string
62 VERIFY( not is_format_string_for("{:", t) );
63
64 // dangling modifiers
65 VERIFY( not is_format_string_for("{:%E}", t) );
66 VERIFY( not is_format_string_for("{:%O}", t) );
67
68 // modifier not valid for conversion specifier
69 VERIFY( not is_format_string_for("{:%Ea}", t) );
70 VERIFY( not is_format_string_for("{:%Oa}", t) );
71
72 // more than one modifier (PR libstdc++/110708)
73 VERIFY( not is_format_string_for("{:%EEc}", t) );
74 VERIFY( not is_format_string_for("{:%EEEc}", t) );
75 VERIFY( not is_format_string_for("{:%OOd}", t) );
76 VERIFY( not is_format_string_for("{:%OOOd}", t) );
77 VERIFY( not is_format_string_for("{:%EEy}", t) );
78 VERIFY( not is_format_string_for("{:%OOy}", t) );
79 VERIFY( not is_format_string_for("{:%OEy}", t) );
80 VERIFY( not is_format_string_for("{:%EOy}", t) );
81 }
82
83 template<typename I>
84 struct move_only_iterator
85 {
86 using iterator = I;
87 using value_type = iterator::value_type;
88 using difference_type = iterator::difference_type;
89 using iterator_category = std::output_iterator_tag;
90
91 move_only_iterator(iterator b) : base_(b) { }
92 move_only_iterator(move_only_iterator&&) = default;
93 move_only_iterator& operator=(move_only_iterator&&) = default;
94
95 move_only_iterator& operator++() { ++base_; return *this; }
96 move_only_iterator operator++(int) { auto tmp = *this; ++base_; return tmp; }
97
98 decltype(auto) operator*() { return *base_; }
99
100 private:
101 iterator base_;
102 };
103
104 void
105 test_move_only_iterator()
106 {
107 using namespace std::chrono;
108 utc_seconds ut(1671543754s);
109 sys_seconds st(1671543727s);
110
111 std::string str;
112 move_only_iterator mo(std::back_inserter(str));
113 std::format_to(std::move(mo), "{:%F} {:%T} {:%Q}", ut, st, 1s);
114 VERIFY( str == "2022-12-20 13:42:07 1" );
115
116 std::vector<wchar_t> vec;
117 move_only_iterator wmo(std::back_inserter(vec));
118 std::format_to(std::move(wmo), L"{:%F} {:%T} {:%Q}", ut, st, 2s);
119 VERIFY( std::wstring_view(vec.data(), vec.size()) == L"2022-12-20 13:42:07 2" );
120 }
121
122 int main()
123 {
124 test_format_strings();
125 test_bad_format_strings();
126 test_move_only_iterator();
127 }