From a102da3794ed9b4cda26e2c018bc601d785a1c84 Mon Sep 17 00:00:00 2001 From: Rainer Kartmann <rainer.kartmann@kit.edu> Date: Fri, 30 Apr 2021 10:19:47 +0200 Subject: [PATCH] Add tests for MemoryItem and MemoryContainer copy/move ctors/ops; make them work --- .../core/base/detail/MemoryContainerBase.h | 15 +- .../armem/core/base/detail/MemoryItem.h | 6 +- .../libraries/armem/test/ArMemMemoryTest.cpp | 317 +++++++++++++++--- 3 files changed, 282 insertions(+), 56 deletions(-) diff --git a/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h b/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h index 9c4596a97..9a0804171 100644 --- a/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h +++ b/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h @@ -33,22 +33,10 @@ namespace armarx::armem::base::detail MemoryItem(id) { } - /* - MemoryContainerBase(const MemoryContainerBase& other) : - MemoryItem(other), - _container(other._container) - {} - - - MemoryContainerBase& operator=(const MemoryContainerBase& other) - { - other._copySelf(*this); - return *this; - } - */ // Container methods + virtual bool empty() const { return _container.empty(); @@ -90,6 +78,7 @@ namespace armarx::armem::base::detail // Copying + virtual DerivedT copy() const { DerivedT d; diff --git a/source/RobotAPI/libraries/armem/core/base/detail/MemoryItem.h b/source/RobotAPI/libraries/armem/core/base/detail/MemoryItem.h index 07e799a13..45226e632 100644 --- a/source/RobotAPI/libraries/armem/core/base/detail/MemoryItem.h +++ b/source/RobotAPI/libraries/armem/core/base/detail/MemoryItem.h @@ -17,7 +17,11 @@ namespace armarx::armem::base::detail MemoryItem(); MemoryItem(const MemoryID& id); - //MemoryItem(const MemoryItem& other); + + MemoryItem(const MemoryItem& other) = default; + MemoryItem(MemoryItem&& other) = default; + MemoryItem& operator=(const MemoryItem& other) = default; + MemoryItem& operator=(MemoryItem&& other) = default; virtual ~MemoryItem(); diff --git a/source/RobotAPI/libraries/armem/test/ArMemMemoryTest.cpp b/source/RobotAPI/libraries/armem/test/ArMemMemoryTest.cpp index a30132549..42c1238a3 100644 --- a/source/RobotAPI/libraries/armem/test/ArMemMemoryTest.cpp +++ b/source/RobotAPI/libraries/armem/test/ArMemMemoryTest.cpp @@ -32,13 +32,13 @@ #include <RobotAPI/libraries/armem/core/error.h> #include <iostream> +#include <SimoxUtility/meta/type_name.h> #include <RobotAPI/libraries/aron/core/navigator/data/container/Dict.h> namespace armem = armarx::armem; namespace aron = armarx::aron; - BOOST_AUTO_TEST_CASE(test_time_to_string) { // 111111: seconds, 345: milliseconds, 789: microseconds @@ -78,6 +78,150 @@ BOOST_AUTO_TEST_CASE(test_time_to_string) } + + +namespace ArMemMemoryTest +{ + struct TestMemoryItem : public armem::base::detail::MemoryItem + { + using MemoryItem::MemoryItem; + using MemoryItem::operator=; + + std::string getKeyString() const override + { + return ""; + } + std::string getLevelName() const override + { + return ""; + } + }; + struct MemoryItemCtorOpTestFixture + { + const armem::MemoryID id {"A/B/C/123/1"}; + const armem::MemoryID moved {"////1"}; // int is not moved + TestMemoryItem item { id }; + + MemoryItemCtorOpTestFixture() + { + BOOST_CHECK_EQUAL(item.id(), id); + } + }; +} + + +BOOST_FIXTURE_TEST_SUITE(MemoryItemTest, ArMemMemoryTest::MemoryItemCtorOpTestFixture) + + +BOOST_AUTO_TEST_CASE(test_copy_ctor) +{ + const ArMemMemoryTest::TestMemoryItem out(item); + BOOST_CHECK_EQUAL(item.id(), id); + BOOST_CHECK_EQUAL(out.id(), id); +} +BOOST_AUTO_TEST_CASE(test_copy_op) +{ + ArMemMemoryTest::TestMemoryItem out; + out = item; + BOOST_CHECK_EQUAL(item.id(), id); + BOOST_CHECK_EQUAL(out.id(), id); +} +BOOST_AUTO_TEST_CASE(test_move_ctor) +{ + ArMemMemoryTest::TestMemoryItem in = item; + const ArMemMemoryTest::TestMemoryItem out(std::move(in)); + BOOST_CHECK_EQUAL(in.id(), moved); + BOOST_CHECK_EQUAL(out.id(), id); +} +BOOST_AUTO_TEST_CASE(test_move_op) +{ + ArMemMemoryTest::TestMemoryItem in = item; + ArMemMemoryTest::TestMemoryItem out; + out = std::move(in); + BOOST_CHECK_EQUAL(in.id(), moved); + BOOST_CHECK_EQUAL(out.id(), id); +} + + +BOOST_AUTO_TEST_SUITE_END() + + + +namespace ArMemMemoryTest +{ + struct TestMemoryContainer : public armem::base::detail::MemoryContainerBase<std::vector<int>, TestMemoryContainer> + { + using MemoryContainerBase::MemoryContainerBase; + using MemoryContainerBase::operator=; + + std::string getKeyString() const override + { + return ""; + } + std::string getLevelName() const override + { + return ""; + } + }; + struct MemoryContainerCtorOpTestFixture + { + const armem::MemoryID id {"A/B/C/123/1"}; + const armem::MemoryID moved {"////1"}; // int is not moved + TestMemoryContainer cont {id}; + + MemoryContainerCtorOpTestFixture() + { + cont.container() = std::vector<int>{ -1, 2, -3 }; + BOOST_CHECK_EQUAL(cont.id(), id); + BOOST_CHECK_EQUAL(cont.size(), 3); + } + }; + +} + +BOOST_FIXTURE_TEST_SUITE(MemoryContainerTest, ArMemMemoryTest::MemoryContainerCtorOpTestFixture) + +BOOST_AUTO_TEST_CASE(test_copy_ctor) +{ + const ArMemMemoryTest::TestMemoryContainer out(cont); + BOOST_CHECK_EQUAL(cont.id(), id); + BOOST_CHECK_EQUAL(cont.size(), 3); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 3); +} +BOOST_AUTO_TEST_CASE(test_copy_op) +{ + ArMemMemoryTest::TestMemoryContainer out; + out = cont; + BOOST_CHECK_EQUAL(cont.id(), id); + BOOST_CHECK_EQUAL(cont.size(), 3); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 3); +} +BOOST_AUTO_TEST_CASE(test_move_ctor) +{ + ArMemMemoryTest::TestMemoryContainer in = cont; + const ArMemMemoryTest::TestMemoryContainer out(std::move(in)); + BOOST_CHECK_EQUAL(in.id(), moved); + BOOST_CHECK_EQUAL(in.size(), 0); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 3); +} +BOOST_AUTO_TEST_CASE(test_move_op) +{ + ArMemMemoryTest::TestMemoryContainer in = cont; + ArMemMemoryTest::TestMemoryContainer out; + out = std::move(in); + BOOST_CHECK_EQUAL(in.id(), moved); + BOOST_CHECK_EQUAL(in.size(), 0); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 3); +} + +BOOST_AUTO_TEST_SUITE_END() + + + BOOST_AUTO_TEST_CASE(test_key_ctors) { armem::wm::EntityInstance instance(10); @@ -100,6 +244,136 @@ BOOST_AUTO_TEST_CASE(test_key_ctors) } +template <class T> +void test_the_copy_move_ctors_ops_instance() +{ + const armem::MemoryID id("A/B/C/123000"); // int index would not be moved + const armem::MemoryID idMoved; + + BOOST_TEST_CONTEXT("Type " << simox::meta::get_type_name<T>()) + { + T in(id); + BOOST_CHECK_EQUAL(in.id(), id); + BOOST_TEST_CONTEXT("copy ctor") + { + T out { in }; + BOOST_CHECK_EQUAL(out.id(), id); + } + BOOST_TEST_CONTEXT("copy op") + { + T out; + out = in; + BOOST_CHECK_EQUAL(out.id(), id); + } + in = T{id}; + BOOST_TEST_CONTEXT("move ctor") + { + T out { std::move(in) }; + BOOST_CHECK_EQUAL(in.id(), idMoved); + BOOST_CHECK_EQUAL(out.id(), id); + } + in = T{id}; + BOOST_TEST_CONTEXT("move op") + { + T out; + out = std::move(in); + BOOST_CHECK_EQUAL(in.id(), idMoved); + BOOST_CHECK_EQUAL(out.id(), id); + } + } +} + + +template <class ...Args> +auto add_element(std::vector<Args...>& vector) +{ + return vector.emplace_back(); +} +template <class ...Args> +auto add_element(std::map<Args...>& map) +{ + return map.emplace(); +} + +template <class T> +void test_the_copy_move_ctors_ops_container() +{ + const armem::MemoryID id("A/B/C/123000"); // int index would not be moved + const armem::MemoryID idMoved; + + BOOST_TEST_CONTEXT("Type " << simox::meta::get_type_name<T>()) + { + T in(id); + add_element(in.container()); + BOOST_CHECK_EQUAL(in.id(), id); + BOOST_CHECK_EQUAL(in.size(), 1); + + BOOST_TEST_CONTEXT("copy ctor") + { + T out { in }; + BOOST_CHECK_EQUAL(in.id(), id); + BOOST_CHECK_EQUAL(in.size(), 1); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 1); + } + BOOST_TEST_CONTEXT("copy op") + { + T out; + out = in; + BOOST_CHECK_EQUAL(in.id(), id); + BOOST_CHECK_EQUAL(in.size(), 1); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 1); + } + in = T{id}; + BOOST_TEST_CONTEXT("move ctor") + { + T out { std::move(in) }; + BOOST_CHECK_EQUAL(in.id(), idMoved); + BOOST_CHECK(in.empty()); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 1); + } + in = T{id}; + BOOST_TEST_CONTEXT("move op") + { + T out; + out = std::move(in); + BOOST_CHECK_EQUAL(in.id(), idMoved); + BOOST_CHECK(in.empty()); + BOOST_CHECK_EQUAL(out.id(), id); + BOOST_CHECK_EQUAL(out.size(), 1); + } + } +} + + +BOOST_AUTO_TEST_CASE(test_copy_move_ctors_ops) +{ + test_the_copy_move_ctors_ops_instance<armem::wm::EntityInstance>(); + test_the_copy_move_ctors_ops_container<armem::wm::EntitySnapshot>(); + test_the_copy_move_ctors_ops_container<armem::wm::Entity>(); + test_the_copy_move_ctors_ops_container<armem::wm::ProviderSegment>(); + test_the_copy_move_ctors_ops_container<armem::wm::CoreSegment>(); + test_the_copy_move_ctors_ops_container<armem::wm::Memory>(); + + test_the_copy_move_ctors_ops_instance<armem::ltm::EntityInstance>(); + test_the_copy_move_ctors_ops_container<armem::ltm::EntitySnapshot>(); + test_the_copy_move_ctors_ops_container<armem::ltm::Entity>(); + test_the_copy_move_ctors_ops_container<armem::ltm::ProviderSegment>(); + test_the_copy_move_ctors_ops_container<armem::ltm::CoreSegment>(); + test_the_copy_move_ctors_ops_container<armem::ltm::Memory>(); + + test_the_copy_move_ctors_ops_instance<armem::d_ltm::EntityInstance>(); + test_the_copy_move_ctors_ops_container<armem::d_ltm::EntitySnapshot>(); + test_the_copy_move_ctors_ops_container<armem::d_ltm::Entity>(); + test_the_copy_move_ctors_ops_container<armem::d_ltm::ProviderSegment>(); + test_the_copy_move_ctors_ops_container<armem::d_ltm::CoreSegment>(); + test_the_copy_move_ctors_ops_container<armem::d_ltm::Memory>(); +} + + + BOOST_AUTO_TEST_CASE(test_segment_setup) { armem::EntityUpdate update; @@ -290,44 +564,3 @@ BOOST_AUTO_TEST_CASE(test_history_size_in_provider_segment) } } - -template <class T> -void test_the_copy_move_ctors_ops() -{ - T t1; - { - T t2 { t1 }; - t2 = t1; - } - { - T t2 { std::move(t1) }; - t2 = std::move(t1); - } -} - - -BOOST_AUTO_TEST_CASE(test_copy_move_ctors_ops) -{ - test_the_copy_move_ctors_ops<armem::wm::EntityInstance>(); - test_the_copy_move_ctors_ops<armem::wm::EntitySnapshot>(); - test_the_copy_move_ctors_ops<armem::wm::Entity>(); - test_the_copy_move_ctors_ops<armem::wm::ProviderSegment>(); - test_the_copy_move_ctors_ops<armem::wm::CoreSegment>(); - test_the_copy_move_ctors_ops<armem::wm::Memory>(); - - test_the_copy_move_ctors_ops<armem::ltm::EntityInstance>(); - test_the_copy_move_ctors_ops<armem::ltm::EntitySnapshot>(); - test_the_copy_move_ctors_ops<armem::ltm::Entity>(); - test_the_copy_move_ctors_ops<armem::ltm::ProviderSegment>(); - test_the_copy_move_ctors_ops<armem::ltm::CoreSegment>(); - test_the_copy_move_ctors_ops<armem::ltm::Memory>(); - - test_the_copy_move_ctors_ops<armem::d_ltm::EntityInstance>(); - test_the_copy_move_ctors_ops<armem::d_ltm::EntitySnapshot>(); - test_the_copy_move_ctors_ops<armem::d_ltm::Entity>(); - test_the_copy_move_ctors_ops<armem::d_ltm::ProviderSegment>(); - test_the_copy_move_ctors_ops<armem::d_ltm::CoreSegment>(); - test_the_copy_move_ctors_ops<armem::d_ltm::Memory>(); - - BOOST_CHECK(true); -} -- GitLab