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
86 lines (72 loc) · 2.02 KB
/
Copy pathtest_cycle.cpp
File metadata and controls
86 lines (72 loc) · 2.02 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
#include <cycle.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#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: 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);
}