Debian Codegraph @

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

raw
1 // { dg-do run { target c++26 } }
2
3 #include <format>
4 #include <testsuite_hooks.h>
5
6 void
7 test_char()
8 {
9 std::string fmt = "{}";
10 auto s = std::format(std::runtime_format(fmt), 123);
11 VERIFY( s == "123" );
12 }
13
14 void
15 test_wchar()
16 {
17 std::wstring fmt = L"{:#o}";
18 auto s = std::format(std::runtime_format(fmt), 456);
19 VERIFY( s == L"0710" );
20 }
21
22 void
23 test_internal_api()
24 {
25 // Using _Runtime_format_string directly works even in C++20 mode.
26 // This can be used internally by libstdc++.
27 std::string fmt = "{:#x}";
28 auto s = std::format(std::__format::_Runtime_format_string<char>(fmt), 789);
29 VERIFY( s == "0x315" );
30 }
31
32 static_assert( noexcept(std::format_string<>(std::runtime_format(""))) );
33 static_assert( noexcept(std::wformat_string<>(std::runtime_format(L""))) );
34 static_assert( noexcept(std::format_string<int>(std::runtime_format(""))) );
35 static_assert( noexcept(std::wformat_string<char>(std::runtime_format(L""))) );
36 // A format string can be constructed from the result of std::runtime_format
37 // using copy elision, but cannot be constructed from an xvalue.
38 static_assert( !std::is_constructible_v<std::format_string<>,
39 decltype(std::runtime_format(""))&&> );
40 static_assert( !std::is_constructible_v<std::wformat_string<>,
41 decltype(std::runtime_format(L""))&&> );
42
43 int main()
44 {
45 test_char();
46 test_wchar();
47 test_internal_api();
48 }