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