|
9 | 9 |
|
10 | 10 | #include <stdexcept> |
11 | 11 |
|
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); |
66 | 51 | } |
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 | + } |
70 | 73 | } |
71 | | -} |
72 | 74 |
|
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 }; |
75 | 77 |
|
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 | + } |
80 | 82 |
|
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 }; |
84 | 86 |
|
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 | + } |
89 | 91 |
|
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 | + } |
97 | 99 |
|
98 | | -TEST_CASE("TrackState covariance", "[cov_matrix_utils]") { |
99 | | - auto trackState = edm4hep::TrackState{}; |
| 100 | + SECTION("TrackState covariance") { |
| 101 | + auto trackState = edm4hep::TrackState{}; |
100 | 102 |
|
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 | + } |
106 | 108 |
|
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); |
113 | 115 |
|
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); |
116 | 118 |
|
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}); |
119 | 121 |
|
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 | + } |
123 | 126 | } |
0 commit comments