forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_count.cpp
More file actions
145 lines (121 loc) · 2.93 KB
/
Copy pathtest_count.cpp
File metadata and controls
145 lines (121 loc) · 2.93 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
#include <count.hpp>
#include "helpers.hpp"
#include <iterator>
#include <utility>
#include <vector>
#include "catch.hpp"
using iter::count;
TEST_CASE("count: watch for 10 elements", "[count]") {
std::vector<int> v{};
for (auto i : count()) {
v.push_back(i);
if (i == 9) break;
}
const std::vector<int> vc{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
REQUIRE(v == vc);
}
TEST_CASE("count: const watch for 10 elements", "[count][const]") {
std::vector<int> v{};
const auto c = count();
for (auto i : c) {
v.push_back(i);
if (i == 9) break;
}
const std::vector<int> vc{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
REQUIRE(v == vc);
}
TEST_CASE("count: start at 10", "[count]") {
std::vector<int> v{};
for (auto i : count(10)) {
v.push_back(i);
if (i == 14) break;
}
const std::vector<int> vc{10, 11, 12, 13, 14};
REQUIRE(v == vc);
}
TEST_CASE("count: with step", "[count]") {
std::vector<int> v{};
for (auto i : count(2, -1)) {
v.push_back(i);
if (i == -3) break;
}
const std::vector<int> vc{2, 1, 0, -1, -2, -3};
REQUIRE(v == vc);
}
TEST_CASE("count: with step > 1", "[count]") {
std::vector<int> v{};
for (auto i : count(10, 2)) {
v.push_back(i);
if (i == 16) break;
}
const std::vector<int> vc{10, 12, 14, 16};
REQUIRE(v == vc);
}
TEST_CASE("count: with unsigned", "[count]") {
constexpr unsigned int uint_max =
static_cast<unsigned int>(std::numeric_limits<int>::max());
std::vector<unsigned int> v{};
int steps = 0;
for (auto i : count(uint_max)) {
v.push_back(i);
if (steps == 2) {
break;
}
++steps;
}
const std::vector<unsigned int> vc{uint_max, uint_max + 1, uint_max + 2};
REQUIRE(v == vc);
}
TEST_CASE("count: with negative step", "[count]") {
std::vector<int> v{};
int steps = 0;
for (auto i : count(0, -1)) {
v.push_back(i);
if (steps == 2) {
break;
}
++steps;
}
const std::vector<int> vc{0, -1, -2};
REQUIRE(v == vc);
}
TEST_CASE("count: with double", "[count]") {
std::vector<double> v{};
int steps = 0;
for (auto i : count(1.0, 0.5)) {
v.push_back(i);
if (steps == 3) {
break;
}
++steps;
}
const std::vector<double> vc{1.0, 1.5, 2.0, 2.5};
REQUIRE(v == vc);
}
TEST_CASE("count: with negative double step", "[count]") {
std::vector<double> v{};
int steps = 0;
for (auto i : count(1.0, -0.5)) {
v.push_back(i);
if (steps == 3) {
break;
}
++steps;
}
const std::vector<double> vc{1.0, 0.5, 0.0, -0.5};
REQUIRE(v == vc);
}
TEST_CASE("count: can bo constexpr", "[count]") {
constexpr auto c = count();
constexpr auto c2 = count(5);
(void)c2;
constexpr auto c3 = count(5, 2);
(void)c3;
constexpr auto it = c.begin();
constexpr auto i = *it;
static_assert(i == 0, "count begin not correct value");
}
TEST_CASE("count: iterator meets requirements", "[count]") {
auto c = count();
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}