forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_takewhile.cpp
More file actions
112 lines (95 loc) · 2.88 KB
/
Copy pathtest_takewhile.cpp
File metadata and controls
112 lines (95 loc) · 2.88 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
#include <takewhile.hpp>
#include <vector>
#include <array>
#include <string>
#include <utility>
#include "helpers.hpp"
#include "catch.hpp"
using iter::takewhile;
using Vec = const std::vector<int>;
namespace {
bool under_ten(int i) {
return i < 10;
}
struct UnderTen {
bool operator()(int i) {
return i < 10;
}
};
}
TEST_CASE("takewhile: works with lambda, callable, and function pointer",
"[takewhile]") {
Vec ns = {1, 3, 5, 20, 2, 4, 6, 8};
SECTION("function pointer") {
auto tw = takewhile(under_ten, ns);
Vec v(std::begin(tw), std::end(tw));
Vec vc = {1, 3, 5};
REQUIRE(v == vc);
}
SECTION("callable object") {
auto tw = takewhile(UnderTen{}, ns);
Vec v(std::begin(tw), std::end(tw));
Vec vc = {1, 3, 5};
REQUIRE(v == vc);
}
SECTION("lambda") {
auto tw = takewhile([](int i) { return i < 10; }, ns);
Vec v(std::begin(tw), std::end(tw));
Vec vc = {1, 3, 5};
REQUIRE(v == vc);
}
}
TEST_CASE("takewhile: everything passes predicate", "[takewhile]") {
Vec ns{1, 2, 3};
auto tw = takewhile(under_ten, ns);
Vec v(std::begin(tw), std::end(tw));
Vec vc = {1, 2, 3};
}
TEST_CASE("takewhile: empty iterable is empty", "[takewhile]") {
Vec ns{};
auto tw = takewhile(under_ten, ns);
REQUIRE(std::begin(tw) == std::end(tw));
}
TEST_CASE(
"takewhile: when first element fails predicate, it's empty"
"[takewhile]") {
SECTION("First element is only element") {
Vec ns = {20};
auto tw = takewhile(under_ten, ns);
REQUIRE(std::begin(tw) == std::end(tw));
}
SECTION("First element followed by elements that pass") {
Vec ns = {20, 1, 1};
auto tw = takewhile(under_ten, ns);
REQUIRE(std::begin(tw) == std::end(tw));
}
}
TEST_CASE("takewhile: moves rvalues, binds to lvalues", "[takewhile]") {
itertest::BasicIterable<int> bi{1, 2};
takewhile(under_ten, bi);
REQUIRE_FALSE(bi.was_moved_from());
takewhile(under_ten, std::move(bi));
REQUIRE(bi.was_moved_from());
}
TEST_CASE(
"takewhile: with iterable doesn't move or copy elements", "[takewhile]") {
constexpr std::array<itertest::SolidInt, 3> arr{{{8}, {9}, {10}}};
auto func = [](const itertest::SolidInt& si) { return si.getint() < 10; };
for (auto&& i : takewhile(func, arr)) {
(void)i;
}
}
TEST_CASE("takewhile: iterator meets requirements", "[takewhile]") {
std::string s{};
auto c = takewhile([] { return true; }, s);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
template <typename T, typename U>
using ImpT = decltype(takewhile(std::declval<T>(), std::declval<U>()));
TEST_CASE("takewhile: has correct ctor and assign ops", "[takewhile]") {
using T1 = ImpT<bool (*)(char c), std::string&>;
auto lam = [](char) { return false; };
using T2 = ImpT<decltype(lam), std::string>;
REQUIRE(itertest::IsMoveConstructibleOnly<T1>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<T2>::value);
}