1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <memory>
10
11// template <class ForwardIterator, class T>
12// void
13// uninitialized_fill(ForwardIterator first, ForwardIterator last,
14// const T& x);
15
16#include <memory>
17#include <cassert>
18
19#include "test_macros.h"
20
21struct B
22{
23 static int count_;
24 static int population_;
25 int data_;
26 explicit B() : data_(1) { ++population_; }
27 B(const B &b) {
28 ++count_;
29 if (count_ == 3)
30 TEST_THROW(1);
31 data_ = b.data_;
32 ++population_;
33 }
34 ~B() {data_ = 0; --population_; }
35};
36
37int B::count_ = 0;
38int B::population_ = 0;
39
40struct Nasty
41{
42 Nasty() : i_ ( counter_++ ) {}
43 Nasty * operator &() const { return nullptr; }
44 int i_;
45 static int counter_;
46};
47
48int Nasty::counter_ = 0;
49
50int main(int, char**)
51{
52 {
53 const int N = 5;
54 char pool[sizeof(B)*N] = {0};
55 B* bp = (B*)pool;
56 assert(B::population_ == 0);
57#ifndef TEST_HAS_NO_EXCEPTIONS
58 try
59 {
60 std::uninitialized_fill(first: bp, last: bp+N, x: B());
61 assert(false);
62 }
63 catch (...)
64 {
65 assert(B::population_ == 0);
66 }
67#endif
68 B::count_ = 0;
69 std::uninitialized_fill(first: bp, last: bp+2, x: B());
70 for (int i = 0; i < 2; ++i)
71 assert(bp[i].data_ == 1);
72 assert(B::population_ == 2);
73 }
74 {
75 const int N = 5;
76 char pool[N*sizeof(Nasty)] = {0};
77 Nasty* bp = (Nasty*)pool;
78
79 Nasty::counter_ = 23;
80 std::uninitialized_fill(first: bp, last: bp+N, x: Nasty());
81 for (int i = 0; i < N; ++i)
82 assert(bp[i].i_ == 23);
83 }
84
85 return 0;
86}
87

source code of libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill/uninitialized_fill.pass.cpp