1// -*- C++ -*-
2//===-- uninitialized_fill_destroy.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 <memory>
16
17#include "support/utils.h"
18
19using namespace TestUtils;
20
21struct test_uninitialized_fill_destroy
22{
23 template <typename Policy, typename Iterator, typename T>
24 void
25 operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::false_type)
26 {
27 using namespace std;
28 {
29 T::SetCount(0);
30 uninitialized_fill(exec, first, last, in);
31 size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; });
32 EXPECT_TRUE(n == count, "wrong work of uninitialized_fill");
33 destroy(exec, first, last);
34 EXPECT_TRUE(T::Count() == 0, "wrong work of destroy");
35 }
36
37 {
38 auto res = uninitialized_fill_n(exec, first, n, in);
39 EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n");
40 size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; });
41 EXPECT_TRUE(n == count, "wrong work of uninitialized_fill_n");
42 destroy_n(exec, first, n);
43 EXPECT_TRUE(T::Count() == 0, "wrong work of destroy_n");
44 }
45 }
46 template <typename Policy, typename Iterator, typename T>
47 void
48 operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::true_type)
49 {
50 using namespace std;
51 {
52 destroy(exec, first, last);
53 uninitialized_fill(exec, first, last, in);
54 size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; });
55 EXPECT_EQ(n, count, "wrong work of uninitialized:_fill");
56 }
57 {
58 destroy_n(exec, first, n);
59 auto res = uninitialized_fill_n(exec, first, n, in);
60 size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; });
61 EXPECT_EQ(n, count, "wrong work of uninitialized_fill_n");
62 EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n");
63 }
64 }
65};
66
67template <typename T>
68void
69test_uninitialized_fill_destroy_by_type()
70{
71 std::size_t N = 100000;
72 for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
73 {
74 std::unique_ptr<T[]> p(new T[n]);
75 invoke_on_all_policies(test_uninitialized_fill_destroy(), p.get(), std::next(p.get(), n), T(), n,
76 std::is_trivial<T>());
77 }
78}
79
80int
81main()
82{
83 // for trivial types
84 test_uninitialized_fill_destroy_by_type<int32_t>();
85 test_uninitialized_fill_destroy_by_type<float64_t>();
86
87 // for user-defined types
88 test_uninitialized_fill_destroy_by_type<Wrapper<std::string>>();
89 test_uninitialized_fill_destroy_by_type<Wrapper<int8_t*>>();
90 std::cout << done() << std::endl;
91
92 return 0;
93}
94

source code of pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp