18static vector<int> getVector(
int n)
30 SECTION(
"non-empty") {
31 vector<int> v = {1, 2, 3, 4, 5};
53 SECTION(
"non-empty") {
54 std::list<int> l = {1, 2, 3, 4, 5};
68 REQUIRE(
sizeof(vector<int>*) !=
sizeof(vector<int>));
70 vector<int> v = {0, 1, 2, 3};
73 CHECK(
sizeof(d) == (
sizeof(vector<int>*) +
sizeof(
size_t)));
76 auto d =
drop(getVector(4), 1);
78 CHECK(
sizeof(d) == (
sizeof(vector<int>) +
sizeof(
size_t)));
90 SECTION(
"non-empty") {
91 vector<int> v = {1, 2, 3, 4, 5};
113 SECTION(
"non-empty") {
114 std::list<int> l = {1, 2, 3, 4, 5};
131 vector<int> in = {1, 2, 3, 4};
132 for (
auto&
e :
reverse(in)) out.push_back(
e);
133 CHECK(out == vector<int>{4, 3, 2, 1});
136 for (
auto&
e :
reverse(getVector(3))) out.push_back(
e);
137 CHECK(out == vector<int>{2, 1, 0});
139 SECTION(
"2 x reverse") {
141 CHECK(out == vector<int>{0, 1, 2, 3});
147 auto square = [](
auto& x) {
return x * x; };
149 auto plus_i = [&](
auto& x) {
return int(x + i); };
152 vector<int> v = {1, 2, 3, 4};
193template<
typename RANGE,
typename T>
194static void check(
const RANGE& range,
const vector<T>& expected)
196 CHECK(
equal(range.begin(), range.end(), expected.begin(), expected.end()));
199template<
typename RANGE,
typename T>
200static void check_unordered(
const RANGE& range,
const vector<T>& expected_)
202 auto result = to_vector<T>(range);
203 auto expected = expected_;
206 CHECK(result == expected);
210 SECTION(
"std::map") {
211 std::map<int, int> m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
212 check(
keys (m), vector<int>{1, 3, 5, 7});
213 check(
values(m), vector<int>{2, 4, 6, 8});
215 SECTION(
"std::vector<std::pair>") {
216 vector<std::pair<int, int>> v = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
217 check(
keys (v), vector<int>{1, 3, 5, 7});
218 check(
values(v), vector<int>{2, 4, 6, 8});
220 SECTION(
"hash_map") {
222 {{
"foo", 1}, {
"bar", 2}, {
"qux", 3},
223 {
"baz", 4}, {
"a", 5}, {
"z", 6}};
224 check_unordered(
keys(m), vector<std::string>{
225 "foo",
"bar",
"qux",
"baz",
"a",
"z"});
226 check_unordered(
values(m), vector<int>{1, 2, 3, 4, 5, 6});
228 SECTION(
"std::vector<std::tuple>") {
229 vector<std::tuple<int, char, double, std::string>> v = {
230 std::tuple(1, 2, 1.2,
"foo"),
231 std::tuple(3, 4, 3.4,
"bar"),
232 std::tuple(5, 6, 5.6,
"qux")
234 check(
keys (v), vector<int>{1, 3, 5});
235 check(
values(v), vector<char>{2, 4, 6});
242 operator int()
const {
return i; }
247 vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
249 SECTION(
"removed front") {
253 SECTION(
"removed back") {
257 SECTION(
"keep front and back") {
259 vector{1, 3, 5, 7, 9});
261 SECTION(
"remove front and back") {
262 check(
view::filter(v, [](
int i) {
return (i & 1) == 0; }),
266 SECTION(
"projection") {
267 vector<F> f = {1, 2, 3, 4, 5};
269 auto it =
view.begin();
270 auto et =
view.end();
285template<
typename In1,
typename In2,
typename Expected>
286void test_zip(
const In1& in1,
const In2& in2,
const Expected& expected)
292 CHECK(result == expected);
295template<
typename In1,
typename In2,
typename In3,
typename Expected>
296void test_zip(
const In1& in1,
const In2& in2,
const In3& in3,
const Expected& expected)
299 for (
const auto&
t :
view::zip(in1, in2, in3)) {
302 CHECK(result == expected);
307 std::vector
v4 = {1, 2, 3, 4};
308 std::array a3 = {
'a',
'b',
'c'};
309 std::list
l4 = {1.2, 2.3, 3.4, 4.5};
311 test_zip(
v4, a3, std::vector<std::tuple<int, char>>{{1,
'a'}, {2,
'b'}, {3,
'c'}});
312 test_zip(a3,
v4, std::vector<std::tuple<char, int>>{{
'a', 1}, {
'b', 2}, {
'c', 3}});
314 test_zip(
v4,
l4, std::vector<std::tuple<int, double>>{{1, 1.2}, {2, 2.3}, {3, 3.4}, {4, 4.5}});
315 test_zip(
l4,
v4, std::vector<std::tuple<double, int>>{{1.2, 1}, {2.3, 2}, {3.4, 3}, {4.5, 4}});
317 test_zip(a3,
l4, std::vector<std::tuple<char, double>>{{
'a', 1.2}, {
'b', 2.3}, {
'c', 3.4}});
318 test_zip(
l4, a3, std::vector<std::tuple<double, char>>{{1.2,
'a'}, {2.3,
'b'}, {3.4,
'c'}});
320 test_zip(
v4, a3,
l4, std::vector<std::tuple<int, char, double>>{{1,
'a', 1.2}, {2,
'b', 2.3}, {3,
'c', 3.4}});
322 for (
auto [x, y] :
zip(
v4, a3)) {
326 CHECK(
v4 == std::vector{0, 0, 0, 4});
327 CHECK(a3 == std::array{
'b',
'c',
'd'});
330template<
typename In1,
typename In2,
typename Expected>
337 CHECK(result == expected);
340template<
typename In1,
typename In2,
typename In3,
typename Expected>
341void test_zip_equal(
const In1& in1,
const In2& in2,
const In3& in3,
const Expected& expected)
347 CHECK(result == expected);
352 std::vector v = {1, 2, 3};
353 std::array a = {
'a',
'b',
'c'};
354 std::list l = {1.2, 2.3, 3.4};
356 test_zip_equal(v, a, std::vector<std::tuple<int, char>>{{1,
'a'}, {2,
'b'}, {3,
'c'}});
357 test_zip_equal(v, l, std::vector<std::tuple<int, double>>{{1, 1.2}, {2, 2.3}, {3, 3.4}});
358 test_zip_equal(a, l, std::vector<std::tuple<char, double>>{{
'a', 1.2}, {
'b', 2.3}, {
'c', 3.4}});
360 test_zip_equal(v, a, l, std::vector<std::tuple<int, char, double>>{{1,
'a', 1.2}, {2,
'b', 2.3}, {3,
'c', 3.4}});
362 for (
auto [x, y, z] :
zip_equal(v, l, a)) {
367 CHECK(v == std::vector{0, 0, 0});
368 CHECK(a == std::array{
'c',
'd',
'e'});
369 CHECK(l == std::list{2.4, 4.6, 6.8});
imat4 l4(ivec4(1, 2, 3, 4), ivec4(3, 4, 5, 6), ivec4(5, 6, 7, 0), ivec4(7, 8, 9, 0))
bool equal(InputRange1 &&range1, InputRange2 &&range2, Pred pred={}, Proj1 proj1={}, Proj2 proj2={})
auto transform(InputRange &&range, OutputIter out, UnaryOperation op)
constexpr void sort(RandomAccessRange &&range)
Zip< true, RangesTuple, Is... > zip(RangesTuple &&ranges, std::index_sequence< Is... >)
Zip< false, RangesTuple, Is... > zip_equal(RangesTuple &&ranges, std::index_sequence< Is... >)
auto zip_equal(Ranges &&... ranges)
auto filter(ForwardRange &&range, Predicate pred)
constexpr auto reverse(Range &&range)
constexpr auto drop(Range &&range, size_t n)
constexpr auto drop_back(Range &&range, size_t n)
auto zip(Ranges &&... ranges)
constexpr auto keys(Map &&map)
constexpr auto values(Map &&map)
auto to_vector(Range &&range) -> std::vector< detail::ToVectorType< T, decltype(std::begin(range))> >
TEST_CASE("view::drop random-access-range")
void test_zip(const In1 &in1, const In2 &in2, const Expected &expected)
void test_zip_equal(const In1 &in1, const In2 &in2, const Expected &expected)
constexpr auto xrange(T e)