Skip to content
Snippets Groups Projects
Commit b0119c42 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add ArmemMemoryTest (unit test), add history size in Entity

parent 8740ab11
No related branches found
No related tags found
1 merge request!89Implement core data structure of ArMem Memories
......@@ -47,3 +47,6 @@ set(LIB_HEADERS
)
armarx_add_library("${LIB_NAME}" "${LIB_FILES}" "${LIB_HEADERS}" "${LIBS}")
# add unit tests
add_subdirectory(test)
#include "Entity.h"
#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
namespace armarx::armem
{
......@@ -25,9 +27,29 @@ namespace armarx::armem
// Insert into history.
it = history.emplace(update.timeCreated,
std::make_unique<EntitySnapshot>()).first;
truncateHistoryToSize();
}
// Update entry.
it->second->update(update);
}
void Entity::setHistorySize(long maxSize)
{
this->historySize = maxSize;
truncateHistoryToSize();
}
void Entity::truncateHistoryToSize()
{
if (historySize >= 0)
{
while (history.size() > size_t(historySize))
{
history.erase(history.begin());
}
ARMARX_CHECK_LESS_EQUAL(historySize, history.size());
}
}
}
......@@ -28,12 +28,32 @@ namespace armarx::armem
void update(const InternalEntityUpdate& update);
/**
* @brief Sets the maximum history size.
*
* The current history is truncated if necessary.
*/
void setHistorySize(long maxSize);
private:
/// If maximum size is set, ensure `history`'s is not higher.
void truncateHistoryToSize();
public:
std::string name;
std::map<Time, EntitySnapshotPtr> history;
/**
* @brief Maximum size of `history`
*
* If negative, the size of `history` is not limited.
*/
long historySize = -1;
};
using EntityPtr = std::unique_ptr<Entity>;
......
/*
* This file is part of ArmarX.
*
* ArmarX is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* ArmarX is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package RobotAPI::ArmarXObjects::armem
* @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
* @date 2020
* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
* GNU General Public License
*/
#define BOOST_TEST_MODULE RobotAPI::ArmarXLibraries::armem
#define ARMARX_BOOST_TEST
#include <RobotAPI/Test.h>
#include "../memory/Memory.h"
#include "../error/ArmemError.h"
#include <iostream>
namespace armem = armarx::armem;
namespace aron = armarx::aron;
BOOST_AUTO_TEST_CASE(test_segment_setup)
{
armem::InternalEntityUpdate update;
armem::MemoryPtr memory = std::make_unique<armem::Memory>("Memory");
BOOST_CHECK_EQUAL(memory->name, "Memory");
{
update.entityID = armem::MemoryID::fromString("OtherMemory/SomeSegment");
BOOST_CHECK_THROW(memory->update(update), armem::error::StorageNameMismatch);
update.entityID = armem::MemoryID::fromString("Memory/MissingSegment");
BOOST_CHECK_THROW(memory->update(update), armem::error::MissingEntry);
}
armem::CoreSegment* coreSegment = memory->addCoreSegment("ImageRGB");
BOOST_CHECK_EQUAL(coreSegment->name, "ImageRGB");
BOOST_CHECK(memory->hasCoreSegment(coreSegment->name));
{
update.entityID = armem::MemoryID::fromString("Memory/OtherCoreSegment");
BOOST_CHECK_THROW(coreSegment->update(update), armem::error::StorageNameMismatch);
update.entityID = armem::MemoryID::fromString("Memory/ImageRGB/MissingProvider");
BOOST_CHECK_THROW(coreSegment->update(update), armem::error::MissingEntry);
}
armem::ProviderSegment* providerSegment = coreSegment->addProviderSegment("SomeRGBImageProvider");
BOOST_CHECK_EQUAL(providerSegment->name, "SomeRGBImageProvider");
BOOST_CHECK(coreSegment->hasProviderSegment(providerSegment->name));
{
update.entityID = armem::MemoryID::fromString("Memory/ImageRGB/OtherRGBImageProvider");
BOOST_CHECK_THROW(providerSegment->update(update), armem::error::StorageNameMismatch);
}
// A successful update.
update.entityID = armem::MemoryID::fromString("Memory/ImageRGB/SomeRGBImageProvider/image");
update.instancesData = { new aron::AronData(), new aron::AronData() };
update.timeCreated = armem::Time::milliSeconds(1000);
BOOST_CHECK_NO_THROW(providerSegment->update(update));
BOOST_CHECK_EQUAL(providerSegment->entities.size(), 1);
BOOST_CHECK(providerSegment->hasEntity("image"));
BOOST_CHECK(!providerSegment->hasEntity("other_image"));
armem::Entity* entity = providerSegment->entities.at("image").get();
BOOST_CHECK_EQUAL(entity->name, "image");
BOOST_CHECK_EQUAL(entity->history.size(), 1);
BOOST_CHECK_EQUAL(entity->history.count(update.timeCreated), 1);
armem::EntitySnapshot* entitySnapshot = entity->history.at(update.timeCreated).get();
BOOST_CHECK_EQUAL(entitySnapshot->instances.size(), update.instancesData.size());
// Another update (on memory).
update.instancesData = { new aron::AronData() };
update.timeCreated = armem::Time::milliSeconds(2000);
memory->update(update);
BOOST_CHECK_EQUAL(entity->history.size(), 2);
BOOST_CHECK_EQUAL(entity->history.count(update.timeCreated), 1);
BOOST_CHECK_EQUAL(entity->history.at(update.timeCreated)->instances.size(), update.instancesData.size());
// A third update (on entity).
update.instancesData = { new aron::AronData() };
update.timeCreated = armem::Time::milliSeconds(3000);
entity->update(update);
BOOST_CHECK_EQUAL(entity->history.size(), 3);
// Now with maximum history size.
entity->setHistorySize(2);
BOOST_CHECK_EQUAL(entity->history.size(), 2);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(1000)), 0);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(2000)), 1);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(3000)), 1);
update.timeCreated = armem::Time::milliSeconds(4000);
coreSegment->update(update);
BOOST_CHECK_EQUAL(entity->history.size(), 2);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(2000)), 0);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(3000)), 1);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(4000)), 1);
// Disable maximum history size.
entity->setHistorySize(-1);
update.timeCreated = armem::Time::milliSeconds(5000);
entity->update(update);
BOOST_CHECK_EQUAL(entity->history.size(), 3);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(3000)), 1);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(4000)), 1);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(5000)), 1);
}
# Libs required for the tests
SET(LIBS ${LIBS} ArmarXCore armem)
armarx_add_test(armemTest armemTest.cpp "${LIBS}")
\ No newline at end of file
SET(LIBS ${LIBS} ArmarXCore ${LIB_NAME})
# armarx_add_test(armemTest armemTest.cpp "${LIBS}")
armarx_add_test(ArmemMemoryTest ArmemMemoryTest.cpp "${LIBS}")
......@@ -24,13 +24,12 @@
#define ARMARX_BOOST_TEST
/ Test.h >
#include "../armem.h"
#include <RobotAPI/Test.h>
// #include "../armem.h"
#include <iostream>
BOOST_AUTO_TEST_CASE(testExample)
{
BOOST_CHECK_EQUAL(true, true);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment