]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/tr1/2_general_utilities/shared_ptr/dest/dest.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / tr1 / 2_general_utilities / shared_ptr / dest / dest.cc
1 // Copyright (C) 2005-2021 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // TR1 2.2.2 Template class shared_ptr [tr.util.smartptr.shared]
19
20 #include <tr1/memory>
21 #include <testsuite_hooks.h>
22
23 struct A
24 {
25 A() { ++ctor_count; }
26 ~A() { ++dtor_count; }
27 static long ctor_count;
28 static long dtor_count;
29 };
30 long A::ctor_count = 0;
31 long A::dtor_count = 0;
32
33 struct B : A
34 {
35 B() { ++ctor_count; }
36 ~B() { ++dtor_count; }
37 static long ctor_count;
38 static long dtor_count;
39 };
40 long B::ctor_count = 0;
41 long B::dtor_count = 0;
42
43 struct D
44 {
45 void operator()(const B* p) { delete p; ++delete_count; }
46 static long delete_count;
47 };
48 long D::delete_count = 0;
49
50 struct reset_count_struct
51 {
52 ~reset_count_struct()
53 {
54 A::ctor_count = 0;
55 A::dtor_count = 0;
56 B::ctor_count = 0;
57 B::dtor_count = 0;
58 D::delete_count = 0;
59 }
60 };
61
62
63 // 2.2.3.2 shared_ptr destructor [tr.util.smartptr.shared.dest]
64
65 // empty shared_ptr
66 int
67 test01()
68 {
69 reset_count_struct __attribute__((unused)) reset;
70
71 {
72 std::tr1::shared_ptr<A> a;
73 }
74 VERIFY( A::ctor_count == 0 );
75 VERIFY( A::dtor_count == 0 );
76 VERIFY( B::ctor_count == 0 );
77 VERIFY( B::dtor_count == 0 );
78 VERIFY( D::delete_count == 0 );
79
80 return 0;
81 }
82
83 // shared ownership
84 int
85 test02()
86 {
87 reset_count_struct __attribute__((unused)) reset;
88
89 std::tr1::shared_ptr<A> a;
90 {
91 a = std::tr1::shared_ptr<A>(new B, D());
92 }
93 VERIFY( A::ctor_count == 1 );
94 VERIFY( A::dtor_count == 0 );
95 VERIFY( B::ctor_count == 1 );
96 VERIFY( B::dtor_count == 0 );
97 VERIFY( D::delete_count == 0 );
98
99 return 0;
100 }
101
102 // exclusive ownership
103 int
104 test03()
105 {
106 reset_count_struct __attribute__((unused)) reset;
107
108 {
109 std::tr1::shared_ptr<A> a1(new B);
110 std::tr1::shared_ptr<A> a2(new B, D());
111 }
112 VERIFY( A::ctor_count == 2 );
113 VERIFY( A::dtor_count == 2 );
114 VERIFY( B::ctor_count == 2 );
115 VERIFY( B::dtor_count == 2 );
116 VERIFY( D::delete_count == 1 );
117
118 return 0;
119 }
120
121
122 int
123 main()
124 {
125 test01();
126 test02();
127 test03();
128 return 0;
129 }