-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathsorter_facade.cpp
118 lines (99 loc) · 3.9 KB
/
sorter_facade.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
/*
* Copyright (c) 2015-2022 Morwenn
* SPDX-License-Identifier: MIT
*/
#include <functional>
#include <type_traits>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include <cpp-sort/sorter_facade.h>
#include <cpp-sort/sorter_traits.h>
#include <cpp-sort/utility/functional.h>
#include <testing-tools/wrapper.h>
namespace
{
struct comparison_sorter_impl
{
template<typename Iterator, typename Compare=std::less<>>
auto operator()(Iterator, Iterator, Compare={}) const
-> bool
{
return true;
}
};
struct projection_sorter_impl
{
template<typename Iterator, typename Projection=cppsort::utility::identity>
auto operator()(Iterator, Iterator, Projection={}) const
-> bool
{
return true;
}
};
struct comparison_projection_sorter_impl
{
template<
typename Iterator,
typename Compare = std::less<>,
typename Projection = cppsort::utility::identity,
typename = std::enable_if_t<cppsort::is_projection_iterator_v<
Projection, Iterator, Compare
>>
>
auto operator()(Iterator, Iterator, Compare={}, Projection={}) const
-> bool
{
return true;
}
};
struct comparison_sorter:
cppsort::sorter_facade<comparison_sorter_impl>
{};
struct projection_sorter:
cppsort::sorter_facade<projection_sorter_impl>
{};
struct comparison_projection_sorter:
cppsort::sorter_facade<comparison_projection_sorter_impl>
{};
}
TEST_CASE( "sorter_facade miscellaneous checks",
"[sorter_facade][comparison][projection]" )
{
// Some checks to make sure that sorter_facade always
// forwards the value correctly in the most common cases
using wrapper = generic_wrapper<int>;
// Collection to "sort"
std::vector<int> vec;
std::vector<wrapper> vec_wrap;
SECTION( "with comparison only" )
{
CHECK( comparison_sorter{}(vec, std::less<>{}) );
CHECK( comparison_sorter{}(vec.begin(), vec.end(), std::less<>{}) );
CHECK( comparison_sorter{}(vec, std::greater<>{}) );
CHECK( comparison_sorter{}(vec.begin(), vec.end(), std::greater<>{}) );
}
SECTION( "with projection only" )
{
CHECK( projection_sorter{}(vec, cppsort::utility::identity{}) );
CHECK( projection_sorter{}(vec.begin(), vec.end(), cppsort::utility::identity{}) );
CHECK( projection_sorter{}(vec_wrap, &wrapper::value) );
CHECK( projection_sorter{}(vec_wrap.begin(), vec_wrap.end(), &wrapper::value) );
}
SECTION( "with both comparison and projection" )
{
CHECK( comparison_projection_sorter{}(vec, std::less<>{}) );
CHECK( comparison_projection_sorter{}(vec.begin(), vec.end(), std::less<>{}) );
CHECK( comparison_projection_sorter{}(vec, std::greater<>{}) );
CHECK( comparison_projection_sorter{}(vec.begin(), vec.end(), std::greater<>{}) );
CHECK( comparison_projection_sorter{}(vec, cppsort::utility::identity{}) );
CHECK( comparison_projection_sorter{}(vec.begin(), vec.end(), cppsort::utility::identity{}) );
CHECK( comparison_projection_sorter{}(vec_wrap, &wrapper::value) );
CHECK( comparison_projection_sorter{}(vec_wrap.begin(), vec_wrap.end(), &wrapper::value) );
CHECK( comparison_projection_sorter{}(vec, std::greater<>{}, cppsort::utility::identity{}) );
CHECK( comparison_projection_sorter{}(vec.begin(), vec.end(),
std::greater<>{}, cppsort::utility::identity{}) );
CHECK( comparison_projection_sorter{}(vec_wrap, std::greater<>{}, &wrapper::value) );
CHECK( comparison_projection_sorter{}(vec_wrap.begin(), vec_wrap.end(),
std::greater<>{}, &wrapper::value) );
}
}