Back to home page

EIC code displayed by LXR

 
 

    


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

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 #include <boost/test/unit_test_suite.hpp>
0011 
0012 #include "Acts/Utilities/DelegateChainBuilder.hpp"
0013 
0014 using namespace Acts;
0015 
0016 struct AddTo {
0017   int value = 0;
0018 
0019   void add(int &x) const { x += value; }
0020 };
0021 
0022 void addFive(int &x) {
0023   x += 5;
0024 }
0025 
0026 BOOST_AUTO_TEST_SUITE(DelegateChainBuilderTests)
0027 
0028 BOOST_AUTO_TEST_CASE(DelegateChainBuilderAdd) {
0029   AddTo a1{1}, a2{2}, a3{3};
0030   int x = 0;
0031 
0032   // Basic building
0033   OwningDelegate<void(int &)> chain = DelegateChainBuilder<void(int &)>{}
0034                                           .add<&AddTo::add>(&a1)
0035                                           .add<&addFive>()
0036                                           .add<&AddTo::add>(&a2)
0037                                           .add<&AddTo::add>(&a3)
0038                                           .build();
0039   chain(x);
0040   BOOST_CHECK_EQUAL(x, 11);
0041 
0042   chain.disconnect();
0043 
0044   // In case of no return types, we can rebind the owning delegate with a chain
0045   // of different size
0046   chain = DelegateChainBuilder<void(int &)>{}
0047               .add<&AddTo::add>(&a1)
0048               .add<&addFive>()
0049               .add<&AddTo::add>(&a3)
0050               .build();
0051 
0052   x = 0;
0053 
0054   chain(x);
0055   BOOST_CHECK_EQUAL(x, 9);
0056 
0057   // CTAD helper from delegate type
0058   chain = DelegateChainBuilder{chain}
0059               .add<&AddTo::add>(&a1)
0060               .add<&addFive>()
0061               .add<&AddTo::add>(&a3)
0062               .build();
0063 
0064   x = 0;
0065 
0066   chain(x);
0067   BOOST_CHECK_EQUAL(x, 9);
0068 
0069   Delegate<void(int &)> nonOwning;
0070 
0071   // In case of a single callable, we can store it in a non-owning delegate
0072   DelegateChainBuilder<void(int &)>{}.add<&AddTo::add>(&a1).store(nonOwning);
0073 
0074   x = 0;
0075   nonOwning(x);
0076   BOOST_CHECK_EQUAL(x, 1);
0077 }
0078 
0079 struct GetInt {
0080   int value;
0081 
0082   int get() const { return value; }
0083 };
0084 
0085 int getSix() {
0086   return 6;
0087 }
0088 
0089 BOOST_AUTO_TEST_CASE(DelegateChainBuilderReturn) {
0090   GetInt g1{1}, g2{2}, g3{3};
0091 
0092   Delegate<std::array<int, 4>(), void, DelegateType::Owning> chain =
0093       DelegateChainBuilder<int()>{}
0094           .add<&GetInt::get>(&g1)
0095           .add<&getSix>()
0096           .add<&GetInt::get>(&g2)
0097           .add<&GetInt::get>(&g3)
0098           .build();
0099 
0100   auto results = chain();
0101   std::vector<int> expected = {1, 6, 2, 3};
0102   BOOST_CHECK_EQUAL_COLLECTIONS(results.begin(), results.end(),
0103                                 expected.begin(), expected.end());
0104 
0105   Delegate<std::array<int, 3>(), void, DelegateType::Owning> delegate;
0106   DelegateChainBuilder<int()>{}
0107       .add<&GetInt::get>(&g1)
0108       .add<&getSix>()
0109       .add<&GetInt::get>(&g3)
0110       .store(delegate);
0111 
0112   auto results2 = delegate();
0113   expected = {1, 6, 3};
0114   BOOST_CHECK_EQUAL_COLLECTIONS(results2.begin(), results2.end(),
0115                                 expected.begin(), expected.end());
0116 }
0117 
0118 BOOST_AUTO_TEST_SUITE_END()