File indexing completed on 2026-04-19 07:48:51
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/tools/old/interface.hpp>
0010 #include <boost/test/unit_test.hpp>
0011
0012 #include "Acts/Utilities/detail/ContainerIterator.hpp"
0013 #include "Acts/Utilities/detail/ContainerRange.hpp"
0014 #include "Acts/Utilities/detail/ContainerSubset.hpp"
0015
0016 #include <set>
0017
0018 using namespace Acts;
0019 using namespace Acts::detail;
0020
0021 namespace ActsTests {
0022
0023 class Container {
0024 public:
0025 using Index = std::size_t;
0026 using IndexRange = std::pair<std::size_t, std::size_t>;
0027 using SubsetIndices = std::span<const std::size_t>;
0028 using OwningSubsetIndices = std::vector<std::size_t>;
0029 using OwningOrderedSubsetIndices = std::set<std::size_t>;
0030
0031 Container() = default;
0032
0033 Container(std::initializer_list<int> init) : m_data(init) {}
0034
0035 int& operator[](std::size_t index) { return m_data[index]; }
0036
0037 int operator[](std::size_t index) const { return m_data[index]; }
0038
0039 int& at(std::size_t index) {
0040 if (index >= m_data.size()) {
0041 throw std::out_of_range("Index out of bounds");
0042 }
0043 return m_data.at(index);
0044 }
0045
0046 int at(std::size_t index) const {
0047 if (index >= m_data.size()) {
0048 throw std::out_of_range("Index out of bounds");
0049 }
0050 return m_data.at(index);
0051 }
0052
0053 template <bool ReadOnly>
0054 using Iterator = ContainerIterator<Container, int, std::size_t, ReadOnly>;
0055
0056 Iterator<false> begin() { return {*this, 0}; }
0057
0058 Iterator<false> end() { return {*this, m_data.size()}; }
0059
0060 Iterator<true> begin() const { return {*this, 0}; }
0061
0062 Iterator<true> end() const { return {*this, m_data.size()}; }
0063
0064 template <bool ReadOnly>
0065 class Range : public ContainerRange<Range<ReadOnly>, Range<true>, Container,
0066 std::size_t, ReadOnly> {
0067 public:
0068 using Base = ContainerRange<Range<ReadOnly>, Range<true>, Container,
0069 std::size_t, ReadOnly>;
0070
0071 using Base::Base;
0072 };
0073
0074 Range<false> range(const IndexRange& indexRange) {
0075 return {*this, indexRange};
0076 }
0077
0078 Range<true> range(const IndexRange& indexRange) const {
0079 return {*this, indexRange};
0080 }
0081
0082 template <bool ReadOnly>
0083 class Subset
0084 : public ContainerSubset<Subset<ReadOnly>, Subset<true>, Container, int,
0085 SubsetIndices, ReadOnly> {
0086 public:
0087 using Base = ContainerSubset<Subset<ReadOnly>, Subset<true>, Container, int,
0088 SubsetIndices, ReadOnly>;
0089
0090 using Base::Base;
0091 };
0092
0093 Subset<false> subset(SubsetIndices indices) { return {*this, indices}; }
0094
0095 Subset<true> subset(SubsetIndices indices) const { return {*this, indices}; }
0096
0097 template <bool ReadOnly>
0098 class OwningSubset
0099 : public ContainerSubset<OwningSubset<ReadOnly>, OwningSubset<true>,
0100 Container, int, OwningSubsetIndices, ReadOnly> {
0101 public:
0102 using Base = ContainerSubset<OwningSubset<ReadOnly>, OwningSubset<true>,
0103 Container, int, OwningSubsetIndices, ReadOnly>;
0104
0105 using Base::Base;
0106 };
0107
0108 OwningSubset<false> owningSubset(OwningSubsetIndices indices) {
0109 return {*this, std::move(indices)};
0110 }
0111
0112 OwningSubset<true> owningSubset(OwningSubsetIndices indices) const {
0113 return {*this, std::move(indices)};
0114 }
0115
0116 template <bool ReadOnly>
0117 class OwningOrderedSubset
0118 : public ContainerSubset<OwningOrderedSubset<ReadOnly>,
0119 OwningOrderedSubset<true>, Container, int,
0120 OwningOrderedSubsetIndices, ReadOnly> {
0121 public:
0122 using Base = ContainerSubset<OwningOrderedSubset<ReadOnly>,
0123 OwningOrderedSubset<true>, Container, int,
0124 OwningOrderedSubsetIndices, ReadOnly>;
0125
0126 using Base::Base;
0127 };
0128
0129 OwningOrderedSubset<false> owningOrderedSubset(
0130 OwningOrderedSubsetIndices indices) {
0131 return {*this, std::move(indices)};
0132 }
0133
0134 OwningOrderedSubset<true> owningOrderedSubset(
0135 OwningOrderedSubsetIndices indices) const {
0136 return {*this, std::move(indices)};
0137 }
0138
0139 private:
0140 std::vector<int> m_data;
0141 };
0142
0143 BOOST_AUTO_TEST_SUITE(ContainerHelpersTests)
0144
0145 BOOST_AUTO_TEST_CASE(All) {
0146 Container mutableContainer{10, 20, 30, 40, 50};
0147 const Container& constContainer = mutableContainer;
0148
0149 {
0150 std::vector<int> expected{10, 20, 30, 40, 50};
0151 std::vector<int> actual;
0152 for (const int i : mutableContainer) {
0153 actual.push_back(i);
0154 }
0155 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0156 actual.begin(), actual.end());
0157 }
0158
0159 {
0160 std::vector<int> expected{10, 20, 30, 40, 50};
0161 std::vector<int> actual;
0162 for (const int i : constContainer) {
0163 actual.push_back(i);
0164 }
0165 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0166 actual.begin(), actual.end());
0167 }
0168
0169 {
0170 std::vector<int> expected{20, 30, 40};
0171 std::vector<int> actual;
0172 for (const int i : mutableContainer.range({1, 4})) {
0173 actual.push_back(i);
0174 }
0175 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0176 actual.begin(), actual.end());
0177 }
0178
0179 {
0180 std::vector<int> expected{20, 30, 40};
0181 std::vector<int> actual;
0182 for (const int i : constContainer.range({1, 4})) {
0183 actual.push_back(i);
0184 }
0185 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0186 actual.begin(), actual.end());
0187 }
0188
0189 {
0190 std::vector<std::size_t> indices{0, 2, 4};
0191 std::vector<int> expected{10, 30, 50};
0192 std::vector<int> actual;
0193 for (const int i : mutableContainer.subset(indices)) {
0194 actual.push_back(i);
0195 }
0196 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0197 actual.begin(), actual.end());
0198
0199 BOOST_CHECK_EQUAL(2, mutableContainer.subset(indices).subrange(1).size());
0200 BOOST_CHECK_EQUAL(1,
0201 mutableContainer.subset(indices).subrange(1, 1).size());
0202 }
0203
0204 {
0205 std::vector<std::size_t> indices{0, 2, 4};
0206 std::vector<int> expected{10, 30, 50};
0207 std::vector<int> actual;
0208 for (const int i : constContainer.subset(indices)) {
0209 actual.push_back(i);
0210 }
0211 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0212 actual.begin(), actual.end());
0213
0214 BOOST_CHECK_EQUAL(2, constContainer.subset(indices).subrange(1).size());
0215 BOOST_CHECK_EQUAL(1, constContainer.subset(indices).subrange(1, 1).size());
0216 }
0217
0218 {
0219 std::vector<std::size_t> indices{0, 2, 4};
0220 std::vector<int> expected{10, 30, 50};
0221 std::vector<int> actual;
0222 for (const int i : mutableContainer.owningSubset(indices)) {
0223 actual.push_back(i);
0224 }
0225 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0226 actual.begin(), actual.end());
0227
0228 BOOST_CHECK_EQUAL(
0229 2, mutableContainer.owningSubset(indices).subrange(1).size());
0230 BOOST_CHECK_EQUAL(
0231 1, mutableContainer.owningSubset(indices).subrange(1, 1).size());
0232 }
0233
0234 {
0235 std::vector<std::size_t> indices{0, 2, 4};
0236 std::vector<int> expected{10, 30, 50};
0237 std::vector<int> actual;
0238 for (const int i : constContainer.owningSubset(indices)) {
0239 actual.push_back(i);
0240 }
0241 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0242 actual.begin(), actual.end());
0243
0244 BOOST_CHECK_EQUAL(2,
0245 constContainer.owningSubset(indices).subrange(1).size());
0246 BOOST_CHECK_EQUAL(
0247 1, constContainer.owningSubset(indices).subrange(1, 1).size());
0248 }
0249
0250 {
0251 std::set<std::size_t> indices{0, 2, 4};
0252 std::vector<int> expected{10, 30, 50};
0253 std::vector<int> actual;
0254 for (const int i : mutableContainer.owningOrderedSubset(indices)) {
0255 actual.push_back(i);
0256 }
0257 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0258 actual.begin(), actual.end());
0259
0260 BOOST_CHECK_EQUAL(
0261 2, mutableContainer.owningOrderedSubset(indices).subrange(1).size());
0262 BOOST_CHECK_EQUAL(
0263 1, mutableContainer.owningOrderedSubset(indices).subrange(1, 1).size());
0264 }
0265
0266 {
0267 std::set<std::size_t> indices{0, 2, 4};
0268 std::vector<int> expected{10, 30, 50};
0269 std::vector<int> actual;
0270 for (const int i : constContainer.owningOrderedSubset(indices)) {
0271 actual.push_back(i);
0272 }
0273 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
0274 actual.begin(), actual.end());
0275
0276 BOOST_CHECK_EQUAL(
0277 2, constContainer.owningOrderedSubset(indices).subrange(1).size());
0278 BOOST_CHECK_EQUAL(
0279 1, constContainer.owningOrderedSubset(indices).subrange(1, 1).size());
0280 }
0281 }
0282
0283 BOOST_AUTO_TEST_SUITE_END()
0284
0285 }