File indexing completed on 2025-01-18 09:12:56
0001
0002
0003
0004
0005
0006
0007
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
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
0045
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
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
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()