Debian Codegraph @

libstdc++-v3/testsuite/std/format/functions/format_to_n.cc

raw
1 // { dg-do run { target c++20 } }
2
3 #include <format>
4 #include <vector>
5 #include <testsuite_hooks.h>
6
7 struct punct : std::numpunct<char>
8 {
9 std::string do_grouping() const override { return "\2"; }
10 std::string do_truename() const override { return "troo"; }
11 std::string do_falsename() const override { return "falz"; }
12 };
13
14 void
15 test()
16 {
17 char buf[4] = { };
18 auto [out, len] = std::format_to_n(buf, 3, "123 + 456 = {}", 579);
19 VERIFY( out == buf+3 );
20 VERIFY( len == 15 );
21
22 std::locale loc({}, new punct);
23 auto [out2, len2] = std::format_to_n(buf, 4, loc, "{:Ld}", 12345);
24 VERIFY( out2 == buf+4 );
25 VERIFY( len2 == 7 );
26 VERIFY( std::string_view(buf, 4) == "1,23" );
27 }
28
29 struct wpunct : std::numpunct<wchar_t>
30 {
31 std::string do_grouping() const override { return "\2"; }
32 std::wstring do_truename() const override { return L"troo"; }
33 std::wstring do_falsename() const override { return L"falz"; }
34 };
35
36 void
37 test_wchar()
38 {
39 wchar_t buf[4] = { };
40 auto [out, len] = std::format_to_n(buf, 3, L"123 + 456 = {}", 579);
41 VERIFY( out == buf+3 );
42 VERIFY( len == 15 );
43
44 std::locale loc({}, new wpunct);
45 auto [out2, len2] = std::format_to_n(buf, 4, loc, L"{:Ld}", 12345);
46 VERIFY( out2 == buf+4 );
47 VERIFY( len2 == 7 );
48 VERIFY( std::wstring_view(buf, 4) == L"1,23" );
49 }
50
51 template<typename I>
52 struct move_only_iterator
53 {
54 using iterator = I;
55 using value_type = iterator::value_type;
56 using difference_type = iterator::difference_type;
57 using iterator_category = std::output_iterator_tag;
58
59 move_only_iterator(iterator b) : base_(b) { }
60 move_only_iterator(move_only_iterator&&) = default;
61 move_only_iterator& operator=(move_only_iterator&&) = default;
62
63 move_only_iterator& operator++() { ++base_; return *this; }
64 move_only_iterator operator++(int) { auto tmp = *this; ++base_; return tmp; }
65
66 decltype(auto) operator*() { return *base_; }
67
68 private:
69 iterator base_;
70 };
71
72 void
73 test_move_only()
74 {
75 std::string str;
76 move_only_iterator mo(std::back_inserter(str));
77 auto [res, len] = std::format_to_n(std::move(mo), 4, "for{:.3} that{:c}",
78 "matte", (int)'!');
79 VERIFY( str == "form" );
80 VERIFY( len == 12 );
81
82 std::vector<wchar_t> vec;
83 move_only_iterator wmo(std::back_inserter(vec));
84 auto [wres, wlen] = std::format_to_n(std::move(wmo), 9, L"for{:.3} hat{:c}",
85 L"matte", (long)L'!');
86 VERIFY( std::wstring_view(vec.data(), vec.size()) == L"format ha" );
87 VERIFY( wlen == 11 );
88 }
89
90 void
91 test_pr110990()
92 {
93 // PR libstdc++/110990 - format_to_n returns wrong value
94
95 char buf[2];
96 auto [ptr, len] = std::format_to_n(buf, 2, "x");
97 VERIFY( len == 1 );
98 VERIFY( ptr == buf + len );
99
100 wchar_t wbuf[3];
101 auto [wptr, wlen] = std::format_to_n(wbuf, 3, L"yz");
102 VERIFY( wlen == 2 );
103 VERIFY( wptr == wbuf + wlen );
104 }
105
106 int main()
107 {
108 test();
109 test_wchar();
110 test_move_only();
111 test_pr110990();
112 }