forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_repeat.cpp
More file actions
94 lines (78 loc) · 2.35 KB
/
Copy pathtest_repeat.cpp
File metadata and controls
94 lines (78 loc) · 2.35 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
#include <repeat.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#include "catch.hpp"
using iter::repeat;
TEST_CASE("repeat: one argument keeps giving value back", "[repeat]") {
auto r = repeat('a');
auto it = std::begin(r);
REQUIRE(*it == 'a');
++it;
REQUIRE(*it == 'a');
++it;
REQUIRE(*it == 'a');
++it;
REQUIRE(*it == 'a');
++it;
REQUIRE(*it == 'a');
}
TEST_CASE("repeat: can be used as constexpr", "[repeat]") {
static constexpr char c = 'a';
{
constexpr auto r = repeat(c);
constexpr auto i = r.begin();
constexpr char c2 = *i;
static_assert(c == c2, "repeat value not as expected");
constexpr auto i2 = ++i;
(void)i2;
}
{
constexpr static auto r = repeat('a');
constexpr auto i = r.begin();
constexpr char c2 = *i;
static_assert(c2 == 'a', "repeat value not as expected");
}
{
constexpr auto r = repeat(c, 2);
constexpr auto i = r.begin();
constexpr char c2 = *i;
static_assert(c2 == c, "repeat value not as expected");
}
}
TEST_CASE("repeat: two argument repeats a number of times", "[repeat]") {
auto r = repeat('a', 3);
std::string s(std::begin(r), std::end(r));
REQUIRE(s == "aaa");
}
TEST_CASE("repeat: 0 count gives empty sequence", "[repeat]") {
auto r = repeat('a', 0);
REQUIRE(std::begin(r) == std::end(r));
}
TEST_CASE("repeat: negative count gives empty sequence", "[repeat]") {
auto r = repeat('a', -2);
REQUIRE(std::begin(r) == std::end(r));
auto r2 = repeat('a', -1);
REQUIRE(std::begin(r2) == std::end(r2));
}
TEST_CASE("repeat: doesn't duplicate item", "[repeat]") {
itertest::SolidInt si{2};
auto r = repeat(si);
auto it = std::begin(r);
(void)*it;
}
TEST_CASE("repeat: iterator meets requirements", "[repeat]") {
auto r = repeat(1);
REQUIRE(itertest::IsIterator<decltype(std::begin(r))>::value);
}
template <typename T>
using ImpT = decltype(repeat(std::declval<T>()));
template <typename T>
using ImpT2 = decltype(repeat(std::declval<T>(), 1));
TEST_CASE("repeat: has correct ctor and assign ops", "[repeat]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT2<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT2<std::string>>::value);
}