Skip to content

Commit ea34507

Browse files
committed
Add tests for generalized callables
1 parent 38a3a2d commit ea34507

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

tests/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,22 @@ add_executable(cxx_98_tests
7979
configure_tests(cxx_98_tests)
8080
target_compile_features(cxx_98_tests PRIVATE cxx_std_98)
8181

82-
# Tests that require C++11 support
82+
# Tests requiring C++11 support
8383
add_executable(cxx_11_tests
8484
main.cpp
8585
cxx_11_tests.cpp
8686
)
8787
configure_tests(cxx_11_tests)
8888
target_compile_features(cxx_11_tests PRIVATE cxx_std_11)
8989

90+
# Tests requiring C++17 support
91+
add_executable(cxx_17_tests
92+
main.cpp
93+
cxx_17_tests.cpp
94+
)
95+
configure_tests(cxx_17_tests)
96+
target_compile_features(cxx_17_tests PRIVATE cxx_std_17)
97+
9098
# Windows-specific tests
9199
if (WIN32)
92100
add_executable(windows_tests
@@ -102,6 +110,7 @@ include(Catch)
102110

103111
catch_discover_tests(cxx_98_tests)
104112
catch_discover_tests(cxx_11_tests)
113+
catch_discover_tests(cxx_17_tests)
105114
if (WIN32)
106115
catch_discover_tests(windows_tests)
107116
endif()

tests/cxx_17_tests.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2011 Fuji, Goro (gfx) <[email protected]>.
3+
* Copyright (c) 2019 Morwenn.
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
#include <algorithm>
8+
#include <functional>
9+
#include <numeric>
10+
#include <random>
11+
#include <vector>
12+
#include <catch2/catch.hpp>
13+
#include <gfx/timsort.hpp>
14+
15+
namespace
16+
{
17+
struct wrapper {
18+
wrapper() = default;
19+
wrapper(wrapper const&) = default;
20+
wrapper(wrapper&&) = default;
21+
22+
wrapper(int val) : value(val) {
23+
}
24+
25+
wrapper& operator=(wrapper const&) = default;
26+
wrapper& operator=(wrapper&&) = default;
27+
28+
wrapper& operator=(int val) {
29+
value = val;
30+
return *this;
31+
}
32+
33+
bool compare_to(wrapper const& other) const {
34+
return value < other.value;
35+
}
36+
37+
int value = 0;
38+
};
39+
}
40+
41+
#ifdef __cpp_lib_invoke
42+
43+
TEST_CASE( "generalized callables" ) {
44+
std::vector<wrapper> vec(50);
45+
std::iota(vec.begin(), vec.end(), -25);
46+
std::mt19937 gen(123456); // fixed seed is enough
47+
std::shuffle(vec.begin(), vec.end(), gen);
48+
49+
SECTION( "for comparisons" ) {
50+
gfx::timsort(vec, &wrapper::compare_to);
51+
CHECK(std::is_sorted(vec.begin(), vec.end(), [](wrapper const& lhs, wrapper const& rhs) {
52+
return lhs.value < rhs.value;
53+
}));
54+
}
55+
56+
SECTION( "for projections" ) {
57+
gfx::timsort(vec, std::less<>{}, &wrapper::value);
58+
CHECK(std::is_sorted(vec.begin(), vec.end(), [](wrapper const& lhs, wrapper const& rhs) {
59+
return lhs.value < rhs.value;
60+
}));
61+
}
62+
}
63+
64+
#endif // __cpp_lib_invoke

0 commit comments

Comments
 (0)