Skip to content

Commit

Permalink
added factory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Jan 3, 2024
1 parent 392f02b commit 932d797
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/loki/common/pddl/garbage_collected_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cassert>
#include <unordered_map>
#include <unordered_set>
#include <memory>
Expand Down
59 changes: 47 additions & 12 deletions tests/common/pddl/garbage_collected_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gtest/gtest.h>
Expand All @@ -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<typename... Ts>
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<ObjectImpl>;

}


namespace std {
template<>
struct hash<loki::domain::tests::ObjectImpl> {
size_t operator()(const loki::domain::tests::ObjectImpl& object) const {
return object.hash();
}
};
}


namespace loki::domain::tests {

TEST(LokiTests, GarbageCollectedFactoryTest) {
GarbageCollectedFactory<Object> factory;
EXPECT_EQ(factory.size<Object>(), 0);
GarbageCollectedFactory<ObjectImpl> factory;
EXPECT_EQ(factory.size<ObjectImpl>(), 0);

{
const auto object_0 = factory.get_or_create<Object>("object_0");
EXPECT_EQ(factory.size<Object>(), 1);
EXPECT_EQ(object_0->get_name(), "object_0");
// destructor is called
const auto object_0_0 = factory.get_or_create<ObjectImpl>("object_0");
EXPECT_EQ(factory.size<ObjectImpl>(), 1);
EXPECT_EQ(object_0_0->get_name(), "object_0");

const auto object_0_1 = factory.get_or_create<ObjectImpl>("object_0");
EXPECT_EQ(factory.size<ObjectImpl>(), 1);
EXPECT_EQ(object_0_0, object_0_1);

const auto object_1 = factory.get_or_create<ObjectImpl>("object_1");
EXPECT_EQ(factory.size<ObjectImpl>(), 2);

// destructors are called
}
EXPECT_EQ(factory.size<Object>(), 0);
EXPECT_EQ(factory.size<ObjectImpl>(), 0);
}
*/

}
5 changes: 5 additions & 0 deletions tests/common/pddl/persistent_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<pddl::ObjectImpl>("object_0");
EXPECT_EQ(factory.size(), 1);
EXPECT_EQ(object_0_0, object_0_1);

const auto object_1 = factory.get_or_create<pddl::ObjectImpl>("object_1");
EXPECT_EQ(factory.size(), 2);
EXPECT_NE(object_0_0, object_1);

}

}

0 comments on commit 932d797

Please sign in to comment.