nullptr_t_deleter_throw.pass.cpp revision c52f43e72dfcea03037729649da84c23b3beb04a
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <memory>
11
12// shared_ptr
13
14// template<class D> shared_ptr(nullptr_t, D d);
15
16#include <memory>
17#include <cassert>
18#include <new>
19#include <cstdlib>
20#include "../test_deleter.h"
21
22struct A
23{
24    static int count;
25
26    A() {++count;}
27    A(const A&) {++count;}
28    ~A() {--count;}
29};
30
31int A::count = 0;
32
33bool throw_next = false;
34
35void* operator new(std::size_t s) throw(std::bad_alloc)
36{
37    if (throw_next)
38        throw std::bad_alloc();
39    return std::malloc(s);
40}
41
42void  operator delete(void* p) throw()
43{
44    std::free(p);
45}
46
47int main()
48{
49    throw_next = true;
50    try
51    {
52        std::shared_ptr<A> p(nullptr, test_deleter<A>(3));
53        assert(false);
54    }
55    catch (std::bad_alloc&)
56    {
57        assert(A::count == 0);
58        assert(test_deleter<A>::count == 0);
59        assert(test_deleter<A>::dealloc_count == 1);
60    }
61}
62