20static vector<int> getVector(
int n)
32 SECTION(
"non-empty") {
33 vector<int> v = {1, 2, 3, 4, 5};
55 SECTION(
"non-empty") {
56 std::list<int> l = {1, 2, 3, 4, 5};
70 REQUIRE(
sizeof(vector<int>*) !=
sizeof(vector<int>));
72 vector<int> v = {0, 1, 2, 3};
75 CHECK(
sizeof(d) == (
sizeof(vector<int>*) +
sizeof(
size_t)));
78 auto d =
drop(getVector(4), 1);
80 CHECK(
sizeof(d) == (
sizeof(vector<int>) +
sizeof(
size_t)));
92 SECTION(
"non-empty") {
93 vector<int> v = {1, 2, 3, 4, 5};
115 SECTION(
"non-empty") {
116 std::list<int> l = {1, 2, 3, 4, 5};
133 vector<int> in = {1, 2, 3, 4};
134 for (
auto&
e :
reverse(in)) out.push_back(
e);
135 CHECK(out == vector<int>{4, 3, 2, 1});
138 for (
auto&
e :
reverse(getVector(3))) out.push_back(
e);
139 CHECK(out == vector<int>{2, 1, 0});
141 SECTION(
"2 x reverse") {
143 CHECK(out == vector<int>{0, 1, 2, 3});
149 auto square = [](
auto& x) {
return x * x; };
151 auto plus_i = [&](
auto& x) {
return int(x + i); };
154 vector<int> v = {1, 2, 3, 4};
195template<
typename RANGE,
typename T>
196static void check(
const RANGE& range,
const vector<T>& expected)
201template<
typename RANGE,
typename T>
202static void check_unordered(
const RANGE& range,
const vector<T>& expected_)
204 auto result = to_vector<T>(range);
205 auto expected = expected_;
208 CHECK(result == expected);
212 SECTION(
"std::map") {
213 std::map<int, int> m = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
214 check(
keys (m), vector<int>{1, 3, 5, 7});
215 check(
values(m), vector<int>{2, 4, 6, 8});
217 SECTION(
"std::vector<std::pair>") {
218 vector<std::pair<int, int>> v = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
219 check(
keys (v), vector<int>{1, 3, 5, 7});
220 check(
values(v), vector<int>{2, 4, 6, 8});
222 SECTION(
"hash_map") {
224 {{
"foo", 1}, {
"bar", 2}, {
"qux", 3},
225 {
"baz", 4}, {
"a", 5}, {
"z", 6}};
226 check_unordered(
keys(m), vector<std::string>{
227 "foo",
"bar",
"qux",
"baz",
"a",
"z"});
228 check_unordered(
values(m), vector<int>{1, 2, 3, 4, 5, 6});
230 SECTION(
"std::vector<std::tuple>") {
231 vector<std::tuple<int, char, double, std::string>> v = {
232 std::tuple(1, 2, 1.2,
"foo"),
233 std::tuple(3, 4, 3.4,
"bar"),
234 std::tuple(5, 6, 5.6,
"qux")
236 check(
keys (v), vector<int>{1, 3, 5});
237 check(
values(v), vector<char>{2, 4, 6});
244 operator int()
const {
return i; }
249 vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
251 SECTION(
"removed front") {
255 SECTION(
"removed back") {
259 SECTION(
"keep front and back") {
261 vector{1, 3, 5, 7, 9});
263 SECTION(
"remove front and back") {
264 check(
view::filter(v, [](
int i) {
return (i & 1) == 0; }),
268 SECTION(
"projection") {
269 vector<F> f = {1, 2, 3, 4, 5};
271 auto it =
view.begin();
272 auto et =
view.end();
288 vector v = {1, 2, 3, 4};
297 SECTION(
"split_view") {
298 std::string_view str =
"abc def\t \tghi jkl mno pqr";
299 auto v =
view::take(StringOp::split_view<StringOp::REMOVE_EMPTY_PARTS>(str,
" \t"), 3);
319template<
typename In1,
typename In2,
typename Expected>
320void test_zip(
const In1& in1,
const In2& in2,
const Expected& expected)
326 CHECK(result == expected);
329template<
typename In1,
typename In2,
typename In3,
typename Expected>
330void test_zip(
const In1& in1,
const In2& in2,
const In3& in3,
const Expected& expected)
333 for (
const auto&
t :
view::zip(in1, in2, in3)) {
336 CHECK(result == expected);
341 std::vector
v4 = {1, 2, 3, 4};
342 std::array a3 = {
'a',
'b',
'c'};
343 std::list
l4 = {1.2, 2.3, 3.4, 4.5};
345 test_zip(
v4, a3, std::vector<std::tuple<int, char>>{{1,
'a'}, {2,
'b'}, {3,
'c'}});
346 test_zip(a3,
v4, std::vector<std::tuple<char, int>>{{
'a', 1}, {
'b', 2}, {
'c', 3}});
348 test_zip(
v4,
l4, std::vector<std::tuple<int, double>>{{1, 1.2}, {2, 2.3}, {3, 3.4}, {4, 4.5}});
349 test_zip(
l4,
v4, std::vector<std::tuple<double, int>>{{1.2, 1}, {2.3, 2}, {3.4, 3}, {4.5, 4}});
351 test_zip(a3,
l4, std::vector<std::tuple<char, double>>{{
'a', 1.2}, {
'b', 2.3}, {
'c', 3.4}});
352 test_zip(
l4, a3, std::vector<std::tuple<double, char>>{{1.2,
'a'}, {2.3,
'b'}, {3.4,
'c'}});
354 test_zip(
v4, a3,
l4, std::vector<std::tuple<int, char, double>>{{1,
'a', 1.2}, {2,
'b', 2.3}, {3,
'c', 3.4}});
356 for (
auto [x, y] :
zip(
v4, a3)) {
360 CHECK(
v4 == std::vector{0, 0, 0, 4});
361 CHECK(a3 == std::array{
'b',
'c',
'd'});
364template<
typename In1,
typename In2,
typename Expected>
371 CHECK(result == expected);
374template<
typename In1,
typename In2,
typename In3,
typename Expected>
375void test_zip_equal(
const In1& in1,
const In2& in2,
const In3& in3,
const Expected& expected)
381 CHECK(result == expected);
386 std::vector v = {1, 2, 3};
387 std::array a = {
'a',
'b',
'c'};
388 std::list l = {1.2, 2.3, 3.4};
390 test_zip_equal(v, a, std::vector<std::tuple<int, char>>{{1,
'a'}, {2,
'b'}, {3,
'c'}});
391 test_zip_equal(v, l, std::vector<std::tuple<int, double>>{{1, 1.2}, {2, 2.3}, {3, 3.4}});
392 test_zip_equal(a, l, std::vector<std::tuple<char, double>>{{
'a', 1.2}, {
'b', 2.3}, {
'c', 3.4}});
394 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}});
396 for (
auto [x, y, z] :
zip_equal(v, l, a)) {
401 CHECK(v == std::vector{0, 0, 0});
402 CHECK(a == std::array{
'c',
'd',
'e'});
403 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 take(ForwardRange &&range, size_t n)
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)