forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_iteratoriterator.cpp
More file actions
169 lines (147 loc) · 4.34 KB
/
test_iteratoriterator.cpp
File metadata and controls
169 lines (147 loc) · 4.34 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
#include <internal/iteratoriterator.hpp>
#include <string>
#include <vector>
#include <utility>
#include "catch.hpp"
using iter::impl::IterIterWrapper;
TEST_CASE("Iterator over a vector of vector iterators", "[iteratoriterator]") {
using std::vector;
vector<int> v = {2, 4, 6, 8};
IterIterWrapper<vector<vector<int>::iterator>> itr;
itr.get().push_back(std::begin(v) + 1);
itr.get().push_back(std::end(v) - 1);
itr.get().push_back(std::begin(v));
auto it = std::begin(itr);
REQUIRE(*it == 4);
REQUIRE(it != std::end(itr));
++it;
REQUIRE(*it == 8);
it++;
REQUIRE(*it == 2);
++it;
REQUIRE(it == std::end(itr));
REQUIRE(itr[0] == 4);
REQUIRE(itr[1] == 8);
REQUIRE(itr[2] == 2);
auto rit = itr.rbegin();
REQUIRE(*rit == 2);
REQUIRE(rit != itr.rend());
++rit;
REQUIRE(*rit == 8);
++rit;
REQUIRE(*rit == 4);
++rit;
REQUIRE(rit == itr.rend());
}
TEST_CASE("IteratorIterator operator->", "[iteratoriterator]") {
using std::vector;
using std::string;
vector<string> v = {"hello", "everyone"};
IterIterWrapper<vector<vector<string>::iterator>> itritr;
itritr.get().push_back(std::end(v) - 1);
itritr.get().push_back(std::begin(v));
auto it = std::begin(itritr);
REQUIRE(it->size() == 8);
}
TEST_CASE("Iterate over a vector of string iterators", "[iteratoriterator]") {
std::string str = "hello world";
IterIterWrapper<std::vector<std::string::iterator>> itritr;
auto it = std::begin(itritr);
static_assert(std::is_same<decltype(*it),
std::iterator_traits<decltype(it)>::reference>::value,
"iterator is mis marked");
}
TEST_CASE("IteratorIterator supports mutable RandomAccessIterator operators",
"[iteratoriterator]") {
using std::vector;
struct S {
int value;
};
vector<S> v = {{2}, {4}, {6}, {8}};
IterIterWrapper<vector<vector<S>::iterator>> itr;
itr.get().push_back(std::begin(v) + 1);
itr.get().push_back(std::end(v) - 1);
itr.get().push_back(std::begin(v));
// RandomAccessIterator (and ForwardIterator):
auto a = itr.begin();
auto r = a;
auto r2 = r;
((r += 2) -= 2) += 2;
REQUIRE(&(++r2) == &r2); // Required by OutputIterator.
REQUIRE(&(*r2++) == &a[1]);
REQUIRE(r == r2);
auto test_const_or_not = [&itr](auto& a, auto& b) {
REQUIRE(!(b == a));
REQUIRE(b == a + 2);
REQUIRE(b == 2 + a);
REQUIRE(b - 2 == a);
REQUIRE(&a[2] == &b[0]);
REQUIRE(b - a == 2);
REQUIRE(a < b);
REQUIRE(!(a < a));
REQUIRE(b > a);
REQUIRE(!(a > a));
REQUIRE(a <= b);
REQUIRE(!(b <= a));
REQUIRE(a <= a);
REQUIRE(b >= a);
REQUIRE(!(a >= b));
REQUIRE(a >= a);
// InputIterator:
REQUIRE(b != a);
REQUIRE(!(a != a));
REQUIRE(&(*a) != &(*b));
REQUIRE(&(a->value) == &(*a).value);
// Added methods, not from ...Iterator:
REQUIRE(a.get() == std::begin(itr.get()));
};
test_const_or_not(a, r);
test_const_or_not(std::as_const(a), r);
test_const_or_not(a, std::as_const(r));
test_const_or_not(std::as_const(a), std::as_const(r));
// BidirectionalIterator (and RandomAccessIterator):
REQUIRE((--r)-- == a + 1);
REQUIRE(r == a);
REQUIRE(&(*r2--) == &a[2]);
// OutputIterator (and RandomAccessIterator):
*r++ = {10};
REQUIRE(r == a + 1);
REQUIRE(v[1].value == 10);
*++r = {12};
REQUIRE(r == a + 2);
REQUIRE(v[0].value == 12);
*r = {14};
REQUIRE(r == a + 2);
REQUIRE(v[0].value == 14);
a[1] = {16};
REQUIRE(a == itr.begin());
REQUIRE(v[3].value == 16);
}
TEST_CASE("IterIterWrapper supports several SequenceContainer methodes",
"[iteratoriterator]") {
using std::vector;
vector<int> v = {2, 4, 6, 8};
IterIterWrapper<vector<vector<int>::iterator>> itr;
itr.get().push_back(std::begin(v) + 1);
itr.get().push_back(std::end(v) - 1);
auto test_const_or_not = [&v](auto& c) {
REQUIRE(c.at(0) == 4);
REQUIRE(c.at(1) == 8);
REQUIRE(c[0] == 4);
REQUIRE(c[1] == 8);
REQUIRE(!c.empty());
REQUIRE(c.size() == 2);
REQUIRE(*c.begin() == 4);
REQUIRE(*(c.end() - 1) == 8);
REQUIRE(*c.cbegin() == 4);
REQUIRE(*(c.cend() - 1) == 8);
REQUIRE(*c.rbegin() == 8);
REQUIRE(*(c.rend() - 1) == 4);
REQUIRE(*c.crbegin() == 8);
REQUIRE(*(c.crend() - 1) == 4);
// Added methods, not from SequenceContainer:
REQUIRE(c.get()[0] == std::begin(v) + 1);
};
test_const_or_not(itr);
test_const_or_not(std::as_const(itr));
}