openMSX
enumerate_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "enumerate.hh"
3#include <type_traits>
4#include <vector>
5
6TEST_CASE("enumerate: basic")
7{
8 char in[3] = {'a', 'd', 'z'};
9 std::vector<size_t> out1;
10 std::vector<char> out2;
11
12 for (auto [i, e] : enumerate(in)) {
13 static_assert(std::is_same_v<decltype(i), const size_t&>);
14 static_assert(std::is_same_v<decltype(e), char&>);
15 out1.push_back(i);
16 out2.push_back(e);
17 }
18
19 CHECK(out1 == std::vector<size_t>{0, 1, 2});
20 CHECK(out2 == std::vector{'a', 'd', 'z'});
21}
22
23TEST_CASE("enumerate: transform")
24{
25 std::vector in{3, 9, 11};
26 std::vector<size_t> out;
27
28 for (auto [i, e] : enumerate(in)) {
29 static_assert(std::is_same_v<decltype(i), const size_t&>);
30 static_assert(std::is_same_v<decltype(e), int&>);
31 out.push_back(i);
32 e = 2 * e + i;
33 }
34
35 CHECK(in == std::vector{6, 19, 24});
36 CHECK(out == std::vector<size_t>{0, 1, 2});
37}
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)
constexpr double e
Definition: Math.hh:20