forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sorted.cpp
More file actions
239 lines (201 loc) · 6.12 KB
/
Copy pathtest_sorted.cpp
File metadata and controls
239 lines (201 loc) · 6.12 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <cppitertools/sorted.hpp>
#include <array>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "catch.hpp"
#include "helpers.hpp"
using iter::sorted;
using Vec = const std::vector<int>;
TEST_CASE("sorted: iterates through a vector in sorted order", "[sorted]") {
Vec ns = {4, 0, 5, 1, 6, 7, 9, 3, 2, 8};
std::vector<int> v;
SECTION("Normal Call") {
auto s = sorted(ns);
v.assign(std::begin(s), std::end(s));
}
SECTION("Pipe") {
auto s = ns | sorted;
v.assign(std::begin(s), std::end(s));
}
Vec vc = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
REQUIRE(v == vc);
}
TEST_CASE("sorted: handles pointer to member function", "[sorted]") {
using itertest::Point;
const std::vector<Point> ps = {{5, 0}, {3, 0}, {10, 0}, {6, 0}};
auto s = sorted(ps, &Point::left_of);
const std::vector<Point> v(std::begin(s), std::end(s));
const std::vector<Point> vc = {{3, 0}, {5, 0}, {6, 0}, {10, 0}};
REQUIRE(v == vc);
}
TEST_CASE("sorted: const iteration", "[sorted][const]") {
Vec ns = {4, 0, 5, 1, 6, 7, 9, 3, 2, 8};
const auto s = sorted(ns);
Vec v(std::begin(s), std::end(s));
Vec vc = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
REQUIRE(v == vc);
}
//FIXME: This test currently fails (STL assertion fails on MSVC with debug library, simple test failure on gcc). The problem is 'sorted' will sort twice, once for non-const and once for const container; the resulting iterators are thus not on the same container (violating domain of == as specified in C++17 [forward.iterators]�2). Remove [!hide] tag when fixed.
TEST_CASE("sorted: const iterators can be compared to non-const iterators",
"[sorted][const][!hide]") {
auto s = sorted(Vec{1});
const auto& cs = s;
REQUIRE(std::begin(s) == std::begin(cs));
}
TEST_CASE("sorted: can modify elements through sorted", "[sorted]") {
std::vector<int> ns(3, 9);
for (auto&& n : sorted(ns)) {
n = -1;
}
Vec vc(3, -1);
REQUIRE(ns == vc);
}
char inc_vowels(char c) {
return c == 'a' || c == 'e' ? c + 10 : c;
}
TEST_CASE("sorted: Works with different begin and end types", "[sorted]") {
using VecC = std::vector<char>;
CharRange cr{'g'};
auto s =
sorted(cr, [](char x, char y) { return inc_vowels(x) < inc_vowels(y); });
VecC v(s.begin(), s.end());
VecC vc{'b', 'c', 'd', 'f', 'a', 'e'};
REQUIRE(v == vc);
}
TEST_CASE("sorted: can iterate over unordered container", "[sorted]") {
std::unordered_set<int> ns = {1, 3, 2, 0, 4};
auto s = sorted(ns);
Vec v(std::begin(s), std::end(s));
Vec vc = {0, 1, 2, 3, 4};
REQUIRE(v == vc);
}
TEST_CASE("sorted: empty when iterable is empty", "[sorted]") {
Vec ns{};
auto s = sorted(ns);
REQUIRE(std::begin(s) == std::end(s));
}
namespace {
bool int_greater_than(int lhs, int rhs) {
return lhs > rhs;
}
struct IntGreaterThan {
bool operator()(int lhs, int rhs) const {
return lhs > rhs;
}
};
}
TEST_CASE("sorted: works with different callable types", "[sorted]") {
Vec ns = {4, 1, 3, 2, 0};
std::vector<int> v;
SECTION("with function pointer") {
auto s = sorted(ns, int_greater_than);
v.assign(std::begin(s), std::end(s));
}
SECTION("with callable object") {
SECTION("Normal call") {
auto s = sorted(ns, IntGreaterThan{});
v.assign(std::begin(s), std::end(s));
}
SECTION("Pipe") {
auto s = ns | sorted(IntGreaterThan{});
v.assign(std::begin(s), std::end(s));
}
}
SECTION("with lambda") {
auto s = sorted(ns, [](int lhs, int rhs) { return lhs > rhs; });
v.assign(std::begin(s), std::end(s));
}
Vec vc = {4, 3, 2, 1, 0};
REQUIRE(v == vc);
}
namespace {
template <typename T>
class BasicIterableWithConstDeref {
private:
T* data;
std::size_t size;
bool was_moved_from_ = false;
public:
BasicIterableWithConstDeref(std::initializer_list<T> il)
: data{new T[il.size()]}, size{il.size()} {
// would like to use enumerate, can't because it's for unit
// testing enumerate
std::size_t i = 0;
for (auto&& e : il) {
data[i] = e;
++i;
}
}
BasicIterableWithConstDeref& operator=(
BasicIterableWithConstDeref&&) = delete;
BasicIterableWithConstDeref& operator=(
const BasicIterableWithConstDeref&) = delete;
BasicIterableWithConstDeref(const BasicIterableWithConstDeref&) = delete;
BasicIterableWithConstDeref(BasicIterableWithConstDeref&& other)
: data{other.data}, size{other.size} {
other.data = nullptr;
other.was_moved_from_ = true;
}
bool was_moved_from() const {
return this->was_moved_from_;
}
~BasicIterableWithConstDeref() {
delete[] this->data;
}
class Iterator {
private:
T* p;
public:
Iterator(T* b) : p{b} {}
bool operator!=(const Iterator& other) const {
return this->p != other.p;
}
Iterator& operator++() {
++this->p;
return *this;
}
T& operator*() {
return *this->p;
}
const T& operator*() const {
return *this->p;
}
};
Iterator begin() {
return {this->data};
}
Iterator end() {
return {this->data + this->size};
}
};
}
TEST_CASE("sorted: moves rvalues and binds to lvalues", "[sorted]") {
BasicIterableWithConstDeref<int> bi{1, 2};
sorted(bi);
REQUIRE_FALSE(bi.was_moved_from());
sorted(std::move(bi));
REQUIRE(bi.was_moved_from());
}
TEST_CASE("sorted: doesn't move or copy elements of iterable", "[sorted]") {
using itertest::SolidInt;
constexpr SolidInt arr[] = {{6}, {7}, {8}};
for (auto &&i : sorted(arr, [](const SolidInt&lhs, const SolidInt&rhs) {
return lhs.getint() < rhs.getint();
})) {
(void)i;
}
}
template <typename T>
using ImpT = decltype(sorted(std::declval<T>()));
TEST_CASE("sorted: has correct ctor and assign ops", "[sorted]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}
TEST_CASE("sorted: iterator meets requirements", "[sorted]") {
Vec v;
auto r = sorted(v);
REQUIRE(itertest::IsIterator<decltype(std::begin(r))>::value);
}