From 32217d22d52cb162bc6809ddeae9c54189869f87 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Thu, 23 Jul 2015 16:30:30 +0200 Subject: [PATCH] Add first test to verify libunwind integration behaves sane. Turns out that you must not build libunwind with the gold linker, it completely breaks the stack unwinding functionality for me. TODO: use a proper testing framework here. --- CMakeLists.txt | 1 + tests/CMakeLists.txt | 4 ++++ tests/tst_trace.cpp | 35 +++++++++++++++++++++++++++++++++++ trace.h | 4 +++- 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/tst_trace.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c495405..36c18b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 2.8.9) project(heaptrack) +enable_testing() set(HEAPTRACK_VERSION_MAJOR 0) set(HEAPTRACK_VERSION_MINOR 1) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8c10c18..558118d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,3 +10,7 @@ add_executable(callgraph callgraph.cpp) add_library(testlib SHARED lib.cpp) add_executable(test_lib test_lib.cpp) target_link_libraries(test_lib testlib) + +add_executable(tst_trace tst_trace.cpp) +target_link_libraries(tst_trace ${LIBUNWIND_LIBRARY}) +add_test(NAME tst_trace COMMAND tst_trace ) \ No newline at end of file diff --git a/tests/tst_trace.cpp b/tests/tst_trace.cpp new file mode 100644 index 0000000..1cad5f3 --- /dev/null +++ b/tests/tst_trace.cpp @@ -0,0 +1,35 @@ +#include "../trace.h" + +#include +#include + +using namespace std; + +bool fill(Trace& trace, int depth, int skip) +{ + if (!depth) { + return trace.fill(skip); + } else { + return fill(trace, depth - 1, skip); + } +} + +int main() +{ + Trace trace; + assert(trace.size() == 0); + + assert(trace.fill(0)); + const auto offset = trace.size(); + assert(offset > 1); + + for (auto skip : {0, 1, 2}) { + for (int i = 0; i < 2 * Trace::MAX_SIZE; ++i) { + assert(fill(trace, i, skip)); + const auto expectedSize = min(i + offset + 1 - skip, static_cast(Trace::MAX_SIZE) - skip); + assert(trace.size() == expectedSize); + } + } + + return 0; +} diff --git a/trace.h b/trace.h index 9a66a7d..3fa04c9 100644 --- a/trace.h +++ b/trace.h @@ -32,7 +32,9 @@ struct Trace { using ip_t = void*; - static const int MAX_SIZE = 64; + enum : int { + MAX_SIZE = 64 + }; const ip_t* begin() const { -- 2.7.4