Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: GeometryIdentifier checks value range #4094

Merged
merged 1 commit into from
Feb 18, 2025
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
11 changes: 10 additions & 1 deletion Core/include/Acts/Geometry/GeometryIdentifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ class GeometryIdentifier {
static constexpr Value kLayerMask = 0x0000fff000000000;
/// (2^8)-1 = 255 approach surfaces
static constexpr Value kApproachMask = 0x0000000ff0000000;
static constexpr Value kPassiveMask = kApproachMask;
/// (2^20)-1 = 1048575 sensitive surfaces
static constexpr Value kSensitiveMask = 0x000000000fffff00;
/// (2^8)-1 = 255 extra values
Expand All @@ -190,12 +189,22 @@ class GeometryIdentifier {
return __builtin_ctzll(mask);
}

constexpr static Value getMaxValue(Value mask) {
return mask >> extractShift(mask);
}

/// Extract the masked bits from the encoded value.
constexpr Value getBits(Value mask) const {
return (m_value & mask) >> extractShift(mask);
}
/// Set the masked bits to id in the encoded value.
constexpr GeometryIdentifier& setBits(Value mask, Value id) {
if (id > getMaxValue(mask)) {
throw std::invalid_argument(
"Value " + std::to_string(id) + " exceeds maximum value " +
std::to_string(getMaxValue(mask)) + " for this field");
}

m_value = (m_value & ~mask) | ((id << extractShift(mask)) & mask);
// return *this here that we need to write fewer lines in the setXXX
// methods
Expand Down
24 changes: 12 additions & 12 deletions Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ BOOST_AUTO_TEST_CASE(GeometryIdentifier_max_values) {
constexpr GeometryIdentifier ref = 0xdeadaffe01234567;
// values above the maximum are truncated
// max+1 has all available bits zeroed
BOOST_CHECK_EQUAL(GeometryIdentifier(ref).setVolume(volumeMax + 1),
GeometryIdentifier(ref).setVolume(0u));
BOOST_CHECK_EQUAL(GeometryIdentifier(ref).setBoundary(boundaryMax + 1),
GeometryIdentifier(ref).setBoundary(0u));
BOOST_CHECK_EQUAL(GeometryIdentifier(ref).setLayer(layerMax + 1),
GeometryIdentifier(ref).setLayer(0u));
BOOST_CHECK_EQUAL(GeometryIdentifier(ref).setApproach(approachMax + 1),
GeometryIdentifier(ref).setApproach(0u));
BOOST_CHECK_EQUAL(GeometryIdentifier(ref).setSensitive(sensitiveMax + 1),
GeometryIdentifier(ref).setSensitive(0u));
BOOST_CHECK_EQUAL(GeometryIdentifier(ref).setExtra(extraMax + 1),
GeometryIdentifier(ref).setExtra(0u));
BOOST_CHECK_THROW(GeometryIdentifier(ref).setVolume(volumeMax + 1),
std::invalid_argument);
BOOST_CHECK_THROW(GeometryIdentifier(ref).setBoundary(boundaryMax + 1),
std::invalid_argument);
BOOST_CHECK_THROW(GeometryIdentifier(ref).setLayer(layerMax + 1),
std::invalid_argument);
BOOST_CHECK_THROW(GeometryIdentifier(ref).setApproach(approachMax + 1),
std::invalid_argument);
BOOST_CHECK_THROW(GeometryIdentifier(ref).setSensitive(sensitiveMax + 1),
std::invalid_argument);
BOOST_CHECK_THROW(GeometryIdentifier(ref).setExtra(extraMax + 1),
std::invalid_argument);
}

BOOST_AUTO_TEST_CASE(GeometryIdentifier_order) {
Expand Down
Loading