forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chunked.cpp
More file actions
80 lines (66 loc) · 1.96 KB
/
Copy pathtest_chunked.cpp
File metadata and controls
80 lines (66 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <chunked.hpp>
#include <vector>
#include <array>
#include <string>
#include <utility>
#include "helpers.hpp"
#include "catch.hpp"
using iter::chunked;
using Vec = std::vector<int>;
using ResVec = std::vector<Vec>;
TEST_CASE("chunked: basic test", "[chunked]") {
Vec ns = {1, 2, 3, 4, 5, 6};
ResVec results;
SECTION("Normal call") {
for (auto&& g : chunked(ns, 2)) {
results.emplace_back(std::begin(g), std::end(g));
}
}
SECTION("Pipe") {
for (auto&& g : ns | chunked(2)) {
results.emplace_back(std::begin(g), std::end(g));
}
}
ResVec rc = {{1, 2}, {3, 4}, {5, 6}};
REQUIRE(results == rc);
}
TEST_CASE("chunked: len(iterable) % groupsize != 0", "[chunked]") {
Vec ns = {1, 2, 3, 4, 5, 6, 7};
ResVec results;
for (auto&& g : chunked(ns, 3)) {
results.emplace_back(std::begin(g), std::end(g));
}
ResVec rc = {{1, 2, 3}, {4, 5, 6}, {7}};
REQUIRE(results == rc);
}
TEST_CASE("chunked: iterators can be compared", "[chunked]") {
Vec ns = {1, 2, 3, 4, 5, 6, 7};
auto g = chunked(ns, 3);
auto it = std::begin(g);
REQUIRE(it == std::begin(g));
REQUIRE_FALSE(it != std::begin(g));
++it;
REQUIRE(it != std::begin(g));
REQUIRE_FALSE(it == std::begin(g));
}
TEST_CASE("chunked: size 0 is empty", "[chunked]") {
Vec ns{1, 2, 3};
auto g = chunked(ns, 0);
REQUIRE(std::begin(g) == std::end(g));
}
TEST_CASE("chunked: empty iterable gives empty chunked", "[chunked]") {
Vec ns{};
auto g = chunked(ns, 1);
REQUIRE(std::begin(g) == std::end(g));
}
TEST_CASE("chunked: iterator meets requirements", "[chunked]") {
std::string s{};
auto c = chunked(s, 1);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
template <typename T>
using ImpT = decltype(chunked(std::declval<T>(), 1));
TEST_CASE("chunked: has correct ctor and assign ops", "[chunked]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}