|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2022 NUClear Contributors |
| 5 | + * |
| 6 | + * This file is part of the NUClear codebase. |
| 7 | + * See https://github.com/Fastcode/NUClear for further info. |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 10 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 11 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 15 | + * Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 18 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 20 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "util/FunctionFusion.hpp" |
| 24 | + |
| 25 | +#include <catch2/catch_test_macros.hpp> |
| 26 | +#include <tuple> |
| 27 | +#include <utility> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | + |
| 31 | +// There are several linting rules from clang-tidy that will trigger in this code. |
| 32 | +// However as the point of these tests is to show what the behaviour is in these situations they are suppressed for this |
| 33 | +// file |
| 34 | +// NOLINTBEGIN(bugprone-use-after-move) |
| 35 | + |
| 36 | +namespace { // Make everything here internal linkage |
| 37 | + |
| 38 | +/** |
| 39 | + * This struct is used to test what is passed from FunctionFusion to the call function. |
| 40 | + * |
| 41 | + * It has four different versions of append to see which one is called |
| 42 | + * The ones that take rvalue references will move the arguments into new temporaries to ensure the original arguments |
| 43 | + * will be empty after the function runs. |
| 44 | + */ |
| 45 | +struct Appender { |
| 46 | + |
| 47 | + /// rvalue/rvalue |
| 48 | + static std::vector<char> append(std::vector<char>&& x, std::vector<char>&& y) { |
| 49 | + const std::vector<char> x1 = std::move(x); |
| 50 | + const std::vector<char> y1 = std::move(y); |
| 51 | + |
| 52 | + std::vector<char> out; |
| 53 | + out.push_back('r'); |
| 54 | + out.push_back('r'); |
| 55 | + out.insert(out.end(), x1.begin(), x1.end()); // HERE |
| 56 | + out.insert(out.end(), y1.begin(), y1.end()); |
| 57 | + |
| 58 | + return out; |
| 59 | + } |
| 60 | + |
| 61 | + /// rvalue/lvalue |
| 62 | + static std::vector<char> append(std::vector<char>&& x, const std::vector<char>& y) { |
| 63 | + const std::vector<char> x1 = std::move(x); |
| 64 | + const auto& y1 = y; |
| 65 | + |
| 66 | + std::vector<char> out; |
| 67 | + out.push_back('r'); |
| 68 | + out.push_back('l'); |
| 69 | + out.insert(out.end(), x1.begin(), x1.end()); |
| 70 | + out.insert(out.end(), y1.begin(), y1.end()); |
| 71 | + |
| 72 | + return out; |
| 73 | + } |
| 74 | + |
| 75 | + /// lvalue/rvalue |
| 76 | + static std::vector<char> append(const std::vector<char>& x, std::vector<char>&& y) { |
| 77 | + const auto& x1 = x; |
| 78 | + const std::vector<char> y1 = std::move(y); |
| 79 | + |
| 80 | + std::vector<char> out; |
| 81 | + out.push_back('l'); |
| 82 | + out.push_back('r'); |
| 83 | + out.insert(out.end(), x1.begin(), x1.end()); // HERE |
| 84 | + out.insert(out.end(), y1.begin(), y1.end()); |
| 85 | + |
| 86 | + return out; |
| 87 | + } |
| 88 | + |
| 89 | + /// lvalue/lvalue |
| 90 | + static std::vector<char> append(const std::vector<char>& x, const std::vector<char>& y) { |
| 91 | + const auto& x1 = x; |
| 92 | + const auto& y1 = y; |
| 93 | + |
| 94 | + std::vector<char> out; |
| 95 | + out.push_back('l'); |
| 96 | + out.push_back('l'); |
| 97 | + out.insert(out.end(), x.begin(), x.end()); |
| 98 | + out.insert(out.end(), y.begin(), y.end()); |
| 99 | + |
| 100 | + return out; |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +template <typename T> |
| 105 | +struct AppendCaller { |
| 106 | + template <typename... Args> |
| 107 | + static auto call(Args&&... args) -> decltype(T::append(std::forward<Args>(args)...)) { |
| 108 | + return T::append(std::forward<Args>(args)...); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +template <int Shared, typename... Args> |
| 113 | +auto do_fusion(Args&&... args) { |
| 114 | + return NUClear::util::FunctionFusion<std::tuple<Appender, Appender>, |
| 115 | + decltype(std::forward_as_tuple(std::forward<Args>(args)...)), |
| 116 | + AppendCaller, |
| 117 | + std::tuple<>, |
| 118 | + Shared>::call(std::forward<Args>(args)...); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace |
| 122 | + |
| 123 | +SCENARIO("Shared arguments to FunctionFusion are not moved", "[util][FunctionFusion][shared][move]") { |
| 124 | + |
| 125 | + WHEN("calling append with 1 shared and 1 selected arguments") { |
| 126 | + std::vector<char> shared({'s'}); |
| 127 | + std::vector<char> arg1({'1'}); |
| 128 | + std::vector<char> arg2({'2'}); |
| 129 | + |
| 130 | + auto result = do_fusion<1>(std::move(shared), std::move(arg1), std::move(arg2)); |
| 131 | + |
| 132 | + const auto& r1 = std::get<0>(result); |
| 133 | + const auto& r2 = std::get<1>(result); |
| 134 | + |
| 135 | + THEN("the results are correct") { |
| 136 | + CHECK(r1 == std::vector<char>({'l', 'r', 's', '1'})); |
| 137 | + CHECK(r2 == std::vector<char>({'l', 'r', 's', '2'})); |
| 138 | + } |
| 139 | + THEN("the shared argument is not moved") { |
| 140 | + CHECK(shared == std::vector<char>({'s'})); |
| 141 | + } |
| 142 | + THEN("the selected arguments are moved") { |
| 143 | + CHECK(arg1.empty()); |
| 144 | + CHECK(arg2.empty()); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + WHEN("calling append with 0 shared and 2 selected arguments") { |
| 149 | + std::vector<char> arg1({'1'}); |
| 150 | + std::vector<char> arg2({'2'}); |
| 151 | + std::vector<char> arg3({'3'}); |
| 152 | + std::vector<char> arg4({'4'}); |
| 153 | + |
| 154 | + auto result = do_fusion<0>(std::move(arg1), std::move(arg2), std::move(arg3), std::move(arg4)); |
| 155 | + |
| 156 | + const auto& r1 = std::get<0>(result); |
| 157 | + const auto& r2 = std::get<1>(result); |
| 158 | + |
| 159 | + THEN("the results are correct") { |
| 160 | + CHECK(r1 == std::vector<char>({'r', 'r', '1', '2'})); |
| 161 | + CHECK(r2 == std::vector<char>({'r', 'r', '3', '4'})); |
| 162 | + } |
| 163 | + THEN("the selected arguments are moved") { |
| 164 | + CHECK(arg1.empty()); |
| 165 | + CHECK(arg2.empty()); |
| 166 | + CHECK(arg3.empty()); |
| 167 | + CHECK(arg4.empty()); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// NOLINTEND(bugprone-use-after-move) |
0 commit comments