-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathsorter_facade_defaults.cpp
126 lines (105 loc) · 3.65 KB
/
sorter_facade_defaults.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
/*
* Copyright (c) 2015-2022 Morwenn
* SPDX-License-Identifier: MIT
*/
#include <functional>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include <cpp-sort/sorter_facade.h>
#include <cpp-sort/utility/functional.h>
namespace
{
struct comparison_sorter_impl
{
template<typename Iterator, typename Compare>
auto operator()(Iterator, Iterator, Compare) const
-> bool
{
return true;
}
template<typename Iterator>
auto operator()(Iterator, Iterator) const
-> bool
{
return false;
}
};
struct non_comparison_sorter_impl
{
template<typename Iterator>
auto operator()(Iterator, Iterator) const
-> bool
{
return true;
}
};
struct non_comparison_iterable_sorter_impl
{
template<typename Iterator>
auto operator()(Iterator, Iterator) const
-> bool
{
return true;
}
template<typename Iterable>
auto operator()(Iterable&) const
-> bool
{
return false;
}
};
struct comparison_projection_sorter_impl
{
template<typename Iterator, typename Compare, typename Projection>
auto operator()(Iterator, Iterator, Compare, Projection) const
-> bool
{
return true;
}
};
struct comparison_sorter:
cppsort::sorter_facade<comparison_sorter_impl>
{};
struct non_comparison_sorter:
cppsort::sorter_facade<non_comparison_sorter_impl>
{};
struct non_comparison_iterable_sorter:
cppsort::sorter_facade<non_comparison_iterable_sorter_impl>
{};
struct comparison_projection_sorter:
cppsort::sorter_facade<comparison_projection_sorter_impl>
{};
}
TEST_CASE( "std::less<> forwarding to sorters",
"[sorter_facade][comparison]" )
{
// Check that sorter_facade only creates the overloads for
// std::less when the original sorter does not support
// custom comparison functions
// Make sure the iterator overload calls the operator() from
// the sorter for iterators, and that the iterable overload
// calls the original sorter's iterable operator() overload
// Equivalent tests are done for the automatic overloads
// of operator() for utility::identity
std::vector<int> vec(3);
SECTION( "with std::less<>" )
{
CHECK( comparison_sorter{}(vec, std::less<>{}) );
CHECK( comparison_sorter{}(vec.begin(), vec.end(), std::less<>{}) );
CHECK( non_comparison_sorter{}(vec, std::less<>{}) );
CHECK( non_comparison_sorter{}(vec.begin(), vec.end(), std::less<>{}) );
CHECK( not non_comparison_iterable_sorter{}(vec, std::less<>{}) );
CHECK( non_comparison_iterable_sorter{}(vec.begin(), vec.end(), std::less<>{}) );
}
SECTION( "with utility::identity" )
{
CHECK( comparison_sorter{}(vec, cppsort::utility::identity{}) );
CHECK( comparison_sorter{}(vec.begin(), vec.end(), cppsort::utility::identity{}) );
CHECK( non_comparison_sorter{}(vec, cppsort::utility::identity{}) );
CHECK( non_comparison_sorter{}(vec.begin(), vec.end(), cppsort::utility::identity{}) );
CHECK( not non_comparison_iterable_sorter{}(vec, cppsort::utility::identity{}) );
CHECK( non_comparison_iterable_sorter{}(vec.begin(), vec.end(), cppsort::utility::identity{}) );
CHECK( comparison_projection_sorter{}(vec, cppsort::utility::identity{}) );
CHECK( comparison_projection_sorter{}(vec.begin(), vec.end(), cppsort::utility::identity{}) );
}
}