Add a test case for the stack trace formatter

This commit is contained in:
Julian Brost 2020-10-23 15:04:28 +02:00
parent b931194f59
commit 24f6283362
2 changed files with 46 additions and 0 deletions

View File

@ -16,6 +16,7 @@ set(base_test_SOURCES
base-object-packer.cpp
base-serialize.cpp
base-shellescape.cpp
base-stacktrace.cpp
base-stream.cpp
base-string.cpp
base-timer.cpp
@ -89,6 +90,7 @@ add_boost_test(base
base_serialize/object
base_shellescape/escape_basic
base_shellescape/escape_quoted
base_stacktrace/stacktrace
base_stream/readline_stdio
base_string/construct
base_string/equal

44
test/base-stacktrace.cpp Normal file
View File

@ -0,0 +1,44 @@
/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */
#include "base/stacktrace.hpp"
#include <BoostTestTargetConfig.h>
using namespace icinga;
#pragma GCC push_options
#pragma GCC optimize ("O0")
#pragma clang optimize off
BOOST_AUTO_TEST_SUITE(base_stacktrace)
[[gnu::noinline]]
void stack_test_func_b()
{
boost::stacktrace::stacktrace stack;
std::ostringstream obuf;
obuf << StackTraceFormatter(stack);
std::string result = obuf.str();
BOOST_CHECK_MESSAGE(!result.empty(), "stack trace must not be empty");
size_t pos_a = result.find("stack_test_func_a");
size_t pos_b = result.find("stack_test_func_b");
BOOST_CHECK_MESSAGE(pos_a != std::string::npos, "'stack_test_func_a' not found\n\n" << result);
BOOST_CHECK_MESSAGE(pos_b != std::string::npos, "'stack_test_func_b' not found\n\n" << result);
BOOST_CHECK_MESSAGE(pos_a > pos_b, "'stack_test_func_a' must appear after 'stack_test_func_b'\n\n" << result);
}
[[gnu::noinline]]
void stack_test_func_a()
{
stack_test_func_b();
}
BOOST_AUTO_TEST_CASE(stacktrace)
{
stack_test_func_a();
}
BOOST_AUTO_TEST_SUITE_END()
#pragma GCC pop_options
#pragma clang optimize on