Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Jan 3, 2024
1 parent fcba4a8 commit 74e2e1d
Show file tree
Hide file tree
Showing 25 changed files with 223 additions and 69 deletions.
6 changes: 3 additions & 3 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ add_executable(undefined_behavior "undefined_behavior.cpp")
target_link_libraries(undefined_behavior parsers)


add_custom_target(state_space_delivery_example_domain ALL
add_custom_target(example_domain ALL
COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/benchmarks/gripper/domain.pddl" "${CMAKE_BINARY_DIR}/benchmarks/gripper/domain.pddl")
add_custom_target(state_space_delivery_example_instance_1 ALL
add_custom_target(example_problem_0 ALL
COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/benchmarks/gripper/p-2-0.pddl" "${CMAKE_BINARY_DIR}/benchmarks/gripper/p-2-0.pddl")
add_custom_target(state_space_delivery_example_instance_2 ALL
add_custom_target(example_problem_1 ALL
COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/benchmarks/gripper/p-2-1.pddl" "${CMAKE_BINARY_DIR}/benchmarks/gripper/p-2-1.pddl")
11 changes: 9 additions & 2 deletions include/loki/common/pddl/garbage_collected_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace loki {
/// Custom deleter idea: https://stackoverflow.com/questions/49782011/herb-sutters-10-liner-with-cleanup
/// TODO: improve the quality of the answer on stackoverflow
template<typename... Ts>
class ReferenceCountedObjectFactory {
class GarbageCollectedFactory {
private:

/// @brief Encapsulates the data of a single type.
Expand All @@ -56,7 +56,7 @@ class ReferenceCountedObjectFactory {
std::shared_ptr<Cache> m_cache;

public:
ReferenceCountedObjectFactory()
GarbageCollectedFactory()
: m_cache(std::make_shared<Cache>()) { }

/// @brief Gets a shared reference to the object of type T with the given arguments.
Expand Down Expand Up @@ -101,6 +101,13 @@ class ReferenceCountedObjectFactory {
t_cache.identifier_to_object.emplace(identifier, sp);
return sp;
}

template<typename T>
size_t size() const {
std::lock_guard<std::mutex> hold(m_cache->mutex);
auto& t_cache = std::get<PerTypeCache<T>>(m_cache->data);
return t_cache.uniqueness.size();
}
};

}
Expand Down
8 changes: 4 additions & 4 deletions include/loki/common/pddl/persistent_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define LOKI_INCLUDE_LOKI_COMMON_PDDL_PERSISTENT_FACTORY_HPP_

#include "declarations.hpp"
#include "segmented_persistent_vector.hpp"
#include "segmented_vector.hpp"

#include <unordered_set>
#include <memory>
Expand All @@ -32,7 +32,7 @@
namespace loki {
/// @brief The PersistentFactory class manages unique objects in a persistent
/// and efficient manner, utilizing a combination of unordered_set for
/// uniqueness checks and SegmentedPersistentVector for continuous and
/// uniqueness checks and SegmentedVector for continuous and
/// cache-efficient storage.
/// @tparam HolderType is the nested type which can be an std::variant.
/// @tparam N is the number of elements per segment
Expand All @@ -58,7 +58,7 @@ class PersistentFactory {
// We use pointers to the persistent memory to reduce allocations.
std::unordered_set<const HolderType*, DerferencedHash<HolderType>, DereferencedEquality<HolderType>> m_uniqueness_set;
// Use pre-allocated memory to store PDDL object persistent and continuously for improved cache locality.
SegmentedPersistentVector<HolderType, N> m_persistent_vector;
SegmentedVector<HolderType, N> m_persistent_vector;

int m_count = 0;

Expand Down Expand Up @@ -101,7 +101,7 @@ class PersistentFactory {
}

size_t size() const {
return m_persistent_vector.size();
return m_count;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@


namespace loki {
/// @brief The SegmentedPersistentVector persistently stores elements of type T
/// @brief The SegmentedVector persistently stores elements of type T
/// in segments of size N, ensuring that references to elements do not
/// become invalidated upon reallocation.
/// @tparam T is the nested type
/// @tparam N is the number of elements per segment
template<typename T, ElementsPerSegment N>
class SegmentedPersistentVector {
class SegmentedVector {
private:
std::vector<std::vector<T>> m_data;

Expand All @@ -59,7 +59,7 @@ class SegmentedPersistentVector {
}

public:
explicit SegmentedPersistentVector() : m_size(0), m_capacity(0) { }
explicit SegmentedVector() : m_size(0), m_capacity(0) { }

const T& push_back(T value) {
// Increase capacity if necessary
Expand All @@ -82,13 +82,6 @@ class SegmentedPersistentVector {
return m_data[segment_index(index)][element_index(index)];
}

void pop_back() {
assert(size() > 0);
auto& segment = m_data[segment_index(size() - 1)];
segment.pop_back();
--m_size;
}

const T& operator[](int identifier) const {
assert(identifier >= 0 && identifier <= static_cast<int>(size()));
return m_data[segment_index(identifier)][element_index(identifier)];
Expand Down
13 changes: 12 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ FetchContent_MakeAvailable(googletest)
add_library(GTest::GTest INTERFACE IMPORTED)
target_link_libraries(GTest::GTest INTERFACE gtest_main)

add_subdirectory(domain)
file(GLOB SRC_FILES
"common/pddl/*.cpp"
"domain/ast/*.cpp")

add_executable(domain_tests ${SRC_FILES})

target_link_libraries(domain_tests
PRIVATE
loki::parsers
GTest::GTest)

add_test(domain_gtests domain_tests)
56 changes: 56 additions & 0 deletions tests/common/pddl/garbage_collected_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 Dominik Drexler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 <https://www.gnu.org/licenses/>.
*/

#include "../../../include/loki/common/pddl/garbage_collected_factory.hpp"
#include "../../../include/loki/domain/pddl/object.hpp"

#include <gtest/gtest.h>

#include <string>


namespace loki::domain::tests {
/*
class Object {
private:
int m_identifier;
std::string m_name;
Object(int identifier, std::string name) : m_identifier(identifier), m_name(std::move(name)) { }
template<typename... Ts>
friend class GarbageCollectedFactory;
public:
int get_identifier() const { return m_identifier; }
const std::string& get_name() const { return m_name; }
};
TEST(LokiTests, GarbageCollectedFactoryTest) {
GarbageCollectedFactory<Object> factory;
EXPECT_EQ(factory.size<Object>(), 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
}
EXPECT_EQ(factory.size<Object>(), 0);
}
*/
}
41 changes: 41 additions & 0 deletions tests/common/pddl/persistent_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2023 Dominik Drexler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 <https://www.gnu.org/licenses/>.
*/

#include <gtest/gtest.h>

#include "../../../include/loki/common/pddl/persistent_factory.hpp"
#include "../../../include/loki/domain/pddl/object.hpp"


namespace loki::domain::tests {

TEST(LokiTests, PersistentFactoryTest) {
PersistentFactory<pddl::ObjectImpl, 2> factory;
EXPECT_EQ(factory.size(), 0);

// Test uniqueness: insert the same element twice
const auto object_0_0 = factory.get_or_create<pddl::ObjectImpl>("object_0");
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);

}

}
46 changes: 46 additions & 0 deletions tests/common/pddl/segmented_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2023 Dominik Drexler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 <https://www.gnu.org/licenses/>.
*/

#include <gtest/gtest.h>

#include "../../../include/loki/common/pddl/segmented_vector.hpp"


namespace loki::domain::tests {

TEST(LokiTests, SegmentedVectorTest) {
SegmentedVector<int, 2> vec;
EXPECT_EQ(vec.size(), 0);
EXPECT_EQ(vec.capacity(), 0);

vec.push_back(2);
EXPECT_EQ(vec.size(), 1);
EXPECT_EQ(vec.capacity(), 2);
EXPECT_EQ(vec[0], 2);

vec.push_back(1);
EXPECT_EQ(vec.size(), 2);
EXPECT_EQ(vec[1], 1);
EXPECT_EQ(vec.capacity(), 2);

vec.push_back(0);
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[2], 0);
EXPECT_EQ(vec.capacity(), 4);
}

}
2 changes: 1 addition & 1 deletion tests/domain/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
file(GLOB SRC_FILES
"*.cpp")
"ast/*.cpp")

add_executable(domain_tests ${SRC_FILES})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
6 changes: 3 additions & 3 deletions tests/domain/name.cpp → tests/domain/ast/name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
6 changes: 3 additions & 3 deletions tests/domain/number.cpp → tests/domain/ast/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
6 changes: 3 additions & 3 deletions tests/domain/predicate.cpp → tests/domain/ast/predicate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include <gtest/gtest.h>

#include "../../src/domain/ast/parser.hpp"
#include "../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../include/loki/domain/ast/printer.hpp"
#include "../../../src/domain/ast/parser.hpp"
#include "../../../include/loki/common/ast/parser_wrapper.hpp"
#include "../../../include/loki/domain/ast/printer.hpp"


namespace loki::domain::tests {
Expand Down
Loading

0 comments on commit 74e2e1d

Please sign in to comment.