aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/algorithms
diff options
context:
space:
mode:
authorA. Jiang <de34@live.cn>2025-03-07 01:27:48 +0800
committerGitHub <noreply@github.com>2025-03-07 01:27:48 +0800
commitba9aeedf8e4b0ae3a62ee5a2234205b203f1286f (patch)
treeaa3feb4ca271f4d49a109bb89ba68243b0f6d77c /libcxx/test/std/algorithms
parent6e2fd4b269d0bd971e904aa07fddff19a7ec13bb (diff)
downloadllvm-ba9aeedf8e4b0ae3a62ee5a2234205b203f1286f.zip
llvm-ba9aeedf8e4b0ae3a62ee5a2234205b203f1286f.tar.gz
llvm-ba9aeedf8e4b0ae3a62ee5a2234205b203f1286f.tar.bz2
[libc++] Implement part of P2562R1: constexpr `ranges::stable_sort` (#128860)
Drive-by: Enables test coverage for `ranges::stable_sort` with proxy iterators, and changes "constexpr in" to "constexpr since" in comments in `<algorithm>`.
Diffstat (limited to 'libcxx/test/std/algorithms')
-rw-r--r--libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp32
-rw-r--r--libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp4
-rw-r--r--libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp4
-rw-r--r--libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp4
4 files changed, 29 insertions, 15 deletions
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
index c8f76b2..10e0874 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
@@ -13,11 +13,12 @@
// template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
// class Proj = identity>
// requires sortable<I, Comp, Proj>
-// I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
+// constexpr I // constexpr since C++26
+// ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
//
// template<random_access_range R, class Comp = ranges::less, class Proj = identity>
// requires sortable<iterator_t<R>, Comp, Proj>
-// borrowed_iterator_t<R>
+// constexpr borrowed_iterator_t<R> // constexpr since C++26
// ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
#include <algorithm>
@@ -57,7 +58,7 @@ static_assert(!HasStableSortR<UncheckedRange<int*>, BadComparator>);
static_assert(!HasStableSortR<UncheckedRange<const int*>>); // Doesn't satisfy `sortable`.
template <class Iter, class Sent, std::size_t N>
-void test_one(std::array<int, N> input, std::array<int, N> expected) {
+TEST_CONSTEXPR_CXX26 void test_one(std::array<int, N> input, std::array<int, N> expected) {
{ // (iterator, sentinel) overload.
auto sorted = input;
auto b = Iter(sorted.data());
@@ -81,7 +82,7 @@ void test_one(std::array<int, N> input, std::array<int, N> expected) {
}
template <class Iter, class Sent>
-void test_iterators_2() {
+TEST_CONSTEXPR_CXX26 void test_iterators_2() {
// Empty sequence.
test_one<Iter, Sent, 0>({}, {});
// 1-element sequence.
@@ -105,25 +106,25 @@ void test_iterators_2() {
}
template <class Iter>
-void test_iterators_1() {
+TEST_CONSTEXPR_CXX26 void test_iterators_1() {
test_iterators_2<Iter, Iter>();
test_iterators_2<Iter, sentinel_wrapper<Iter>>();
}
-void test_iterators() {
+TEST_CONSTEXPR_CXX26 void test_iterators() {
test_iterators_1<random_access_iterator<int*>>();
test_iterators_1<contiguous_iterator<int*>>();
test_iterators_1<int*>();
}
-void test() {
+TEST_CONSTEXPR_CXX26 bool test() {
test_iterators();
struct OrderedValue {
int value;
double original_order;
bool operator==(const OrderedValue&) const = default;
- auto operator<=>(const OrderedValue& rhs) const { return value <=> rhs.value; }
+ TEST_CONSTEXPR_CXX26 auto operator<=>(const OrderedValue& rhs) const { return value <=> rhs.value; }
};
{ // The sort is stable (equivalent elements remain in the same order).
@@ -214,10 +215,10 @@ void test() {
{ // `std::invoke` is used in the implementation.
struct S {
int i;
- S(int i_) : i(i_) {}
+ TEST_CONSTEXPR_CXX26 S(int i_) : i(i_) {}
- bool comparator(const S& rhs) const { return i < rhs.i; }
- const S& projection() const { return *this; }
+ TEST_CONSTEXPR_CXX26 bool comparator(const S& rhs) const { return i < rhs.i; }
+ TEST_CONSTEXPR_CXX26 const S& projection() const { return *this; }
bool operator==(const S&) const = default;
};
@@ -242,8 +243,6 @@ void test() {
std::ranges::stable_sort(std::array{1, 2, 3});
}
- // TODO: Enable the tests once the implementation switched to use iter_move/iter_swap
- /*
{ // ProxyIterator
{
std::array in = {2, 1, 3};
@@ -260,12 +259,15 @@ void test() {
assert((in == std::array{1, 2, 3}));
}
}
- */
+
+ return true;
}
int main(int, char**) {
test();
- // Note: `stable_sort` is not `constexpr`.
+#if TEST_STD_VER >= 26
+ static_assert(test());
+#endif
return 0;
}
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
index e893030..e4316095 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
@@ -204,8 +204,12 @@ constexpr bool test_all() {
dangling_1st(std::ranges::stable_partition, in, unary_pred);
}
dangling_1st(std::ranges::sort, in);
+#if TEST_STD_VER < 26
if (!std::is_constant_evaluated())
+#endif
+ {
dangling_1st(std::ranges::stable_sort, in);
+ }
dangling_1st(std::ranges::partial_sort, in, mid);
dangling_1st(std::ranges::nth_element, in, mid);
if (!std::is_constant_evaluated())
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
index 3105a32..aec07e9 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
@@ -171,8 +171,12 @@ constexpr bool test_all() {
test(std::ranges::stable_partition, in, &Foo::unary_pred, &Bar::val);
}
test(std::ranges::sort, in, &Foo::binary_pred, &Bar::val);
+#if TEST_STD_VER < 26
if (!std::is_constant_evaluated())
+#endif
+ {
test(std::ranges::stable_sort, in, &Foo::binary_pred, &Bar::val);
+ }
test_mid(std::ranges::partial_sort, in, mid, &Foo::binary_pred, &Bar::val);
test_mid(std::ranges::nth_element, in, mid, &Foo::binary_pred, &Bar::val);
if (!std::is_constant_evaluated())
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index 578fecd..675c2d1 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -174,8 +174,12 @@ constexpr void run_tests() {
test(std::ranges::stable_partition, in, unary_pred);
}
test(std::ranges::sort, in);
+#if TEST_STD_VER < 26
if (!std::is_constant_evaluated())
+#endif
+ {
test(std::ranges::stable_sort, in);
+ }
test_mid(std::ranges::partial_sort, in, mid);
test_mid(std::ranges::nth_element, in, mid);
if (!std::is_constant_evaluated())