Skip to content

Commit

Permalink
Add dedicated test case for only reading partial frames
Browse files Browse the repository at this point in the history
Reading only part of the collections stored in a frame should not lead
to leaked memory (see e.g. AIDASoft#500). The tests that are added in this
commit are targetting that, specifically by running them with sanitizers
enabled.
  • Loading branch information
tmadlener committed Nov 28, 2024
1 parent 9942d56 commit 2fd68cb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/CTestCustom.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if ((NOT "@FORCE_RUN_ALL_TESTS@" STREQUAL "ON") AND (NOT "@USE_SANITIZER@" STREQ
write_python_frame_root
read_python_frame_root
read_and_write_frame_root
read_partioal_root

param_reading_rdataframe

Expand Down
39 changes: 39 additions & 0 deletions tests/read_partial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef PODIO_TESTS_READ_PARTIAL_H // NOLINT(llvm-header-guard): folder structure not suitable
#define PODIO_TESTS_READ_PARTIAL_H // NOLINT(llvm-header-guard): folder structure not suitable

#include "podio/Frame.h"

#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

void read_partial_collections(const podio::Frame& event, const std::vector<std::string>& collsToRead) {
for (const auto& name : collsToRead) {
event.get(name);
}
}

/**
* This is a test case that will always work in normal builds and will only fail
* in builds with sanitizers enabled, where they will start to fail in case of
* e.g. memory leaks.
*/
template <typename ReaderT>
int read_partial_frames(const std::string& filename) {
auto reader = ReaderT();
try {
reader.openFile(filename);
} catch (const std::runtime_error& e) {
std::cerr << "File " << filename << " could not be opened. aborting." << std::endl;
return 1;
}

for (auto i = 0u; i < reader.getEntries(podio::Category::Event); ++i) {
const auto event = podio::Frame(reader.readEntry(podio::Category::Event, i));
read_partial_collections(event, {"mcparticles", "info", "hits", "clusters"});
}

return 0;
}
#endif // PODIO_TESTS_READ_PARTIAL_H
11 changes: 10 additions & 1 deletion tests/root_io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(root_dependent_tests
read_and_write_frame_root.cpp
write_interface_root.cpp
read_interface_root.cpp
read_partial_root.cpp
)
if(ENABLE_RNTUPLE)
set(root_dependent_tests
Expand All @@ -17,6 +18,7 @@ if(ENABLE_RNTUPLE)
read_python_frame_rntuple.cpp
write_interface_rntuple.cpp
read_interface_rntuple.cpp
read_partial_rntuple.cpp
)
endif()
if(ENABLE_DATASOURCE)
Expand All @@ -39,13 +41,20 @@ set_tests_properties(
read_frame_root
read_frame_root_multiple
read_and_write_frame_root
read_partial_root

PROPERTIES
DEPENDS write_frame_root
)

if(ENABLE_RNTUPLE)
set_property(TEST read_rntuple PROPERTY DEPENDS write_rntuple)
set_tests_properties(
read_rntuple
read_partial_rntuple

PROPERTIES
DEPENDS write_rntuple
)
set_property(TEST read_interface_rntuple PROPERTY DEPENDS write_interface_rntuple)
endif()

Expand Down
7 changes: 7 additions & 0 deletions tests/root_io/read_partial_rntuple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "read_partial.h"

#include "podio/RNTupleReader.h"

int main() {
return read_partial_frames<podio::RNTupleReader>("example_rntuple.root");
}
7 changes: 7 additions & 0 deletions tests/root_io/read_partial_root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "read_partial.h"

#include "podio/ROOTReader.h"

int main() {
return read_partial_frames<podio::ROOTReader>("example_frame.root");
}
3 changes: 3 additions & 0 deletions tests/sio_io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ set(sio_dependent_tests
read_python_frame_sio.cpp
write_interface_sio.cpp
read_interface_sio.cpp
read_partial_sio.cpp
)

set(sio_libs podio::podioSioIO podio::podioIO)
foreach( sourcefile ${sio_dependent_tests} )
CREATE_PODIO_TEST(${sourcefile} "${sio_libs}")
Expand All @@ -14,6 +16,7 @@ endforeach()
set_tests_properties(
read_frame_sio
read_and_write_frame_sio
read_partial_sio

PROPERTIES
DEPENDS
Expand Down
7 changes: 7 additions & 0 deletions tests/sio_io/read_partial_sio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "read_partial.h"

#include "podio/SIOReader.h"

int main() {
return read_partial_frames<podio::SIOReader>("example_frame.sio");
}

0 comments on commit 2fd68cb

Please sign in to comment.