1 // { dg-options "-std=gnu++0x" }
2 
3 // Copyright (C) 2007-2013 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 { };
deletefunc(A * p)30 void deletefunc(A* p) { delete p; }
31 struct D
32 {
operator ()D33   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
test01()42 test01()
43 {
44   bool test __attribute__((unused)) = true;
45   tracker_allocator_counter::reset();
46 
47   std::shared_ptr<A> p1(new A, deletefunc, tracker_allocator<A>());
48   std::size_t const sz = tracker_allocator_counter::get_allocation_count();
49   VERIFY( sz > 0 );
50   {
51     std::shared_ptr<A> p2(p1);
52     VERIFY( p2.use_count() == 2 );
53     VERIFY( tracker_allocator_counter::get_allocation_count() == sz );
54     VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
55   }
56   VERIFY( p1.use_count() == 1 );
57   VERIFY( tracker_allocator_counter::get_allocation_count() == sz );
58   VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
59   p1.reset();
60   VERIFY( p1.use_count() == 0 );
61   VERIFY( tracker_allocator_counter::get_allocation_count() == sz );
62   VERIFY( tracker_allocator_counter::get_deallocation_count() == sz );
63 
64   return 0;
65 }
66 
67 // Construction with allocator
68 int
test02()69 test02()
70 {
71   bool test __attribute__((unused)) = true;
72   tracker_allocator_counter::reset();
73 
74   std::shared_ptr<A> p1(new A, deletefunc, tracker_allocator<A>());
75   std::size_t const sz1 = tracker_allocator_counter::get_allocation_count();
76   VERIFY( sz1 > 0 );
77   std::shared_ptr<A> p2(new A, D(), tracker_allocator<A>());
78   std::size_t const sz2 = tracker_allocator_counter::get_allocation_count();
79   VERIFY( sz2 > sz1 );
80   VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 );
81   p1 = p2;
82   VERIFY( p2.use_count() == 2 );
83   VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
84   VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1 );
85   p1.reset();
86   VERIFY( p2.use_count() == 1 );
87   VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
88   VERIFY( tracker_allocator_counter::get_deallocation_count() == sz1 );
89   p2.reset();
90   VERIFY( tracker_allocator_counter::get_allocation_count() == sz2 );
91   VERIFY( tracker_allocator_counter::get_deallocation_count() == sz2 );
92   VERIFY( D::delete_count == 1 );
93 
94   return 0;
95 }
96 
97 int
main()98 main()
99 {
100   test01();
101   test02();
102   return 0;
103 }
104