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
91 lines (76 loc) · 2.65 KB
/
Copy pathtest_iterbase.cpp
File metadata and controls
91 lines (76 loc) · 2.65 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
// 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 <internal/iterbase.hpp>
#include <type_traits>
#include <vector>
#include <iterator>
#include <string>
#include <list>
#include <enumerate.hpp>
#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(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]") {
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]") {
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);
dh.reset(std::move(b));
REQUIRE(dh.get() == 5);
}