Danny Robson
0a7cd59eb9
Taken from the illustrative text at https://en.cppreference.com/w/cpp/ranges/chunk_view
42 lines
1.0 KiB
C++
42 lines
1.0 KiB
C++
#include <cruft/util/tap.hpp>
|
|
|
|
#include <cruft/util/ranges/chunk.hpp>
|
|
#include <cruft/util/view.hpp>
|
|
|
|
|
|
int main ()
|
|
{
|
|
cruft::TAP::logger tap;
|
|
|
|
static constexpr int src[] = {
|
|
1, 2, 3,
|
|
4, 5, 6,
|
|
7, 8,
|
|
};
|
|
|
|
static constexpr int expected[] = {
|
|
1, 2, 3, -1,
|
|
4, 5, 6, -2,
|
|
7, 8, -3,
|
|
};
|
|
|
|
std::vector<int> dst;
|
|
dst.reserve (std::size (expected));
|
|
|
|
// Copy out the src array in groups of 3, inserting a decrementing 'line number' value.
|
|
// It's a little easier that manually juggling iterators and such.
|
|
|
|
int line = 0;
|
|
for (auto const chunk: src | cruft::ranges::chunk (3)) {
|
|
// This could be made more efficient by directly insert the chunk in one go,
|
|
// but it's better to exercise the single iteration case for increased code coverage.
|
|
for (auto const &i: chunk)
|
|
dst.push_back (i);
|
|
|
|
dst.push_back (--line);
|
|
}
|
|
|
|
tap.expect (cruft::equal (cruft::view (dst), cruft::view (expected)), "chunk(3)");
|
|
|
|
return tap.status ();
|
|
} |