From 620b312a701a872a67ad074d551bc2b4cfee09c8 Mon Sep 17 00:00:00 2001 From: neeraj31285 Date: Thu, 4 Sep 2025 12:20:39 +0530 Subject: [PATCH] updated move-constructor tests. --- .../MoveConstructorTests.cpp | 18 ++++++++++++++++++ .../src/TestMirrorProvider.cpp | 4 ++-- CxxTestProps/inc/Date.h | 4 ++++ CxxTestProps/src/Date.cpp | 12 ++++++++++++ CxxTestUtils/inc/TestUtilsDate.h | 2 ++ CxxTestUtils/src/TestUtilsDate.cpp | 10 ++++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) diff --git a/CxxRTLTestApplication/src/FunctionalityTests/MoveConstructorTests.cpp b/CxxRTLTestApplication/src/FunctionalityTests/MoveConstructorTests.cpp index 3b6a703a..96b48921 100644 --- a/CxxRTLTestApplication/src/FunctionalityTests/MoveConstructorTests.cpp +++ b/CxxRTLTestApplication/src/FunctionalityTests/MoveConstructorTests.cpp @@ -33,8 +33,14 @@ namespace rtl_tests // 'Event' has a unique_ptr and two 'Event' instances exists, So- EXPECT_TRUE(date::get_instance_count() == 2); + // Sets Calender's move operation counter to zero + calender::reset_move_ops_counter(); + // Moving a RObject created via alloc::Stack, invokes Calender's move constructor. RObject calender1 = std::move(calender0); + + // Calender's move-constructor called once. + EXPECT_TRUE(calender::get_move_ops_count() == 1); ASSERT_FALSE(calender1.isEmpty()); EXPECT_TRUE(calender1.isConstCastSafe()); @@ -91,10 +97,16 @@ namespace rtl_tests // 'Event' has a unique_ptr and two 'Event' instances exists, So- EXPECT_TRUE(date::get_instance_count() == 2); + // Sets Calender's move operation counter to zero + calender::reset_move_ops_counter(); + // RObject created via alloc::HEAP, contains pointer to reflected type internally, So just the // address wrapped in std::any inside Robject is moved. Calender's move constructor is not called. RObject calender1 = std::move(calender0); + // Calender's move constructor isn't called. + EXPECT_TRUE(calender::get_move_ops_count() == 0); + ASSERT_FALSE(calender1.isEmpty()); EXPECT_TRUE(calender1.isConstCastSafe()); EXPECT_TRUE(calender1.isOnHeap()); @@ -228,9 +240,15 @@ namespace rtl_tests // 'Event' has a unique_ptr and two 'Event' instances exists, So- EXPECT_TRUE(date::get_instance_count() == 2); + // Sets Calender's move operation counter to zero + calender::reset_move_ops_counter(); + // Moving a RObject created via alloc::Stack, invokes Calender's move constructor. RObject calender1 = std::move(calender0); + // Calender's move-constructor called once. + EXPECT_TRUE(calender::get_move_ops_count() == 1); + ASSERT_FALSE(calender1.isEmpty()); EXPECT_TRUE(calender1.isConstCastSafe()); EXPECT_FALSE(calender1.isOnHeap()); diff --git a/CxxRTLTypeRegistration/src/TestMirrorProvider.cpp b/CxxRTLTypeRegistration/src/TestMirrorProvider.cpp index ec3b013a..980c4456 100644 --- a/CxxRTLTypeRegistration/src/TestMirrorProvider.cpp +++ b/CxxRTLTypeRegistration/src/TestMirrorProvider.cpp @@ -242,8 +242,8 @@ namespace test_mirror static const auto _ = [&]() { const std::string pathStr = std::filesystem::current_path().string() + "/MyReflection.json"; - std::cout << "\n[ OUTPUT] test_mirror::cxx::mirror()==> dumping metadata as JSON." - << "\n file path: " << pathStr << std::endl; + std::cout << "\n[ OUTPUT] test_mirror::cxx::mirror() ==> dumping 'CxxMirror' as JSON." + << "\n file path: " << pathStr << "\n" << std::endl; rtl::CxxMirrorToJson::dump(cxx_mirror, pathStr); return 0; }(); diff --git a/CxxTestProps/inc/Date.h b/CxxTestProps/inc/Date.h index 855ba891..343bc927 100644 --- a/CxxTestProps/inc/Date.h +++ b/CxxTestProps/inc/Date.h @@ -54,7 +54,9 @@ namespace nsdate const Event& getTheEvent(); const Event& getSavedEvent(); + static void resetMoveOpsCounter(); static std::size_t instanceCount(); + static std::size_t getMoveOpsCount(); static Calender create(); @@ -65,6 +67,8 @@ namespace nsdate std::unique_ptr m_savedEvent; static std::size_t m_instanceCount; + + static std::size_t m_moveOpsCount; }; diff --git a/CxxTestProps/src/Date.cpp b/CxxTestProps/src/Date.cpp index 3a194d4a..cc96d73d 100644 --- a/CxxTestProps/src/Date.cpp +++ b/CxxTestProps/src/Date.cpp @@ -10,6 +10,7 @@ namespace nsdate std::size_t Date::m_instanceCount = 0; std::size_t Event::m_instanceCount = 0; std::size_t Calender::m_instanceCount = 0; + std::size_t Calender::m_moveOpsCount = 0; Calender::~Calender() { @@ -34,6 +35,7 @@ namespace nsdate : m_theEvent(std::move(pOther.m_theEvent)) , m_savedEvent(std::move(pOther.m_savedEvent)) { + m_moveOpsCount++; m_instanceCount++; } @@ -66,6 +68,16 @@ namespace nsdate { return m_instanceCount; } + + std::size_t Calender::getMoveOpsCount() + { + return m_moveOpsCount; + } + + void Calender::resetMoveOpsCounter() + { + m_moveOpsCount = 0; + } } namespace nsdate diff --git a/CxxTestUtils/inc/TestUtilsDate.h b/CxxTestUtils/inc/TestUtilsDate.h index e0477e7b..8281dc5b 100644 --- a/CxxTestUtils/inc/TestUtilsDate.h +++ b/CxxTestUtils/inc/TestUtilsDate.h @@ -34,8 +34,10 @@ namespace test_utils static constexpr const char* str_getTheEvent = "getTheEvent"; static constexpr const char* str_getSavedEvent = "getSavedEvent"; + static void reset_move_ops_counter(); static const bool assert_zero_instance_count(); static const std::size_t get_instance_count(); + static const std::size_t get_move_ops_count(); }; struct date diff --git a/CxxTestUtils/src/TestUtilsDate.cpp b/CxxTestUtils/src/TestUtilsDate.cpp index d5cb3699..4787615b 100644 --- a/CxxTestUtils/src/TestUtilsDate.cpp +++ b/CxxTestUtils/src/TestUtilsDate.cpp @@ -20,6 +20,16 @@ namespace test_utils return Calender::instanceCount(); } + void calender::reset_move_ops_counter() + { + Calender::resetMoveOpsCounter(); + } + + const std::size_t calender::get_move_ops_count() + { + return Calender::getMoveOpsCount(); + } + const bool event::assert_zero_instance_count() { return (Event::instanceCount() == 0);