forked from tcbrindle/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_map.cpp
125 lines (90 loc) · 3.85 KB
/
test_map.cpp
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
// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <algorithm>
#include <array>
#include <ranges>
#include "test_utils.hpp"
namespace {
constexpr bool test_map()
{
{
int arr[] = {0, 1, 2, 3, 4};
auto mapped = flux::map(flux::mut_ref(arr), [](int& i) { return i * 2; });
using M = decltype(mapped);
static_assert(flux::random_access_sequence<M>);
static_assert(not flux::contiguous_sequence<M>);
static_assert(flux::bounded_sequence<M>);
static_assert(flux::sized_sequence<M>);
static_assert(std::same_as<int, flux::element_t<M>>);
static_assert(std::same_as<int, flux::rvalue_element_t<M>>);
static_assert(flux::random_access_sequence<M const>);
static_assert(not flux::contiguous_sequence<M const>);
static_assert(flux::bounded_sequence<M const>);
static_assert(flux::sized_sequence<M const>);
STATIC_CHECK(check_equal(mapped, {0, 2, 4, 6, 8}));
STATIC_CHECK(mapped.size() == 5);
}
{
auto mapped = flux::map(std::array{0, 1, 2, 3, 4}, [](int const& i) -> int const& { return i; });
using M = decltype(mapped);
static_assert(flux::random_access_sequence<M>);
static_assert(not flux::contiguous_sequence<M>);
static_assert(flux::bounded_sequence<M>);
static_assert(flux::sized_sequence<M>);
static_assert(std::same_as<int const&, flux::element_t<M>>);
static_assert(std::same_as<int const&&, flux::rvalue_element_t<M>>);
static_assert(std::same_as<int, flux::value_t<M>>);
static_assert(flux::random_access_sequence<M const>);
static_assert(not flux::contiguous_sequence<M const>);
static_assert(flux::bounded_sequence<M const>);
static_assert(flux::sized_sequence<M const>);
STATIC_CHECK(check_equal(mapped, {0, 1, 2, 3, 4}));
STATIC_CHECK(mapped.size() == 5);
}
{
int arr[] = {0, 1, 2, 3, 4};
auto cur = flux::ref(arr).map([](int i) { return i * 2; }).find(4);
STATIC_CHECK(cur == 2);
}
{
std::pair<int, int> pairs[] = { {0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}};
auto mapped = flux::mut_ref(pairs).map(&std::pair<int, int>::first);
using M = decltype(mapped);
static_assert(flux::random_access_sequence<M>);
static_assert(std::same_as<int&, flux::element_t<M>>);
static_assert(std::same_as<int&&, flux::rvalue_element_t<M>>);
static_assert(std::same_as<int, flux::value_t<M>>);
STATIC_CHECK(check_equal(mapped, {0, 1, 2, 3, 4}));
}
{
constexpr auto seq = flux::from(std::array{0, 1, 2, 3, 4})
.map([](int i) { return i * 2; })
.reverse()
.take(3);
static_assert(flux::random_access_sequence<decltype(seq)>);
static_assert(flux::size(seq) == 3);
static_assert(check_equal(seq, {8, 6, 4}));
}
{
auto times_two = [](int i) { return i * 2; };
int arr[] = {0, 1, 2, 3, 4};
auto view = flux::map(flux::ref(arr), times_two);
using V = decltype(view);
static_assert(std::ranges::view<V>);
static_assert(std::ranges::random_access_range<V>);
static_assert(not std::ranges::contiguous_range<V>);
static_assert(std::ranges::common_range<V>);
static_assert(std::ranges::sized_range<V>);
STATIC_CHECK(std::ranges::equal(std::views::transform(view, times_two),
std::array{0, 4, 8, 12, 16}));
}
return true;
}
static_assert(test_map());
}
TEST_CASE("map")
{
bool result = test_map();
REQUIRE(result);
}