diff --git a/libs/core/algorithms/tests/unit/container_algorithms/CMakeLists.txt b/libs/core/algorithms/tests/unit/container_algorithms/CMakeLists.txt index dfbdf19b7981..a4c03be0bcd4 100644 --- a/libs/core/algorithms/tests/unit/container_algorithms/CMakeLists.txt +++ b/libs/core/algorithms/tests/unit/container_algorithms/CMakeLists.txt @@ -59,6 +59,7 @@ set(tests is_partitioned_projection_range is_sorted_range is_sorted_until_range + is_sorted_until_range_exceptions lexicographical_compare_range make_heap_range max_element_range diff --git a/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range.cpp b/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range.cpp index bb40a007a334..745f6e5fac76 100644 --- a/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range.cpp +++ b/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range.cpp @@ -714,7 +714,7 @@ void test_sorted_until3_seq() std::iota(std::begin(c1), std::end(c1), 0); std::iota(std::begin(c2), std::end(c2), 0); - auto until1 = + auto const until1 = hpx::ranges::is_sorted_until(c1, std::less(), [&](int x) { if (x == 0) { @@ -729,7 +729,7 @@ void test_sorted_until3_seq() return x; } }); - auto until2 = + auto const until2 = hpx::ranges::is_sorted_until(c2, std::less(), [&](int x) { if (x == static_cast(c2.size()) / 3 || x == 2 * static_cast(c2.size()) / 3) @@ -742,8 +742,8 @@ void test_sorted_until3_seq() } }); - auto test_index1 = std::begin(c1) + 1; - auto test_index2 = std::begin(c2) + c2.size() / 3; + auto const test_index1 = std::begin(c1) + 1; + auto const test_index2 = std::begin(c2) + c2.size() / 3; HPX_TEST(until1 == test_index1); HPX_TEST(until2 == test_index2); @@ -780,817 +780,11 @@ void sorted_until_test3() test_sorted_until3_seq(); } -//////////////////////////////////////////////////////////////////////////////// -template -void test_sorted_until_exception(ExPolicy policy, IteratorTag) -{ - static_assert(hpx::is_execution_policy::value, - "hpx::is_execution_policy::value"); - - typedef std::vector::iterator base_iterator; - typedef test::test_iterator iterator; - typedef test::decorated_iterator - decorated_iterator; - - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_exception = false; - try - { - hpx::ranges::is_sorted_until(policy, - decorated_iterator( - std::begin(c), []() { throw std::runtime_error("test"); }), - decorated_iterator( - std::end(c), []() { throw std::runtime_error("test"); })); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(policy, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), - iterator(std::end(c)), - [](int, int) -> bool { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(policy, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), - iterator(std::end(c)), std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(policy, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); -} - -template -void test_sorted_until_async_exception(ExPolicy p, IteratorTag) -{ - typedef std::vector::iterator base_iterator; - typedef test::test_iterator iterator; - typedef test::decorated_iterator - decorated_iterator; - - std::vector c(10007); - std::iota(std::begin(c), std::end(c), 0); - - bool caught_exception = false; - try - { - hpx::future f = hpx::ranges::is_sorted_until(p, - decorated_iterator( - std::begin(c), []() { throw std::runtime_error("test"); }), - decorated_iterator( - std::end(c), []() { throw std::runtime_error("test"); })); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(p, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::future f = hpx::ranges::is_sorted_until(p, - iterator(std::begin(c)), iterator(std::end(c)), - [](int, int) -> int { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(p, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::future f = hpx::ranges::is_sorted_until(p, - iterator(std::begin(c)), iterator(std::end(c)), std::less(), - [](int) -> bool { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(p, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); -} - -template -void test_sorted_until_seq_exception(IteratorTag) -{ - typedef std::vector::iterator base_iterator; - typedef test::test_iterator iterator; - typedef test::decorated_iterator - decorated_iterator; - - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_exception = false; - try - { - hpx::ranges::is_sorted_until( - decorated_iterator( - std::begin(c), []() { throw std::runtime_error("test"); }), - decorated_iterator( - std::end(c), []() { throw std::runtime_error("test"); })); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(hpx::execution::seq, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::ranges::is_sorted_until(iterator(std::begin(c)), - iterator(std::end(c)), - [](int, int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(hpx::execution::seq, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::ranges::is_sorted_until(iterator(std::begin(c)), - iterator(std::end(c)), std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions::call(hpx::execution::seq, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); -} - -template -void test_sorted_until_exception(ExPolicy policy) -{ - static_assert(hpx::is_execution_policy::value, - "hpx::is_execution_policy::value"); - - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_exception = false; - try - { - hpx::ranges::is_sorted_until(policy, c, - [](int, int) -> bool { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions_base::call(policy, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::ranges::is_sorted_until(policy, c, std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions_base::call(policy, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); -} - -template -void test_sorted_until_async_exception(ExPolicy p) -{ - std::vector c(10007); - std::iota(std::begin(c), std::end(c), 0); - - bool caught_exception = false; - try - { - auto f = hpx::ranges::is_sorted_until( - p, c, [](int, int) -> int { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions_base::call(p, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - auto f = hpx::ranges::is_sorted_until(p, c, std::less(), - [](int) -> bool { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions_base::call(p, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); -} - -void test_sorted_until_seq_exception() -{ - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_exception = false; - try - { - hpx::ranges::is_sorted_until( - c, [](int, int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions_base::call( - hpx::execution::seq, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); - - caught_exception = false; - try - { - hpx::ranges::is_sorted_until(c, std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_exception = true; - test::test_num_exceptions_base::call( - hpx::execution::seq, e); - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_exception); -} - -template -void test_sorted_until_exception() -{ - using namespace hpx::execution; - //If the execution policy object is of type vector_execution_policy, - // std::terminate shall be called. Therefore we do not test exceptions - // with a vector execution policy - test_sorted_until_exception(seq, IteratorTag()); - test_sorted_until_exception(par, IteratorTag()); - - test_sorted_until_async_exception(seq(task), IteratorTag()); - test_sorted_until_async_exception(par(task), IteratorTag()); - - test_sorted_until_seq_exception(IteratorTag()); -} - -void sorted_until_exception_test() -{ - test_sorted_until_exception(); - test_sorted_until_exception(); - - using namespace hpx::execution; - - test_sorted_until_exception(seq); - test_sorted_until_exception(par); - - test_sorted_until_async_exception(seq(task)); - test_sorted_until_async_exception(par(task)); - - test_sorted_until_seq_exception(); -} - -//////////////////////////////////////////////////////////////////////////////// -template -void test_sorted_until_bad_alloc(ExPolicy policy, IteratorTag) -{ - static_assert(hpx::is_execution_policy::value, - "hpx::is_execution_policy::value"); - - typedef std::vector::iterator base_iterator; - typedef test::test_iterator iterator; - typedef test::decorated_iterator - decorated_iterator; - - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(policy, - decorated_iterator( - std::begin(c), []() { throw std::runtime_error("test"); }), - decorated_iterator( - std::end(c), []() { throw std::runtime_error("test"); })); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), - iterator(std::end(c)), - [](int, int) -> bool { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), - iterator(std::end(c)), std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); -} - -template -void test_sorted_until_async_bad_alloc(ExPolicy p, IteratorTag) -{ - typedef std::vector::iterator base_iterator; - typedef test::test_iterator iterator; - typedef test::decorated_iterator - decorated_iterator; - - std::vector c(10007); - std::iota(std::begin(c), std::end(c), 0); - - bool caught_bad_alloc = false; - try - { - hpx::future f = hpx::ranges::is_sorted_until(p, - decorated_iterator( - std::begin(c), []() { throw std::runtime_error("test"); }), - decorated_iterator( - std::end(c), []() { throw std::runtime_error("test"); })); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::future f = hpx::ranges::is_sorted_until(p, - iterator(std::begin(c)), iterator(std::end(c)), - [](int, int) -> int { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::future f = hpx::ranges::is_sorted_until(p, - iterator(std::begin(c)), iterator(std::end(c)), std::less(), - [](int) -> bool { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); -} - -template -void test_sorted_until_seq_bad_alloc(IteratorTag) -{ - typedef std::vector::iterator base_iterator; - typedef test::test_iterator iterator; - typedef test::decorated_iterator - decorated_iterator; - - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until( - decorated_iterator( - std::begin(c), []() { throw std::runtime_error("test"); }), - decorated_iterator( - std::end(c), []() { throw std::runtime_error("test"); })); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(iterator(std::begin(c)), - iterator(std::end(c)), - [](int, int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(iterator(std::begin(c)), - iterator(std::end(c)), std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); -} - -template -void test_sorted_until_bad_alloc(ExPolicy policy) -{ - static_assert(hpx::is_execution_policy::value, - "hpx::is_execution_policy::value"); - - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(policy, c, - [](int, int) -> bool { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(policy, c, std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); -} - -template -void test_sorted_until_async_bad_alloc(ExPolicy p) -{ - std::vector c(10007); - std::iota(std::begin(c), std::end(c), 0); - - bool caught_bad_alloc = false; - try - { - auto f = hpx::ranges::is_sorted_until( - p, c, [](int, int) -> int { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - auto f = hpx::ranges::is_sorted_until(p, c, std::less(), - [](int) -> bool { throw std::runtime_error("test"); }); - f.get(); - - HPX_TEST(false); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); -} - -void test_sorted_until_seq_bad_alloc() -{ - std::vector c(10007); - //fill first half of array with even numbers and second half - //with odd numbers - std::iota(std::begin(c), std::end(c), 0); - - bool caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until( - c, [](int, int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); - - caught_bad_alloc = false; - try - { - hpx::ranges::is_sorted_until(c, std::less(), - [](int) -> int { throw std::runtime_error("test"); }); - } - catch (hpx::exception_list const& e) - { - caught_bad_alloc = true; - } - catch (...) - { - HPX_TEST(false); - } - - HPX_TEST(caught_bad_alloc); -} - -template -void test_sorted_until_bad_alloc() -{ - using namespace hpx::execution; - - // If the execution policy object is of type vector_execution_policy, - // std::terminate shall be called. therefore we do not test exceptions - // with a vector execution policy - test_sorted_until_bad_alloc(par, IteratorTag()); - test_sorted_until_bad_alloc(seq, IteratorTag()); - - test_sorted_until_async_bad_alloc(seq(task), IteratorTag()); - test_sorted_until_async_bad_alloc(par(task), IteratorTag()); - - test_sorted_until_seq_bad_alloc(IteratorTag()); -} - -void sorted_until_bad_alloc_test() -{ - test_sorted_until_bad_alloc(); - test_sorted_until_bad_alloc(); - - using namespace hpx::execution; - - test_sorted_until_bad_alloc(par); - test_sorted_until_bad_alloc(seq); - - test_sorted_until_async_bad_alloc(seq(task)); - test_sorted_until_async_bad_alloc(par(task)); - - test_sorted_until_seq_bad_alloc(); -} - int hpx_main() { sorted_until_test1(); sorted_until_test2(); sorted_until_test3(); - sorted_until_exception_test(); - sorted_until_bad_alloc_test(); - - std::vector c(100); - hpx::future f = - hpx::dataflow(hpx::ranges::is_sorted, hpx::execution::par, c); - f = hpx::dataflow( - hpx::unwrapping(hpx::ranges::is_sorted), hpx::execution::par, c, f); - f.get(); return hpx::local::finalize(); } diff --git a/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range_exceptions.cpp b/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range_exceptions.cpp new file mode 100644 index 000000000000..c34598f8fffb --- /dev/null +++ b/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range_exceptions.cpp @@ -0,0 +1,854 @@ +// Copyright (c) 2015 Daniel Bourgeois +// +// SPDX-License-Identifier: BSL-1.0 +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "test_utils.hpp" + +//////////////////////////////////////////////////////////////////////////////// +int seed = std::random_device{}(); +std::mt19937 gen(seed); +std::uniform_int_distribution<> dis(0, 99); + +//////////////////////////////////////////////////////////////////////////////// +template +void test_sorted_until_exception(ExPolicy policy, IteratorTag) +{ + static_assert(hpx::is_execution_policy::value, + "hpx::is_execution_policy::value"); + + typedef std::vector::iterator base_iterator; + typedef test::test_iterator iterator; + typedef test::decorated_iterator + decorated_iterator; + + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_exception = false; + try + { + hpx::ranges::is_sorted_until(policy, + decorated_iterator( + std::begin(c), []() { throw std::runtime_error("test"); }), + decorated_iterator( + std::end(c), []() { throw std::runtime_error("test"); })); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(policy, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), + iterator(std::end(c)), + [](int, int) -> bool { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(policy, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), + iterator(std::end(c)), std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(policy, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); +} + +template +void test_sorted_until_async_exception(ExPolicy p, IteratorTag) +{ + typedef std::vector::iterator base_iterator; + typedef test::test_iterator iterator; + typedef test::decorated_iterator + decorated_iterator; + + std::vector c(10007); + std::iota(std::begin(c), std::end(c), 0); + + bool caught_exception = false; + try + { + hpx::future f = hpx::ranges::is_sorted_until(p, + decorated_iterator( + std::begin(c), []() { throw std::runtime_error("test"); }), + decorated_iterator( + std::end(c), []() { throw std::runtime_error("test"); })); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(p, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::future f = hpx::ranges::is_sorted_until(p, + iterator(std::begin(c)), iterator(std::end(c)), + [](int, int) -> int { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(p, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::future f = hpx::ranges::is_sorted_until(p, + iterator(std::begin(c)), iterator(std::end(c)), std::less(), + [](int) -> bool { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(p, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); +} + +template +void test_sorted_until_seq_exception(IteratorTag) +{ + typedef std::vector::iterator base_iterator; + typedef test::test_iterator iterator; + typedef test::decorated_iterator + decorated_iterator; + + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_exception = false; + try + { + hpx::ranges::is_sorted_until( + decorated_iterator( + std::begin(c), []() { throw std::runtime_error("test"); }), + decorated_iterator( + std::end(c), []() { throw std::runtime_error("test"); })); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(hpx::execution::seq, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::ranges::is_sorted_until(iterator(std::begin(c)), + iterator(std::end(c)), + [](int, int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(hpx::execution::seq, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::ranges::is_sorted_until(iterator(std::begin(c)), + iterator(std::end(c)), std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions::call(hpx::execution::seq, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); +} + +template +void test_sorted_until_exception(ExPolicy policy) +{ + static_assert(hpx::is_execution_policy::value, + "hpx::is_execution_policy::value"); + + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_exception = false; + try + { + hpx::ranges::is_sorted_until(policy, c, + [](int, int) -> bool { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions_base::call(policy, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::ranges::is_sorted_until(policy, c, std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions_base::call(policy, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); +} + +template +void test_sorted_until_async_exception(ExPolicy p) +{ + std::vector c(10007); + std::iota(std::begin(c), std::end(c), 0); + + bool caught_exception = false; + try + { + auto f = hpx::ranges::is_sorted_until( + p, c, [](int, int) -> int { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions_base::call(p, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + auto f = hpx::ranges::is_sorted_until(p, c, std::less(), + [](int) -> bool { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions_base::call(p, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); +} + +void test_sorted_until_seq_exception() +{ + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_exception = false; + try + { + hpx::ranges::is_sorted_until( + c, [](int, int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions_base::call( + hpx::execution::seq, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); + + caught_exception = false; + try + { + hpx::ranges::is_sorted_until(c, std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_exception = true; + test::test_num_exceptions_base::call( + hpx::execution::seq, e); + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_exception); +} + +template +void test_sorted_until_exception() +{ + using namespace hpx::execution; + //If the execution policy object is of type vector_execution_policy, + // std::terminate shall be called. Therefore we do not test exceptions + // with a vector execution policy + test_sorted_until_exception(seq, IteratorTag()); + test_sorted_until_exception(par, IteratorTag()); + + test_sorted_until_async_exception(seq(task), IteratorTag()); + test_sorted_until_async_exception(par(task), IteratorTag()); + + test_sorted_until_seq_exception(IteratorTag()); +} + +void sorted_until_exception_test() +{ + test_sorted_until_exception(); + test_sorted_until_exception(); + + using namespace hpx::execution; + + test_sorted_until_exception(seq); + test_sorted_until_exception(par); + + test_sorted_until_async_exception(seq(task)); + test_sorted_until_async_exception(par(task)); + + test_sorted_until_seq_exception(); +} + +//////////////////////////////////////////////////////////////////////////////// +template +void test_sorted_until_bad_alloc(ExPolicy policy, IteratorTag) +{ + static_assert(hpx::is_execution_policy::value, + "hpx::is_execution_policy::value"); + + typedef std::vector::iterator base_iterator; + typedef test::test_iterator iterator; + typedef test::decorated_iterator + decorated_iterator; + + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(policy, + decorated_iterator( + std::begin(c), []() { throw std::runtime_error("test"); }), + decorated_iterator( + std::end(c), []() { throw std::runtime_error("test"); })); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), + iterator(std::end(c)), + [](int, int) -> bool { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)), + iterator(std::end(c)), std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); +} + +template +void test_sorted_until_async_bad_alloc(ExPolicy p, IteratorTag) +{ + typedef std::vector::iterator base_iterator; + typedef test::test_iterator iterator; + typedef test::decorated_iterator + decorated_iterator; + + std::vector c(10007); + std::iota(std::begin(c), std::end(c), 0); + + bool caught_bad_alloc = false; + try + { + hpx::future f = hpx::ranges::is_sorted_until(p, + decorated_iterator( + std::begin(c), []() { throw std::runtime_error("test"); }), + decorated_iterator( + std::end(c), []() { throw std::runtime_error("test"); })); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::future f = hpx::ranges::is_sorted_until(p, + iterator(std::begin(c)), iterator(std::end(c)), + [](int, int) -> int { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::future f = hpx::ranges::is_sorted_until(p, + iterator(std::begin(c)), iterator(std::end(c)), std::less(), + [](int) -> bool { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); +} + +template +void test_sorted_until_seq_bad_alloc(IteratorTag) +{ + typedef std::vector::iterator base_iterator; + typedef test::test_iterator iterator; + typedef test::decorated_iterator + decorated_iterator; + + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until( + decorated_iterator( + std::begin(c), []() { throw std::runtime_error("test"); }), + decorated_iterator( + std::end(c), []() { throw std::runtime_error("test"); })); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(iterator(std::begin(c)), + iterator(std::end(c)), + [](int, int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(iterator(std::begin(c)), + iterator(std::end(c)), std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); +} + +template +void test_sorted_until_bad_alloc(ExPolicy policy) +{ + static_assert(hpx::is_execution_policy::value, + "hpx::is_execution_policy::value"); + + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(policy, c, + [](int, int) -> bool { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(policy, c, std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); +} + +template +void test_sorted_until_async_bad_alloc(ExPolicy p) +{ + std::vector c(10007); + std::iota(std::begin(c), std::end(c), 0); + + bool caught_bad_alloc = false; + try + { + auto f = hpx::ranges::is_sorted_until( + p, c, [](int, int) -> int { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + auto f = hpx::ranges::is_sorted_until(p, c, std::less(), + [](int) -> bool { throw std::runtime_error("test"); }); + f.get(); + + HPX_TEST(false); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); +} + +void test_sorted_until_seq_bad_alloc() +{ + std::vector c(10007); + //fill first half of array with even numbers and second half + //with odd numbers + std::iota(std::begin(c), std::end(c), 0); + + bool caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until( + c, [](int, int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); + + caught_bad_alloc = false; + try + { + hpx::ranges::is_sorted_until(c, std::less(), + [](int) -> int { throw std::runtime_error("test"); }); + } + catch (hpx::exception_list const& e) + { + caught_bad_alloc = true; + } + catch (...) + { + HPX_TEST(false); + } + + HPX_TEST(caught_bad_alloc); +} + +template +void test_sorted_until_bad_alloc() +{ + using namespace hpx::execution; + + // If the execution policy object is of type vector_execution_policy, + // std::terminate shall be called. therefore we do not test exceptions + // with a vector execution policy + test_sorted_until_bad_alloc(par, IteratorTag()); + test_sorted_until_bad_alloc(seq, IteratorTag()); + + test_sorted_until_async_bad_alloc(seq(task), IteratorTag()); + test_sorted_until_async_bad_alloc(par(task), IteratorTag()); + + test_sorted_until_seq_bad_alloc(IteratorTag()); +} + +void sorted_until_bad_alloc_test() +{ + test_sorted_until_bad_alloc(); + test_sorted_until_bad_alloc(); + + using namespace hpx::execution; + + test_sorted_until_bad_alloc(par); + test_sorted_until_bad_alloc(seq); + + test_sorted_until_async_bad_alloc(seq(task)); + test_sorted_until_async_bad_alloc(par(task)); + + test_sorted_until_seq_bad_alloc(); +} + +int hpx_main() +{ + sorted_until_exception_test(); + sorted_until_bad_alloc_test(); + + std::vector c(100); + hpx::future f = + hpx::dataflow(hpx::ranges::is_sorted, hpx::execution::par, c); + f = hpx::dataflow( + hpx::unwrapping(hpx::ranges::is_sorted), hpx::execution::par, c, f); + f.get(); + + return hpx::local::finalize(); +} + +int main(int argc, char* argv[]) +{ + using namespace hpx::program_options; + options_description desc_commandline( + "Usage: " HPX_APPLICATION_STRING " [options]"); + + std::vector const cfg = {"hpx.os_threads=all"}; + + hpx::local::init_params init_args; + init_args.desc_cmdline = desc_commandline; + init_args.cfg = cfg; + + HPX_TEST_EQ_MSG(hpx::local::init(hpx_main, argc, argv, init_args), 0, + "HPX main exited with non-zero status"); + + return hpx::util::report_errors(); +}