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