diff --git a/src/cpp-utils/CMakeLists.txt b/src/cpp-utils/CMakeLists.txt index c16568e4..089c201f 100644 --- a/src/cpp-utils/CMakeLists.txt +++ b/src/cpp-utils/CMakeLists.txt @@ -37,6 +37,7 @@ set(SOURCES data/DataFixture.cpp data/DataUtils.cpp data/Data.cpp + assert/assert.h assert/backtrace_nonwindows.cpp assert/backtrace_windows.cpp assert/AssertFailed.cpp diff --git a/src/cpp-utils/assert/assert.cpp b/src/cpp-utils/assert/assert.cpp new file mode 100644 index 00000000..e6adb8fd --- /dev/null +++ b/src/cpp-utils/assert/assert.cpp @@ -0,0 +1 @@ +#include "assert.h" diff --git a/test/cpp-utils/CMakeLists.txt b/test/cpp-utils/CMakeLists.txt index b0c22dda..4c2b9996 100644 --- a/test/cpp-utils/CMakeLists.txt +++ b/test/cpp-utils/CMakeLists.txt @@ -43,8 +43,6 @@ set(SOURCES logging/LoggingIncludeTest.cpp assert/assert_release_test.cpp assert/backtrace_test.cpp - assert/backtrace_include_test.cpp - assert/assert_include_test.cpp assert/assert_debug_test.cpp system/GetTotalMemoryTest.cpp system/TimeTest.cpp diff --git a/test/cpp-utils/assert/assert_debug_test.cpp b/test/cpp-utils/assert/assert_debug_test.cpp index 7e6d4be1..cc5b3f5e 100644 --- a/test/cpp-utils/assert/assert_debug_test.cpp +++ b/test/cpp-utils/assert/assert_debug_test.cpp @@ -23,3 +23,10 @@ TEST(AssertTest_DebugBuild, AssertMessage) { "Assertion \\[2==5\\] failed in .*/assert_debug_test.cpp:[0-9]+: my message" ); } + +TEST(AssertTest_DebugBuild, AssertMessageContainsBacktrace) { + EXPECT_DEATH( + ASSERT(2==5, "my message"), + "cpputils::backtrace" + ); +} diff --git a/test/cpp-utils/assert/assert_include_test.cpp b/test/cpp-utils/assert/assert_include_test.cpp deleted file mode 100644 index b7dbf23a..00000000 --- a/test/cpp-utils/assert/assert_include_test.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "cpp-utils/assert/assert.h" - -// Test the header can be included without needing additional dependencies diff --git a/test/cpp-utils/assert/assert_release_test.cpp b/test/cpp-utils/assert/assert_release_test.cpp index 792bf57a..2043d946 100644 --- a/test/cpp-utils/assert/assert_release_test.cpp +++ b/test/cpp-utils/assert/assert_release_test.cpp @@ -8,6 +8,7 @@ #include "cpp-utils/assert/assert.h" using testing::MatchesRegex; +using testing::HasSubstr; TEST(AssertTest_ReleaseBuild, DoesntThrowIfTrue) { ASSERT(true, "bla"); @@ -26,7 +27,18 @@ TEST(AssertTest_ReleaseBuild, AssertMessage) { FAIL(); } catch (const cpputils::AssertFailed &e) { EXPECT_THAT(e.what(), MatchesRegex( - "Assertion \\[2==5\\] failed in .*/assert_release_test.cpp:25: my message.*" + "Assertion \\[2==5\\] failed in .*/assert_release_test.cpp:26: my message.*" )); } -} \ No newline at end of file +} + +TEST(AssertTest_ReleaseBuild, AssertMessageContainsBacktrace) { + try { + ASSERT(2==5, "my message"); + FAIL(); + } catch (const cpputils::AssertFailed &e) { + EXPECT_THAT(e.what(), HasSubstr( + "cpputils::backtrace" + )); + } +} diff --git a/test/cpp-utils/assert/backtrace_include_test.cpp b/test/cpp-utils/assert/backtrace_include_test.cpp deleted file mode 100644 index fc2e6226..00000000 --- a/test/cpp-utils/assert/backtrace_include_test.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "cpp-utils/assert/backtrace.h" - -// Test the header can be included without needing additional dependencies - diff --git a/test/cpp-utils/assert/backtrace_test.cpp b/test/cpp-utils/assert/backtrace_test.cpp index 199cf736..e1a24422 100644 --- a/test/cpp-utils/assert/backtrace_test.cpp +++ b/test/cpp-utils/assert/backtrace_test.cpp @@ -14,3 +14,19 @@ TEST(BacktraceTest, ContainsTopLevelLine) { EXPECT_THAT(backtrace, HasSubstr("BacktraceTest")); EXPECT_THAT(backtrace, HasSubstr("ContainsTopLevelLine")); } + +namespace { +void cause_sigsegv() { + cpputils::showBacktraceOnSigSegv(); + int* ptr = nullptr; + int a = *ptr; + (void)a; +} +} + +TEST(BacktraceTest, ShowBacktraceOnSigSegv) { + EXPECT_DEATH( + cause_sigsegv(), + "cpputils::backtrace" + ); +}