Skip to content

Commit

Permalink
Make pipes::override work on C arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
joboccara committed Aug 26, 2019
1 parent e7c981f commit f19f4a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/pipes/override.hpp
Expand Up @@ -5,6 +5,7 @@

#include "pipeline_base.hpp"

#include <iterator>
#include <utility>

namespace pipes
Expand Down Expand Up @@ -37,6 +38,7 @@ namespace pipes
template<typename Container>
auto override(Container& container)
{
using std::begin;
return override_pipeline<decltype(begin(std::declval<Container&>()))>{begin(container)};
}
}
Expand Down
18 changes: 16 additions & 2 deletions tests/override.cpp
@@ -1,9 +1,12 @@
#include "catch.hpp"
#include "pipes/pipes.hpp"

#include <algorithm>
#include <vector>

TEST_CASE("Override existing contents on STL containers")
{
auto input = std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto input = std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

std::vector<int> results = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Expand All @@ -28,11 +31,22 @@ namespace

TEST_CASE("Override existing contents on collections with ADL begin")
{
auto input = std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto input = std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

auto results = MyCollectionNamespace::MyCollection(std::vector<int>{0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
input >>= pipes::override(results);

REQUIRE(results.data_ == expected);
}

TEST_CASE("Override contents of a C array")
{
auto input = std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int results[10] = {};
input >>= pipes::override(results);

REQUIRE(std::equal(std::begin(results), std::end(results), begin(expected), end(expected)));
}

0 comments on commit f19f4a3

Please sign in to comment.