forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_accumulate.cpp
More file actions
155 lines (133 loc) · 3.97 KB
/
Copy pathtest_accumulate.cpp
File metadata and controls
155 lines (133 loc) · 3.97 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <accumulate.hpp>
#include "helpers.hpp"
#include <iterator>
#include <string>
#include <vector>
#include "catch.hpp"
using iter::accumulate;
using itertest::BasicIterable;
using Vec = const std::vector<int>;
TEST_CASE("Simple sum", "[accumulate]") {
Vec ns{1, 2, 3, 4, 5};
std::vector<int> v;
SECTION("Normal call") {
auto a = accumulate(ns);
v.assign(std::begin(a), std::end(a));
}
SECTION("Pipe") {
auto a = ns | accumulate;
v.assign(std::begin(a), std::end(a));
}
Vec vc{1, 3, 6, 10, 15};
REQUIRE(v == vc);
}
TEST_CASE("accumulate: With subtraction lambda", "[accumulate]") {
Vec ns{5, 4, 3, 2, 1};
std::vector<int> v;
SECTION("Normal call") {
auto a = accumulate(ns, [](int a, int b) { return a - b; });
v.assign(std::begin(a), std::end(a));
}
SECTION("Pipe") {
auto a = ns | accumulate([](int a, int b) { return a - b; });
v.assign(std::begin(a), std::end(a));
}
Vec vc{5, 1, -2, -4, -5};
REQUIRE(v == vc);
}
TEST_CASE("accumulate: handles pointer to member function", "[accumulate]") {
using itertest::Point;
std::vector<Point> ps = {{1, 2}, {10, 50}, {300, 600}};
auto a = accumulate(ps, &Point::add);
const std::vector<Point> v(std::begin(a), std::end(a));
const std::vector<Point> vc = {{1, 2}, {11, 52}, {311, 652}};
REQUIRE(v == vc);
}
TEST_CASE("accumulate: const iterators", "[accumulate][const]") {
std::vector<int> v;
SECTION("lvalue") {
Vec ns{1, 2, 3, 4, 5};
const auto a = accumulate(ns);
v.assign(std::begin(a), std::end(a));
}
SECTION("rvalue") {
const auto a = accumulate(Vec{1, 2, 3, 4, 5});
v.assign(std::begin(a), std::end(a));
}
SECTION("const lvalue") {
const Vec ns{1, 2, 3, 4, 5};
const auto a = accumulate(ns);
v.assign(std::begin(a), std::end(a));
}
Vec vc{1, 3, 6, 10, 15};
REQUIRE(v == vc);
}
TEST_CASE(
"accumulate: const iterators can be compared", "[accumulate][const]") {
auto e = accumulate(std::string("hello"));
const auto& ce = e;
(void)(std::begin(e) == std::end(ce));
}
struct Integer {
const int value;
constexpr Integer(int i) : value{i} {}
constexpr Integer operator+(Integer other) const noexcept {
return {this->value + other.value};
}
};
TEST_CASE("accumulate: intermidate type need not be default constructible",
"[accumulate]") {
std::vector<Integer> v = {{2}, {3}, {10}};
auto a = accumulate(v, std::plus<Integer>{});
(void)std::begin(a);
}
TEST_CASE("accumulate: binds reference when it should", "[accumulate]") {
BasicIterable<int> bi{1, 2};
accumulate(bi);
REQUIRE_FALSE(bi.was_moved_from());
}
TEST_CASE("accumulate: moves rvalues when it should", "[accumulate]") {
BasicIterable<int> bi{1, 2};
accumulate(std::move(bi));
REQUIRE(bi.was_moved_from());
}
TEST_CASE("accumulate: operator==", "[accumulate]") {
Vec v;
auto a = accumulate(v);
REQUIRE(std::begin(a) == std::end(a));
}
TEST_CASE("accumulate: postfix ++", "[accumulate]") {
Vec ns{2, 3};
auto a = accumulate(ns);
auto it = std::begin(a);
it++;
REQUIRE(*it == 5);
}
TEST_CASE("accumulate: operator->", "[accumulate]") {
Vec ns{7, 3};
auto a = accumulate(ns);
auto it = std::begin(a);
const int* p = it.operator->();
REQUIRE(*p == 7);
}
TEST_CASE("accumulate: iterator meets requirements", "[accumulate]") {
Vec ns{};
auto a = accumulate(ns, [](int a, int b) { return a + b; });
auto it = std::begin(a);
it = std::begin(a);
REQUIRE(itertest::IsIterator<decltype(std::begin(a))>::value);
}
TEST_CASE(
"accumulate: Works with different begin and end types", "[accumulate]") {
CharRange cr{'d'};
auto a = accumulate(cr);
Vec v(a.begin(), a.end());
Vec vc{'a', 'a' + 'b', 'a' + 'b' + 'c'};
REQUIRE(v == vc);
}
template <typename T>
using ImpT = decltype(accumulate(std::declval<T>()));
TEST_CASE("accumulate: has correct ctor and assign ops", "[accumulate]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}