This repository was archived by the owner on Nov 15, 2022. It is now read-only.
forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_powerset.cpp
More file actions
108 lines (92 loc) · 2.78 KB
/
Copy pathtest_powerset.cpp
File metadata and controls
108 lines (92 loc) · 2.78 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
#include <powerset.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#include "catch.hpp"
using iter::powerset;
using IntPermSet = std::multiset<std::multiset<int>>;
TEST_CASE("powerset: basic test, [1, 2, 3]", "[powerset]") {
const std::vector<int> ns = {1, 2, 3};
IntPermSet v;
for (auto&& st : powerset(ns)) {
v.emplace(std::begin(st), std::end(st));
}
const IntPermSet vc = { {}, {1}, {2}, {3,}, {1,2}, {1,3}, {2,3}, {1,2,3} };
REQUIRE( v == vc );
}
TEST_CASE("powerset: empty sequence gives only empty set", "[powerset]") {
const std::vector<int> ns = {};
auto ps = powerset(ns);
auto it = std::begin(ps);
REQUIRE( std::begin(*it) == std::end(*it) ); // it's empty
++it;
REQUIRE( it == std::end(ps) );
}
TEST_CASE("powerset: iterators can be compared", "[powerset]") {
std::vector<int> ns = {1, 2};
auto p = powerset(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;
++it;
++it;
REQUIRE( it == std::end(p) );
}
ns.push_back(3);
{
auto it = std::begin(p);
auto it2 = std::begin(p);
std::advance(it, 4);
std::advance(it2, 4);
REQUIRE( it == it2 );
++it2;
REQUIRE( it != it2 );
}
}
TEST_CASE("powerset: iterator copy ctor is correct", "[powerset]") {
// { {}, {1}, {2}, {1, 2} }
std::vector<int> ns = {1, 2};
auto p = powerset(ns);
auto it = std::begin(p);
auto it2(it);
REQUIRE( it == it2 );
++it2;
REQUIRE( it != it2 );
REQUIRE( std::begin(*it) == std::end(*it) );
}
TEST_CASE("powerset: binds to lvalues, moves rvalues", "[powerset]") {
itertest::BasicIterable<int> bi{1, 2};
SECTION("binds to lvalues") {
powerset(bi);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
powerset(std::move(bi));
REQUIRE(bi.was_moved_from());
}
}
TEST_CASE("powerset: doesn't move or copy elements of iterable", "[powerset]"){
constexpr itertest::SolidInt arr[] = {{1}, {0}, {2}};
for (auto&& st : powerset(arr)) {
for (auto&& i : st) {
(void)i;
}
}
}
TEST_CASE("powerset: iterator meets requirements", "[powerset]") {
std::string s{"abc"};
auto c = powerset(s);
REQUIRE( itertest::IsIterator<decltype(std::begin(c))>::value );
auto&& row = *std::begin(c);
REQUIRE( itertest::IsIterator<decltype(std::begin(row))>::value );
}