forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sliding_window.cpp
More file actions
134 lines (116 loc) · 3.79 KB
/
Copy pathtest_sliding_window.cpp
File metadata and controls
134 lines (116 loc) · 3.79 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
#include <cppitertools/sliding_window.hpp>
#include <array>
#include <string>
#include <utility>
#include <vector>
#include "catch.hpp"
#include "helpers.hpp"
using iter::sliding_window;
using Vec = const std::vector<int>;
TEST_CASE("sliding_window: window of size 3", "[sliding_window]") {
Vec ns = {10, 20, 30, 40, 50};
std::vector<std::vector<int>> vc = {{10, 20, 30}, {20, 30, 40}, {30, 40, 50}};
std::vector<std::vector<int>> v;
SECTION("Normal call") {
for (auto&& win : sliding_window(ns, 3)) {
v.emplace_back(std::begin(win), std::end(win));
}
}
SECTION("Pipe") {
for (auto&& win : ns | sliding_window(3)) {
v.emplace_back(std::begin(win), std::end(win));
}
}
REQUIRE(v == vc);
}
TEST_CASE("sliding_window: const iteration", "[sliding_window][const]") {
Vec ns = {10, 20, 30, 40, 50};
std::vector<std::vector<int>> vc = {{10, 20, 30}, {20, 30, 40}, {30, 40, 50}};
std::vector<std::vector<int>> v;
const auto sw = sliding_window(ns, 3);
for (auto&& win : sw) {
v.emplace_back(std::begin(win), std::end(win));
}
REQUIRE(v == vc);
}
TEST_CASE(
"sliding_window: const iterators can be compared to non-const iterators",
"[sliding_window][const]") {
auto sw = sliding_window(Vec{}, 2);
const auto& csw = sw;
(void)(std::begin(sw) == std::end(csw));
}
TEST_CASE("sliding_window: Works with different begin and end types",
"[sliding_window]") {
CharRange cr{'f'};
std::vector<std::vector<char>> results;
for (auto&& g : sliding_window(cr, 3)) {
results.emplace_back(std::begin(g), std::end(g));
}
std::vector<std::vector<char>> rc = {
{'a', 'b', 'c'}, {'b', 'c', 'd'}, {'c', 'd', 'e'}};
REQUIRE(results == rc);
}
TEST_CASE("sliding window: oversized window is empty", "[sliding_window]") {
Vec ns = {10, 20, 30};
auto sw = sliding_window(ns, 5);
REQUIRE(std::begin(sw) == std::end(sw));
}
TEST_CASE("sliding window: window size == len(iterable)", "[sliding_window]") {
Vec ns = {10, 20, 30};
auto sw = sliding_window(ns, 3);
auto it = std::begin(sw);
REQUIRE(it != std::end(sw));
Vec v(std::begin(*it), std::end(*it));
REQUIRE(ns == v);
++it;
REQUIRE(it == std::end(sw));
}
TEST_CASE("sliding window: empty iterable is empty", "[sliding_window]") {
Vec ns{};
auto sw = sliding_window(ns, 1);
REQUIRE(std::begin(sw) == std::end(sw));
}
TEST_CASE("sliding window: window size of 1", "[sliding_window]") {
Vec ns = {10, 20, 30};
auto sw = sliding_window(ns, 1);
auto it = std::begin(sw);
REQUIRE(*std::begin(*it) == 10);
++it;
REQUIRE(*std::begin(*it) == 20);
++it;
REQUIRE(*std::begin(*it) == 30);
++it;
REQUIRE(it == std::end(sw));
}
TEST_CASE("sliding window: window size of 0", "[sliding_window]") {
Vec ns = {10, 20, 30};
auto sw = sliding_window(ns, 0);
REQUIRE(std::begin(sw) == std::end(sw));
}
TEST_CASE(
"sliding window: moves rvalues and binds to lvalues", "[sliding_window]") {
itertest::BasicIterable<int> bi{1, 2};
sliding_window(bi, 1);
REQUIRE_FALSE(bi.was_moved_from());
sliding_window(std::move(bi), 1);
REQUIRE(bi.was_moved_from());
}
TEST_CASE("sliding window: doesn't copy elements", "[sliding_window]") {
constexpr std::array<itertest::SolidInt, 3> arr{{{6}, {7}, {8}}};
for (auto&& i : sliding_window(arr, 1)) {
(void)*std::begin(i);
}
}
TEST_CASE("sliding_window: iterator meets requirements", "[sliding_window]") {
std::string s{"abcdef"};
auto c = sliding_window(s, 2);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
template <typename T>
using ImpT = decltype(sliding_window(std::declval<T>(), 1));
TEST_CASE(
"sliding_window: has correct ctor and assign ops", "[sliding_window]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}