]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/uninitialized_default_construct_n.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / utilities / memory / specialized.algorithms / uninitialized.construct.default / uninitialized_default_construct_n.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11
12 // <memory>
13
14 // template <class ForwardIt>
15 // void uninitialized_default_construct(ForwardIt, ForwardIt);
16
17 #include <memory>
18 #include <cstdlib>
19 #include <cassert>
20
21 #include "test_macros.h"
22 #include "test_iterators.h"
23
24 struct Counted {
25   static int count;
26   static int constructed;
27   static void reset() { count = constructed =  0; }
28   explicit Counted() { ++count; ++constructed; }
29   Counted(Counted const&) { assert(false); }
30   ~Counted() { assert(count > 0); --count; }
31   friend void operator&(Counted) = delete;
32 };
33 int Counted::count = 0;
34 int Counted::constructed = 0;
35
36
37 struct ThrowsCounted {
38   static int count;
39   static int constructed;
40   static int throw_after;
41   static void reset() { throw_after = count = constructed =  0; }
42   explicit ThrowsCounted() {
43       ++constructed;
44       if (throw_after > 0 && --throw_after == 0) {
45           TEST_THROW(1);
46       }
47       ++count;
48   }
49   ThrowsCounted(ThrowsCounted const&) { assert(false); }
50   ~ThrowsCounted() { assert(count > 0); --count; }
51   friend void operator&(ThrowsCounted) = delete;
52 };
53 int ThrowsCounted::count = 0;
54 int ThrowsCounted::constructed = 0;
55 int ThrowsCounted::throw_after = 0;
56
57 void test_ctor_throws()
58 {
59 #ifndef TEST_HAS_NO_EXCEPTIONS
60     using It = forward_iterator<ThrowsCounted*>;
61     const int N = 5;
62     alignas(ThrowsCounted) char pool[sizeof(ThrowsCounted)*N] = {};
63     ThrowsCounted* p = (ThrowsCounted*)pool;
64     try {
65         ThrowsCounted::throw_after = 4;
66         std::uninitialized_default_construct_n(It(p), N);
67         assert(false);
68     } catch (...) {}
69     assert(ThrowsCounted::count == 0);
70     assert(ThrowsCounted::constructed == 4); // forth construction throws
71 #endif
72 }
73
74 void test_counted()
75 {
76     using It = forward_iterator<Counted*>;
77     const int N = 5;
78     alignas(Counted) char pool[sizeof(Counted)*N] = {};
79     Counted* p = (Counted*)pool;
80     It e = std::uninitialized_default_construct_n(It(p), 1);
81     assert(e == It(p+1));
82     assert(Counted::count == 1);
83     assert(Counted::constructed = 1);
84     e = std::uninitialized_default_construct_n(It(p+1), 4);
85     assert(e == It(p+N));
86     assert(Counted::count == 5);
87     assert(Counted::constructed == 5);
88     std::destroy(p, p+N);
89     assert(Counted::count == 0);
90 }
91
92 void test_value_initialized()
93 {
94     using It = forward_iterator<int*>;
95     const int N = 5;
96     int pool[N] = {-1, -1, -1, -1, -1};
97     int* p = pool;
98     auto e = std::uninitialized_default_construct_n(It(p), 1);
99     assert(e == It(p+1));
100     assert(pool[0] == -1);
101     assert(pool[1] == -1);
102     e = std::uninitialized_default_construct_n(It(p+1), 4);
103     assert(e == It(p+N));
104     assert(pool[1] == -1);
105     assert(pool[2] == -1);
106     assert(pool[3] == -1);
107     assert(pool[4] == -1);
108 }
109
110
111 int main()
112 {
113     test_counted();
114     test_value_initialized();
115     test_ctor_throws();
116 }