forked from tcbrindle/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_filter_map.cpp
156 lines (122 loc) · 4.32 KB
/
test_filter_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2024 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 "catch.hpp"
#include <array>
#include <utility>
#include "test_utils.hpp"
namespace {
constexpr auto is_even_opt = [](int i) { return i % 2 == 0 ? std::optional{i} : std::nullopt; };
struct Pair {
int i;
bool ok;
[[nodiscard]] constexpr auto map_if_ok() const { return ok ? std::optional{*this} : std::nullopt; }
constexpr bool operator==(const Pair&) const = default;
};
using filter_fn = decltype(flux::filter_map);
// int is not a sequence
static_assert(not std::invocable<filter_fn, int, decltype(is_even_opt)>);
// int is not a function
static_assert(not std::invocable<filter_fn, int(&)[10], int>);
// "func" does not return optional_like
static_assert(not std::invocable<filter_fn, int(&)[10], decltype([](int) {})>);
// Incompatible predicate
static_assert(not std::invocable<filter_fn, int(&)[10], decltype([](int*) { return true; })>);
constexpr bool test_filter()
{
// Basic filtering
{
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto filtered = flux::filter_map(flux::ref(arr), is_even_opt);
using F = decltype(filtered);
static_assert(flux::sequence<F>);
static_assert(flux::bidirectional_sequence<F>);
static_assert(flux::bounded_sequence<F>);
static_assert(not flux::ordered_cursor<F>);
static_assert(not flux::sized_sequence<F>);
static_assert(flux::sequence<F const>);
static_assert(flux::bidirectional_sequence<F const>);
static_assert(flux::bounded_sequence<F const>);
static_assert(not flux::ordered_cursor<F const>);
static_assert(not flux::sized_sequence<F const>);
STATIC_CHECK(check_equal(filtered, {0, 2, 4, 6, 8}));
STATIC_CHECK(check_equal(std::as_const(filtered), {0, 2, 4, 6, 8}));
}
// A predicate that always returns true returns what it was given
{
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto filtered = flux::filter_map(flux::ref(arr), [](auto&& i) { return std::optional{i}; });
if (!check_equal(arr, filtered)) {
return false;
}
}
// A predicate that always returns false returns an empty sequence
{
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto filtered = flux::filter_map(flux::ref(arr), [](auto&&) -> std::optional<int> { return std::nullopt; });
if (!filtered.is_empty()) {
return false;
}
}
// We can use any optional_like such as a pointer
{
std::array<int*, 4> arr{};
arr[0] = new int(1);
arr[1] = nullptr;
arr[2] = new int(3);
arr[3] = nullptr;
auto filtered = flux::filter_map(arr, [](auto ptr) { return ptr; });
if (!check_equal(filtered, {1, 3})) {
return false;
}
}
// ... Better expressed as filter_deref
{
std::array<int*, 4> arr{};
arr[0] = new int(1);
arr[1] = nullptr;
arr[2] = new int(3);
arr[3] = nullptr;
auto filtered = flux::filter_deref(arr);
if (!check_equal(filtered, {1, 3})) {
return false;
}
}
// We can use a PMF to filter_map
{
std::array<Pair, 4> pairs = {
Pair{1, true},
{2, false},
{3, true},
{4, false}
};
auto f = flux::filter_map(pairs, &Pair::map_if_ok);
if (!check_equal(f, {Pair{1, true}, Pair{3, true}})) {
return false;
}
}
// Reversed sequences can be filtered
{
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto filtered = flux::ref(arr).reverse().filter_map(is_even_opt);
if (!check_equal(filtered, {8, 6, 4, 2, 0})) {
return false;
}
}
// ... and filtered sequences can be reversed
{
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto filtered = flux::filter_map(flux::ref(arr), is_even_opt).reverse();
if (!check_equal(filtered, {8, 6, 4, 2, 0})) {
return false;
}
}
return true;
}
// FIXME: static_assert(test_filter());
}
TEST_CASE("filter_map")
{
bool result = test_filter();
REQUIRE(result);
}