forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cycle.cpp
More file actions
132 lines (113 loc) · 2.99 KB
/
Copy pathtest_cycle.cpp
File metadata and controls
132 lines (113 loc) · 2.99 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
#include <cycle.hpp>
#include "helpers.hpp"
#include <iterator>
#include <string>
#include <vector>
#include "catch.hpp"
using iter::cycle;
TEST_CASE("cycle: iterate twice", "[cycle]") {
std::vector<int> ns{2, 4, 6};
std::vector<int> v{};
std::size_t count = 0;
for (auto i : cycle(ns)) {
v.push_back(i);
++count;
if (count == ns.size() * 2) {
break;
}
}
auto vc = ns;
vc.insert(std::end(vc), std::begin(ns), std::end(ns));
REQUIRE(v == vc);
}
TEST_CASE("cycle: const iteration, iterate twice", "[cycle][const]") {
std::vector<int> ns{2, 4, 6};
std::vector<int> v{};
std::size_t count = 0;
const auto c = cycle(ns);
for (auto i : c) {
v.push_back(i);
++count;
if (count == ns.size() * 2) {
break;
}
}
auto vc = ns;
vc.insert(std::end(vc), std::begin(ns), std::end(ns));
REQUIRE(v == vc);
}
TEST_CASE("cycle: const iterators can be compared to non-const iterators",
"[cycle][const]") {
auto c = cycle(std::vector<int>{});
const auto& cc = c;
(void)(std::begin(c) == std::end(cc));
}
TEST_CASE("cycle: Works with different begin and end types", "[cycle]") {
constexpr auto sz = 'd' - 'a';
CharRange cr{'d'};
const std::vector<char> vc{'a', 'b', 'c', 'a', 'b', 'c'};
std::vector<char> v;
std::size_t count = 0;
for (auto i : cycle(cr)) {
v.push_back(i);
++count;
if (count == sz * 2) {
break;
}
}
REQUIRE(v == vc);
}
TEST_CASE("cycle: with pipe", "[cycle]") {
std::vector<int> ns{2, 4, 6};
std::vector<int> v;
std::size_t count = 0;
for (auto i : ns | cycle) {
v.push_back(i);
++count;
if (count == ns.size() * 2) {
break;
}
}
auto vc = ns;
vc.insert(std::end(vc), std::begin(ns), std::end(ns));
REQUIRE(v == vc);
}
TEST_CASE("cycle: empty cycle terminates", "[cycle]") {
std::vector<int> ns;
auto c = cycle(ns);
std::vector<int> v(std::begin(c), std::end(c));
REQUIRE(v.empty());
}
TEST_CASE("cycle: binds to lvalues, moves rvalues", "[cycle]") {
itertest::BasicIterable<char> bi{'x', 'y', 'z'};
SECTION("binds to lvalues") {
cycle(bi);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
cycle(std::move(bi));
REQUIRE(bi.was_moved_from());
}
}
TEST_CASE("cycle: doesn't move or copy elements of iterable", "[cycle]") {
constexpr itertest::SolidInt arr[] = {{6}, {7}, {8}};
auto c = cycle(arr);
*std::begin(c);
}
TEST_CASE("cycle: iterator meets requirements", "[cycle]") {
std::string s{};
auto c = cycle(s);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
TEST_CASE("cycle: arrow works", "[cycle]") {
std::vector<std::string> v = {"hello"};
auto c = cycle(v);
auto it = std::begin(c);
REQUIRE(it->size() == 5);
}
template <typename T>
using ImpT = decltype(cycle(std::declval<T>()));
TEST_CASE("cycle: has correct ctor and assign ops", "[cycle]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}