Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make accessing single elements in empty parameter vectors return an empty optional #693

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/podio/GenericParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ std::optional<T> GenericParameters::get(const std::string& key) const {
return it->second;
} else {
const auto& iv = it->second;
if (iv.empty()) {
return std::nullopt;
}
return iv[0];
}
}
Expand Down
7 changes: 7 additions & 0 deletions python/podio/test_Frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ def test_frame_put_parameters(self):
frame.put_parameter("float_as_float", 3.14, as_type="float")
self.assertAlmostEqual(frame.get_parameter("float_as_float"), 3.14, places=5)

def test_frame_empty_parameters(self):
"""Check that working with empty parameters works"""
frame = Frame()
frame.put_parameter("empty_param", [], as_type="int")
vals = frame.get_parameter("empty_param")
self.assertEqual(len(vals), 0)


class FrameReadTest(unittest.TestCase):
"""Unit tests for the Frame python bindings for Frames read from file.
Expand Down
6 changes: 6 additions & 0 deletions tests/unittests/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ TEST_CASE("Frame parameters", "[frame][basics]") {
// Can't rely on an insertion order here
REQUIRE(std::find(stringKeys.begin(), stringKeys.end(), "aString") != stringKeys.end());
REQUIRE(std::find(stringKeys.begin(), stringKeys.end(), "someStrings") != stringKeys.end());

// Check the cases with empty vectors as parameters
event.putParameter("emptyVec", std::vector<int>{});
const auto emptyVec = event.getParameter<std::vector<int>>("emptyVec").value();
REQUIRE(emptyVec.empty());
REQUIRE_FALSE(event.getParameter<int>("emptyVec").has_value());
}

// NOTE: Due to the extremely small tasks that are done in these tests, they will
Expand Down
15 changes: 15 additions & 0 deletions tests/unittests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,21 @@ TEST_CASE("GenericParameters", "[generic-parameters]") {
REQUIRE_FALSE(gp.get<std::vector<std::string>>("Missing"));
}

TEST_CASE("GenericParameters empty vector single value access", "[generic-parameters]") {
auto gp = podio::GenericParameters();
gp.set("empty-ints", std::vector<int>{});

// Getting the whole vector works
const auto maybeVec = gp.get<std::vector<int>>("empty-ints");
REQUIRE(maybeVec.has_value());
const auto& vec = maybeVec.value();
REQUIRE(vec.empty());

// Trying to get a single (i.e. the first) value will not work
const auto maybeVal = gp.get<int>("empty-ints");
REQUIRE_FALSE(maybeVal.has_value());
}

TEST_CASE("GenericParameters constructors", "[generic-parameters]") {
// Tests for making sure that generic parameters can be moved / copied correctly
auto originalParams = podio::GenericParameters{};
Expand Down