This repository was archived by the owner on Nov 15, 2022. It is now read-only.
forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_enumerate.cpp
More file actions
123 lines (100 loc) · 2.99 KB
/
Copy pathtest_enumerate.cpp
File metadata and controls
123 lines (100 loc) · 2.99 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
#include <enumerate.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#include <utility>
#include <sstream>
namespace Catch {
template <typename A, typename B>
std::string toString( const std::pair<A, B>& p) {
std::ostringstream oss;
oss << '{' << p.first << ", " << p.second << '}';
return oss.str();
}
}
#include "catch.hpp"
using Vec = std::vector<std::pair<std::size_t, char>>;
using iter::enumerate;
using itertest::BasicIterable;
using itertest::SolidInt;
TEST_CASE("Basic Functioning enumerate", "[enumerate]") {
std::string str = "abc";
auto e = enumerate(str);
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'a'}, {1, 'b'}, {2, 'c'}};
REQUIRE( v == vc );
}
TEST_CASE("Empty enumerate", "[enumerate]") {
std::string emp{};
auto e = enumerate(emp);
REQUIRE( std::begin(e) == std::end(e) );
}
TEST_CASE("Postfix ++ enumerate", "[enumerate]") {
std::string s{"amz"};
auto e = enumerate(s);
auto it = std::begin(e);
it++;
REQUIRE( (*it).first == 1 );
}
TEST_CASE("Modifications through enumerate affect container", "[enumerate]") {
std::vector<int> v{1, 2, 3, 4};
std::vector<int> vc(v.size(), -1);
for (auto&& p : enumerate(v)){
p.second = -1;
}
REQUIRE( v == vc );
}
TEST_CASE("enumerate with static array works", "[enumerate]") {
char arr[] = {'w', 'x', 'y'};
SECTION("Conversion to vector") {
auto e = enumerate(arr);
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'w'}, {1, 'x'}, {2, 'y'}};
REQUIRE( v == vc );
}
SECTION("Modification through enumerate") {
for (auto&& p : enumerate(arr)) {
p.second = 'z';
}
std::vector<char> v(std::begin(arr), std::end(arr));
decltype(v) vc(v.size(), 'z');
REQUIRE( v == vc );
}
}
TEST_CASE("initializer_list works", "[enumerate]") {
auto e = enumerate({'a', 'b', 'c'});
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'a'}, {1, 'b'}, {2, 'c'}};
REQUIRE( v == vc );
}
TEST_CASE("binds reference when it should", "[enumerate]") {
BasicIterable<char> bi{'x', 'y', 'z'};
auto e = enumerate(bi);
(void)e;
REQUIRE_FALSE( bi.was_moved_from() );
}
TEST_CASE("moves rvalues into enumerable object", "[enumerate]") {
BasicIterable<char> bi{'x', 'y', 'z'};
auto e = enumerate(std::move(bi));
REQUIRE( bi.was_moved_from());
(void)e;
}
TEST_CASE("Works with const iterable", "[enumerate]") {
const std::string s{"ace"};
auto e = enumerate(s);
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'a'}, {1, 'c'}, {2, 'e'}};
REQUIRE( v == vc );
}
TEST_CASE("Doesn't move or copy elements of iterable", "[enumerate]") {
constexpr SolidInt arr[] = {{6}, {7}, {8}};
for (auto&& i : enumerate(arr)) {
(void)i;
}
}
TEST_CASE("enumerate: iterator meets requirements", "[enumerate]") {
std::string s{};
auto c = enumerate(s);
REQUIRE( itertest::IsIterator<decltype(std::begin(c))>::value );
}