// -*- C++ -*- //===-- swap_ranges.pass.cpp ----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "support/pstl_test_config.h" #include #include #include #include "support/utils.h" using namespace TestUtils; template struct wrapper { T t; std::size_t number_of_swaps = 0; wrapper() {} explicit wrapper(T t_) : t(t_) {} template void operator=(const U& b) { t = b; } bool operator==(const wrapper& a) const { return t == a.t; } }; template void swap(wrapper& a, wrapper& b) { std::swap(a.t, b.t); a.number_of_swaps++; b.number_of_swaps++; } template struct check_swap { bool operator()(T& a) { return true; } }; template struct check_swap> { bool operator()(wrapper& a) { bool temp = (a.number_of_swaps == 1); a.number_of_swaps = 0; return temp; } }; struct test_one_policy { template void operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e) { using namespace std; using T_ref = typename iterator_traits::reference; using T = typename iterator_traits::value_type; iota(data_b, data_e, 0); iota(actual_b, actual_e, std::distance(data_b, data_e)); Iterator2 actual_return = swap_ranges(exec, data_b, data_e, actual_b); bool check_return = (actual_return == actual_e); EXPECT_TRUE(check_return, "wrong result of swap_ranges"); if (check_return) { std::size_t i = 0; bool check = all_of(actual_b, actual_e, [&i](T_ref a) { return a == T(i++); }) && all_of(data_b, data_e, [&i](T_ref a) { return a == T(i++); }); EXPECT_TRUE(check, "wrong effect of swap_ranges"); if (check) { bool swap_check = all_of(data_b, data_e, check_swap()) && all_of(actual_b, actual_e, check_swap()); EXPECT_TRUE(swap_check, "wrong effect of swap_ranges swap check"); } } } }; template void test() { const std::size_t max_len = 100000; Sequence data(max_len); Sequence actual(max_len); for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) { invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), actual.begin() + len); } } int32_t main() { test>(); test>(); test(); test(); std::cout << done() << std::endl; return 0; }