]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_throw.pass.cpp
Import libc++ 3.7.0 release (r246257).
[FreeBSD/FreeBSD.git] / test / std / utilities / memory / util.smartptr / util.smartptr.shared / util.smartptr.shared.const / pointer_deleter_throw.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 // <memory>
11
12 // shared_ptr
13
14 // template<class Y, class D> shared_ptr(Y* p, D d);
15
16 // UNSUPPORTED: sanitizer-new-delete
17
18 #include <memory>
19 #include <cassert>
20 #include <new>
21 #include <cstdlib>
22 #include "../test_deleter.h"
23
24 struct A
25 {
26     static int count;
27
28     A() {++count;}
29     A(const A&) {++count;}
30     ~A() {--count;}
31 };
32
33 int A::count = 0;
34
35 bool throw_next = false;
36
37 void* operator new(std::size_t s) throw(std::bad_alloc)
38 {
39     if (throw_next)
40         throw std::bad_alloc();
41     return std::malloc(s);
42 }
43
44 void  operator delete(void* p) throw()
45 {
46     std::free(p);
47 }
48
49 int main()
50 {
51     A* ptr = new A;
52     throw_next = true;
53     try
54     {
55         std::shared_ptr<A> p(ptr, test_deleter<A>(3));
56         assert(false);
57     }
58     catch (std::bad_alloc&)
59     {
60         assert(A::count == 0);
61         assert(test_deleter<A>::count == 0);
62         assert(test_deleter<A>::dealloc_count == 1);
63     }
64 }