|
| 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