From c640562e4a7867bcc4b328733d9957469ff7bff8 Mon Sep 17 00:00:00 2001 From: Ishaan400 Date: Sun, 22 Nov 2026 01:41:08 +0000 Subject: [PATCH 1/2] Add sentinel cases to the copy, copy_if, mismatch and is_heap range tests The tests of these algorithms never called them with an end of a different type than the iterator. Add cases with test::sentinel_from_iterator without an execution policy and test::sized_sentinel_from_iterator with seq, par and par_unseq, and compare the results with the standard algorithms. Signed-off-by: Ishaan400 --- .../unit/container_algorithms/copy_range.cpp | 50 +++++++++++++++++++ .../container_algorithms/copyif_range.cpp | 37 ++++++++++++++ .../container_algorithms/is_heap_range.cpp | 32 ++++++++++++ .../container_algorithms/mismatch_range.cpp | 49 ++++++++++++++++++ 4 files changed, 168 insertions(+) diff --git a/libs/core/algorithms/tests/unit/container_algorithms/copy_range.cpp b/libs/core/algorithms/tests/unit/container_algorithms/copy_range.cpp index 261f08e8c418..6dc26c15df1d 100644 --- a/libs/core/algorithms/tests/unit/container_algorithms/copy_range.cpp +++ b/libs/core/algorithms/tests/unit/container_algorithms/copy_range.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "test_utils.hpp" @@ -85,6 +86,47 @@ void test_copy_async(ExPolicy&& p, IteratorTag) HPX_TEST_EQ(count, d.size()); } +template +void test_copy_sent(IteratorTag) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + using sentinel = test::sentinel_from_iterator; + + std::vector c(10007); + std::vector d(c.size()); + std::iota(std::begin(c), std::end(c), std::rand()); + + auto result = hpx::ranges::copy(iterator(std::begin(c)), + sentinel(iterator(std::end(c))), std::begin(d)); + + HPX_TEST(result.in == iterator(std::end(c))); + HPX_TEST(result.out == std::end(d)); + HPX_TEST(c == d); +} + +template +void test_copy_sent(ExPolicy&& policy, IteratorTag) +{ + static_assert(hpx::is_execution_policy::value, + "hpx::is_execution_policy::value"); + + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + using sentinel = test::sized_sentinel_from_iterator; + + std::vector c(10007); + std::vector d(c.size()); + std::iota(std::begin(c), std::end(c), std::rand()); + + auto result = hpx::ranges::copy(policy, iterator(std::begin(c)), + sentinel(iterator(std::end(c))), std::begin(d)); + + HPX_TEST(result.in == iterator(std::end(c))); + HPX_TEST(result.out == std::end(d)); + HPX_TEST(c == d); +} + template void test_copy() { @@ -98,6 +140,14 @@ void test_copy() test_copy_async(seq(task), IteratorTag()); test_copy_async(par(task), IteratorTag()); + + test_copy_sent(IteratorTag()); + if constexpr (std::is_same_v) + { + test_copy_sent(seq, IteratorTag()); + test_copy_sent(par, IteratorTag()); + test_copy_sent(par_unseq, IteratorTag()); + } } void copy_test() diff --git a/libs/core/algorithms/tests/unit/container_algorithms/copyif_range.cpp b/libs/core/algorithms/tests/unit/container_algorithms/copyif_range.cpp index 2f3047d630ad..a249f788e511 100644 --- a/libs/core/algorithms/tests/unit/container_algorithms/copyif_range.cpp +++ b/libs/core/algorithms/tests/unit/container_algorithms/copyif_range.cpp @@ -9,10 +9,12 @@ #include #include +#include #include #include #include #include +#include #include #include @@ -111,6 +113,34 @@ void test_copy_if_async(ExPolicy&& p) HPX_TEST_EQ(count, d.size()); } +template +void test_copy_if_sent(ExPolicy&&... policy) +{ + using base_iterator = std::vector::iterator; + using iterator = + test::test_iterator; + + std::vector c(10007); + auto middle = std::begin(c) + static_cast(c.size() / 2); + std::iota(std::begin(c), middle, std::rand()); + std::fill(middle, std::end(c), -1); + std::shuffle(std::begin(c), std::end(c), std::mt19937(std::rand())); + + auto pred = [](int i) { return !(i < 0); }; + + std::vector expected(c.size(), 0); + auto expected_end = + std::copy_if(std::begin(c), std::end(c), std::begin(expected), pred); + + std::vector d(c.size(), 0); + auto result = hpx::ranges::copy_if(policy..., iterator(std::begin(c)), + Sentinel(iterator(std::end(c))), std::begin(d), pred); + + HPX_TEST(result.in == iterator(std::end(c))); + HPX_TEST(result.out - std::begin(d) == expected_end - std::begin(expected)); + HPX_TEST(d == expected); +} + void test_copy_if() { using namespace hpx::execution; @@ -123,6 +153,13 @@ void test_copy_if() test_copy_if_async(seq(task)); test_copy_if_async(par(task)); + + using iterator = test::test_iterator::iterator, + std::random_access_iterator_tag>; + test_copy_if_sent>(); + test_copy_if_sent>(seq); + test_copy_if_sent>(par); + test_copy_if_sent>(par_unseq); } int hpx_main(hpx::program_options::variables_map& vm) diff --git a/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp b/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp index ccfed5cc45aa..096fccfb7ad5 100644 --- a/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp +++ b/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp @@ -113,6 +113,29 @@ void test_is_heap_async(ExPolicy&& policy, DataType) HPX_TEST_EQ(result, solution); } +template +void test_is_heap_sent(ExPolicy&&... policy) +{ + using base_iterator = std::vector::iterator; + using iterator = + test::test_iterator; + + std::size_t const size = 10007; + std::vector c(size); + std::iota(std::begin(c), std::end(c), std::rand()); + + // a heap that covers all, part, or none of the range + for (std::size_t heap_size : + {size, std::size_t(std::rand()) % size, std::size_t(0)}) + { + std::make_heap(std::begin(c), std::next(std::begin(c), heap_size)); + + bool result = hpx::ranges::is_heap(policy..., iterator(std::begin(c)), + Sentinel(iterator(std::end(c)))); + HPX_TEST_EQ(result, std::is_heap(std::begin(c), std::end(c))); + } +} + template void test_is_heap() { @@ -132,6 +155,15 @@ void test_is_heap() { test_is_heap(); test_is_heap(); + + using namespace hpx::execution; + using iterator = test::test_iterator::iterator, + std::random_access_iterator_tag>; + using sized_sentinel = test::sized_sentinel_from_iterator; + test_is_heap_sent>(); + test_is_heap_sent(seq); + test_is_heap_sent(par); + test_is_heap_sent(par_unseq); } int hpx_main(hpx::program_options::variables_map& vm) diff --git a/libs/core/algorithms/tests/unit/container_algorithms/mismatch_range.cpp b/libs/core/algorithms/tests/unit/container_algorithms/mismatch_range.cpp index e109fc7e6b6a..9f4ff8277138 100644 --- a/libs/core/algorithms/tests/unit/container_algorithms/mismatch_range.cpp +++ b/libs/core/algorithms/tests/unit/container_algorithms/mismatch_range.cpp @@ -8,12 +8,14 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include @@ -154,6 +156,42 @@ void test_mismatch1_async(ExPolicy&& p, IteratorTag) } } +// both ranges end with a sentinel, compared with std::mismatch on the +// underlying vectors +template +void test_mismatch_sent(IteratorTag, ExPolicy&&... policy) +{ + using base_iterator = std::vector::iterator; + using iterator = test::test_iterator; + + std::vector c1(10007); + std::size_t const first_value = gen(); //-V101 + std::iota(std::begin(c1), std::end(c1), first_value); + + // the second range is as long, shorter, or longer, and differs at a + // random position or not at all + for (std::size_t size2 : {c1.size(), c1.size() / 2, c1.size() + 10}) + { + for (bool change : {false, true}) + { + std::vector c2(size2); + std::iota(std::begin(c2), std::end(c2), first_value); + if (change) + ++c2[static_cast(dis(gen)) % c2.size()]; + + auto expected = std::mismatch( + std::begin(c1), std::end(c1), std::begin(c2), std::end(c2)); + + auto result = hpx::ranges::mismatch(policy..., + iterator(std::begin(c1)), Sentinel(iterator(std::end(c1))), + iterator(std::begin(c2)), Sentinel(iterator(std::end(c2)))); + + HPX_TEST(result.in1 == iterator(expected.first)); + HPX_TEST(result.in2 == iterator(expected.second)); + } + } +} + template void test_mismatch1() { @@ -167,6 +205,17 @@ void test_mismatch1() test_mismatch1_async(seq(task), IteratorTag()); test_mismatch1_async(par(task), IteratorTag()); + + using iterator = + test::test_iterator::iterator, IteratorTag>; + test_mismatch_sent>(IteratorTag()); + if constexpr (std::is_same_v) + { + using sentinel = test::sized_sentinel_from_iterator; + test_mismatch_sent(IteratorTag(), seq); + test_mismatch_sent(IteratorTag(), par); + test_mismatch_sent(IteratorTag(), par_unseq); + } } void mismatch_test1() From 7385c79901daf1f3ac626ec1336e53836c7de48b Mon Sep 17 00:00:00 2001 From: Ishaan400 Date: Mon, 23 Nov 2026 03:21:14 +0000 Subject: [PATCH 2/2] Test the false path of is_heap with a sentinel Start each case of the new is_heap loop from ascending values, so that only the full heap is a heap and the partial and empty cases check the false path, and cast the heap size to the difference type for std::next. Signed-off-by: Ishaan400 --- .../tests/unit/container_algorithms/is_heap_range.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp b/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp index 096fccfb7ad5..e4135411c03f 100644 --- a/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp +++ b/libs/core/algorithms/tests/unit/container_algorithms/is_heap_range.cpp @@ -122,13 +122,15 @@ void test_is_heap_sent(ExPolicy&&... policy) std::size_t const size = 10007; std::vector c(size); - std::iota(std::begin(c), std::end(c), std::rand()); - // a heap that covers all, part, or none of the range + // a heap that covers all, part, or none of the range; each case starts + // from ascending values, so only the first one is a heap for (std::size_t heap_size : {size, std::size_t(std::rand()) % size, std::size_t(0)}) { - std::make_heap(std::begin(c), std::next(std::begin(c), heap_size)); + std::iota(std::begin(c), std::end(c), std::rand()); + std::make_heap(std::begin(c), + std::next(std::begin(c), static_cast(heap_size))); bool result = hpx::ranges::is_heap(policy..., iterator(std::begin(c)), Sentinel(iterator(std::end(c))));