forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_combinations.cpp
More file actions
146 lines (127 loc) · 3.95 KB
/
Copy pathtest_combinations.cpp
File metadata and controls
146 lines (127 loc) · 3.95 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define DEFINE_DEFAULT_ITERATOR_CTOR
#define CHAR_RANGE_DEFAULT_CONSTRUCTIBLE
#include "helpers.hpp"
#undef CHAR_RANGE_DEFAULT_CONSTRUCTIBLE
#undef DEFINE_DEFAULT_ITERATOR_CTOR
#include <combinations.hpp>
#include <iterator>
#include <set>
#include <string>
#include <vector>
#include "catch.hpp"
using iter::combinations;
using itertest::BasicIterable;
using itertest::SolidInt;
using CharCombSet = std::vector<std::vector<char>>;
TEST_CASE("combinations: Simple combination of 4", "[combinations]") {
std::string s{"ABCD"};
CharCombSet sc;
SECTION("Normal call") {
for (auto&& v : combinations(s, 2)) {
sc.emplace_back(std::begin(v), std::end(v));
}
}
SECTION("Pipe") {
for (auto&& v : s | combinations(2)) {
sc.emplace_back(std::begin(v), std::end(v));
}
}
CharCombSet ans = {
{'A', 'B'}, {'A', 'C'}, {'A', 'D'}, {'B', 'C'}, {'B', 'D'}, {'C', 'D'}};
REQUIRE(ans == sc);
}
TEST_CASE("combinations: const iteration", "[combinations][const]") {
std::string s{"ABCD"};
CharCombSet sc;
const auto comb = combinations(s, 2);
for (auto&& v : comb) {
sc.emplace_back(std::begin(v), std::end(v));
}
CharCombSet ans = {
{'A', 'B'}, {'A', 'C'}, {'A', 'D'}, {'B', 'C'}, {'B', 'D'}, {'C', 'D'}};
REQUIRE(ans == sc);
}
TEST_CASE(
"combinations: const iterators can be compared to non-const iterators",
"[combinations][const]") {
std::string s{"ABC"};
auto c = combinations(s, 2);
const auto& cc = c;
(void)(std::begin(c) == std::end(cc));
}
TEST_CASE("combinations: Works with different begin and end types",
"[combinations]") {
CharRange cr{'e'};
CharCombSet sc;
for (auto&& v : combinations(cr, 2)) {
sc.emplace_back(std::begin(v), std::end(v));
}
CharCombSet ans = {
{'a', 'b'}, {'a', 'c'}, {'a', 'd'}, {'b', 'c'}, {'b', 'd'}, {'c', 'd'}};
REQUIRE(ans == sc);
}
TEST_CASE("combinations: iterators can be compared", "[combinations]") {
std::string s{"ABCD"};
auto c = combinations(s, 2);
auto it = std::begin(c);
REQUIRE(it == std::begin(c));
REQUIRE_FALSE(it != std::begin(c));
++it;
REQUIRE(it != std::begin(c));
REQUIRE_FALSE(it == std::begin(c));
}
TEST_CASE("combinations: operator->", "[combinations]") {
std::string s{"ABCD"};
auto c = combinations(s, 2);
auto it = std::begin(c);
REQUIRE(it->size() == 2);
}
TEST_CASE("combinations: size too large gives no results", "[combinations]") {
std::string s{"ABCD"};
auto c = combinations(s, 5);
REQUIRE(std::begin(c) == std::end(c));
}
TEST_CASE("combinations: size 0 gives nothing", "[combinations]") {
std::string s{"ABCD"};
auto c = combinations(s, 0);
REQUIRE(std::begin(c) == std::end(c));
}
TEST_CASE(
"combinations: iterable without operator*() const", "[combinations]") {
BasicIterable<char> bi{'x', 'y', 'z'};
auto c = combinations(bi, 1);
auto it = std::begin(c);
++it;
(*it)[0];
}
TEST_CASE("combinations: binds to lvalues, moves rvalues", "[combinations]") {
BasicIterable<char> bi{'x', 'y', 'z'};
SECTION("binds to lvalues") {
combinations(bi, 1);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
combinations(std::move(bi), 1);
REQUIRE(bi.was_moved_from());
}
}
TEST_CASE("combinations: doesn't move or copy elements of iterable",
"[combinations]") {
constexpr SolidInt arr[] = {{6}, {7}, {8}};
for (auto&& i : combinations(arr, 1)) {
(void)i;
}
}
TEST_CASE("combinations: iterator meets requirements", "[combinations]") {
std::string s{"abc"};
auto c = combinations(s, 1);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
auto&& row = *std::begin(c);
REQUIRE(itertest::IsIterator<decltype(std::begin(row))>::value);
}
template <typename T>
using ImpT = decltype(combinations(std::declval<T>(), 1));
TEST_CASE("combinations: has correct ctor and assign ops", "[combinations]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}