| 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 |
|
| 23 |
struct 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 |
}; |
| 31 |
int Counted::count = 0; |
| 32 |
int Counted::constructed = 0; |
| 33 |
|
| 34 |
|
| 35 |
struct 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 |
}; |
| 50 |
int ThrowsCounted::count = 0; |
| 51 |
int ThrowsCounted::constructed = 0; |
| 52 |
int ThrowsCounted::throw_after = 0; |
| 53 |
|
| 54 |
|
| 55 |
void 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 |
|
| 72 |
void 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(p, p+N); |
| 85 |
assert(Counted::count == 0); |
| 86 |
} |
| 87 |
|
| 88 |
int main(int, char**) |
| 89 |
{ |
| 90 |
test_counted(); |
| 91 |
test_ctor_throws(); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |