forked from tcbrindle/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_find_min_max.cpp
145 lines (106 loc) · 3.8 KB
/
test_find_min_max.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
// Copyright (c) 2023 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 <array>
#include "test_utils.hpp"
namespace {
struct IntPair {
int a, b;
friend bool operator==(IntPair const&, IntPair const&) = default;
};
constexpr bool test_find_min()
{
// Empty range -> no min value
{
auto seq = flux::empty<int>;
auto cur = flux::find_min(seq);
STATIC_CHECK(seq.is_last(cur));
}
// Basic min works as expected
{
auto arr = std::array{5, 4, 3, 2, 1};
STATIC_CHECK(flux::read_at(arr, flux::find_min(arr)) == 1);
auto ref = flux::ref(arr);
STATIC_CHECK(ref[ref.find_min()] == 1); // much better!
}
// Can use custom comparator and projection
{
IntPair arr[] = { {1, 2}, {3, 4}, {5, 6}};
auto cur = flux::find_min(arr, flux::proj(flux::cmp::reverse_compare, &IntPair::a));
STATIC_CHECK(not flux::is_last(arr, cur));
STATIC_CHECK(flux::read_at(arr, cur) == IntPair{5, 6});
}
// If several elements are equally minimal, returns the first
{
IntPair arr[] = { {1, 2}, {1, 3}, {1, 4}};
auto cur = flux::find_min(arr, flux::proj(flux::cmp::compare, &IntPair::a));
STATIC_CHECK(not flux::is_last(arr, cur));
STATIC_CHECK(flux::read_at(arr, cur).b == 2);
}
return true;
}
static_assert(test_find_min());
constexpr bool test_find_max()
{
// Empty range -> no max value
{
auto seq = flux::filter(std::array{2, 4, 6, 8, 10}, flux::pred::odd);
auto max = seq.find_max();
STATIC_CHECK(seq.is_last(max));
}
// Basic max works as expected
{
auto seq = flux::from(std::array{5, 4, 3, 2, 1});
auto cur = seq.find_max();
STATIC_CHECK(cur == 0);
STATIC_CHECK(seq[cur] == 5);
}
// Can use custom comparator and projection
{
IntPair arr[] = { {1, 2}, {3, 4}, {5, 6}};
auto cur = flux::find_max(arr, flux::proj(flux::cmp::reverse_compare, &IntPair::a));
STATIC_CHECK(flux::read_at(arr, cur) == IntPair{1, 2});
}
// If several elements are equally maximal, returns the last
{
IntPair arr[] = { {1, 2}, {1, 3}, {1, 4}};
auto cur = flux::find_max(arr, flux::proj(flux::cmp::compare, &IntPair::b));
STATIC_CHECK(flux::read_at(arr, cur).b == 4);
}
return true;
}
static_assert(test_find_max());
constexpr bool test_find_minmax()
{
// Empty range -> no minmax
{
auto seq = flux::filter(std::array{2, 4, 6, 8, 10}, flux::pred::odd);
auto [min, max] = seq.find_minmax();
STATIC_CHECK(seq.is_last(min));
STATIC_CHECK(seq.is_last(max));
}
// Basic minmax works as expected
{
auto seq = flux::from(std::array{5, 4, 3, 2, 1});
auto result = seq.find_minmax();
STATIC_CHECK(seq[result.min] == 1);
STATIC_CHECK(seq[result.max] == 5);
}
// Can use custom comparator and projection
{
IntPair arr[] = { {1, 2}, {3, 4}, {5, 6}};
auto result = flux::find_minmax(arr, flux::proj(flux::cmp::reverse_compare, &IntPair::a));
STATIC_CHECK(flux::read_at(arr, result.min) == IntPair{5, 6});
STATIC_CHECK(flux::read_at(arr, result.max) == IntPair{1, 2});
}
// If several elements are equally minimal/maximal, returns the first/last resp.
{
IntPair arr[] = { {1, 2}, {1, 3}, {1, 4}};
auto [min, max] = flux::find_minmax(arr, flux::proj(flux::cmp::compare, &IntPair::a));
STATIC_CHECK(flux::read_at(arr, min) == IntPair{1, 2});
STATIC_CHECK(flux::read_at(arr, max) == IntPair{1, 4});
}
return true;
}
static_assert(test_find_minmax());
}