openMSX
enumerate_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "enumerate.hh"
3
4#include "narrow.hh"
5
6#include <array>
7#include <type_traits>
8#include <vector>
9
10TEST_CASE("enumerate: basic")
11{
12 std::array in = {'a', 'd', 'z'};
13 std::vector<size_t> out1;
14 std::vector<char> out2;
15
16 for (auto [i, e] : enumerate(in)) {
17 static_assert(std::is_same_v<decltype(i), const size_t&>);
18 static_assert(std::is_same_v<decltype(e), char&>);
19 out1.push_back(i);
20 out2.push_back(e);
21 }
22
23 CHECK(out1 == std::vector<size_t>{0, 1, 2});
24 CHECK(out2 == std::vector{'a', 'd', 'z'});
25}
26
27TEST_CASE("enumerate: transform")
28{
29 std::vector in{3, 9, 11};
30 std::vector<size_t> out;
31
32 for (auto [i, e] : enumerate(in)) {
33 static_assert(std::is_same_v<decltype(i), const size_t&>);
34 static_assert(std::is_same_v<decltype(e), int&>);
35 out.push_back(i);
36 e = narrow_cast<int>(2 * e + i);
37 }
38
39 CHECK(in == std::vector{6, 19, 24});
40 CHECK(out == std::vector<size_t>{0, 1, 2});
41}
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition enumerate.hh:28
TEST_CASE("enumerate: basic")
CHECK(m3==m3)