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 329e4a4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
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
2 changes: 2 additions & 0 deletions 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 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_frame.sio");
}
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.sio");
}
2 changes: 2 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 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 329e4a4

Please sign in to comment.