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_compress.cpp
More file actions
153 lines (127 loc) · 4.14 KB
/
Copy pathtest_compress.cpp
File metadata and controls
153 lines (127 loc) · 4.14 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
#include <compress.hpp>
#include "helpers.hpp"
#include <iterator>
#include <string>
#include <utility>
#include <vector>
#include "catch.hpp"
using iter::compress;
using itertest::SolidInt;
using itertest::BasicIterable;
using Vec = const std::vector<int>;
TEST_CASE("compress: alternating", "[compress]") {
std::vector<int> ivec{1, 2, 3, 4, 5, 6};
std::vector<bool> bvec{true, false, true, false, true, false};
auto c = compress(ivec, bvec);
Vec v(std::begin(c), std::end(c));
Vec vc = {1, 3, 5};
REQUIRE(v == vc);
}
TEST_CASE("compress: consecutive falses", "[compress]") {
std::vector<int> ivec{1, 2, 3, 4, 5};
std::vector<bool> bvec{true, false, false, false, true};
auto c = compress(ivec, bvec);
Vec v(std::begin(c), std::end(c));
Vec vc = {1, 5};
REQUIRE(v == vc);
}
TEST_CASE("compress: consecutive trues", "[compress]") {
std::vector<int> ivec{1, 2, 3, 4, 5};
std::vector<bool> bvec{false, true, true, true, false};
auto c = compress(ivec, bvec);
Vec v(std::begin(c), std::end(c));
Vec vc = {2, 3, 4};
REQUIRE(v == vc);
}
TEST_CASE("compress: all true", "[compress]") {
std::vector<int> ivec{1, 2, 3, 4, 5};
std::vector<bool> bvec(ivec.size(), true);
auto c = compress(ivec, bvec);
Vec v(std::begin(c), std::end(c));
REQUIRE(v == ivec);
}
TEST_CASE("compress: all false", "[compress]") {
std::vector<int> ivec{1, 2, 3, 4, 5};
std::vector<bool> bvec(ivec.size(), false);
auto c = compress(ivec, bvec);
REQUIRE(std::begin(c) == std::end(c));
}
TEST_CASE("compress: operator->", "[compress") {
std::vector<std::string> svec = {"a", "abc", "abcde"};
std::vector<bool> bvec = {false, false, true};
auto c = compress(svec, bvec);
auto it = std::begin(c);
REQUIRE(it->size() == 5);
}
TEST_CASE("compress: binds to lvalues, moves rvalues", "[compress]") {
BasicIterable<char> bi{'x', 'y', 'z'};
std::vector<bool> bl{true, true, true};
SECTION("binds to lvalues") {
compress(bi, bl);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
compress(std::move(bi), bl);
REQUIRE(bi.was_moved_from());
}
}
struct BoolLike {
public:
bool state;
explicit operator bool() const {
return this->state;
}
};
TEST_CASE("compress: workds with truthy and falsey values", "[compress]") {
std::vector<BoolLike> bvec{{true}, {false}, {true}, {false}};
Vec ivec{1, 2, 3, 4};
auto c = compress(ivec, bvec);
Vec v(std::begin(c), std::end(c));
Vec vc = {1, 3};
REQUIRE(v == vc);
}
TEST_CASE("compress: terminates on shorter selectors", "[compress]") {
std::vector<int> ivec{1, 2, 3, 4, 5};
std::vector<bool> bvec{true};
auto c = compress(ivec, bvec);
REQUIRE(std::distance(std::begin(c), std::end(c)) == 1);
}
TEST_CASE("compress: terminates on shorter data", "[compress]") {
std::vector<int> ivec{1};
std::vector<bool> bvec{true, true, true, true, true};
auto c = compress(ivec, bvec);
REQUIRE(std::distance(std::begin(c), std::end(c)) == 1);
}
TEST_CASE("compress: nothing on empty selectors", "[compress]") {
std::vector<int> ivec{1, 2, 3};
std::vector<bool> bvec{};
auto c = compress(ivec, bvec);
REQUIRE(std::begin(c) == std::end(c));
}
TEST_CASE("compress: nothing on empty data", "[compress]") {
std::vector<int> ivec{};
std::vector<bool> bvec{true, true, true};
auto c = compress(ivec, bvec);
REQUIRE(std::begin(c) == std::end(c));
}
TEST_CASE("compress: iterator meets requirements", "[compress]") {
std::string s{};
std::vector<bool> bv;
auto c = compress(s, bv);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
TEST_CASE("compress: Works with different begin and end types", "[compress]") {
CharRange cr{'d'};
auto c = compress(cr, std::vector<bool>{true, false, true});
Vec v(c.begin(), c.end());
Vec vc{'a', 'c'};
REQUIRE(v == vc);
}
template <typename T, typename U>
using ImpT = decltype(compress(std::declval<T>(), std::declval<U>()));
TEST_CASE("compress: has correct ctor and assign ops", "[compress]") {
using T1 = ImpT<std::string&, std::vector<bool>&>;
using T2 = ImpT<std::string, std::vector<bool>>;
REQUIRE(itertest::IsMoveConstructibleOnly<T1>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<T2>::value);
}