forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_iterbase.cpp
More file actions
131 lines (109 loc) · 3.55 KB
/
Copy pathtest_iterbase.cpp
File metadata and controls
131 lines (109 loc) · 3.55 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
// AGAIN the contents of iterbase are completely subject to change, do not rely
// on any of this. Users of the library must consider all of this undocumented
//
#include <cppitertools/enumerate.hpp>
#include <cppitertools/internal/iterbase.hpp>
#include <iterator>
#include <list>
#include <string>
#include <type_traits>
#include <vector>
#include "catch.hpp"
#include "helpers.hpp"
namespace it = iter::impl;
using IVec = std::vector<int>;
template <typename T>
using hrai = it::has_random_access_iter<T>;
TEST_CASE("Detects random access iterators correctly", "[iterbase]") {
REQUIRE(hrai<std::vector<int>>::value);
REQUIRE(hrai<std::string>::value);
REQUIRE(hrai<int[10]>::value);
REQUIRE_FALSE(hrai<std::list<int>>::value);
REQUIRE_FALSE(hrai<decltype(iter::enumerate(std::list<int>{}))>::value);
REQUIRE_FALSE(hrai<itertest::BasicIterable<int>>::value);
}
TEST_CASE("Detects correct iterator types", "[iterbase]") {
REQUIRE((std::is_same<it::iterator_type<IVec>, IVec::iterator>::value));
REQUIRE((std::is_same<it::iterator_type<IVec>, IVec::iterator>::value));
REQUIRE((std::is_same<it::iterator_deref<IVec>,
IVec::iterator::reference>::value));
REQUIRE((std::is_same<it::const_iterator_deref<IVec>,
IVec::iterator::reference>::value));
REQUIRE((std::is_same<it::iterator_traits_deref<IVec>,
IVec::iterator::value_type>::value));
REQUIRE(
(std::is_same<it::iterator_arrow<IVec>, IVec::iterator::pointer>::value));
REQUIRE((std::is_same<it::iterator_arrow<int[10]>, int*>::value));
}
TEST_CASE("advance, next, size", "[iterbase]") {
IVec v = {2, 4, 6, 8, 10, 12, 14, 16, 18};
auto itr = std::begin(v);
REQUIRE(it::apply_arrow(itr) == &v[0]);
it::dumb_advance_unsafe(itr, 3);
REQUIRE(itr == (std::begin(v) + 3));
REQUIRE(it::dumb_next(std::begin(v), 3) == std::begin(v) + 3);
REQUIRE(it::dumb_size(v) == v.size());
}
TEST_CASE("are_same", "[iterbase]") {
REQUIRE((it::are_same<int, int, int, int>::value));
REQUIRE_FALSE((it::are_same<double, int, int, int>::value));
REQUIRE_FALSE((it::are_same<int, int, int, double>::value));
REQUIRE_FALSE((it::are_same<int, double, int, int>::value));
}
TEST_CASE("DerefHolder lvalue reference", "[iterbase]") {
REQUIRE_FALSE(it::DerefHolder<int&>::stores_value);
it::DerefHolder<int&> dh;
int a = 2;
int b = 5;
REQUIRE_FALSE(dh);
dh.reset(a);
REQUIRE(dh);
REQUIRE(dh.get_ptr() == &a);
REQUIRE(&dh.get() == &a);
dh.reset(b);
REQUIRE(dh.get_ptr() == &b);
REQUIRE(&dh.get() == &b);
}
TEST_CASE("DerefHolder non-reference", "[iterbase]") {
REQUIRE(it::DerefHolder<int>::stores_value);
it::DerefHolder<int> dh;
int a = 2;
int b = 5;
REQUIRE_FALSE(dh);
dh.reset(std::move(a));
REQUIRE(dh.get() == 2);
REQUIRE(&dh.get() != &a);
REQUIRE(dh.get_ptr() != &a);
dh.reset(std::move(b));
REQUIRE(dh.get() == 5);
}
TEST_CASE("get_begin returns correct type", "[iterbase]") {
std::vector<int> v;
REQUIRE((std::is_same<decltype(it::get_begin(v)), decltype(v.begin())>{}));
}
namespace NS1 {
struct Dummy {
auto begin() {
return 0;
}
auto end() {
return 0;
}
};
template <typename T>
auto begin(T& t) {
return t.begin();
}
template <typename T>
auto end(T& t) {
return t.end();
}
} // namespace NS1
TEST_CASE("Detects is_iterable with ADL conflicts", "[iterbase]") {
int a[1]{};
const int b[1]{};
REQUIRE(iter::impl::is_iterable<NS1::Dummy>);
REQUIRE(iter::impl::is_iterable<std::vector<NS1::Dummy>>);
REQUIRE(iter::impl::is_iterable<decltype(a)>);
REQUIRE(iter::impl::is_iterable<decltype(b)>);
}