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_dropwhile.cpp
More file actions
91 lines (76 loc) · 2.44 KB
/
Copy pathtest_dropwhile.cpp
File metadata and controls
91 lines (76 loc) · 2.44 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
#include <dropwhile.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#include "catch.hpp"
using iter::dropwhile;
using Vec = const std::vector<int>;
TEST_CASE("dropwhile: skips initial elements", "[dropwhile]") {
Vec ns{1,2,3,4,5,6,7,8};
auto d = dropwhile([](int i){return i < 5; }, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {5,6,7,8};
REQUIRE( v == vc );
}
TEST_CASE("dropwhile: doesn't skip anything if it shouldn't", "[dropwhile]") {
Vec ns {3,4,5,6};
auto d = dropwhile([](int i){return i < 3; }, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {3,4,5,6};
REQUIRE( v == vc );
}
TEST_CASE("dropwhile: skips all elements when all are true under predicate",
"[dropwhile]") {
Vec ns {3,4,5,6};
auto d = dropwhile([](int i){return i != 0; }, ns);
REQUIRE( std::begin(d) == std::end(d) );
}
TEST_CASE("dropwhile: empty case is empty", "[dropwhile]") {
Vec ns{};
auto d = dropwhile([](int i){return i != 0; }, ns);
REQUIRE( std::begin(d) == std::end(d) );
}
TEST_CASE("dropwhile: only drops from beginning", "[dropwhile]") {
Vec ns {1,2,3,4,5,6,5,4,3,2,1};
auto d = dropwhile([](int i){return i < 5; }, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {5,6,5,4,3,2,1};
REQUIRE( v == vc );
}
namespace {
int less_than_five(int i) {
return i < 5;
}
}
TEST_CASE("dropwhile: works with function pointer", "[dropwhile]") {
Vec ns{1,2,3,4,5,6,7,8};
auto d = dropwhile(less_than_five, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {5,6,7,8};
REQUIRE( v == vc );
}
TEST_CASE("dropwhile: binds to lvalues, moves rvalues", "[dropwhile]") {
itertest::BasicIterable<int> bi{1,2,3,4};
SECTION("binds to lvalues") {
dropwhile(less_than_five, bi);
REQUIRE_FALSE( bi.was_moved_from() );
}
SECTION("moves rvalues") {
dropwhile(less_than_five, std::move(bi));
REQUIRE( bi.was_moved_from() );
}
}
TEST_CASE("dropwhile: doesn't move or copy elements of iterable",
"[dropwhile]") {
constexpr itertest::SolidInt arr[] = {{6}, {7}, {8}};
for (auto&& i : dropwhile(
[](const itertest::SolidInt&){return false;} , arr)) {
(void)i;
}
}
TEST_CASE("dropwhile: iterator meets requirements", "[dropwhile]") {
std::string s{};
auto c = dropwhile([]{return true;}, s);
REQUIRE( itertest::IsIterator<decltype(std::begin(c))>::value );
}