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 Size, class T> |
12 | // ForwardIterator |
13 | // uninitialized_fill_n(ForwardIterator first, Size n, const T& x); |
14 | |
15 | #include <memory> |
16 | #include <cassert> |
17 | |
18 | #include "test_macros.h" |
19 | |
20 | struct B |
21 | { |
22 | static int count_; |
23 | static int population_; |
24 | int data_; |
25 | explicit B() : data_(1) { ++population_; } |
26 | B(const B &b) { |
27 | ++count_; |
28 | if (count_ == 3) |
29 | TEST_THROW(1); |
30 | data_ = b.data_; |
31 | ++population_; |
32 | } |
33 | ~B() {data_ = 0; --population_; } |
34 | }; |
35 | |
36 | int B::count_ = 0; |
37 | int B::population_ = 0; |
38 | |
39 | struct Nasty |
40 | { |
41 | Nasty() : i_ ( counter_++ ) {} |
42 | Nasty * operator &() const { return nullptr; } |
43 | int i_; |
44 | static int counter_; |
45 | }; |
46 | |
47 | int Nasty::counter_ = 0; |
48 | |
49 | int main(int, char**) |
50 | { |
51 | { |
52 | const int N = 5; |
53 | char pool[sizeof(B)*N] = {0}; |
54 | B* bp = (B*)pool; |
55 | assert(B::population_ == 0); |
56 | #ifndef TEST_HAS_NO_EXCEPTIONS |
57 | try |
58 | { |
59 | std::uninitialized_fill_n(first: bp, n: 5, x: B()); |
60 | assert(false); |
61 | } |
62 | catch (...) |
63 | { |
64 | assert(B::population_ == 0); |
65 | } |
66 | #endif |
67 | B::count_ = 0; |
68 | B* r = std::uninitialized_fill_n(first: bp, n: 2, x: B()); |
69 | assert(r == bp + 2); |
70 | for (int i = 0; i < 2; ++i) |
71 | assert(bp[i].data_ == 1); |
72 | assert(B::population_ == 2); |
73 | } |
74 | { |
75 | { |
76 | const int N = 5; |
77 | char pool[N*sizeof(Nasty)] = {0}; |
78 | Nasty* bp = (Nasty*)pool; |
79 | |
80 | Nasty::counter_ = 23; |
81 | std::uninitialized_fill_n(first: bp, n: N, x: Nasty()); |
82 | for (int i = 0; i < N; ++i) |
83 | assert(bp[i].i_ == 23); |
84 | } |
85 | |
86 | } |
87 | |
88 | return 0; |
89 | } |
90 | |