| 1 | // { dg-options "-funwind-tables -lstdc++exp" } |
| 2 | // { dg-do run { target c++23 } } |
| 3 | // { dg-require-cpp-feature-test __cpp_lib_stacktrace } |
| 4 | // { dg-add-options no_pch } |
| 5 | |
| 6 | #include <stacktrace> |
| 7 | #include <sstream> |
| 8 | #include <testsuite_hooks.h> |
| 9 | |
| 10 | #ifndef __cpp_lib_formatters |
| 11 | # error "Feature-test macro for formatters missing in <stacktrace>" |
| 12 | #elif __cpp_lib_formatters < 202302L |
| 13 | # error "Feature-test macro for formatters has wrong value in <stacktrace>" |
| 14 | #endif |
| 15 | |
| 16 | void |
| 17 | test_to_string() |
| 18 | { |
| 19 | auto trace = std::stacktrace::current(); |
| 20 | std::string s1 = std::to_string(trace.at(0)); |
| 21 | VERIFY( s1.contains("test_to_string()") ); |
| 22 | VERIFY( s1.contains("output.cc:19") ); |
| 23 | std::string s2 = std::to_string(trace); |
| 24 | VERIFY( s2.contains(s1) ); |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | test_ostream() |
| 29 | { |
| 30 | std::ostringstream out; |
| 31 | auto trace = std::stacktrace::current(); |
| 32 | out << trace.at(0); |
| 33 | VERIFY( out.str() == std::to_string(trace.at(0)) ); |
| 34 | out.str(""); |
| 35 | out << trace; |
| 36 | VERIFY( out.str() == std::to_string(trace) ); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | test_format() |
| 41 | { |
| 42 | static_assert( std::is_default_constructible_v<std::formatter<std::stacktrace_entry, char>> ); |
| 43 | static_assert( std::is_default_constructible_v<std::formatter<std::stacktrace, char>> ); |
| 44 | static_assert( std::is_default_constructible_v<std::formatter<std::pmr::stacktrace, char>> ); |
| 45 | |
| 46 | auto trace = std::pmr::stacktrace::current(); |
| 47 | VERIFY( std::format("{}", trace) == std::to_string(trace) ); |
| 48 | |
| 49 | std::stacktrace_entry entry = trace.at(0); |
| 50 | std::string str = std::to_string(entry); |
| 51 | VERIFY( std::format("{}", entry) == str ); |
| 52 | auto len = str.size(); |
| 53 | // with width |
| 54 | VERIFY( std::format("{0:{1}}", entry, len + 1) == (str + " ") ); |
| 55 | // with align + width |
| 56 | VERIFY( std::format("{0:<{1}}", entry, len + 2) == (str + " ") ); |
| 57 | VERIFY( std::format("{0:^{1}}", entry, len + 3) == (" " + str + " ") ); |
| 58 | VERIFY( std::format("{0:>{1}}", entry, len + 4) == (" " + str) ); |
| 59 | // with fill-and-align + width |
| 60 | VERIFY( std::format("{0:!<{1}}", entry, len + 2) == (str + "!!") ); |
| 61 | VERIFY( std::format("{0:!^{1}}", entry, len + 3) == ("!" + str + "!!") ); |
| 62 | VERIFY( std::format("{0:!>{1}}", entry, len + 4) == ("!!!!" + str) ); |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | test_to_string(); |
| 68 | test_ostream(); |
| 69 | test_format(); |
| 70 | } |