File indexing completed on 2025-04-03 08:01:18
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010
0011 #include "Acts/Utilities/AnyGridView.hpp"
0012 #include "Acts/Utilities/Axis.hpp"
0013 #include "Acts/Utilities/AxisDefinitions.hpp"
0014 #include "Acts/Utilities/Grid.hpp"
0015 #include "Acts/Utilities/TypeTraits.hpp"
0016
0017 #include <stdexcept>
0018 #include <string>
0019 #include <utility>
0020 #include <vector>
0021
0022 namespace Acts::Test {
0023
0024 BOOST_AUTO_TEST_SUITE(AnyGridViewTests)
0025
0026
0027 auto createIntGrid1D() {
0028 Axis a(0.0, 4.0, 4u);
0029 Grid g(Type<int>, a);
0030
0031
0032 for (std::size_t i = 0; i < g.size(); i++) {
0033 g.at(i) = static_cast<int>(i * 10);
0034 }
0035
0036 return g;
0037 }
0038
0039
0040 auto createDoubleGrid2D() {
0041 Axis a(0.0, 4.0, 4u);
0042 Axis b(0.0, 6.0, 3u);
0043 Grid g(Type<double>, std::move(a), std::move(b));
0044
0045
0046 for (std::size_t i = 0; i < g.size(); i++) {
0047 g.at(i) = static_cast<double>(i) * 1.5;
0048 }
0049
0050 return g;
0051 }
0052
0053
0054 auto createStringGrid2D() {
0055 Axis a(0.0, 4.0, 4u);
0056 Axis b(0.0, 6.0, 3u);
0057 Grid g(Type<std::string>, std::move(a), std::move(b));
0058
0059
0060 for (std::size_t i = 0; i < g.size(); i++) {
0061 g.at(i) = "Value_" + std::to_string(i);
0062 }
0063
0064 return g;
0065 }
0066
0067
0068 auto createVectorGrid3D() {
0069 Axis a(0.0, 4.0, 2u);
0070 Axis b(0.0, 6.0, 2u);
0071 Axis c(0.0, 8.0, 2u);
0072 Grid g(Type<std::vector<float>>, std::move(a), std::move(b), std::move(c));
0073
0074
0075 for (std::size_t i = 0; i < g.size(); i++) {
0076 g.at(i) = {static_cast<float>(i), static_cast<float>(i * 2)};
0077 }
0078
0079 return g;
0080 }
0081
0082
0083 BOOST_AUTO_TEST_CASE(IntGridReadWriteAccess) {
0084 auto grid = createIntGrid1D();
0085
0086
0087 AnyGridView<int> view(grid);
0088
0089
0090 BOOST_CHECK_EQUAL(view.atLocalBins({1}), 10);
0091 BOOST_CHECK_EQUAL(view.atLocalBins({2}), 20);
0092
0093
0094 view.atLocalBins({1}) = 100;
0095 BOOST_CHECK_EQUAL(view.atLocalBins({1}), 100);
0096 BOOST_CHECK_EQUAL(grid.atLocalBins({1}), 100);
0097 }
0098
0099
0100 BOOST_AUTO_TEST_CASE(IntGridReadOnlyAccess) {
0101 auto grid = createIntGrid1D();
0102
0103
0104 AnyGridConstView<int> constView(grid);
0105
0106
0107 BOOST_CHECK_EQUAL(constView.atLocalBins({1}), 10);
0108 BOOST_CHECK_EQUAL(constView.atLocalBins({2}), 20);
0109
0110
0111
0112 }
0113
0114
0115 BOOST_AUTO_TEST_CASE(CreateFromConcreteGrid) {
0116 auto doubleGrid = createDoubleGrid2D();
0117
0118
0119 AnyGridView<double> view(doubleGrid);
0120
0121
0122 BOOST_CHECK_EQUAL(view.dimensions(), 2u);
0123
0124
0125 BOOST_CHECK_CLOSE(view.atLocalBins({1, 1}),
0126 1.5 * doubleGrid.globalBinFromLocalBins({1, 1}), 1e-10);
0127
0128
0129 view.atLocalBins({1, 1}) = 42.0;
0130 BOOST_CHECK_CLOSE(doubleGrid.atLocalBins({1, 1}), 42.0, 1e-10);
0131 }
0132
0133
0134 BOOST_AUTO_TEST_CASE(CreateFromIGrid) {
0135 auto doubleGrid = createDoubleGrid2D();
0136 IGrid& iGrid = doubleGrid;
0137
0138
0139 AnyGridView<double> view(iGrid);
0140
0141
0142 BOOST_CHECK_EQUAL(view.dimensions(), 2u);
0143
0144
0145 BOOST_CHECK_CLOSE(view.atLocalBins({1, 1}),
0146 1.5 * doubleGrid.globalBinFromLocalBins({1, 1}), 1e-10);
0147 }
0148
0149
0150 BOOST_AUTO_TEST_CASE(CreateConstViewFromConstIGrid) {
0151 auto doubleGrid = createDoubleGrid2D();
0152 const IGrid& constIGrid = doubleGrid;
0153
0154
0155 AnyGridConstView<double> constView(constIGrid);
0156
0157
0158 BOOST_CHECK_EQUAL(constView.dimensions(), 2u);
0159
0160
0161 BOOST_CHECK_CLOSE(constView.atLocalBins({1, 1}),
0162 1.5 * doubleGrid.globalBinFromLocalBins({1, 1}), 1e-10);
0163 }
0164
0165
0166 BOOST_AUTO_TEST_CASE(TypeMismatchFromIGrid) {
0167 auto doubleGrid = createDoubleGrid2D();
0168 IGrid& iGrid = doubleGrid;
0169
0170
0171 BOOST_CHECK_THROW((AnyGridView<int>(iGrid)), std::invalid_argument);
0172 }
0173
0174
0175 BOOST_AUTO_TEST_CASE(TypeMismatchFromConstIGrid) {
0176 auto doubleGrid = createDoubleGrid2D();
0177 const IGrid& constIGrid = doubleGrid;
0178
0179
0180 BOOST_CHECK_THROW((AnyGridConstView<int>(constIGrid)), std::invalid_argument);
0181 }
0182
0183
0184 BOOST_AUTO_TEST_CASE(MutableViewFromMutableGrid) {
0185 auto stringGrid = createStringGrid2D();
0186
0187
0188 AnyGridView<std::string> view(stringGrid);
0189
0190
0191 view.atLocalBins({2, 2}) = "Modified";
0192
0193
0194 BOOST_CHECK_EQUAL(stringGrid.atLocalBins({2, 2}), "Modified");
0195 }
0196
0197
0198 BOOST_AUTO_TEST_CASE(ConstViewFromMutableGrid) {
0199 auto stringGrid = createStringGrid2D();
0200
0201
0202 AnyGridConstView<std::string> constView(stringGrid);
0203
0204
0205 BOOST_CHECK_EQUAL(constView.atLocalBins({2, 2}),
0206 stringGrid.atLocalBins({2, 2}));
0207
0208
0209
0210
0211 }
0212
0213
0214 BOOST_AUTO_TEST_CASE(ConstViewFromConstGrid) {
0215 const auto stringGrid = createStringGrid2D();
0216
0217
0218 AnyGridConstView<std::string> constView(stringGrid);
0219
0220
0221 BOOST_CHECK_EQUAL(constView.atLocalBins({2, 2}),
0222 stringGrid.atLocalBins({2, 2}));
0223 }
0224
0225
0226
0227 BOOST_AUTO_TEST_CASE(VectorTypeWithBothConstructions) {
0228 auto vectorGrid = createVectorGrid3D();
0229
0230
0231 AnyGridView<std::vector<float>> concreteView(vectorGrid);
0232 BOOST_CHECK_EQUAL(concreteView.dimensions(), 3u);
0233 BOOST_CHECK_EQUAL(concreteView.atLocalBins({1, 1, 1}).size(), 2u);
0234 BOOST_CHECK_EQUAL(concreteView.atLocalBins({1, 1, 1})[0],
0235 vectorGrid.atLocalBins({1, 1, 1})[0]);
0236
0237
0238 IGrid& iGrid = vectorGrid;
0239 AnyGridView<std::vector<float>> iGridView(iGrid);
0240 BOOST_CHECK_EQUAL(iGridView.dimensions(), 3u);
0241 BOOST_CHECK_EQUAL(iGridView.atLocalBins({1, 1, 1}).size(), 2u);
0242 BOOST_CHECK_EQUAL(iGridView.atLocalBins({1, 1, 1})[0],
0243 vectorGrid.atLocalBins({1, 1, 1})[0]);
0244
0245
0246 const IGrid& constIGrid = vectorGrid;
0247 AnyGridConstView<std::vector<float>> constIGridView(constIGrid);
0248 BOOST_CHECK_EQUAL(constIGridView.dimensions(), 3u);
0249 BOOST_CHECK_EQUAL(constIGridView.atLocalBins({1, 1, 1}).size(), 2u);
0250 BOOST_CHECK_EQUAL(constIGridView.atLocalBins({1, 1, 1})[0],
0251 vectorGrid.atLocalBins({1, 1, 1})[0]);
0252
0253
0254 std::vector<float> newValue = {99.0f, 88.0f};
0255 concreteView.atLocalBins({1, 1, 1}) = newValue;
0256 BOOST_CHECK_EQUAL(vectorGrid.atLocalBins({1, 1, 1})[0], 99.0f);
0257 BOOST_CHECK_EQUAL(vectorGrid.atLocalBins({1, 1, 1})[1], 88.0f);
0258 }
0259
0260
0261 BOOST_AUTO_TEST_CASE(GridPropertiesAccess) {
0262 auto doubleGrid = createDoubleGrid2D();
0263 AnyGridView<double> view(doubleGrid);
0264
0265
0266 BOOST_CHECK_EQUAL(view.dimensions(), 2u);
0267
0268
0269 auto center = view.binCenter({1, 1});
0270 auto expectedCenter = doubleGrid.binCenter({1, 1});
0271 BOOST_CHECK_CLOSE(center[0], expectedCenter[0], 1e-10);
0272 BOOST_CHECK_CLOSE(center[1], expectedCenter[1], 1e-10);
0273
0274
0275 auto lowerLeft = view.lowerLeftBinEdge({1, 1});
0276 auto expectedLowerLeft = doubleGrid.lowerLeftBinEdge({1, 1});
0277 BOOST_CHECK_CLOSE(lowerLeft[0], expectedLowerLeft[0], 1e-10);
0278 BOOST_CHECK_CLOSE(lowerLeft[1], expectedLowerLeft[1], 1e-10);
0279
0280
0281 auto upperRight = view.upperRightBinEdge({1, 1});
0282 auto expectedUpperRight = doubleGrid.upperRightBinEdge({1, 1});
0283 BOOST_CHECK_CLOSE(upperRight[0], expectedUpperRight[0], 1e-10);
0284 BOOST_CHECK_CLOSE(upperRight[1], expectedUpperRight[1], 1e-10);
0285
0286
0287 auto numBins = view.numLocalBins();
0288 auto expectedNumBins = doubleGrid.numLocalBins();
0289 BOOST_CHECK_EQUAL(numBins[0], expectedNumBins[0]);
0290 BOOST_CHECK_EQUAL(numBins[1], expectedNumBins[1]);
0291 }
0292
0293
0294 BOOST_AUTO_TEST_CASE(GridPropertiesAccessConstView) {
0295 auto doubleGrid = createDoubleGrid2D();
0296 const IGrid& constIGrid = doubleGrid;
0297 AnyGridConstView<double> constView(constIGrid);
0298
0299
0300 BOOST_CHECK_EQUAL(constView.dimensions(), 2u);
0301
0302
0303 auto center = constView.binCenter({1, 1});
0304 auto expectedCenter = doubleGrid.binCenter({1, 1});
0305 BOOST_CHECK_CLOSE(center[0], expectedCenter[0], 1e-10);
0306 BOOST_CHECK_CLOSE(center[1], expectedCenter[1], 1e-10);
0307
0308
0309 auto lowerLeft = constView.lowerLeftBinEdge({1, 1});
0310 auto expectedLowerLeft = doubleGrid.lowerLeftBinEdge({1, 1});
0311 BOOST_CHECK_CLOSE(lowerLeft[0], expectedLowerLeft[0], 1e-10);
0312 BOOST_CHECK_CLOSE(lowerLeft[1], expectedLowerLeft[1], 1e-10);
0313
0314
0315 auto upperRight = constView.upperRightBinEdge({1, 1});
0316 auto expectedUpperRight = doubleGrid.upperRightBinEdge({1, 1});
0317 BOOST_CHECK_CLOSE(upperRight[0], expectedUpperRight[0], 1e-10);
0318 BOOST_CHECK_CLOSE(upperRight[1], expectedUpperRight[1], 1e-10);
0319
0320
0321 auto numBins = constView.numLocalBins();
0322 auto expectedNumBins = doubleGrid.numLocalBins();
0323 BOOST_CHECK_EQUAL(numBins[0], expectedNumBins[0]);
0324 BOOST_CHECK_EQUAL(numBins[1], expectedNumBins[1]);
0325 }
0326
0327
0328 BOOST_AUTO_TEST_CASE(ErrorCases) {
0329 auto intGrid = createIntGrid1D();
0330 auto doubleGrid = createDoubleGrid2D();
0331
0332
0333 AnyGridView<int> intView(intGrid);
0334 BOOST_CHECK_THROW(intView.atLocalBins({1, 2}), std::invalid_argument);
0335
0336
0337 BOOST_CHECK_THROW(intView.atLocalBins({10}), std::out_of_range);
0338
0339
0340 IGrid& iGrid = doubleGrid;
0341 BOOST_CHECK_THROW(AnyGridView<std::string>{iGrid}, std::invalid_argument);
0342
0343
0344 const IGrid& constIGrid = doubleGrid;
0345 BOOST_CHECK_THROW(AnyGridConstView<std::string>{constIGrid},
0346 std::invalid_argument);
0347 }
0348
0349
0350 BOOST_AUTO_TEST_CASE(CopyOperations) {
0351
0352 auto doubleGrid1 = createDoubleGrid2D();
0353 auto doubleGrid2 = createDoubleGrid2D();
0354 auto doubleGrid3 = createDoubleGrid2D();
0355
0356
0357 AnyGridView<double> view1(doubleGrid1);
0358 AnyGridView<double> view2(view1);
0359 BOOST_CHECK_CLOSE(view1.atLocalBins({1, 1}), view2.atLocalBins({1, 1}),
0360 1e-10);
0361
0362
0363 AnyGridView<double> view3(doubleGrid2);
0364 view3 = view1;
0365 BOOST_CHECK_CLOSE(view1.atLocalBins({1, 1}), view3.atLocalBins({1, 1}),
0366 1e-10);
0367
0368
0369 AnyGridConstView<double> constView1(doubleGrid1);
0370 AnyGridConstView<double> constView2(constView1);
0371 BOOST_CHECK_CLOSE(constView1.atLocalBins({1, 1}),
0372 constView2.atLocalBins({1, 1}), 1e-10);
0373
0374 auto doubleGrid4 = createDoubleGrid2D();
0375 AnyGridConstView<double> constView3(doubleGrid4);
0376 constView3 = constView1;
0377 BOOST_CHECK_CLOSE(constView1.atLocalBins({1, 1}),
0378 constView3.atLocalBins({1, 1}), 1e-10);
0379
0380
0381 const IGrid& constIGrid = doubleGrid1;
0382 AnyGridConstView<double> constView4(constIGrid);
0383 AnyGridConstView<double> constView5(constView4);
0384 BOOST_CHECK_CLOSE(constView4.atLocalBins({1, 1}),
0385 constView5.atLocalBins({1, 1}), 1e-10);
0386 }
0387
0388 BOOST_AUTO_TEST_CASE(TypeDeduction) {
0389 auto grid = createIntGrid1D();
0390 AnyGridView view(grid);
0391 static_assert(std::is_same_v<decltype(view), AnyGridView<int>>);
0392
0393 const auto constGrid = createIntGrid1D();
0394 AnyGridConstView<int> constView(constGrid);
0395 static_assert(std::is_same_v<decltype(constView), AnyGridConstView<int>>);
0396 }
0397
0398 BOOST_AUTO_TEST_SUITE_END()
0399
0400 }