forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_permutations.cpp
More file actions
138 lines (118 loc) · 3.7 KB
/
Copy pathtest_permutations.cpp
File metadata and controls
138 lines (118 loc) · 3.7 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
#include <permutations.hpp>
#include "helpers.hpp"
#include <iterator>
#include <set>
#include <string>
#include <vector>
#include "catch.hpp"
using iter::permutations;
using IntPermSet = std::multiset<std::vector<int>>;
TEST_CASE("permutations: basic test, 3 element sequence", "[permutations]") {
const std::vector<int> ns = {1, 7, 9};
IntPermSet v;
SECTION("Normal call") {
for (auto&& st : permutations(ns)) {
v.emplace(std::begin(st), std::end(st));
}
}
SECTION("Pipe") {
for (auto&& st : ns | permutations) {
v.emplace(std::begin(st), std::end(st));
}
}
const IntPermSet vc = {
{1, 7, 9}, {1, 9, 7}, {7, 1, 9}, {7, 9, 1}, {9, 1, 7}, {9, 7, 1}};
REQUIRE(v == vc);
}
TEST_CASE("permutations: const iteration", "[permutations][const]") {
const std::vector<int> ns = {1, 7, 9};
IntPermSet v;
const auto perm = permutations(ns);
for (auto&& st : perm) {
v.emplace(std::begin(st), std::end(st));
}
const IntPermSet vc = {
{1, 7, 9}, {1, 9, 7}, {7, 1, 9}, {7, 9, 1}, {9, 1, 7}, {9, 7, 1}};
REQUIRE(v == vc);
}
TEST_CASE(
"permutations: const iterators can be compared to non-const iteration",
"[permutations][const]") {
auto p = permutations(std::vector<int>{});
const auto& cp = p;
(void)(std::begin(p) == std::end(cp));
}
TEST_CASE("permutations: Works with different begin and end types",
"[permutations]") {
CharRange cr{'d'};
using CharPermSet = std::multiset<std::vector<char>>;
CharPermSet sc;
for (auto&& v : permutations(cr)) {
sc.emplace(std::begin(v), std::end(v));
}
const CharPermSet ans = {{'a', 'b', 'c'}, {'a', 'c', 'b'}, {'b', 'a', 'c'},
{'b', 'c', 'a'}, {'c', 'a', 'b'}, {'c', 'b', 'a'}};
REQUIRE(ans == sc);
}
TEST_CASE(
"permutations: empty sequence has one empy permutation", "[permutations]") {
const std::vector<int> ns{};
auto p = permutations(ns);
auto it = std::begin(p);
REQUIRE((*it).empty());
it++;
REQUIRE(it == std::end(p));
}
TEST_CASE("permutations: iterators can be compared", "[permutations]") {
const std::vector<int> ns = {1, 2};
auto p = permutations(ns);
auto it = std::begin(p);
REQUIRE(it == std::begin(p));
REQUIRE_FALSE(it != std::begin(p));
REQUIRE(it != std::end(p));
REQUIRE_FALSE(it == std::end(p));
++it;
REQUIRE_FALSE(it == std::begin(p));
REQUIRE(it != std::begin(p));
REQUIRE_FALSE(it == std::end(p));
REQUIRE(it != std::end(p));
++it;
REQUIRE(it == std::end(p));
REQUIRE_FALSE(it != std::end(p));
}
TEST_CASE("permutations: binds to lvalues, moves rvalues", "[permutations]") {
itertest::BasicIterable<int> bi{1, 2};
SECTION("binds to lvalues") {
permutations(bi);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
permutations(std::move(bi));
REQUIRE(bi.was_moved_from());
}
}
namespace itertest {
bool operator<(const SolidInt& lhs, const SolidInt& rhs) {
return lhs.getint() < rhs.getint();
}
}
TEST_CASE("permutations doesn't move or copy elements of iterable",
"[permutations]") {
constexpr itertest::SolidInt arr[] = {{1}, {0}, {2}};
for (auto&& st : permutations(arr)) {
(void)st;
}
}
TEST_CASE("permutations: iterator meets requirements", "[permutations]") {
std::string s{"abc"};
auto c = permutations(s);
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(permutations(std::declval<T>()));
TEST_CASE("permutations: has correct ctor and assign ops", "[permutations]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}