Skip to content

Commit

Permalink
Add unittest to cover parts the CollectionBufferFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Oct 7, 2023
1 parent 2bf7c49 commit c3d7760
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if(NOT Catch2_FOUND)
endif()

find_package(Threads REQUIRED)
add_executable(unittest_podio unittest.cpp frame.cpp)
add_executable(unittest_podio unittest.cpp frame.cpp buffer_factory.cpp)
target_link_libraries(unittest_podio PUBLIC TestDataModel PRIVATE Catch2::Catch2WithMain Threads::Threads podio::podioRootIO)
if (ENABLE_SIO)
target_link_libraries(unittest_podio PRIVATE podio::podioSioIO)
Expand Down
152 changes: 152 additions & 0 deletions tests/unittests/buffer_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include "datamodel/ExampleHitCollectionData.h"
#include "podio/CollectionBufferFactory.h"

#include "datamodel/DatamodelDefinition.h"
#include "datamodel/ExampleClusterCollection.h"
#include "datamodel/ExampleHitCollection.h"
#include "datamodel/ExampleWithVectorMemberCollection.h"

#include "catch2/catch_test_macros.hpp"

TEST_CASE("createBuffers", "[internals][memory-management]") {
const auto& factory = podio::CollectionBufferFactory::instance();

SECTION("Simple type") {
auto maybeBuffers = factory.createBuffers("ExampleHitCollection", datamodel::meta::schemaVersion, false);

REQUIRE(maybeBuffers.has_value());
auto buffers = maybeBuffers.value();

// All pointers should be initialized
REQUIRE(buffers.data);
REQUIRE(buffers.references);
REQUIRE(buffers.vectorMembers);

// Cast this to something useful again
auto dataBuffers = static_cast<ExampleHitDataContainer*>(buffers.data);
REQUIRE(dataBuffers->empty());
REQUIRE(buffers.references->empty());
REQUIRE(buffers.vectorMembers->empty());

// Do the necessary cleanup
delete dataBuffers;
delete buffers.references;
delete buffers.vectorMembers;
}

SECTION("Type with relations") {
auto maybeBuffers = factory.createBuffers("ExampleClusterCollection", datamodel::meta::schemaVersion, false);

REQUIRE(maybeBuffers.has_value());
auto buffers = maybeBuffers.value();

// All pointers should be initialized
REQUIRE(buffers.data);
REQUIRE(buffers.references);
REQUIRE(buffers.vectorMembers);

// Cast this to something useful again
auto dataBuffers = static_cast<ExampleClusterDataContainer*>(buffers.data);
REQUIRE(dataBuffers->empty());
REQUIRE(buffers.references->size() == 2);
REQUIRE(buffers.vectorMembers->empty());

// Do the necessary cleanup
delete dataBuffers;
delete buffers.references;
delete buffers.vectorMembers;
}

SECTION("Type with vector members") {
auto maybeBuffers =
factory.createBuffers("ExampleWithVectorMemberCollection", datamodel::meta::schemaVersion, false);

REQUIRE(maybeBuffers.has_value());
auto buffers = maybeBuffers.value();

// All pointers should be initialized
REQUIRE(buffers.data);
REQUIRE(buffers.references);
REQUIRE(buffers.vectorMembers);

// Cast this to something useful again
auto dataBuffers = static_cast<ExampleWithVectorMemberDataContainer*>(buffers.data);
REQUIRE(dataBuffers->empty());
REQUIRE(buffers.references->empty());
REQUIRE(buffers.vectorMembers->size() == 1);

// Do the necessary cleanup
delete dataBuffers;
delete buffers.references;
auto vecBuffer = static_cast<std::vector<int>*>((*buffers.vectorMembers)[0].second);
delete vecBuffer;
delete buffers.vectorMembers;
}
}

TEST_CASE("construct CollectionData empty buffers", "[internals][memory-management]") {
const auto& factory = podio::CollectionBufferFactory::instance();

SECTION("Simple type") {
auto buffers = factory.createBuffers("ExampleHitCollection", datamodel::meta::schemaVersion, false).value();
auto collData = ExampleHitCollectionData(std::move(buffers), false);

// These tests either get flagged by sanitizers or they work
REQUIRE(true);
}

SECTION("Type with relation") {
auto buffers = factory.createBuffers("ExampleClusterCollection", datamodel::meta::schemaVersion, false).value();
auto collData = ExampleClusterCollectionData(std::move(buffers), false);

// These tests either get flagged by sanitizers or they work
REQUIRE(true);
}

SECTION("Type with vector members") {
auto buffers =
factory.createBuffers("ExampleWithVectorMemberCollection", datamodel::meta::schemaVersion, false).value();

auto collData = ExampleWithVectorMemberCollectionData(std::move(buffers), false);

// These tests either get flagged by sanitizers or they work
REQUIRE(true);
}
}

TEST_CASE("construct CollectionData non-empty buffers", "[internals][memory-management]") {
const auto& factory = podio::CollectionBufferFactory::instance();

SECTION("Simple type") {
auto buffers = factory.createBuffers("ExampleHitCollection", datamodel::meta::schemaVersion, false).value();

// Cast this to something useful again to add one data element
auto dataBuffers = static_cast<ExampleHitDataContainer*>(buffers.data);
dataBuffers->emplace_back(ExampleHitData{0xcaffee, 1.0, 2.0, 3.0, 125.0});

auto collData = ExampleHitCollectionData(std::move(buffers), false);
}

SECTION("Type with relations") {
auto buffers = factory.createBuffers("ExampleClusterCollection", datamodel::meta::schemaVersion, false).value();

// Cast this to something useful again to add one data element
auto dataBuffers = static_cast<ExampleClusterDataContainer*>(buffers.data);
dataBuffers->emplace_back(ExampleClusterData{125.0});

// Add an ObjectID (we don't need to resolve it in any way)
(*buffers.references)[0]->emplace_back(podio::ObjectID{42, 42});

auto collData = ExampleClusterCollectionData(std::move(buffers), false);
}

SECTION("Type with vector members") {
auto buffers =
factory.createBuffers("ExampleWithVectorMemberCollection", datamodel::meta::schemaVersion, false).value();

auto vecBuffer = static_cast<std::vector<int>*>((*buffers.vectorMembers)[0].second);
vecBuffer->emplace_back(42);

auto collData = ExampleWithVectorMemberCollectionData(std::move(buffers), false);
}
}

0 comments on commit c3d7760

Please sign in to comment.