This repository was archived by the owner on Oct 29, 2020. 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_product.cpp
More file actions
184 lines (148 loc) · 4.95 KB
/
Copy pathtest_product.cpp
File metadata and controls
184 lines (148 loc) · 4.95 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <product.hpp>
#define DEFINE_BASIC_ITERABLE_COPY_CTOR
#define DEFINE_BASIC_ITERABLE_CONST_BEGIN_AND_END
#include "helpers.hpp"
#undef DEFINE_BASIC_ITERABLE_CONST_BEGIN_AND_END
#undef DEFINE_BASIC_ITERABLE_COPY_CTOR
#include <iterator>
#include <string>
#include <vector>
#include "catch.hpp"
using iter::product;
using Vec = const std::vector<int>;
TEST_CASE("product: basic test, two sequences", "[product]") {
using TP = std::tuple<int, char>;
using ResType = std::vector<TP>;
Vec n1 = {0, 1};
const std::string s{"abc"};
auto p = product(n1, s);
ResType v(std::begin(p), std::end(p));
ResType vc = {
TP{0, 'a'}, TP{0, 'b'}, TP{0, 'c'}, TP{1, 'a'}, TP{1, 'b'}, TP{1, 'c'}};
REQUIRE(v == vc);
}
TEST_CASE("product: two sequences where one has different begin and end",
"[product]") {
using TP = std::tuple<int, char>;
using ResType = std::vector<TP>;
Vec n1 = {0, 1};
CharRange cr('d');
auto p = product(n1, cr);
ResType v(std::begin(p), std::end(p));
ResType vc = {
TP{0, 'a'}, TP{0, 'b'}, TP{0, 'c'}, TP{1, 'a'}, TP{1, 'b'}, TP{1, 'c'}};
REQUIRE(v == vc);
}
TEST_CASE("product: three sequences", "[product]") {
using TP = std::tuple<int, char, int>;
using ResType = const std::vector<TP>;
Vec n1 = {0, 1};
const std::string s{"ab"};
Vec n2 = {2};
auto p = product(n1, s, n2);
ResType v(std::begin(p), std::end(p));
ResType vc = {TP{0, 'a', 2}, TP{0, 'b', 2}, TP{1, 'a', 2}, TP{1, 'b', 2}};
REQUIRE(v == vc);
}
TEST_CASE("product: with repeat", "[product]") {
using TP = std::tuple<char, char, char>;
using ResType = const std::vector<TP>;
const std::string s = "hop";
auto p = product<3>(s);
ResType v(std::begin(p), std::end(p));
ResType vc = {
TP{'h', 'h', 'h'}, TP{'h', 'h', 'o'}, TP{'h', 'h', 'p'},
TP{'h', 'o', 'h'}, TP{'h', 'o', 'o'}, TP{'h', 'o', 'p'},
TP{'h', 'p', 'h'}, TP{'h', 'p', 'o'}, TP{'h', 'p', 'p'},
TP{'o', 'h', 'h'}, TP{'o', 'h', 'o'}, TP{'o', 'h', 'p'},
TP{'o', 'o', 'h'}, TP{'o', 'o', 'o'}, TP{'o', 'o', 'p'},
TP{'o', 'p', 'h'}, TP{'o', 'p', 'o'}, TP{'o', 'p', 'p'},
TP{'p', 'h', 'h'}, TP{'p', 'h', 'o'}, TP{'p', 'h', 'p'},
TP{'p', 'o', 'h'}, TP{'p', 'o', 'o'}, TP{'p', 'o', 'p'},
TP{'p', 'p', 'h'}, TP{'p', 'p', 'o'}, TP{'p', 'p', 'p'},
};
REQUIRE(v == vc);
}
TEST_CASE("product: empty when any iterable is empty", "[product]") {
Vec n1 = {0, 1};
Vec n2 = {0, 1, 2};
Vec emp = {};
SECTION("first iterable is empty") {
auto p = product(emp, n1, n2);
REQUIRE(std::begin(p) == std::end(p));
}
SECTION("middle iterable is empty") {
auto p = product(n1, emp, n2);
REQUIRE(std::begin(p) == std::end(p));
}
SECTION("last iterable is empty") {
auto p = product(n1, n2, emp);
REQUIRE(std::begin(p) == std::end(p));
}
}
TEST_CASE("product: single iterable", "[product]") {
const std::string s{"ab"};
using TP = std::tuple<char>;
using ResType = const std::vector<TP>;
auto p = product(s);
ResType v(std::begin(p), std::end(p));
ResType vc = {TP{'a'}, TP{'b'}};
REQUIRE(v == vc);
}
TEST_CASE("product: no arguments gives one empty tuple", "[product") {
auto p = product();
auto it = std::begin(p);
REQUIRE(it != std::end(p));
REQUIRE(*it == std::make_tuple());
++it;
REQUIRE(it == std::end(p));
}
TEST_CASE("product: binds to lvalues and moves rvalues", "[product]") {
itertest::BasicIterable<char> bi{'x', 'y'};
itertest::BasicIterable<int> bi2{0, 1};
SECTION("First ref'd, second moved") {
product(bi, std::move(bi2));
REQUIRE_FALSE(bi.was_moved_from());
REQUIRE_FALSE(bi.was_copied_from());
REQUIRE(bi2.was_moved_from());
}
SECTION("First moved, second ref'd") {
product(std::move(bi), bi2);
REQUIRE(bi.was_moved_from());
REQUIRE_FALSE(bi2.was_copied_from());
REQUIRE_FALSE(bi2.was_moved_from());
}
SECTION("repeat, lvalue not moved or copied") {
product<2>(bi);
REQUIRE_FALSE(bi.was_moved_from());
REQUIRE_FALSE(bi.was_copied_from());
}
SECTION("repeat, const lvalue not moved or copied") {
const auto& r = bi;
product<2>(r);
REQUIRE_FALSE(bi.was_moved_from());
REQUIRE_FALSE(bi.was_copied_from());
}
SECTION("repeat, rvalue copied") {
product<2>(std::move(bi));
REQUIRE_FALSE(bi.was_moved_from());
REQUIRE(bi.was_copied_from());
}
}
TEST_CASE("product: doesn't move or copy elements of iterable", "[product]") {
constexpr itertest::SolidInt arr[] = {{1}, {0}, {2}};
for (auto&& t : product(arr)) {
(void)std::get<0>(t);
}
}
TEST_CASE("product: iterator meets requirements", "[product]") {
std::string s{"abc"};
auto c = product(s, s);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
template <typename... Ts>
using ImpT = decltype(product(std::declval<Ts>()...));
TEST_CASE("product: has correct ctor and assign ops", "[product]") {
using T = ImpT<std::string&, std::vector<double>, std::vector<std::string>>;
REQUIRE(itertest::IsMoveConstructibleOnly<T>::value);
}