Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:38

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Geometry/GeometryHierarchyMap.hpp"
0012 #include "Acts/Geometry/GeometryIdentifier.hpp"
0013 
0014 #include <iterator>
0015 #include <stdexcept>
0016 #include <utility>
0017 #include <vector>
0018 
0019 namespace {
0020 
0021 using Acts::GeometryIdentifier;
0022 
0023 // helper function to create geometry ids
0024 GeometryIdentifier makeId(int volume = 0, int layer = 0, int sensitive = 0) {
0025   return GeometryIdentifier().setVolume(volume).setLayer(layer).setSensitive(
0026       sensitive);
0027 }
0028 
0029 // example value type stored in the geometry hierarchy map
0030 struct Thing {
0031   double value = 1.0;
0032 };
0033 
0034 using Container = Acts::GeometryHierarchyMap<Thing>;
0035 
0036 }  // namespace
0037 
0038 // check that an entry exists for the query and compare the id
0039 #define CHECK_ENTRY(container, query, compare)          \
0040   do {                                                  \
0041     auto ret = container.find(query);                   \
0042     BOOST_CHECK_NE(ret, container.end());               \
0043     if (ret != container.end()) {                       \
0044       auto idx = std::distance(container.begin(), ret); \
0045       BOOST_CHECK_EQUAL(container.idAt(idx), compare);  \
0046     }                                                   \
0047   } while (false)
0048 
0049 BOOST_TEST_DONT_PRINT_LOG_VALUE(Container::Iterator)
0050 BOOST_TEST_DONT_PRINT_LOG_VALUE(Thing)
0051 
0052 BOOST_AUTO_TEST_SUITE(GeometryHierarchyMap)
0053 
0054 BOOST_AUTO_TEST_CASE(ConstructDefault) {
0055   Container c;
0056   BOOST_CHECK_EQUAL(c.begin(), c.end());
0057   BOOST_CHECK(c.empty());
0058   BOOST_CHECK_EQUAL(c.size(), 0u);
0059 }
0060 
0061 BOOST_AUTO_TEST_CASE(ConstructNonUnique) {
0062   std::vector<std::pair<GeometryIdentifier, Thing>> entries = {
0063       {makeId(2, 4, 6), {1.0}},
0064       {makeId(3, 5), {1.0}},
0065       {makeId(3), {1.0}},
0066       // duplicate identifier
0067       {makeId(2, 4, 6), {2.0}},
0068   };
0069   BOOST_CHECK_THROW(Container(std::move(entries)), std::invalid_argument);
0070 
0071   std::vector<std::pair<GeometryIdentifier, Thing>> defaults = {
0072       {makeId(), {1.0}},
0073       // duplicate global default
0074       {makeId(), {2.0}},
0075   };
0076   BOOST_CHECK_THROW(Container(std::move(defaults)), std::invalid_argument);
0077 }
0078 
0079 BOOST_AUTO_TEST_CASE(ConstructInitializerList) {
0080   Container c = {
0081       {makeId(0, 1, 2), {1.0}},
0082       {makeId(3, 4), {3.0}},
0083       {makeId(2), {2.0}},
0084       {makeId(), {23.0}},
0085   };
0086   BOOST_CHECK_EQUAL(std::next(c.begin(), 4), c.end());
0087   BOOST_CHECK(!c.empty());
0088   BOOST_CHECK_EQUAL(c.size(), 4u);
0089   // only test that all elements are there; failure test are below
0090   CHECK_ENTRY(c, makeId(0, 1, 2), makeId(0, 1, 2));
0091   CHECK_ENTRY(c, makeId(2), makeId(2));
0092   CHECK_ENTRY(c, makeId(3, 4), makeId(3, 4));
0093 }
0094 
0095 BOOST_AUTO_TEST_CASE(IndexBasedAccess) {
0096   Container c({
0097       {makeId(1, 2, 3), {2.0}},
0098       {makeId(3, 4, 5), {2.5}},
0099       {makeId(3, 5), {3.0}},
0100       {makeId(4, 5, 7), {4.0}},
0101   });
0102 
0103   BOOST_CHECK(!c.empty());
0104   BOOST_CHECK_EQUAL(c.size(), 4u);
0105   // this tests just that the index-based access works
0106   // NOTE order is undefined and should not be tested
0107   for (auto i = c.size(); 0 < i--;) {
0108     // just check that the id is valid
0109     BOOST_CHECK_NE(c.idAt(i), GeometryIdentifier());
0110     // check that something is actually stored by comparing with the default
0111     BOOST_CHECK_NE(c.valueAt(i).value, Thing().value);
0112   }
0113   // test that invalid inputs actually fail
0114   BOOST_CHECK_THROW(c.idAt(c.size()), std::out_of_range);
0115   BOOST_CHECK_THROW(c.valueAt(c.size()), std::out_of_range);
0116 }
0117 
0118 BOOST_AUTO_TEST_CASE(Find) {
0119   Container c = {
0120       // entry for volume 2, layer 4, sensitive 6
0121       {makeId(2, 4, 6), {-23.0}},
0122       // entry for volume 2, layer 8
0123       {makeId(2, 8), {5.0}},
0124       // entry for the whole volume 2
0125       {makeId(2), {1.0}},
0126       // entry for volume 12, layer 16
0127       // NOTE no entry for the volume as a whole
0128       {makeId(12, 16), {-1.0}},
0129   };
0130 
0131   // basic checks
0132   BOOST_CHECK_EQUAL(std::next(c.begin(), 4u), c.end());
0133   BOOST_CHECK(!c.empty());
0134   BOOST_CHECK_EQUAL(c.size(), 4u);
0135 
0136   // find existing sensitive
0137   CHECK_ENTRY(c, makeId(2, 4, 6), makeId(2, 4, 6));
0138   // find existing layer
0139   CHECK_ENTRY(c, makeId(2, 8), makeId(2, 8));
0140   // find existing volume
0141   CHECK_ENTRY(c, makeId(2), makeId(2));
0142   // find existing layer
0143   CHECK_ENTRY(c, makeId(12, 16), makeId(12, 16));
0144 
0145   // find non-existing sensitive, which has a set volume
0146   CHECK_ENTRY(c, makeId(2, 4, 7), makeId(2));
0147   // find non-existing layer id, which has a set volume
0148   CHECK_ENTRY(c, makeId(2, 13), makeId(2));
0149   // find non-existing sensitive id, which has a set layer
0150   CHECK_ENTRY(c, makeId(2, 8, 13), makeId(2, 8));
0151   // find non-existing sensitive, which has a set layer
0152   CHECK_ENTRY(c, makeId(12, 16, 20), makeId(12, 16));
0153 
0154   // find non-existing sensitive, which has no higher hierarchy set
0155   BOOST_CHECK_EQUAL(c.find(makeId(3, 5, 7)), c.end());
0156   // find non-existing layer, which has no higher hierarchy set
0157   BOOST_CHECK_EQUAL(c.find(makeId(3, 5)), c.end());
0158   // find non-existing volume
0159   BOOST_CHECK_EQUAL(c.find(makeId(3)), c.end());
0160   // find non-existing volume, which has only lower hierarchy elements
0161   BOOST_CHECK_EQUAL(c.find(makeId(12)), c.end());
0162 }
0163 
0164 BOOST_AUTO_TEST_CASE(FindWithGlobalDefault) {
0165   Container c = {
0166       // global default entry
0167       {makeId(), {1.0}},
0168       // entry for volume 2, layer 3
0169       {makeId(2, 3), {2.0}},
0170       // entry for volume 4
0171       {makeId(4), {4.0}},
0172   };
0173 
0174   // basic checks
0175   BOOST_CHECK_EQUAL(std::next(c.begin(), 3u), c.end());
0176   BOOST_CHECK(!c.empty());
0177   BOOST_CHECK_EQUAL(c.size(), 3u);
0178 
0179   // find existing entries
0180   CHECK_ENTRY(c, makeId(), makeId());
0181   CHECK_ENTRY(c, makeId(2, 3), makeId(2, 3));
0182   CHECK_ENTRY(c, makeId(4), makeId(4));
0183 
0184   // find missing sensitive w/ set layer
0185   CHECK_ENTRY(c, makeId(2, 3, 4), makeId(2, 3));
0186   // find missing layer w/ set volume
0187   CHECK_ENTRY(c, makeId(4, 5), makeId(4));
0188 
0189   // find missing sensitive w/o set volume/layer -> global default
0190   CHECK_ENTRY(c, makeId(2, 4, 5), makeId());
0191   // find missing layer w/o set volume -> global default
0192   CHECK_ENTRY(c, makeId(2, 4), makeId());
0193   // find missing volume
0194   CHECK_ENTRY(c, makeId(5), makeId());
0195 }
0196 
0197 BOOST_AUTO_TEST_SUITE_END()