Skip to content

Commit

Permalink
Add move only range test
Browse files Browse the repository at this point in the history
Signed-off-by: Nahuel Espinosa <nespinosa@ekumenlabs.com>
  • Loading branch information
nahueespinosa committed Jan 13, 2024
1 parent 272fe68 commit 905fcd0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions beluga/include/beluga/actions/assign.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ struct assign_fn {
/**
* This can be appended to any view closure to evaluate it eagerly and assign the result
* to a given range, effectively converting any view into an action.
*
* The assignment of elements into the container may involve copy which can be less efficient than
* move because lvalue references are produced during the indirection call. Users can opt-in to use
* `ranges::views::move` to adapt the range in order for their elements to always produce an rvalue
* reference during the indirection call which implies move. In this case, make sure that the input
* views don't require accessing the elements in the adapted range more than once.
*/
inline constexpr detail::assign_fn assign;

Expand Down
13 changes: 13 additions & 0 deletions beluga/test/beluga/actions/test_assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#include <range/v3/action/drop.hpp>
#include <range/v3/action/remove.hpp>
#include <range/v3/algorithm/equal.hpp>
#include <range/v3/view/move.hpp>
#include <range/v3/view/reverse.hpp>
#include <range/v3/view/transform.hpp>

#include <memory>

namespace {

TEST(AssignAction, ViewToAction) {
Expand All @@ -33,6 +36,16 @@ TEST(AssignAction, ViewToAction) {
ASSERT_TRUE(ranges::equal(input, std::vector{3, 2, 1}));
}

TEST(AssignAction, MoveOnlyRange) {
auto input = std::vector<std::unique_ptr<int>>{};
input.emplace_back(std::make_unique<int>(1));
input.emplace_back(std::make_unique<int>(2));
input.emplace_back(std::make_unique<int>(3));
input |= ranges::views::move | ranges::views::reverse | beluga::actions::assign;
ASSERT_EQ(*input.front(), 3);
ASSERT_EQ(*input.back(), 1);
}

TEST(AssignAction, ViewToActionComposition) {
auto reverse_and_assign = ranges::views::reverse | beluga::actions::assign;
auto input = std::vector{1, 2, 3};
Expand Down

0 comments on commit 905fcd0

Please sign in to comment.