From 975ca6d305557018e5de83d7f954f73ca05e99cc Mon Sep 17 00:00:00 2001 From: Corentin Schreiber Date: Wed, 30 Nov 2022 21:48:08 +0000 Subject: [PATCH 1/3] Test with doctest --- oup.sublime-project | 8 +-- tests/CMakeLists.txt | 11 ++-- ...runtime_tests_observer_assignment_copy.cpp | 12 ++--- tests/runtime_tests_observer_from_this.cpp | 54 +++++++++---------- tests/testing.cpp | 2 + tests/testing.hpp | 44 ++++++++++++++- tests/tests_common.hpp | 51 +++++++++--------- 7 files changed, 112 insertions(+), 70 deletions(-) create mode 100644 tests/testing.cpp diff --git a/oup.sublime-project b/oup.sublime-project index 1aac163..38f57a6 100644 --- a/oup.sublime-project +++ b/oup.sublime-project @@ -57,6 +57,10 @@ "name": "oup_runtime_tests_run", "shell_cmd": "make -j12 oup_runtime_tests_run", }, + { + "name": "doctest_with_main", + "shell_cmd": "make -j12 doctest_with_main", + }, { "name": "oup_runtime_tests", "shell_cmd": "make -j12 oup_runtime_tests", @@ -69,10 +73,6 @@ "name": "oup_speed_benchmark", "shell_cmd": "make -j12 oup_speed_benchmark", }, - { - "name": "snatch", - "shell_cmd": "make -j12 snatch", - }, ], "working_dir": "$folder/build", } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e14920f..47db5ac 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,12 +31,13 @@ endfunction() include(FetchContent) -FetchContent_Declare(snatch - GIT_REPOSITORY https://github.com/cschreib/snatch.git - GIT_TAG v0.1.3) -FetchContent_MakeAvailable(snatch) +FetchContent_Declare(doctest + GIT_REPOSITORY https://github.com/doctest/doctest.git + GIT_TAG v2.4.9) +FetchContent_MakeAvailable(doctest) set(RUNTIME_TEST_FILES + ${PROJECT_SOURCE_DIR}/tests/testing.cpp ${PROJECT_SOURCE_DIR}/tests/tests_common.cpp ${PROJECT_SOURCE_DIR}/tests/memory_tracker.cpp ${PROJECT_SOURCE_DIR}/tests/runtime_tests_owner_misc.cpp @@ -61,7 +62,7 @@ set(RUNTIME_TEST_FILES add_executable(oup_runtime_tests ${RUNTIME_TEST_FILES}) target_link_libraries(oup_runtime_tests PRIVATE oup::oup) -target_link_libraries(oup_runtime_tests PRIVATE snatch::snatch) +target_link_libraries(oup_runtime_tests PRIVATE doctest::doctest) add_platform_definitions(oup_runtime_tests) add_custom_target(oup_runtime_tests_run diff --git a/tests/runtime_tests_observer_assignment_copy.cpp b/tests/runtime_tests_observer_assignment_copy.cpp index d2cfaee..27502dc 100644 --- a/tests/runtime_tests_observer_assignment_copy.cpp +++ b/tests/runtime_tests_observer_assignment_copy.cpp @@ -217,10 +217,10 @@ TEMPLATE_LIST_TEST_CASE( { TestType ptr = make_pointer_deleter_1(); observer_ptr optr{ptr}; - SNATCH_WARNING_PUSH; - SNATCH_WARNING_DISABLE_SELF_ASSIGN; + // SNATCH_WARNING_PUSH; + // SNATCH_WARNING_DISABLE_SELF_ASSIGN; optr = optr; - SNATCH_WARNING_POP; + // SNATCH_WARNING_POP; CHECK(optr.get() == ptr.get()); CHECK(optr.expired() == false); @@ -236,10 +236,10 @@ TEMPLATE_LIST_TEST_CASE( { observer_ptr optr; - SNATCH_WARNING_PUSH; - SNATCH_WARNING_DISABLE_SELF_ASSIGN; + // SNATCH_WARNING_PUSH; + // SNATCH_WARNING_DISABLE_SELF_ASSIGN; optr = optr; - SNATCH_WARNING_POP; + // SNATCH_WARNING_POP; CHECK(optr.get() == nullptr); CHECK(optr.expired() == true); diff --git a/tests/runtime_tests_observer_from_this.cpp b/tests/runtime_tests_observer_from_this.cpp index 5f7295c..af2df83 100644 --- a/tests/runtime_tests_observer_from_this.cpp +++ b/tests/runtime_tests_observer_from_this.cpp @@ -72,15 +72,14 @@ TEMPLATE_LIST_TEST_CASE( CHECK(coptr.expired() == false); CHECK(coptr.get() == orig_ptr); } else { - REQUIRE_THROWS_MATCHES( - (make_observer_from_this(orig_ptr)), oup::bad_observer_from_this, - snatch::matchers::with_what_contains{ - "observer_from_this() called with uninitialized control block"}); - REQUIRE_THROWS_MATCHES( + REQUIRE_THROWS_WITH_AS( + (make_observer_from_this(orig_ptr)), + "observer_from_this() called with uninitialized control block", + oup::bad_observer_from_this); + REQUIRE_THROWS_WITH_AS( (make_const_observer_from_this(orig_ptr)), - oup::bad_observer_from_this, - snatch::matchers::with_what_contains{ - "observer_from_this() called with uninitialized control block"}); + "observer_from_this() called with uninitialized control block", + oup::bad_observer_from_this); } CHECK_INSTANCES(1, 0); @@ -108,14 +107,14 @@ TEMPLATE_LIST_TEST_CASE("observer from this no owner stack", "[observer_from_thi CHECK(coptr.expired() == false); CHECK(coptr.get() == &obj); } else { - REQUIRE_THROWS_MATCHES( - (make_observer_from_this(&obj)), oup::bad_observer_from_this, - snatch::matchers::with_what_contains{ - "observer_from_this() called with uninitialized control block"}); - REQUIRE_THROWS_MATCHES( - (make_const_observer_from_this(&obj)), oup::bad_observer_from_this, - snatch::matchers::with_what_contains{ - "observer_from_this() called with uninitialized control block"}); + REQUIRE_THROWS_WITH_AS( + (make_observer_from_this(&obj)), + "observer_from_this() called with uninitialized control block", + oup::bad_observer_from_this); + REQUIRE_THROWS_WITH_AS( + (make_const_observer_from_this(&obj)), + "observer_from_this() called with uninitialized control block", + oup::bad_observer_from_this); } CHECK_INSTANCES(1, 0); @@ -145,15 +144,14 @@ TEMPLATE_LIST_TEST_CASE( CHECK(coptr.get() == orig_ptr); CHECK_INSTANCES(1, 1); } else { - REQUIRE_THROWS_MATCHES( - (make_observer_from_this(orig_ptr)), oup::bad_observer_from_this, - snatch::matchers::with_what_contains{ - "observer_from_this() called with uninitialized control block"}); - REQUIRE_THROWS_MATCHES( + REQUIRE_THROWS_WITH_AS( + (make_observer_from_this(orig_ptr)), + "observer_from_this() called with uninitialized control block", + oup::bad_observer_from_this); + REQUIRE_THROWS_WITH_AS( (make_const_observer_from_this(orig_ptr)), - oup::bad_observer_from_this, - snatch::matchers::with_what_contains{ - "observer_from_this() called with uninitialized control block"}); + "observer_from_this() called with uninitialized control block", + oup::bad_observer_from_this); } } @@ -386,10 +384,10 @@ TEMPLATE_LIST_TEST_CASE("observer from this in constructor", "[observer_from_thi CHECK_INSTANCES(1, 1); } else { next_test_object_constructor_calls_observer_from_this = true; - REQUIRE_THROWS_MATCHES( - (make_pointer_deleter_1()), oup::bad_observer_from_this, - snatch::matchers::with_what_contains{ - "observer_from_this() called with uninitialized control block"}); + REQUIRE_THROWS_WITH_AS( + (make_pointer_deleter_1()), + "observer_from_this() called with uninitialized control block", + oup::bad_observer_from_this); next_test_object_constructor_calls_observer_from_this = false; } diff --git a/tests/testing.cpp b/tests/testing.cpp new file mode 100644 index 0000000..2fbbd6c --- /dev/null +++ b/tests/testing.cpp @@ -0,0 +1,2 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "testing.hpp" diff --git a/tests/testing.hpp b/tests/testing.hpp index 3651ece..c453a5e 100644 --- a/tests/testing.hpp +++ b/tests/testing.hpp @@ -1 +1,43 @@ -#include "snatch/snatch.hpp" +#include "doctest/doctest.h" + +#include + +#undef TEST_CASE +#define TEST_CASE(name, tags) DOCTEST_TEST_CASE(tags " " name) +#define TEMPLATE_TEST_CASE(name, tags, ...) \ + DOCTEST_TEST_CASE_TEMPLATE(tags " " name, TestType, __VA_ARGS__) + +#define TEMPLATE_LIST_TEST_CASE(name, tags, types) \ + TEST_CASE_TEMPLATE(tags " " name, TestType, types) + +namespace snatch::impl { +template +constexpr std::string_view get_type_name() noexcept { +#if defined(__clang__) + constexpr auto prefix = std::string_view{"[T = "}; + constexpr auto suffix = "]"; + constexpr auto function = std::string_view{__PRETTY_FUNCTION__}; +#elif defined(__GNUC__) + constexpr auto prefix = std::string_view{"with T = "}; + constexpr auto suffix = "; "; + constexpr auto function = std::string_view{__PRETTY_FUNCTION__}; +#elif defined(_MSC_VER) + constexpr auto prefix = std::string_view{"get_type_name<"}; + constexpr auto suffix = ">(void)"; + constexpr auto function = std::string_view{__FUNCSIG__}; +#else +# error Unsupported compiler +#endif + + const auto start = function.find(prefix) + prefix.size(); + const auto end = function.find(suffix); + const auto size = end - start; + + return function.substr(start, size); +} +} // namespace snatch::impl + +namespace snatch { +template +constexpr std::string_view type_name = impl::get_type_name(); +} // namespace snatch diff --git a/tests/tests_common.hpp b/tests/tests_common.hpp index 1b0b85c..749c391 100644 --- a/tests/tests_common.hpp +++ b/tests/tests_common.hpp @@ -576,33 +576,32 @@ auto make_const_observer_from_this(const get_object* ptr) { } // clang-format off -using owner_types = std::tuple< - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, - oup::observable_unique_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::basic_observable_ptr, - oup::basic_observable_ptr, - oup::basic_observable_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, - oup::observable_sealed_ptr, - oup::observable_unique_ptr, +#define owner_types \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::basic_observable_ptr, \ + oup::basic_observable_ptr, \ + oup::basic_observable_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ + oup::observable_sealed_ptr, \ + oup::observable_unique_ptr, \ oup::observable_sealed_ptr - >; // clang-format on #define CHECK_INSTANCES(TEST_OBJECTS, TEST_DELETER) \ From fe60bdee204f150a1cd1c8f8c3a51b8816955cd5 Mon Sep 17 00:00:00 2001 From: Corentin Schreiber Date: Wed, 30 Nov 2022 21:59:42 +0000 Subject: [PATCH 2/3] Compile doctest library ahead of time --- tests/CMakeLists.txt | 6 ++++-- tests/testing.cpp | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) delete mode 100644 tests/testing.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 47db5ac..3dbc36d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,13 +31,15 @@ endfunction() include(FetchContent) +set(DOCTEST_WITH_MAIN_IN_STATIC_LIB ON) +set(DOCTEST_NO_INSTALL ON) + FetchContent_Declare(doctest GIT_REPOSITORY https://github.com/doctest/doctest.git GIT_TAG v2.4.9) FetchContent_MakeAvailable(doctest) set(RUNTIME_TEST_FILES - ${PROJECT_SOURCE_DIR}/tests/testing.cpp ${PROJECT_SOURCE_DIR}/tests/tests_common.cpp ${PROJECT_SOURCE_DIR}/tests/memory_tracker.cpp ${PROJECT_SOURCE_DIR}/tests/runtime_tests_owner_misc.cpp @@ -62,7 +64,7 @@ set(RUNTIME_TEST_FILES add_executable(oup_runtime_tests ${RUNTIME_TEST_FILES}) target_link_libraries(oup_runtime_tests PRIVATE oup::oup) -target_link_libraries(oup_runtime_tests PRIVATE doctest::doctest) +target_link_libraries(oup_runtime_tests PRIVATE doctest::doctest doctest::doctest_with_main) add_platform_definitions(oup_runtime_tests) add_custom_target(oup_runtime_tests_run diff --git a/tests/testing.cpp b/tests/testing.cpp deleted file mode 100644 index 2fbbd6c..0000000 --- a/tests/testing.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN -#include "testing.hpp" From 4e08b99c13f9be49e7bb187bdc328b1154bec252 Mon Sep 17 00:00:00 2001 From: Corentin Schreiber Date: Sun, 20 Aug 2023 19:01:06 +0100 Subject: [PATCH 3/3] Update doctest --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3dbc36d..8d410b7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -36,7 +36,7 @@ set(DOCTEST_NO_INSTALL ON) FetchContent_Declare(doctest GIT_REPOSITORY https://github.com/doctest/doctest.git - GIT_TAG v2.4.9) + GIT_TAG v2.4.11) FetchContent_MakeAvailable(doctest) set(RUNTIME_TEST_FILES