forked from timsort/cpp-TimSort
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcxx_17_tests.cpp
More file actions
64 lines (53 loc) · 1.55 KB
/
cxx_17_tests.cpp
File metadata and controls
64 lines (53 loc) · 1.55 KB
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
/*
* Copyright (c) 2011 Fuji, Goro (gfx) <gfuji@cpan.org>.
* Copyright (c) 2019 Morwenn.
*
* SPDX-License-Identifier: MIT
*/
#include <algorithm>
#include <functional>
#include <numeric>
#include <random>
#include <vector>
#include <catch2/catch.hpp>
#include <gfx/timsort.hpp>
namespace
{
struct wrapper {
wrapper() = default;
wrapper(wrapper const&) = default;
wrapper(wrapper&&) = default;
wrapper(int val) : value(val) {
}
wrapper& operator=(wrapper const&) = default;
wrapper& operator=(wrapper&&) = default;
wrapper& operator=(int val) {
value = val;
return *this;
}
bool compare_to(wrapper const& other) const {
return value < other.value;
}
int value = 0;
};
}
#ifdef __cpp_lib_invoke
TEST_CASE( "generalized callables" ) {
std::vector<wrapper> vec(50);
std::iota(vec.begin(), vec.end(), -25);
std::mt19937 gen(123456); // fixed seed is enough
std::shuffle(vec.begin(), vec.end(), gen);
SECTION( "for comparisons" ) {
gfx::timsort(vec, &wrapper::compare_to);
CHECK(std::is_sorted(vec.begin(), vec.end(), [](wrapper const& lhs, wrapper const& rhs) {
return lhs.value < rhs.value;
}));
}
SECTION( "for projections" ) {
gfx::timsort(vec, std::less<>{}, &wrapper::value);
CHECK(std::is_sorted(vec.begin(), vec.end(), [](wrapper const& lhs, wrapper const& rhs) {
return lhs.value < rhs.value;
}));
}
}
#endif // __cpp_lib_invoke