1 // -*- C++ -*- 2 //===-- fill.pass.cpp -----------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "support/pstl_test_config.h" 11 12 #ifdef PSTL_STANDALONE_TESTS 13 14 #include "pstl/execution" 15 #include "pstl/algorithm" 16 #else 17 #include <execution> 18 #include <algorithm> 19 #endif // PSTL_STANDALONE_TESTS 20 21 #include "support/utils.h" 22 23 using namespace TestUtils; 24 25 struct test_fill 26 { 27 template <typename It, typename T> 28 bool 29 check(It first, It last, const T& value) 30 { 31 for (; first != last; ++first) 32 if (*first != value) 33 return false; 34 return true; 35 } 36 37 template <typename Policy, typename Iterator, typename T> 38 void 39 operator()(Policy&& exec, Iterator first, Iterator last, const T& value) 40 { 41 fill(first, last, T(value + 1)); // initialize memory with different value 42 43 fill(exec, first, last, value); 44 EXPECT_TRUE(check(first, last, value), "fill wrong result"); 45 } 46 }; 47 48 struct test_fill_n 49 { 50 template <typename It, typename Size, typename T> 51 bool 52 check(It first, Size n, const T& value) 53 { 54 for (Size i = 0; i < n; ++i, ++first) 55 if (*first != value) 56 return false; 57 return true; 58 } 59 60 template <typename Policy, typename Iterator, typename Size, typename T> 61 void 62 operator()(Policy&& exec, Iterator first, Size n, const T& value) 63 { 64 fill_n(first, n, T(value + 1)); // initialize memory with different value 65 66 const Iterator one_past_last = fill_n(exec, first, n, value); 67 const Iterator expected_return = std::next(first, n); 68 69 EXPECT_TRUE(expected_return == one_past_last, "fill_n should return Iterator to one past the element assigned"); 70 EXPECT_TRUE(check(first, n, value), "fill_n wrong result"); 71 72 //n == -1 73 const Iterator res = fill_n(exec, first, -1, value); 74 EXPECT_TRUE(res == first, "fill_n wrong result for n == -1"); 75 } 76 }; 77 78 template <typename T> 79 void 80 test_fill_by_type(std::size_t n) 81 { 82 Sequence<T> in(n, [](std::size_t v) -> T { return T(0); }); //fill with zeros 83 T value = -1; 84 85 invoke_on_all_policies(test_fill(), in.begin(), in.end(), value); 86 invoke_on_all_policies(test_fill_n(), in.begin(), n, value); 87 } 88 89 int32_t 90 main() 91 { 92 93 const std::size_t N = 100000; 94 95 for (std::size_t n = 0; n < N; n = n < 16 ? n + 1 : size_t(3.1415 * n)) 96 { 97 test_fill_by_type<int32_t>(n); 98 test_fill_by_type<float64_t>(n); 99 } 100 101 std::cout << done() << std::endl; 102 103 return 0; 104 } 105