diff --git a/include/loki/common/pddl/garbage_collected_factory.hpp b/include/loki/common/pddl/garbage_collected_factory.hpp index d319101d..5aca59ed 100644 --- a/include/loki/common/pddl/garbage_collected_factory.hpp +++ b/include/loki/common/pddl/garbage_collected_factory.hpp @@ -18,6 +18,7 @@ #ifndef LOKI_INCLUDE_LOKI_COMMON_PDDL_GARBAGE_COLLECTED_FACTORY_HPP_ #define LOKI_INCLUDE_LOKI_COMMON_PDDL_GARBAGE_COLLECTED_FACTORY_HPP_ +#include #include #include #include diff --git a/tests/common/pddl/garbage_collected_factory.cpp b/tests/common/pddl/garbage_collected_factory.cpp index 6b02b93a..323c18a8 100644 --- a/tests/common/pddl/garbage_collected_factory.cpp +++ b/tests/common/pddl/garbage_collected_factory.cpp @@ -16,6 +16,7 @@ */ #include "../../../include/loki/common/pddl/garbage_collected_factory.hpp" +#include "../../../include/loki/common/hash.hpp" #include "../../../include/loki/domain/pddl/object.hpp" #include @@ -24,33 +25,67 @@ namespace loki::domain::tests { -/* -class Object { + +/// @brief A type to avoid friending in the pddl::Object since this class is not used. +class ObjectImpl { private: int m_identifier; std::string m_name; - Object(int identifier, std::string name) : m_identifier(identifier), m_name(std::move(name)) { } + ObjectImpl(int identifier, std::string name) : m_identifier(identifier), m_name(std::move(name)) { } template - friend class GarbageCollectedFactory; + friend class loki::GarbageCollectedFactory; public: + bool operator==(const ObjectImpl& other) const { + return m_name == other.m_name; + } + + size_t hash() const { + return hash_combine(m_name); + } + int get_identifier() const { return m_identifier; } const std::string& get_name() const { return m_name; } }; +using Object = std::shared_ptr; + +} + + +namespace std { + template<> + struct hash { + size_t operator()(const loki::domain::tests::ObjectImpl& object) const { + return object.hash(); + } + }; +} + + +namespace loki::domain::tests { + TEST(LokiTests, GarbageCollectedFactoryTest) { - GarbageCollectedFactory factory; - EXPECT_EQ(factory.size(), 0); + GarbageCollectedFactory factory; + EXPECT_EQ(factory.size(), 0); { - const auto object_0 = factory.get_or_create("object_0"); - EXPECT_EQ(factory.size(), 1); - EXPECT_EQ(object_0->get_name(), "object_0"); - // destructor is called + const auto object_0_0 = factory.get_or_create("object_0"); + EXPECT_EQ(factory.size(), 1); + EXPECT_EQ(object_0_0->get_name(), "object_0"); + + const auto object_0_1 = factory.get_or_create("object_0"); + EXPECT_EQ(factory.size(), 1); + EXPECT_EQ(object_0_0, object_0_1); + + const auto object_1 = factory.get_or_create("object_1"); + EXPECT_EQ(factory.size(), 2); + + // destructors are called } - EXPECT_EQ(factory.size(), 0); + EXPECT_EQ(factory.size(), 0); } -*/ + } diff --git a/tests/common/pddl/persistent_factory.cpp b/tests/common/pddl/persistent_factory.cpp index 17da35d7..1e170160 100644 --- a/tests/common/pddl/persistent_factory.cpp +++ b/tests/common/pddl/persistent_factory.cpp @@ -32,10 +32,15 @@ TEST(LokiTests, PersistentFactoryTest) { EXPECT_EQ(factory.size(), 1); EXPECT_EQ(object_0_0->get_identifier(), 0); EXPECT_EQ(object_0_0->get_name(), "object_0"); + const auto object_0_1 = factory.get_or_create("object_0"); EXPECT_EQ(factory.size(), 1); EXPECT_EQ(object_0_0, object_0_1); + const auto object_1 = factory.get_or_create("object_1"); + EXPECT_EQ(factory.size(), 2); + EXPECT_NE(object_0_0, object_1); + } }