1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
14 // template <class ForwardIt>
15 // void uninitialized_default_construct(ForwardIt, ForwardIt);
21 #include "test_macros.h"
22 #include "test_iterators.h"
26 static int constructed;
27 static void reset() { count = constructed = 0; }
28 explicit Counted() { ++count; ++constructed; }
29 Counted(Counted const&) { assert(false); }
30 ~Counted() { --count; }
31 friend void operator&(Counted) = delete;
33 int Counted::count = 0;
34 int Counted::constructed = 0;
37 struct ThrowsCounted {
39 static int constructed;
40 static int throw_after;
41 static void reset() { throw_after = count = constructed = 0; }
42 explicit ThrowsCounted() {
44 if (throw_after > 0 && --throw_after == 0) {
49 ThrowsCounted(ThrowsCounted const&) { assert(false); }
50 ~ThrowsCounted() { assert(count > 0); --count; }
51 friend void operator&(ThrowsCounted) = delete;
53 int ThrowsCounted::count = 0;
54 int ThrowsCounted::constructed = 0;
55 int ThrowsCounted::throw_after = 0;
58 void test_ctor_throws()
60 #ifndef TEST_HAS_NO_EXCEPTIONS
61 using It = forward_iterator<ThrowsCounted*>;
63 alignas(ThrowsCounted) char pool[sizeof(ThrowsCounted)*N] = {};
64 ThrowsCounted* p = (ThrowsCounted*)pool;
66 ThrowsCounted::throw_after = 4;
67 std::uninitialized_default_construct(It(p), It(p+N));
70 assert(ThrowsCounted::count == 0);
71 assert(ThrowsCounted::constructed == 4); // forth construction throws
77 using It = forward_iterator<Counted*>;
79 alignas(Counted) char pool[sizeof(Counted)*N] = {};
80 Counted* p = (Counted*)pool;
81 std::uninitialized_default_construct(It(p), It(p+1));
82 assert(Counted::count == 1);
83 assert(Counted::constructed = 1);
84 std::uninitialized_default_construct(It(p+1), It(p+N));
85 assert(Counted::count == 5);
86 assert(Counted::constructed == 5);
88 assert(Counted::count == 0);
91 void test_value_initialized()
93 using It = forward_iterator<int*>;
95 int pool[N] = {-1, -1, -1, -1, -1};
97 std::uninitialized_default_construct(It(p), It(p+1));
98 assert(pool[0] == -1);
99 assert(pool[1] == -1);
100 std::uninitialized_default_construct(It(p+1), It(p+N));
101 assert(pool[1] == -1);
102 assert(pool[2] == -1);
103 assert(pool[3] == -1);
104 assert(pool[4] == -1);
110 test_value_initialized();