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// UNSUPPORTED: c++03, c++11, c++14
10
11// <memory>
12
13// template <class ForwardIt>
14// void uninitialized_default_construct(ForwardIt, ForwardIt);
15
16#include <memory>
17#include <cstdlib>
18#include <cassert>
19
20#include "test_macros.h"
21#include "test_iterators.h"
22
23struct Counted {
24 static int count;
25 static int constructed;
26 explicit Counted() { ++count; ++constructed; }
27 Counted(Counted const&) { assert(false); }
28 ~Counted() { --count; }
29 friend void operator&(Counted) = delete;
30};
31int Counted::count = 0;
32int Counted::constructed = 0;
33
34
35struct ThrowsCounted {
36 static int count;
37 static int constructed;
38 static int throw_after;
39 explicit ThrowsCounted() {
40 ++constructed;
41 if (throw_after > 0 && --throw_after == 0) {
42 TEST_THROW(1);
43 }
44 ++count;
45 }
46 ThrowsCounted(ThrowsCounted const&) { assert(false); }
47 ~ThrowsCounted() { assert(count > 0); --count; }
48 friend void operator&(ThrowsCounted) = delete;
49};
50int ThrowsCounted::count = 0;
51int ThrowsCounted::constructed = 0;
52int ThrowsCounted::throw_after = 0;
53
54
55void test_ctor_throws()
56{
57#ifndef TEST_HAS_NO_EXCEPTIONS
58 using It = forward_iterator<ThrowsCounted*>;
59 const int N = 5;
60 alignas(ThrowsCounted) char pool[sizeof(ThrowsCounted)*N] = {};
61 ThrowsCounted* p = (ThrowsCounted*)pool;
62 try {
63 ThrowsCounted::throw_after = 4;
64 std::uninitialized_default_construct(It(p), It(p+N));
65 assert(false);
66 } catch (...) {}
67 assert(ThrowsCounted::count == 0);
68 assert(ThrowsCounted::constructed == 4); // Fourth construction throws
69#endif
70}
71
72void test_counted()
73{
74 using It = forward_iterator<Counted*>;
75 const int N = 5;
76 alignas(Counted) char pool[sizeof(Counted)*N] = {};
77 Counted* p = (Counted*)pool;
78 std::uninitialized_default_construct(It(p), It(p+1));
79 assert(Counted::count == 1);
80 assert(Counted::constructed == 1);
81 std::uninitialized_default_construct(It(p+1), It(p+N));
82 assert(Counted::count == 5);
83 assert(Counted::constructed == 5);
84 std::destroy(first: p, last: p+N);
85 assert(Counted::count == 0);
86}
87
88int main(int, char**)
89{
90 test_counted();
91 test_ctor_throws();
92
93 return 0;
94}
95

source code of libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/uninitialized_default_construct.pass.cpp