]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory/shared_ptr/assign/assign.cc
1 // { dg-options "-std=gnu++14" }
3 // Copyright (C) 2015-2016 Free Software Foundation, Inc.
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)
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.
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/>.
20 // 8.2.1 Class template shared_ptr [memory.smartptr.shared]
23 #include <experimental/memory>
24 #include <testsuite_hooks.h>
29 virtual ~A() { ++dtor_count
; }
30 static long ctor_count
;
31 static long dtor_count
;
33 long A::ctor_count
= 0;
34 long A::dtor_count
= 0;
39 virtual ~B() { ++dtor_count
; }
40 static long ctor_count
;
41 static long dtor_count
;
43 long B::ctor_count
= 0;
44 long B::dtor_count
= 0;
46 struct reset_count_struct
57 // C++14 ยง20.8.2.2.3 shared_ptr assignment
62 reset_count_struct
__attribute__((unused
)) reset
;
63 bool test
__attribute__((unused
)) = true;
65 std::experimental::shared_ptr
<A
[5]> a
;
66 std::experimental::shared_ptr
<A
[]> a1
;
67 std::experimental::shared_ptr
<B
[5]> a2
;
69 a
= std::experimental::shared_ptr
<A
[5]> ();
70 VERIFY( a
.get() == 0 );
71 VERIFY( A::ctor_count
== 0 );
72 VERIFY( A::dtor_count
== 0 );
73 VERIFY( B::ctor_count
== 0 );
74 VERIFY( B::dtor_count
== 0 );
76 a
= std::experimental::shared_ptr
<A
[5]> (new A
[5]);
77 VERIFY( a
.get() != 0 );
78 VERIFY( A::ctor_count
== 5 );
79 VERIFY( A::dtor_count
== 0 );
80 VERIFY( B::ctor_count
== 0 );
81 VERIFY( B::dtor_count
== 0 );
83 a1
= std::experimental::shared_ptr
<A
[5]> (new A
[5]);
84 VERIFY( a1
.get() != 0 );
85 VERIFY( A::ctor_count
== 10 );
86 VERIFY( A::dtor_count
== 0 );
87 VERIFY( B::ctor_count
== 0 );
88 VERIFY( B::dtor_count
== 0 );
90 a2
= std::experimental::shared_ptr
<B
[5]> (new B
[5]);
91 VERIFY( a2
.get() != 0 );
92 VERIFY( A::ctor_count
== 15 );
93 VERIFY( A::dtor_count
== 0 );
94 VERIFY( B::ctor_count
== 5 );
95 VERIFY( B::dtor_count
== 0 );
101 bool test
__attribute__((unused
)) = true;
103 std::experimental::shared_ptr
<A
[5]> p(new A
[5]);
104 std::experimental::shared_ptr
<A
[5]> p1
;
105 std::experimental::shared_ptr
<A
[]> p2
;
108 VERIFY( p
.get() == p1
.get() );
111 VERIFY( p1
.get() == p2
.get() );