Skip to content

Commit f365ae5

Browse files
committed
Mark [weak/partial]_[less/greater] fallbacks constexpr
1 parent 46243d1 commit f365ae5

9 files changed

Lines changed: 136 additions & 6 deletions

File tree

include/cpp-sort/comparators/partial_greater.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2025 Morwenn
2+
* Copyright (c) 2016-2026 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_COMPARATORS_PARTIAL_GREATER_H_
@@ -32,7 +32,7 @@ namespace cppsort
3232
// Generic overload: a weak order is also a partial order
3333

3434
template<typename T>
35-
auto partial_greater(const T& lhs, const T& rhs)
35+
constexpr auto partial_greater(const T& lhs, const T& rhs)
3636
noexcept(noexcept(cppsort::weak_greater(lhs, rhs)))
3737
-> decltype(cppsort::weak_greater(lhs, rhs))
3838
{

include/cpp-sort/comparators/partial_less.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2025 Morwenn
2+
* Copyright (c) 2016-2026 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_COMPARATORS_PARTIAL_LESS_H_
@@ -32,7 +32,7 @@ namespace cppsort
3232
// Generic overload: a weak order is also a partial order
3333

3434
template<typename T>
35-
auto partial_less(const T& lhs, const T& rhs)
35+
constexpr auto partial_less(const T& lhs, const T& rhs)
3636
noexcept(noexcept(cppsort::weak_less(lhs, rhs)))
3737
-> decltype(cppsort::weak_less(lhs, rhs))
3838
{

include/cpp-sort/comparators/weak_greater.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace cppsort
4040
// Generic overload: a total order is also a weak order
4141

4242
template<typename T>
43-
auto weak_greater(const T& lhs, const T& rhs)
43+
constexpr auto weak_greater(const T& lhs, const T& rhs)
4444
noexcept(noexcept(cppsort::total_greater(lhs, rhs)))
4545
-> detail::enable_if_t<
4646
not std::is_floating_point_v<T>,

include/cpp-sort/comparators/weak_less.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace cppsort
4040
// Generic overload: a total order is also a weak order
4141

4242
template<typename T>
43-
auto weak_less(const T& lhs, const T& rhs)
43+
constexpr auto weak_less(const T& lhs, const T& rhs)
4444
noexcept(noexcept(cppsort::total_less(lhs, rhs)))
4545
-> detail::enable_if_t<
4646
not std::is_floating_point_v<T>,

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ add_executable(main-tests
186186
comparators/case_insensitive_less.cpp
187187
comparators/flip_not.cpp
188188
comparators/natural_less.cpp
189+
comparators/partial_order.cpp
189190
comparators/projection_compare.cpp
190191
comparators/total_order.cpp
191192
comparators/transparent_comparators.cpp
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2026 Morwenn
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
#include <catch2/catch_test_macros.hpp>
6+
#include <cpp-sort/comparators/partial_greater.h>
7+
#include <cpp-sort/comparators/partial_less.h>
8+
#include <testing-tools/comparators.h>
9+
10+
TEST_CASE( "Partial order customization point", "[comparison]" )
11+
{
12+
helpers::totally_comparable ta, tb;
13+
helpers::weakly_comparable wa, wb;
14+
helpers::partially_comparable pa, pb;
15+
16+
// Ensure that overload resolution is correct
17+
STATIC_CHECK( cppsort::partial_less(ta, tb) == helpers::compare_result::total_less );
18+
STATIC_CHECK( cppsort::partial_greater(ta, tb) == helpers::compare_result::total_greater );
19+
STATIC_CHECK( cppsort::partial_less(wa, wb) == helpers::compare_result::weak_less );
20+
STATIC_CHECK( cppsort::partial_greater(wa, wb) == helpers::compare_result::weak_greater );
21+
STATIC_CHECK( cppsort::partial_less(pa, pb) == helpers::compare_result::partial_less );
22+
STATIC_CHECK( cppsort::partial_greater(pa, pb) == helpers::compare_result::partial_greater );
23+
}

tests/comparators/total_order.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cpp-sort/comparators/total_greater.h>
99
#include <cpp-sort/comparators/total_less.h>
1010
#include <cpp-sort/sorters/heap_sorter.h>
11+
#include <testing-tools/comparators.h>
1112

1213
TEST_CASE( "IEEE 754 totalOrder implementation", "[comparison]" )
1314
{
@@ -60,3 +61,12 @@ TEST_CASE( "IEEE 754 totalOrder implementation", "[comparison]" )
6061
CHECK( std::signbit(array[7]) );
6162
}
6263
}
64+
65+
TEST_CASE( "Total order customization point", "[comparison]" )
66+
{
67+
helpers::totally_comparable ta, tb;
68+
69+
// Ensure that overload resolution is correct
70+
STATIC_CHECK( cppsort::total_less(ta, tb) == helpers::compare_result::total_less );
71+
STATIC_CHECK( cppsort::total_greater(ta, tb) == helpers::compare_result::total_greater );
72+
}

tests/comparators/weak_order.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cpp-sort/comparators/weak_greater.h>
99
#include <cpp-sort/comparators/weak_less.h>
1010
#include <cpp-sort/sorters/heap_sorter.h>
11+
#include <testing-tools/comparators.h>
1112

1213
TEST_CASE( "Weak ordering of floating-point numbers", "[comparison]" )
1314
{
@@ -55,3 +56,15 @@ TEST_CASE( "Weak ordering of floating-point numbers", "[comparison]" )
5556
CHECK( std::signbit(array[7]) );
5657
}
5758
}
59+
60+
TEST_CASE( "Weak order customization point", "[comparison]" )
61+
{
62+
helpers::totally_comparable ta, tb;
63+
helpers::weakly_comparable wa, wb;
64+
65+
// Ensure that overload resolution is correct
66+
STATIC_CHECK( cppsort::weak_less(ta, tb) == helpers::compare_result::total_less );
67+
STATIC_CHECK( cppsort::weak_greater(ta, tb) == helpers::compare_result::total_greater );
68+
STATIC_CHECK( cppsort::weak_less(wa, wb) == helpers::compare_result::weak_less );
69+
STATIC_CHECK( cppsort::weak_greater(wa, wb) == helpers::compare_result::weak_greater );
70+
}

tests/testing-tools/comparators.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2026 Morwenn
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
#ifndef CPPSORT_TESTSUITE_COMPARATORS_H_
6+
#define CPPSORT_TESTSUITE_COMPARATORS_H_
7+
8+
////////////////////////////////////////////////////////////
9+
// Headers
10+
////////////////////////////////////////////////////////////
11+
#include <functional>
12+
#include <cpp-sort/utility/as_function.h>
13+
#include <cpp-sort/utility/functional.h>
14+
15+
namespace helpers
16+
{
17+
// Identify the function that was called
18+
enum struct compare_result
19+
{
20+
total_less,
21+
total_greater,
22+
weak_less,
23+
weak_greater,
24+
partial_less,
25+
partial_greater,
26+
};
27+
28+
// Collection of empty classes and empty comparison overloads used
29+
// to identify what overload was selected during resolution
30+
31+
struct totally_comparable
32+
{
33+
friend constexpr auto total_less(const totally_comparable&,
34+
const totally_comparable&)
35+
-> compare_result
36+
{
37+
return compare_result::total_less;
38+
}
39+
};
40+
41+
constexpr auto total_greater(const totally_comparable&,
42+
const totally_comparable&)
43+
-> compare_result
44+
{
45+
return compare_result::total_greater;
46+
}
47+
48+
struct weakly_comparable
49+
{
50+
friend constexpr auto weak_less(const weakly_comparable&,
51+
const weakly_comparable&)
52+
-> compare_result
53+
{
54+
return compare_result::weak_less;
55+
}
56+
};
57+
58+
constexpr auto weak_greater(const weakly_comparable&,
59+
const weakly_comparable&)
60+
-> compare_result
61+
{
62+
return compare_result::weak_greater;
63+
}
64+
65+
struct partially_comparable
66+
{
67+
friend constexpr auto partial_less(const partially_comparable&,
68+
const partially_comparable&)
69+
-> compare_result
70+
{
71+
return compare_result::partial_less;
72+
}
73+
};
74+
75+
constexpr auto partial_greater(const partially_comparable&,
76+
const partially_comparable&)
77+
-> compare_result
78+
{
79+
return compare_result::partial_greater;
80+
}
81+
}
82+
83+
#endif // CPPSORT_TESTSUITE_COMPARATORS_H_

0 commit comments

Comments
 (0)