Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:35

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