Skip to content

Commit

Permalink
SonarCloud fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
asalzburger committed Nov 14, 2024
1 parent 7e06951 commit a1f12b9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 57 deletions.
7 changes: 2 additions & 5 deletions core/include/detray/utils/grid/detail/axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ struct single_axis {
/// @param rhs is the right-hand side of the comparison
///
/// @returns whether the two axes are equal
DETRAY_HOST_DEVICE constexpr auto operator==(const single_axis &rhs) const
-> bool {
return m_bounds == rhs.m_bounds && m_binning == rhs.m_binning;
}
DETRAY_HOST_DEVICE constexpr bool operator==(const single_axis &rhs) const =
default;

/// @returns the axis label, i.e. x, y, z, r or phi axis.
DETRAY_HOST_DEVICE
Expand Down Expand Up @@ -418,7 +416,6 @@ class multi_axis {
/// @note in the non-owning case, we compare the values not the pointers
///
/// @returns whether the two axes are equal
// template <bool owner = is_owning>
DETRAY_HOST_DEVICE constexpr auto operator==(const multi_axis &rhs) const
-> bool {
if constexpr (!std::is_pointer_v<edge_range_t>) {
Expand Down
9 changes: 7 additions & 2 deletions core/include/detray/utils/grid/detail/axis_binning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,15 @@ struct irregular {
///
/// @param rhs the axis to compare to
///
/// @note as we cannot guarantee to have the same pointer for the bin edges,
/// we make a fast comparison of the pointer first, but also allow for a
/// value based comparison
///
/// @returns whether the two axes are equal
DETRAY_HOST_DEVICE constexpr bool operator==(const irregular &rhs) const {
return m_offset == rhs.m_offset && m_n_bins == rhs.m_n_bins &&
m_bin_edges == rhs.m_bin_edges;
return (m_offset == rhs.m_offset && m_n_bins == rhs.m_n_bins &&
m_bin_edges == rhs.m_bin_edges) ||
bin_edges() == rhs.bin_edges();
}
};

Expand Down
12 changes: 3 additions & 9 deletions core/include/detray/utils/grid/detail/axis_bounds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ struct open {
///
/// @returns whether the two axes are equal
DETRAY_HOST_DEVICE
constexpr bool operator==(const open &rhs) const noexcept {
return label == rhs.label && type == rhs.type;
}
constexpr bool operator==(const open &rhs) const = default;
};

/// @brief Describes the behaviour of a closed axis.
Expand Down Expand Up @@ -172,9 +170,7 @@ struct closed {
///
/// @returns whether the two axes are equal
DETRAY_HOST_DEVICE
constexpr bool operator==(const closed &rhs) const noexcept {
return label == rhs.label && type == rhs.type;
}
constexpr bool operator==(const closed &rhs) const = default;
};

/// @brief Describes the behaviour of a circular axis.
Expand Down Expand Up @@ -268,9 +264,7 @@ struct circular {
///
/// @returns whether the two axes are equal
DETRAY_HOST_DEVICE
constexpr bool operator==(const circular &rhs) const noexcept {
return label == rhs.label && type == rhs.type;
}
constexpr bool operator==(const circular &rhs) const = default;
};

} // namespace detray::axis
5 changes: 1 addition & 4 deletions core/include/detray/utils/grid/detail/grid_bins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ class dynamic_array
dindex capacity{0u};

DETRAY_HOST_DEVICE
constexpr bool operator==(const data& rhs) const {
return offset == rhs.offset && size == rhs.size &&
capacity == rhs.capacity;
}
constexpr bool operator==(const data& rhs) const = default;

DETRAY_HOST_DEVICE
constexpr void update_offset(std::size_t shift) {
Expand Down
74 changes: 37 additions & 37 deletions tests/unit_tests/cpu/utils/grids/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,57 +342,57 @@ GTEST_TEST(detray_grid, closed_irregular_axis) {
EXPECT_EQ(cir_axis.range(3.f, nhood11s), expected_range);
}

GTEST_TEST(detray_grid, axis_comparison) {
template <typename axis_type>
void test_axis(const axis_type& /*unused*/) {

// Should be sufficient to test the comparison operators
using x_axis_op_r_t = single_axis<open<label::e_x>, regular<>>;
using x_axis_cl_r_t = single_axis<closed<label::e_x>, regular<>>;
using x_axis_ci_r_t = single_axis<circular<label::e_x>, regular<>>;
using x_axis_op_ir_t = single_axis<open<label::e_x>, irregular<>>;
using x_axis_cl_ir_t = single_axis<closed<label::e_x>, irregular<>>;
using x_axis_ci_orr_t = single_axis<circular<label::e_x>, irregular<>>;
// Lower bin edges: min and max bin edge for the regular axis
vecmem::vector<scalar> bin_edges = {-10.f, -5.f, -3.f, 7.f, 7.f, 14.f,
20.f, 50.f, 100.f, 120.f, 150.f};

std::tuple<x_axis_op_r_t, x_axis_cl_r_t, x_axis_ci_r_t, x_axis_op_ir_t,
x_axis_cl_ir_t, x_axis_ci_orr_t>
axes = {};
vecmem::vector<scalar> different_bin_edges = {
-10.f, -5.f, -4.f, 7.f, 7.f, 14.f, 20.f, 50.f, 100.f, 120.f, 150.f};

auto test_axis = [](auto axis) -> void {
using axis_type = decltype(axis);
// Regular axis: first entry is the offset in the bin edges vector (2),
// the second entry is the number of bins (10): Lower and upper bin
// edges of the max and min bin are -3 and 7 => stepsize is (7 - (-3)) /
// 10 = 1
// ... -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 ...
// [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
const dindex_range edge_range = {2u, 10u};

// Lower bin edges: min and max bin edge for the regular axis
vecmem::vector<scalar> bin_edges = {
-10.f, -5.f, -3.f, 7.f, 7.f, 14.f, 20.f, 50.f, 100.f, 120.f, 150.f};
const dindex_range different_edge_range = {2u, 8u};

vecmem::vector<scalar> different_bin_edges = {
-10.f, -5.f, -4.f, 7.f, 7.f, 14.f, 20.f, 50.f, 100.f, 120.f, 150.f};
axis_type t_axis{edge_range, &bin_edges};

// Regular axis: first entry is the offset in the bin edges vector (2),
// the second entry is the number of bins (10): Lower and upper bin
// edges of the max and min bin are -3 and 7 => stepsize is (7 - (-3)) /
// 10 = 1
// ... -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 ...
// [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
const dindex_range edge_range = {2u, 10u};
axis_type another_t_axis{edge_range, &bin_edges};

const dindex_range different_edge_range = {2u, 8u};
axis_type differnt_t_axis{edge_range, &different_bin_edges};

axis_type t_axis{edge_range, &bin_edges};
axis_type differnt_edges_range_t_axis{different_edge_range, &bin_edges};

axis_type another_t_axis{edge_range, &bin_edges};
// Test if the axes are equal
EXPECT_EQ(t_axis, another_t_axis);

axis_type differnt_t_axis{edge_range, &different_bin_edges};
// Confirm that the axes are not equal
EXPECT_NE(t_axis, differnt_t_axis);

axis_type differnt_edges_range_t_axis{different_edge_range, &bin_edges};
// Confirm that the axes are not equal
EXPECT_NE(t_axis, differnt_edges_range_t_axis);
};

// Test if the axes are equal
EXPECT_EQ(t_axis, another_t_axis);
GTEST_TEST(detray_grid, axis_comparison) {

// Confirm that the axes are not equal
EXPECT_NE(t_axis, differnt_t_axis);
// Should be sufficient to test the comparison operators
using x_axis_op_r_t = single_axis<open<label::e_x>, regular<>>;
using x_axis_cl_r_t = single_axis<closed<label::e_x>, regular<>>;
using x_axis_ci_r_t = single_axis<circular<label::e_x>, regular<>>;
using x_axis_op_ir_t = single_axis<open<label::e_x>, irregular<>>;
using x_axis_cl_ir_t = single_axis<closed<label::e_x>, irregular<>>;
using x_axis_ci_orr_t = single_axis<circular<label::e_x>, irregular<>>;

// Confirm that the axes are not equal
EXPECT_NE(t_axis, differnt_edges_range_t_axis);
};
std::tuple<x_axis_op_r_t, x_axis_cl_r_t, x_axis_ci_r_t, x_axis_op_ir_t,
x_axis_cl_ir_t, x_axis_ci_orr_t>
axes = {};

// Test them all
std::apply([&](auto&... axis) { (test_axis(axis), ...); }, axes);
Expand Down

0 comments on commit a1f12b9

Please sign in to comment.