Skip to content

Commit

Permalink
zip supports rval (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Apr 21, 2024
1 parent 08b8fc6 commit 1e8f8db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion inc/mkn/kul/zip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Zipper {
};

template <typename... Args>
auto zip(Args&... args) {
auto zip(Args&&... args) {
return Zipper<Args...>{std::forward_as_tuple(args...)};
}

Expand Down
15 changes: 13 additions & 2 deletions test/test/zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@

#include <array>
#include <vector>
#include <numeric>

TEST(ZipOperations, works) {
std::vector<double> const d0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // 45
auto d0f = []() {
return std::vector<double>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // 45
};
std::vector<double> const d1{10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; // 145
std::array<double, 10> const d2{0, 100, 200, 300, 400, 500, 600, 700, 800, 900}; // 4500 = 4690

auto d0 = d0f();
EXPECT_EQ(d0.size(), d1.size());
EXPECT_EQ(d1.size(), d2.size());

double result = 0;
for (auto const& [a, b, c] : mkn::kul::zip(d0, d1, d2)) result += a + b + c;
for (auto const& [a, b, c] : mkn::kul::zip(d0f(), d1, d2)) result += a + b + c;
EXPECT_EQ(result, 4690);

double expected = 0;
for (auto const& v : d0) expected += v;
for (auto const& v : d1) expected += v;
for (auto const& v : d2) expected += v;

EXPECT_EQ(result, expected);

double acc = 0;
acc += std::accumulate(d0.begin(), d0.end(), 0);
acc += std::accumulate(d1.begin(), d1.end(), 0);
acc += std::accumulate(d2.begin(), d2.end(), 0);
EXPECT_EQ(result, acc);
}

0 comments on commit 1e8f8db

Please sign in to comment.