Skip to content

Commit d3224d9

Browse files
authored
Group small and related tests together (#506)
1 parent 677a07d commit d3224d9

5 files changed

Lines changed: 443 additions & 413 deletions

File tree

test/utils/test_bit_utils.cpp

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,96 @@
11
#include "edm4hep/utils/bit_utils.h"
22

3-
#include <catch2/catch_template_test_macros.hpp>
43
#include <catch2/catch_test_macros.hpp>
54

65
#include <cstdint>
76
#include <tuple>
7+
#include <type_traits>
88

9-
// The integet types that we us as type fields in EDM4hep
9+
// The integer types that we use as type fields in EDM4hep
1010
using BitFieldTypes = std::tuple<int32_t, uint32_t, int64_t, int16_t, uint64_t>;
1111

12-
TEMPLATE_LIST_TEST_CASE("Bitfield utils set and get", "[bit_utils]", BitFieldTypes) {
12+
namespace {
13+
/// Helper to run a test body for every type in BitFieldTypes
14+
template <typename F>
15+
void forEachType(F&& f) {
16+
std::apply([&](auto... types) { (f(types), ...); }, BitFieldTypes{});
17+
}
18+
} // namespace
19+
20+
TEST_CASE("Bitfield utils", "[bit_utils]") {
1321
using namespace edm4hep;
14-
auto bitField = TestType{};
1522

16-
for (auto i = 0u; i < sizeof(TestType) * 8; ++i) {
17-
REQUIRE_FALSE(utils::checkBit(bitField, i));
18-
}
23+
SECTION("set and get") {
24+
forEachType([](auto type) {
25+
using T = std::decay_t<decltype(type)>;
26+
auto bitField = T{};
27+
28+
for (auto i = 0u; i < sizeof(T) * 8; ++i) {
29+
REQUIRE_FALSE(utils::checkBit(bitField, i));
30+
}
1931

20-
bitField = utils::setBit(bitField, 3, true);
21-
REQUIRE(utils::checkBit(bitField, 3));
32+
bitField = utils::setBit(bitField, 3, true);
33+
REQUIRE(utils::checkBit(bitField, 3));
2234

23-
bitField = utils::setBit(bitField, 4, true);
24-
REQUIRE(utils::checkBit(bitField, 3));
25-
REQUIRE(utils::checkBit(bitField, 4));
35+
bitField = utils::setBit(bitField, 4, true);
36+
REQUIRE(utils::checkBit(bitField, 3));
37+
REQUIRE(utils::checkBit(bitField, 4));
2638

27-
bitField = utils::setBit(bitField, 3, false);
28-
REQUIRE_FALSE(utils::checkBit(bitField, 3));
29-
REQUIRE(utils::checkBit(bitField, 4));
39+
bitField = utils::setBit(bitField, 3, false);
40+
REQUIRE_FALSE(utils::checkBit(bitField, 3));
41+
REQUIRE(utils::checkBit(bitField, 4));
3042

31-
if constexpr (sizeof(TestType) >= 8) {
32-
bitField = utils::setBit(bitField, 35, true);
33-
REQUIRE(utils::checkBit(bitField, 35));
34-
REQUIRE(bitField == TestType(1ULL << 4 | 1ULL << 35));
35-
bitField = utils::setBit(bitField, 35, false);
36-
REQUIRE_FALSE(utils::checkBit(bitField, 35));
37-
REQUIRE(bitField == TestType(1ULL << 4));
43+
if constexpr (sizeof(T) >= 8) {
44+
bitField = utils::setBit(bitField, 35, true);
45+
REQUIRE(utils::checkBit(bitField, 35));
46+
REQUIRE(bitField == T(1ULL << 4 | 1ULL << 35));
47+
bitField = utils::setBit(bitField, 35, false);
48+
REQUIRE_FALSE(utils::checkBit(bitField, 35));
49+
REQUIRE(bitField == T(1ULL << 4));
50+
}
51+
});
3852
}
39-
}
4053

41-
TEMPLATE_LIST_TEST_CASE("Bitfield utils set multiple", "[bit_utils]", BitFieldTypes) {
42-
using namespace edm4hep;
43-
auto bitField = TestType{};
44-
bitField = utils::setBits(bitField, true, 3u, 4u, 7u);
45-
46-
REQUIRE(utils::checkBit(bitField, 3));
47-
REQUIRE(utils::checkBit(bitField, 4));
48-
REQUIRE(utils::checkBit(bitField, 7));
49-
REQUIRE_FALSE(utils::checkBit(bitField, 1));
50-
REQUIRE_FALSE(utils::checkBit(bitField, 2));
51-
REQUIRE_FALSE(utils::checkBit(bitField, 5));
52-
REQUIRE_FALSE(utils::checkBit(bitField, 6));
53-
REQUIRE_FALSE(utils::checkBit(bitField, 8));
54-
}
54+
SECTION("set multiple") {
55+
forEachType([](auto type) {
56+
using T = std::decay_t<decltype(type)>;
57+
auto bitField = T{};
58+
bitField = utils::setBits(bitField, true, 3u, 4u, 7u);
5559

56-
TEMPLATE_LIST_TEST_CASE("Bitfield utils check all ", "[bit_utils]", BitFieldTypes) {
57-
using namespace edm4hep;
58-
auto bitField = TestType{};
59-
bitField = utils::setBits(bitField, true, 3u, 4u, 7u);
60+
REQUIRE(utils::checkBit(bitField, 3));
61+
REQUIRE(utils::checkBit(bitField, 4));
62+
REQUIRE(utils::checkBit(bitField, 7));
63+
REQUIRE_FALSE(utils::checkBit(bitField, 1));
64+
REQUIRE_FALSE(utils::checkBit(bitField, 2));
65+
REQUIRE_FALSE(utils::checkBit(bitField, 5));
66+
REQUIRE_FALSE(utils::checkBit(bitField, 6));
67+
REQUIRE_FALSE(utils::checkBit(bitField, 8));
68+
});
69+
}
6070

61-
REQUIRE(utils::checkAllBits(bitField, 7u, 3u, 4u));
62-
REQUIRE(utils::checkAllBits(bitField, 3u, 4u));
63-
REQUIRE_FALSE(utils::checkAllBits(bitField, 2u, 3u, 4u, 7u));
64-
REQUIRE_FALSE(utils::checkAllBits(bitField, 2u, 3u, 4u));
65-
}
71+
SECTION("check all") {
72+
forEachType([](auto type) {
73+
using T = std::decay_t<decltype(type)>;
74+
auto bitField = T{};
75+
bitField = utils::setBits(bitField, true, 3u, 4u, 7u);
6676

67-
TEMPLATE_LIST_TEST_CASE("Bitfield utils check any ", "[bit_utils]", BitFieldTypes) {
68-
using namespace edm4hep;
69-
auto bitField = TestType{};
70-
bitField = utils::setBits(bitField, true, 3u, 4u, 7u);
77+
REQUIRE(utils::checkAllBits(bitField, 7u, 3u, 4u));
78+
REQUIRE(utils::checkAllBits(bitField, 3u, 4u));
79+
REQUIRE_FALSE(utils::checkAllBits(bitField, 2u, 3u, 4u, 7u));
80+
REQUIRE_FALSE(utils::checkAllBits(bitField, 2u, 3u, 4u));
81+
});
82+
}
83+
84+
SECTION("check any") {
85+
forEachType([](auto type) {
86+
using T = std::decay_t<decltype(type)>;
87+
auto bitField = T{};
88+
bitField = utils::setBits(bitField, true, 3u, 4u, 7u);
7189

72-
REQUIRE(utils::checkAnyBits(bitField, 3u, 4u));
73-
REQUIRE(utils::checkAnyBits(bitField, 3u));
74-
REQUIRE(utils::checkAnyBits(bitField, 1u, 2u, 3u));
75-
REQUIRE_FALSE(utils::checkAnyBits(bitField, 1u, 2u, 6u, 8u));
90+
REQUIRE(utils::checkAnyBits(bitField, 3u, 4u));
91+
REQUIRE(utils::checkAnyBits(bitField, 3u));
92+
REQUIRE(utils::checkAnyBits(bitField, 1u, 2u, 3u));
93+
REQUIRE_FALSE(utils::checkAnyBits(bitField, 1u, 2u, 6u, 8u));
94+
});
95+
}
7696
}

test/utils/test_covmatrix_utils.cpp

Lines changed: 101 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -9,115 +9,118 @@
99

1010
#include <stdexcept>
1111

12-
TEST_CASE("CovarianceMatrix indexing", "[cov_matrix_utils]") {
13-
using namespace edm4hep::utils::detail;
14-
15-
STATIC_REQUIRE(get_cov_dim(21) == 6);
16-
STATIC_REQUIRE(get_cov_dim(1) == 1);
17-
18-
// clang-format off
19-
// For better interpretability of the tests below, these are the indices of a
20-
// 2D matrix in lower triangular form together with the matrix indices
21-
//
22-
// | 0 1 2 3 4 5
23-
// --+------------------
24-
// 0 | 0 1 3 6 10 15
25-
// 1 | 1 2 4 7 11 16
26-
// 2 | 3 4 5 8 12 17
27-
// 3 | 6 7 8 9 13 18
28-
// 4 | 10 11 12 13 14 19
29-
// 5 | 15 16 17 18 19 20
30-
// clang-format on
31-
32-
// diagonal elements
33-
STATIC_REQUIRE(to_lower_tri(0, 0) == 0);
34-
STATIC_REQUIRE(to_lower_tri(1, 1) == 2);
35-
STATIC_REQUIRE(to_lower_tri(2, 2) == 5);
36-
STATIC_REQUIRE(to_lower_tri(3, 3) == 9);
37-
STATIC_REQUIRE(to_lower_tri(4, 4) == 14);
38-
STATIC_REQUIRE(to_lower_tri(5, 5) == 20);
39-
40-
// some off diagonal elements
41-
STATIC_REQUIRE(to_lower_tri(1, 0) == 1);
42-
STATIC_REQUIRE(to_lower_tri(0, 1) == 1);
43-
STATIC_REQUIRE(to_lower_tri(0, 2) == 3);
44-
STATIC_REQUIRE(to_lower_tri(2, 0) == 3);
45-
STATIC_REQUIRE(to_lower_tri(2, 3) == 8);
46-
STATIC_REQUIRE(to_lower_tri(3, 2) == 8);
47-
STATIC_REQUIRE(to_lower_tri(5, 3) == 18);
48-
STATIC_REQUIRE(to_lower_tri(2, 5) == 17);
49-
}
50-
51-
TEST_CASE("CovMatrixNf array access", "[cov_matrix_utils]") {
52-
// We use the 3D version here, but since the ExtraCode is effectively
53-
// duplicated for the others as well it shouldn't really matter
54-
auto covMatrix = edm4hep::CovMatrix3f{};
55-
56-
covMatrix[3] = 3.14f;
57-
REQUIRE(covMatrix[3] == 3.14f);
58-
59-
REQUIRE(covMatrix.data()[3] == 3.14f);
60-
covMatrix.data()[2] = 2.13f;
61-
REQUIRE(covMatrix[2] == 2.13f);
62-
63-
float i = 0.f;
64-
for (auto& v : covMatrix) {
65-
v = i++;
12+
TEST_CASE("CovMatrix utils", "[cov_matrix_utils]") {
13+
14+
SECTION("indexing") {
15+
using namespace edm4hep::utils::detail;
16+
17+
STATIC_REQUIRE(get_cov_dim(21) == 6);
18+
STATIC_REQUIRE(get_cov_dim(1) == 1);
19+
20+
// clang-format off
21+
// For better interpretability of the tests below, these are the indices of a
22+
// 2D matrix in lower triangular form together with the matrix indices
23+
//
24+
// | 0 1 2 3 4 5
25+
// --+------------------
26+
// 0 | 0 1 3 6 10 15
27+
// 1 | 1 2 4 7 11 16
28+
// 2 | 3 4 5 8 12 17
29+
// 3 | 6 7 8 9 13 18
30+
// 4 | 10 11 12 13 14 19
31+
// 5 | 15 16 17 18 19 20
32+
// clang-format on
33+
34+
// diagonal elements
35+
STATIC_REQUIRE(to_lower_tri(0, 0) == 0);
36+
STATIC_REQUIRE(to_lower_tri(1, 1) == 2);
37+
STATIC_REQUIRE(to_lower_tri(2, 2) == 5);
38+
STATIC_REQUIRE(to_lower_tri(3, 3) == 9);
39+
STATIC_REQUIRE(to_lower_tri(4, 4) == 14);
40+
STATIC_REQUIRE(to_lower_tri(5, 5) == 20);
41+
42+
// some off diagonal elements
43+
STATIC_REQUIRE(to_lower_tri(1, 0) == 1);
44+
STATIC_REQUIRE(to_lower_tri(0, 1) == 1);
45+
STATIC_REQUIRE(to_lower_tri(0, 2) == 3);
46+
STATIC_REQUIRE(to_lower_tri(2, 0) == 3);
47+
STATIC_REQUIRE(to_lower_tri(2, 3) == 8);
48+
STATIC_REQUIRE(to_lower_tri(3, 2) == 8);
49+
STATIC_REQUIRE(to_lower_tri(5, 3) == 18);
50+
STATIC_REQUIRE(to_lower_tri(2, 5) == 17);
6651
}
67-
i = 0.f;
68-
for (const auto& v : covMatrix) {
69-
REQUIRE(v == i++);
52+
53+
SECTION("array access") {
54+
// We use the 3D version here, but since the ExtraCode is effectively
55+
// duplicated for the others as well it shouldn't really matter
56+
auto covMatrix = edm4hep::CovMatrix3f{};
57+
58+
covMatrix[3] = 3.14f;
59+
REQUIRE(covMatrix[3] == 3.14f);
60+
61+
REQUIRE(covMatrix.data()[3] == 3.14f);
62+
covMatrix.data()[2] = 2.13f;
63+
REQUIRE(covMatrix[2] == 2.13f);
64+
65+
float i = 0.f;
66+
for (auto& v : covMatrix) {
67+
v = i++;
68+
}
69+
i = 0.f;
70+
for (const auto& v : covMatrix) {
71+
REQUIRE(v == i++);
72+
}
7073
}
71-
}
7274

73-
TEST_CASE("CovMatrixNf enum access", "[cov_matrix_utils]") {
74-
enum class TestDims : uint32_t { a = 0, b, c };
75+
SECTION("enum access") {
76+
enum class TestDims : uint32_t { a = 0, b, c };
7577

76-
auto covMatrix = edm4hep::CovMatrix3f{};
77-
covMatrix.setValue(1.23f, TestDims::a, TestDims::c);
78-
REQUIRE(covMatrix.getValue(TestDims::a, TestDims::c) == 1.23f);
79-
}
78+
auto covMatrix = edm4hep::CovMatrix3f{};
79+
covMatrix.setValue(1.23f, TestDims::a, TestDims::c);
80+
REQUIRE(covMatrix.getValue(TestDims::a, TestDims::c) == 1.23f);
81+
}
8082

81-
TEST_CASE("CovMatrixNf invalid enum access", "[cov_matrix_utils]") {
82-
// Invalid dimensions with too many elements to fit the 3D convariance matrix
83-
enum class InvalidDims : edm4hep::DimType { i = 0, j, k, l, m };
83+
SECTION("invalid enum access") {
84+
// Invalid dimensions with too many elements to fit the 3D convariance matrix
85+
enum class InvalidDims : edm4hep::DimType { i = 0, j, k, l, m };
8486

85-
auto covMatrix = edm4hep::CovMatrix3f{};
86-
REQUIRE_THROWS_AS(covMatrix.setValue(1.23f, InvalidDims::k, InvalidDims::l), std::invalid_argument);
87-
REQUIRE_THROWS_AS(covMatrix.getValue(InvalidDims::m, InvalidDims::i), std::invalid_argument);
88-
}
87+
auto covMatrix = edm4hep::CovMatrix3f{};
88+
REQUIRE_THROWS_AS(covMatrix.setValue(1.23f, InvalidDims::k, InvalidDims::l), std::invalid_argument);
89+
REQUIRE_THROWS_AS(covMatrix.getValue(InvalidDims::m, InvalidDims::i), std::invalid_argument);
90+
}
8991

90-
TEST_CASE("CovMatrixNf equality operators", "[cov_matrix_utils]") {
91-
auto covMatrix = edm4hep::CovMatrix3f{};
92-
covMatrix[3] = 3.14f;
93-
covMatrix[2] = 2.13f;
94-
REQUIRE(covMatrix == std::array<float, 6>{0, 0, 2.13f, 3.14f, 0, 0});
95-
REQUIRE(covMatrix != std::array<float, 6>{});
96-
}
92+
SECTION("equality operators") {
93+
auto covMatrix = edm4hep::CovMatrix3f{};
94+
covMatrix[3] = 3.14f;
95+
covMatrix[2] = 2.13f;
96+
REQUIRE(covMatrix == std::array<float, 6>{0, 0, 2.13f, 3.14f, 0, 0});
97+
REQUIRE(covMatrix != std::array<float, 6>{});
98+
}
9799

98-
TEST_CASE("TrackState covariance", "[cov_matrix_utils]") {
99-
auto trackState = edm4hep::TrackState{};
100+
SECTION("TrackState covariance") {
101+
auto trackState = edm4hep::TrackState{};
100102

101-
trackState.setCovMatrix(1.23f, edm4hep::TrackParams::d0, edm4hep::TrackParams::phi);
102-
// We know the expected index in this case
103-
REQUIRE(trackState.covMatrix.values[1] == 1.23f);
104-
REQUIRE(trackState.getCovMatrix(edm4hep::TrackParams::time, edm4hep::TrackParams::omega) == 0);
105-
}
103+
trackState.setCovMatrix(1.23f, edm4hep::TrackParams::d0, edm4hep::TrackParams::phi);
104+
// We know the expected index in this case
105+
REQUIRE(trackState.covMatrix.values[1] == 1.23f);
106+
REQUIRE(trackState.getCovMatrix(edm4hep::TrackParams::time, edm4hep::TrackParams::omega) == 0);
107+
}
106108

107-
TEST_CASE("TrackerHit3D covariance", "[cov_matrix_utils]") {
108-
auto trackerHit = edm4hep::MutableTrackerHit3D{};
109-
trackerHit.setCovMatrix(3.14f, edm4hep::Cartesian::x, edm4hep::Cartesian::z);
110-
REQUIRE(trackerHit.getCovMatrix(edm4hep::Cartesian::x, edm4hep::Cartesian::z) == 3.14f);
111-
// We can also use the expected index of (x, y)
112-
REQUIRE(trackerHit.getCovMatrix().values[3] == 3.14f);
109+
SECTION("TrackerHit3D covariance") {
110+
auto trackerHit = edm4hep::MutableTrackerHit3D{};
111+
trackerHit.setCovMatrix(3.14f, edm4hep::Cartesian::x, edm4hep::Cartesian::z);
112+
REQUIRE(trackerHit.getCovMatrix(edm4hep::Cartesian::x, edm4hep::Cartesian::z) == 3.14f);
113+
// We can also use the expected index of (x, y)
114+
REQUIRE(trackerHit.getCovMatrix().values[3] == 3.14f);
113115

114-
auto hit = edm4hep::TrackerHit3D(trackerHit);
115-
REQUIRE(hit.getCovMatrix(edm4hep::Cartesian::x, edm4hep::Cartesian::z) == 3.14f);
116+
auto hit = edm4hep::TrackerHit3D(trackerHit);
117+
REQUIRE(hit.getCovMatrix(edm4hep::Cartesian::x, edm4hep::Cartesian::z) == 3.14f);
116118

117-
trackerHit.setCovMatrix({1.f, 2.f, 3.f, 4.f, 5.f, 6.f});
118-
REQUIRE(trackerHit.getCovMatrix() == std::array{1.f, 2.f, 3.f, 4.f, 5.f, 6.f});
119+
trackerHit.setCovMatrix({1.f, 2.f, 3.f, 4.f, 5.f, 6.f});
120+
REQUIRE(trackerHit.getCovMatrix() == std::array{1.f, 2.f, 3.f, 4.f, 5.f, 6.f});
119121

120-
const std::array arrValues = {6.f, 5.f, 4.f, 3.f, 2.f, 1.f};
121-
trackerHit.setCovMatrix(arrValues);
122-
REQUIRE(trackerHit.getCovMatrix() == std::array{6.f, 5.f, 4.f, 3.f, 2.f, 1.f});
122+
const std::array arrValues = {6.f, 5.f, 4.f, 3.f, 2.f, 1.f};
123+
trackerHit.setCovMatrix(arrValues);
124+
REQUIRE(trackerHit.getCovMatrix() == std::array{6.f, 5.f, 4.f, 3.f, 2.f, 1.f});
125+
}
123126
}

0 commit comments

Comments
 (0)