From 25e893e3651889aa74fa1aafb5c1ed0899890692 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 1 Oct 2026 23:28:28 +0200 Subject: kstd: tests: extract static tracker types --- libs/kstd/kstd/ring_buffer.tests.cpp | 170 ++++++++++------------------- libs/kstd/kstd/test_support/test_types.hpp | 51 +++++++++ 2 files changed, 109 insertions(+), 112 deletions(-) diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp index 95a235e6..8cd50e2f 100644 --- a/libs/kstd/kstd/ring_buffer.tests.cpp +++ b/libs/kstd/kstd/ring_buffer.tests.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -15,61 +16,6 @@ #include #include -namespace -{ - struct static_dtor_tracker - { - auto static inline dtor_call_count = 0uz; - - ~static_dtor_tracker() - { - ++dtor_call_count; - } - }; - - struct static_copy_move_tracker : static_dtor_tracker - { - auto static inline copy_ctor_call_count = 0uz; - auto static inline copy_assignment_call_count = 0uz; - auto static inline move_ctor_call_count = 0uz; - auto static inline move_assignment_call_count = 0uz; - - constexpr auto static reset() -> void - { - dtor_call_count = 0; - copy_ctor_call_count = 0; - copy_assignment_call_count = 0; - move_ctor_call_count = 0; - move_assignment_call_count = 0; - } - - constexpr static_copy_move_tracker() = default; - - constexpr static_copy_move_tracker(static_copy_move_tracker const &) - { - ++copy_ctor_call_count; - } - - constexpr static_copy_move_tracker(static_copy_move_tracker &&) - { - ++move_ctor_call_count; - } - - constexpr auto operator=(static_copy_move_tracker const &) -> static_copy_move_tracker & - { - ++copy_assignment_call_count; - return *this; - } - - constexpr auto operator=(static_copy_move_tracker &&) -> static_copy_move_tracker & - { - ++move_assignment_call_count; - return *this; - } - }; - -} // namespace - SCENARIO("Ring Buffer interface types", "[kstd][ring_buffer]") { GIVEN("A ring buffer of float") @@ -393,58 +339,58 @@ SCENARIO("Ring Buffer assignment", "[kstd][ring_buffer]") { GIVEN("3 partially populated buffers") { - auto small = kstd::ring_buffer{2}; - auto same = kstd::ring_buffer{2}; - auto large = kstd::ring_buffer{3}; + auto small = kstd::ring_buffer{2}; + auto same = kstd::ring_buffer{2}; + auto large = kstd::ring_buffer{3}; WHEN("copy assigning the small to the large one") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); large = small; THEN("2 copy assignments and 1 destruction occurs") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 1); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 2); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 1); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 2); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 0); } } WHEN("copy assigning the large to the small one") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); small = large; THEN("2 copy assignments and 1 copy construction occurs") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 1); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 2); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 1); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 2); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 0); } } WHEN("copy assigning the small to the same size one") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); same = small; THEN("2 copy assignments occur") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 2); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 2); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 0); } } WHEN("copy assigning a buffer to itself") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); #if defined(__clang__) #pragma clang diagnostic push @@ -457,62 +403,62 @@ SCENARIO("Ring Buffer assignment", "[kstd][ring_buffer]") THEN("no operations occur") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 0); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 0); } } WHEN("move assigning the small to the large one") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); large = std::move(small); THEN("2 move assignments and 1 destruction occurs") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 1); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 0); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 2); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 1); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 2); } } WHEN("move assigning the large to the small one") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); small = std::move(large); THEN("2 move assignments and 1 move construction occurs") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 0); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 1); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 2); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 1); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 2); } } WHEN("move assigning the small to the same size one") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); same = std::move(small); THEN("2 move assignments occur") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 0); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 2); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 2); } } WHEN("move assigning a buffer to itself") { - static_copy_move_tracker::reset(); + kstd::tests::static_copy_move_tracker::reset(); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wself-move" same = std::move(same); @@ -520,11 +466,11 @@ SCENARIO("Ring Buffer assignment", "[kstd][ring_buffer]") THEN("no operations occur") { - REQUIRE(static_copy_move_tracker::dtor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::copy_assignment_call_count == 0); - REQUIRE(static_copy_move_tracker::move_ctor_call_count == 0); - REQUIRE(static_copy_move_tracker::move_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::copy_assignment_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_ctor_call_count == 0); + REQUIRE(kstd::tests::static_copy_move_tracker::move_assignment_call_count == 0); } } } @@ -534,8 +480,8 @@ SCENARIO("Ring Buffer destruction", "[kstd][ring_buffer]") { GIVEN("An empty buffer") { - static_dtor_tracker::dtor_call_count = 0; - auto buffer = std::make_shared>(); + kstd::tests::static_dtor_tracker::dtor_call_count = 0; + auto buffer = std::make_shared>(); WHEN("the buffer is destroyed") { @@ -543,15 +489,15 @@ SCENARIO("Ring Buffer destruction", "[kstd][ring_buffer]") THEN("no dtors are invoked") { - REQUIRE(static_dtor_tracker::dtor_call_count == 0); + REQUIRE(kstd::tests::static_dtor_tracker::dtor_call_count == 0); } } } GIVEN("A buffer containing 3 elements") { - static_dtor_tracker::dtor_call_count = 0; - auto buffer = std::make_shared>(3); + kstd::tests::static_dtor_tracker::dtor_call_count = 0; + auto buffer = std::make_shared>(3); WHEN("the buffer is destroyed") { @@ -559,7 +505,7 @@ SCENARIO("Ring Buffer destruction", "[kstd][ring_buffer]") THEN("3 dtors are invoked") { - REQUIRE(static_dtor_tracker::dtor_call_count == 3); + REQUIRE(kstd::tests::static_dtor_tracker::dtor_call_count == 3); } } } diff --git a/libs/kstd/kstd/test_support/test_types.hpp b/libs/kstd/kstd/test_support/test_types.hpp index baf5e853..2ee8e0fc 100644 --- a/libs/kstd/kstd/test_support/test_types.hpp +++ b/libs/kstd/kstd/test_support/test_types.hpp @@ -8,6 +8,57 @@ namespace kstd::tests { + struct static_dtor_tracker + { + auto static inline dtor_call_count = 0uz; + + ~static_dtor_tracker() + { + ++dtor_call_count; + } + }; + + struct static_copy_move_tracker : static_dtor_tracker + { + auto static inline copy_ctor_call_count = 0uz; + auto static inline copy_assignment_call_count = 0uz; + auto static inline move_ctor_call_count = 0uz; + auto static inline move_assignment_call_count = 0uz; + + constexpr auto static reset() -> void + { + dtor_call_count = 0; + copy_ctor_call_count = 0; + copy_assignment_call_count = 0; + move_ctor_call_count = 0; + move_assignment_call_count = 0; + } + + constexpr static_copy_move_tracker() = default; + + constexpr static_copy_move_tracker(static_copy_move_tracker const &) + { + ++copy_ctor_call_count; + } + + constexpr static_copy_move_tracker(static_copy_move_tracker &&) + { + ++move_ctor_call_count; + } + + constexpr auto operator=(static_copy_move_tracker const &) -> static_copy_move_tracker & + { + ++copy_assignment_call_count; + return *this; + } + + constexpr auto operator=(static_copy_move_tracker &&) -> static_copy_move_tracker & + { + ++move_assignment_call_count; + return *this; + } + }; + //! A type tracking copy and move operations //! //! This type is designed to test move and copy semantics of standard library containers implemented in kstd. -- cgit v1.2.3