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
111 lines (99 loc) · 3.34 KB
/
Copy pathtest_combinations_with_replacement.cpp
File metadata and controls
111 lines (99 loc) · 3.34 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
#include <combinations_with_replacement.hpp>
#include <iterator>
#include <string>
#include <set>
#include <vector>
#include "helpers.hpp"
#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: 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);
}