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