forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_combinations_with_replacement.cpp
More file actions
149 lines (134 loc) · 4.51 KB
/
Copy pathtest_combinations_with_replacement.cpp
File metadata and controls
149 lines (134 loc) · 4.51 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
147
148
149
#include <combinations_with_replacement.hpp>
#include <iterator>
#include <set>
#include <string>
#include <vector>
#define CHAR_RANGE_DEFAULT_CONSTRUCTIBLE
#include "helpers.hpp"
#undef CHAR_RANGE_DEFAULT_CONSTRUCTIBLE
#include "catch.hpp"
using iter::combinations_with_replacement;
using itertest::BasicIterable;
using CharCombSet = std::vector<std::vector<char>>;
TEST_CASE("combinations_with_replacement: Simple combination",
"[combinations_with_replacement]") {
std::string s{"ABC"};
CharCombSet sc;
SECTION("Normal call") {
for (auto v : combinations_with_replacement(s, 2)) {
sc.emplace_back(std::begin(v), std::end(v));
}
}
SECTION("Pipe") {
for (auto v : s | combinations_with_replacement(2)) {
sc.emplace_back(std::begin(v), std::end(v));
}
}
CharCombSet ans = {
{'A', 'A'}, {'A', 'B'}, {'A', 'C'}, {'B', 'B'}, {'B', 'C'}, {'C', 'C'}};
REQUIRE(ans == sc);
}
TEST_CASE("combinations_with_replacement: const iteration",
"[combinations_with_replacement]") {
std::string s{"ABC"};
CharCombSet sc;
const auto cwr = combinations_with_replacement(s, 2);
for (auto v : cwr) {
sc.emplace_back(std::begin(v), std::end(v));
}
CharCombSet ans = {
{'A', 'A'}, {'A', 'B'}, {'A', 'C'}, {'B', 'B'}, {'B', 'C'}, {'C', 'C'}};
REQUIRE(ans == sc);
}
TEST_CASE(
"combinations_with_replacement: const iterators can be compared to "
"non-const iterators",
"[combinations_with_replacement][const]") {
std::string s{"AB"};
auto cwr = combinations_with_replacement(s, 2);
const auto& ccwr = cwr;
(void)(std::begin(cwr) == std::end(ccwr));
}
TEST_CASE(
"combinations_with_replacement: Works with different begin and end types",
"[combinations_with_replacement]") {
CharRange cr{'d'};
CharCombSet sc;
for (auto&& v : combinations_with_replacement(cr, 2)) {
sc.emplace_back(std::begin(v), std::end(v));
}
CharCombSet ans = {
{'a', 'a'}, {'a', 'b'}, {'a', 'c'}, {'b', 'b'}, {'b', 'c'}, {'c', 'c'}};
REQUIRE(ans == sc);
}
TEST_CASE("combinations_with_replacement: iterators can be compared",
"[combinations_with_replacement]") {
std::string s{"ABCD"};
auto c = combinations_with_replacement(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_with_replacement: big size is no problem",
"[combinations_with_replacement]") {
std::string s{"AB"};
CharCombSet sc;
for (auto v : combinations_with_replacement(s, 3)) {
sc.emplace_back(std::begin(v), std::end(v));
}
CharCombSet ans = {
{'A', 'A', 'A'}, {'A', 'A', 'B'}, {'A', 'B', 'B'}, {'B', 'B', 'B'}};
REQUIRE(ans == sc);
}
TEST_CASE("combinations_with_replacement: 0 size is empty",
"[combinations_with_replacement]") {
std::string s{"A"};
auto cwr = combinations_with_replacement(s, 0);
REQUIRE(std::begin(cwr) == std::end(cwr));
}
TEST_CASE("combinations_with_replacement: operator->",
"[combinations_with_replacement]") {
std::string s{"ABCD"};
auto c = combinations_with_replacement(s, 2);
auto it = std::begin(c);
REQUIRE(it->size() == 2);
}
TEST_CASE("combinations_with_replacement: binds to lvalues, moves rvalues",
"[combinations_with_replacement]") {
BasicIterable<char> bi{'x', 'y', 'z'};
SECTION("binds to lvalues") {
combinations_with_replacement(bi, 1);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
combinations_with_replacement(std::move(bi), 1);
REQUIRE(bi.was_moved_from());
}
}
TEST_CASE(
"combinations_with_replacement: "
"doesn't move or copy elements of iterable",
"[combinations_with_replacement]") {
constexpr itertest::SolidInt arr[] = {{6}, {7}, {8}};
for (auto&& i : combinations_with_replacement(arr, 1)) {
(void)i;
}
}
TEST_CASE("combinations_with_replacement: iterator meets requirements",
"[combinations_with_replacement]") {
std::string s{"abc"};
auto c = combinations_with_replacement(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_with_replacement(std::declval<T>(), 1));
TEST_CASE("combinations_with_replacement: has correct ctor and assign ops",
"[combinations_with_replacement]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}