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 261f08e8c41..6dc26c15df1 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 2f3047d630a..a249f788e51 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 ccfed5cc45a..e4135411c03 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,31 @@ 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); + + // 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::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)))); + HPX_TEST_EQ(result, std::is_heap(std::begin(c), std::end(c))); + } +} + template void test_is_heap() { @@ -132,6 +157,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 e109fc7e6b6..9f4ff827713 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()