]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/alloc.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / alloc.cc
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2007-2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
21
22 #include <memory>
23 #include <testsuite_hooks.h>
24 #include <testsuite_allocator.h>
25
26 using __gnu_test::tracker_allocator_counter;
27 using __gnu_test::tracker_allocator;
28
29 struct A { };
30 void deletefunc(A* p) { delete p; }
31 struct D
32 {
33 void operator()(A* p) { delete p; ++delete_count; }
34 static long delete_count;
35 };
36 long D::delete_count = 0;
37
38 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
39
40 // Construction with allocator
41 int
42 test01()
43 {
44 tracker_allocator_counter::reset();
45
46 std::shared_ptr<A> p1(new A, deletefunc, tracker_allocator<A>());
47 std::size_t const sz = tracker_allocator_counter::get_allocation_count();
48 VERIFY( sz > 0 );
49 {
50 std::shared_ptr<A> p2(p1);
51 VERIFY( p2.use_count() == 2 );
52 VERIFY( tracker_allocator_counter::get_allocation_count() == sz );
53 VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
54 }
55 VERIFY( p1.use_count() == 1 );
56 VERIFY( tracker_allocator_counter::get_allocation_count() == sz );
57 VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
58 p1.reset();
59 VERIFY( p1.use_count() == 0 );
60 VERIFY( tracker_allocator_counter::get_allocation_count() == sz );
61 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz );
62
63 return 0;
64 }
65
66 // Construction with allocator
67 int
68 test02()
69 {
70 tracker_allocator_counter::reset();
71
72 std::shared_ptr<A> p1(new A, deletefunc, tracker_allocator<A>());
73 std::size_t const sz1 = tracker_allocator_counter::get_allocation_count();
74 VERIFY( sz1 > 0 );
75 std::shared_ptr<A> p2(new A, D(), tracker_allocator<A>());
76 std::size_t const sz2 = tracker_allocator_counter::get_allocation_count();
77 VERIFY( sz2 > sz1 );
78 VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
79 p1 = p2;
80 VERIFY( p2.use_count() == 2 );
81 VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
82 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1 );
83 p1.reset();
84 VERIFY( p2.use_count() == 1 );
85 VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
86 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1 );
87 p2.reset();
88 VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
89 VERIFY( tracker_allocator_counter::get_deallocation_count() == sz2 );
90 VERIFY( D::delete_count == 1 );
91
92 return 0;
93 }
94
95 int
96 main()
97 {
98 test01();
99 test02();
100 return 0;
101 }