diff --git a/source/RobotAPI/libraries/armem/core/MemoryID.cpp b/source/RobotAPI/libraries/armem/core/MemoryID.cpp index 161b20f8c236c806061d4f4423b52711899a566b..61dd4836ca9f16ffd6b36342facbd2f610c75ff3 100644 --- a/source/RobotAPI/libraries/armem/core/MemoryID.cpp +++ b/source/RobotAPI/libraries/armem/core/MemoryID.cpp @@ -111,18 +111,16 @@ namespace armarx::armem bool MemoryID::hasGap() const { bool emptyFound = false; - for (const std::string& name : getAllItems()) + for (const std::string& item : getAllItems()) { - if (name.empty()) + if (item.empty()) { emptyFound = true; } - else + else if (emptyFound) { - if (emptyFound) - { - return true; - } + // Found a non-empty item after an empty item. + return true; } } return false; @@ -417,6 +415,19 @@ namespace armarx::armem bool contains(const MemoryID& general, const MemoryID& specific) { + if (!general.isWellDefined()) + { + std::stringstream ss; + ss << "ID `general` is not well-defined, which is required for `" << __FUNCTION__ << "()`."; + throw error::InvalidMemoryID(general, ss.str()); + } + if (!specific.isWellDefined()) + { + std::stringstream ss; + ss << "ID `specific` is not well-defined, which is required for `" << __FUNCTION__ << "()`."; + throw error::InvalidMemoryID(specific, ss.str()); + } + if (general.memoryName.empty()) { return true; diff --git a/source/RobotAPI/libraries/armem/test/ArMemMemoryIDTest.cpp b/source/RobotAPI/libraries/armem/test/ArMemMemoryIDTest.cpp index 79dada2701b24589eb9ee8169f731e05cee1461a..1f94d9adb9a2d023a3813c0e09edf8b41367ace1 100644 --- a/source/RobotAPI/libraries/armem/test/ArMemMemoryIDTest.cpp +++ b/source/RobotAPI/libraries/armem/test/ArMemMemoryIDTest.cpp @@ -26,6 +26,7 @@ #include <RobotAPI/Test.h> #include "../core/MemoryID.h" +#include "../core/error.h" #include <iostream> @@ -80,13 +81,13 @@ BOOST_AUTO_TEST_CASE(test_MemoryID_contains) BOOST_CHECK(not armem::contains(specific, general)); } - // with a gap - lower levels are ignored + // Not well-defined ID - throw an exception. specific.coreSegmentName.clear(); BOOST_TEST_CONTEXT(VAROUT(general) << " | " << VAROUT(specific)) { - BOOST_CHECK(armem::contains(general, specific)); - BOOST_CHECK(armem::contains(specific, general)); + BOOST_CHECK_THROW(armem::contains(general, specific), armem::error::InvalidMemoryID); + BOOST_CHECK_THROW(armem::contains(specific, general), armem::error::InvalidMemoryID); } }