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
102 lines (87 loc) · 2.68 KB
/
Copy pathtest_permutations.cpp
File metadata and controls
102 lines (87 loc) · 2.68 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
#include <permutations.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#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: 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);
}