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