File indexing completed on 2025-06-30 07:53:33
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/tools/old/interface.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 #include <boost/test/unit_test_suite.hpp>
0012
0013 #include "Acts/Utilities/TransformRange.hpp"
0014
0015 #include <algorithm>
0016 #include <ranges>
0017
0018 using namespace Acts;
0019
0020 BOOST_AUTO_TEST_SUITE(TransformRangeTests)
0021
0022 auto checkSameAddresses = [](const auto& orig, auto& act) {
0023 BOOST_CHECK_EQUAL(orig.size(), act.size());
0024 auto it = act.begin();
0025 for (std::size_t i = 0; i < orig.size(); ++i) {
0026 BOOST_CHECK_EQUAL(orig.at(i).get(), &act.at(i));
0027 BOOST_CHECK_EQUAL(orig.at(i).get(), &act[i]);
0028 BOOST_CHECK_EQUAL(*orig.at(i), act.at(i));
0029
0030 BOOST_CHECK_EQUAL(orig.at(i).get(), &*it);
0031 BOOST_CHECK_EQUAL(*orig.at(i).get(), *it);
0032
0033 ++it;
0034 }
0035
0036 BOOST_CHECK(it == act.end());
0037 };
0038
0039 BOOST_AUTO_TEST_CASE(TransformRangeDeref) {
0040 {
0041 std::vector<std::unique_ptr<int>> v;
0042 v.push_back(std::make_unique<int>(1));
0043 v.push_back(std::make_unique<int>(2));
0044 v.push_back(std::make_unique<int>(3));
0045
0046 {
0047 auto r = detail::TransformRange{detail::Dereference{}, v};
0048 static_assert(std::is_same_v<decltype(r)::value_type, int>);
0049 static_assert(std::is_same_v<decltype(r)::reference, int&>);
0050 static_assert(std::is_same_v<decltype(r)::const_reference, const int&>);
0051 static_assert(std::is_same_v<decltype(r.at(0)), int&>);
0052 static_assert(std::is_same_v<decltype(r[0]), int&>);
0053 static_assert(std::is_same_v<decltype(*r.begin()), int&>);
0054 static_assert(std::is_same_v<decltype(*r.cbegin()), const int&>);
0055
0056 r.at(2) = 4;
0057 BOOST_CHECK_EQUAL(*v.at(2), 4);
0058
0059 checkSameAddresses(v, r);
0060
0061 const auto& r_cref = r;
0062 static_assert(std::is_same_v<decltype(r_cref.at(0)), const int&>);
0063 static_assert(std::is_same_v<decltype(r_cref[0]), const int&>);
0064 static_assert(std::is_same_v<decltype(*r_cref.begin()), const int&>);
0065 static_assert(std::is_same_v<decltype(*(++r_cref.begin())), const int&>);
0066 checkSameAddresses(v, r_cref);
0067
0068 auto cr = detail::TransformRange{detail::ConstDereference{}, v};
0069 static_assert(std::is_same_v<decltype(cr)::value_type, const int>);
0070 static_assert(std::is_same_v<decltype(cr)::reference, const int&>);
0071 static_assert(std::is_same_v<decltype(cr)::const_reference, const int&>);
0072 static_assert(std::is_same_v<decltype(cr.at(0)), const int&>);
0073 static_assert(std::is_same_v<decltype(cr[0]), const int&>);
0074 static_assert(std::is_same_v<decltype(*cr.begin()), const int&>);
0075 static_assert(std::is_same_v<decltype(*(++cr.begin())), const int&>);
0076 checkSameAddresses(v, cr);
0077
0078 const auto& cr_cref = cr;
0079 static_assert(std::is_same_v<decltype(cr_cref.at(0)), const int&>);
0080 static_assert(std::is_same_v<decltype(cr_cref[0]), const int&>);
0081 static_assert(std::is_same_v<decltype(*cr_cref.begin()), const int&>);
0082 static_assert(std::is_same_v<decltype(*(++cr_cref.begin())), const int&>);
0083 checkSameAddresses(v, cr_cref);
0084
0085 std::vector<int> act;
0086 std::copy(cr_cref.begin(), cr_cref.end(), std::back_inserter(act));
0087 std::vector<int> exp = {1, 2, 4};
0088 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), act.begin(),
0089 act.end());
0090 }
0091
0092 std::vector<int*> raw_v;
0093 for (auto& ptr : v) {
0094 raw_v.push_back(ptr.get());
0095 }
0096 {
0097 auto r = detail::TransformRange{detail::Dereference{}, raw_v};
0098 static_assert(std::is_same_v<decltype(r)::value_type, int>);
0099 static_assert(std::is_same_v<decltype(r)::reference, int&>);
0100 static_assert(std::is_same_v<decltype(r)::const_reference, const int&>);
0101 static_assert(std::is_same_v<decltype(r.at(0)), int&>);
0102 static_assert(std::is_same_v<decltype(r[0]), int&>);
0103 static_assert(std::is_same_v<decltype(*r.begin()), int&>);
0104 static_assert(std::is_same_v<decltype(*(++r.begin())), int&>);
0105 checkSameAddresses(v, r);
0106
0107 std::vector<int> unpacked;
0108 std::ranges::transform(r, std::back_inserter(unpacked),
0109 [](auto val) { return val; });
0110 std::vector<int> exp = {1, 2, 4};
0111
0112 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), unpacked.begin(),
0113 unpacked.end());
0114
0115 auto cr = detail::TransformRange{detail::ConstDereference{}, raw_v};
0116 static_assert(std::is_same_v<decltype(cr)::value_type, const int>);
0117 static_assert(std::is_same_v<decltype(cr)::reference, const int&>);
0118 static_assert(std::is_same_v<decltype(cr)::const_reference, const int&>);
0119 static_assert(std::is_same_v<decltype(cr.at(0)), const int&>);
0120 static_assert(std::is_same_v<decltype(cr[0]), const int&>);
0121 static_assert(std::is_same_v<decltype(*cr.begin()), const int&>);
0122 static_assert(std::is_same_v<decltype(*(++cr.begin())), const int&>);
0123 checkSameAddresses(v, r);
0124
0125 unpacked.clear();
0126 std::ranges::transform(cr, std::back_inserter(unpacked),
0127 [](auto val) { return val; });
0128
0129 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), unpacked.begin(),
0130 unpacked.end());
0131 }
0132 }
0133
0134 {
0135 std::vector<std::unique_ptr<const int>> v;
0136 v.push_back(std::make_unique<const int>(1));
0137 v.push_back(std::make_unique<const int>(2));
0138 v.push_back(std::make_unique<const int>(3));
0139
0140 {
0141 auto r = detail::TransformRange{detail::Dereference{}, v};
0142 static_assert(std::is_same_v<decltype(r)::value_type, const int>);
0143 static_assert(std::is_same_v<decltype(r)::reference, const int&>);
0144 static_assert(std::is_same_v<decltype(r)::const_reference, const int&>);
0145 static_assert(std::is_same_v<decltype(r.at(0)), const int&>);
0146 static_assert(std::is_same_v<decltype(r[0]), const int&>);
0147 static_assert(std::is_same_v<decltype(*r.begin()), const int&>);
0148 static_assert(std::is_same_v<decltype(*(++r.begin())), const int&>);
0149 checkSameAddresses(v, r);
0150
0151 std::vector<int> unpacked;
0152 std::ranges::transform(r, std::back_inserter(unpacked),
0153 [](auto val) { return val; });
0154
0155 BOOST_CHECK(unpacked == std::vector<int>({1, 2, 3}));
0156 }
0157
0158 std::vector<const int*> raw_v;
0159 for (auto& ptr : v) {
0160 raw_v.push_back(ptr.get());
0161 }
0162 {
0163 auto r = detail::TransformRange{detail::Dereference{}, raw_v};
0164 static_assert(std::is_same_v<decltype(r)::value_type, const int>);
0165 static_assert(std::is_same_v<decltype(r)::reference, const int&>);
0166 static_assert(std::is_same_v<decltype(r)::const_reference, const int&>);
0167 static_assert(std::is_same_v<decltype(r.at(0)), const int&>);
0168 static_assert(std::is_same_v<decltype(r[0]), const int&>);
0169 static_assert(std::is_same_v<decltype(*r.begin()), const int&>);
0170 static_assert(std::is_same_v<decltype(*(++r.begin())), const int&>);
0171 checkSameAddresses(v, r);
0172 }
0173 }
0174 }
0175
0176 BOOST_AUTO_TEST_CASE(TransformRangeFromConstRef) {
0177 std::vector<std::unique_ptr<int>> v;
0178 v.push_back(std::make_unique<int>(1));
0179 v.push_back(std::make_unique<int>(2));
0180 v.push_back(std::make_unique<int>(3));
0181
0182 const auto& cv = v;
0183
0184 {
0185 auto r_cv = detail::TransformRange{detail::Dereference{}, cv};
0186 static_assert(std::is_same_v<decltype(r_cv)::value_type, const int>);
0187 static_assert(std::is_same_v<decltype(r_cv)::reference, const int&>);
0188 static_assert(std::is_same_v<decltype(r_cv)::const_reference, const int&>);
0189 static_assert(std::is_same_v<decltype(r_cv.at(0)), const int&>);
0190 static_assert(std::is_same_v<decltype(r_cv[0]), const int&>);
0191 static_assert(std::is_same_v<decltype(*r_cv.begin()), const int&>);
0192 static_assert(std::is_same_v<decltype(*(++r_cv.begin())), const int&>);
0193
0194 checkSameAddresses(v, r_cv);
0195 checkSameAddresses(cv, r_cv);
0196
0197 std::vector<int> act;
0198 std::copy(r_cv.begin(), r_cv.end(), std::back_inserter(act));
0199 std::vector<int> exp = {1, 2, 3};
0200 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), act.begin(),
0201 act.end());
0202 }
0203
0204 {
0205 auto r_cv = detail::TransformRange{detail::ConstDereference{}, cv};
0206 static_assert(std::is_same_v<decltype(r_cv)::value_type, const int>);
0207 static_assert(std::is_same_v<decltype(r_cv)::reference, const int&>);
0208 static_assert(std::is_same_v<decltype(r_cv)::const_reference, const int&>);
0209 static_assert(std::is_same_v<decltype(r_cv.at(0)), const int&>);
0210 static_assert(std::is_same_v<decltype(r_cv[0]), const int&>);
0211 static_assert(std::is_same_v<decltype(*r_cv.begin()), const int&>);
0212 static_assert(std::is_same_v<decltype(*(++r_cv.begin())), const int&>);
0213
0214 checkSameAddresses(v, r_cv);
0215 checkSameAddresses(cv, r_cv);
0216
0217 std::vector<int> act;
0218 std::copy(r_cv.begin(), r_cv.end(), std::back_inserter(act));
0219 std::vector<int> exp = {1, 2, 3};
0220 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), act.begin(),
0221 act.end());
0222 }
0223 }
0224
0225 BOOST_AUTO_TEST_CASE(TransformRangeReferenceWrappers) {
0226 int a = 5;
0227 int b = 6;
0228 int c = 7;
0229
0230 std::vector<std::reference_wrapper<int>> v = {a, b, c};
0231 BOOST_CHECK_EQUAL(&v[0].get(), &a);
0232
0233 auto r = detail::TransformRange{detail::DotGet{}, v};
0234 static_assert(std::is_same_v<decltype(r)::value_type, int>);
0235 static_assert(std::is_same_v<decltype(r)::reference, int&>);
0236 static_assert(std::is_same_v<decltype(r)::const_reference, const int&>);
0237 static_assert(std::is_same_v<decltype(r.at(0)), int&>);
0238 static_assert(std::is_same_v<decltype(r[0]), int&>);
0239 static_assert(std::is_same_v<decltype(*r.begin()), int&>);
0240 static_assert(std::is_same_v<decltype(*(++r.begin())), int&>);
0241
0242 BOOST_CHECK_EQUAL(r.at(0), 5);
0243 BOOST_CHECK_EQUAL(&r.at(0), &a);
0244 BOOST_CHECK_EQUAL(r[0], 5);
0245 BOOST_CHECK_EQUAL(&r[0], &a);
0246 BOOST_CHECK_EQUAL(*r.begin(), 5);
0247 BOOST_CHECK_EQUAL(&*r.begin(), &a);
0248
0249 std::vector<int> act;
0250 std::copy(r.begin(), r.end(), std::back_inserter(act));
0251 std::vector<int> exp = {5, 6, 7};
0252 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), act.begin(), act.end());
0253 }
0254
0255 BOOST_AUTO_TEST_CASE(Ranges) {
0256 std::vector<std::unique_ptr<int>> v;
0257 v.push_back(std::make_unique<int>(1));
0258 v.push_back(std::make_unique<int>(2));
0259 v.push_back(std::make_unique<int>(3));
0260 v.push_back(std::make_unique<int>(4));
0261 v.push_back(std::make_unique<int>(5));
0262 auto r = detail::TransformRange{detail::Dereference{}, v};
0263
0264 std::vector<int> results;
0265 std::ranges::copy(r | std::views::transform([](int val) { return val * 3; }) |
0266 std::views::filter([](int val) { return val < 10; }),
0267 std::back_inserter(results));
0268 BOOST_CHECK_EQUAL(results.size(), 3);
0269 std::vector exp{3, 6, 9};
0270 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), results.begin(),
0271 results.end());
0272 }
0273
0274 BOOST_AUTO_TEST_SUITE_END()