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 // UNSUPPORTED: c++03, c++11, c++14
11
12 #include "support/pstl_test_config.h"
13
14 #include <execution>
15 #include <algorithm>
16
17 #include "support/utils.h"
18
19 using namespace TestUtils;
20
21 struct test_fill
22 {
23 template <typename It, typename T>
24 bool
checktest_fill25 check(It first, It last, const T& value)
26 {
27 for (; first != last; ++first)
28 if (*first != value)
29 return false;
30 return true;
31 }
32
33 template <typename Policy, typename Iterator, typename T>
34 void
operator ()test_fill35 operator()(Policy&& exec, Iterator first, Iterator last, const T& value)
36 {
37 fill(first, last, T(value + 1)); // initialize memory with different value
38
39 fill(exec, first, last, value);
40 EXPECT_TRUE(check(first, last, value), "fill wrong result");
41 }
42 };
43
44 struct test_fill_n
45 {
46 template <typename It, typename Size, typename T>
47 bool
checktest_fill_n48 check(It first, Size n, const T& value)
49 {
50 for (Size i = 0; i < n; ++i, ++first)
51 if (*first != value)
52 return false;
53 return true;
54 }
55
56 template <typename Policy, typename Iterator, typename Size, typename T>
57 void
operator ()test_fill_n58 operator()(Policy&& exec, Iterator first, Size n, const T& value)
59 {
60 fill_n(first, n, T(value + 1)); // initialize memory with different value
61
62 const Iterator one_past_last = fill_n(exec, first, n, value);
63 const Iterator expected_return = std::next(first, n);
64
65 EXPECT_TRUE(expected_return == one_past_last, "fill_n should return Iterator to one past the element assigned");
66 EXPECT_TRUE(check(first, n, value), "fill_n wrong result");
67
68 //n == -1
69 const Iterator res = fill_n(exec, first, -1, value);
70 EXPECT_TRUE(res == first, "fill_n wrong result for n == -1");
71 }
72 };
73
74 template <typename T>
75 void
test_fill_by_type(std::size_t n)76 test_fill_by_type(std::size_t n)
77 {
78 Sequence<T> in(n, [](std::size_t) -> T { return T(0); }); //fill with zeros
79 T value = -1;
80
81 invoke_on_all_policies(test_fill(), in.begin(), in.end(), value);
82 invoke_on_all_policies(test_fill_n(), in.begin(), n, value);
83 }
84
85 int
main()86 main()
87 {
88
89 const std::size_t N = 100000;
90
91 for (std::size_t n = 0; n < N; n = n < 16 ? n + 1 : size_t(3.1415 * n))
92 {
93 test_fill_by_type<int32_t>(n);
94 test_fill_by_type<float64_t>(n);
95 }
96
97 std::cout << done() << std::endl;
98
99 return 0;
100 }
101