]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/memory/shared_ptr/assign/assign.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / memory / shared_ptr / assign / assign.cc
1 // { dg-options "-std=gnu++14" }
2
3 // Copyright (C) 2015-2016 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 // 8.2.1 Class template shared_ptr [memory.smartptr.shared]
21
22
23 #include <experimental/memory>
24 #include <testsuite_hooks.h>
25
26 struct A
27 {
28 A() { ++ctor_count; }
29 virtual ~A() { ++dtor_count; }
30 static long ctor_count;
31 static long dtor_count;
32 };
33 long A::ctor_count = 0;
34 long A::dtor_count = 0;
35
36 struct B : A
37 {
38 B() { ++ctor_count; }
39 virtual ~B() { ++dtor_count; }
40 static long ctor_count;
41 static long dtor_count;
42 };
43 long B::ctor_count = 0;
44 long B::dtor_count = 0;
45
46 struct reset_count_struct
47 {
48 ~reset_count_struct()
49 {
50 A::ctor_count = 0;
51 A::dtor_count = 0;
52 B::ctor_count = 0;
53 B::dtor_count = 0;
54 }
55 };
56
57 // C++14 ยง20.8.2.2.3 shared_ptr assignment
58
59 void
60 test01()
61 {
62 reset_count_struct __attribute__((unused)) reset;
63 bool test __attribute__((unused)) = true;
64
65 std::experimental::shared_ptr<A[5]> a;
66 std::experimental::shared_ptr<A[]> a1;
67 std::experimental::shared_ptr<B[5]> a2;
68
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 );
75
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 );
82
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 );
89
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 );
96 }
97
98 void
99 test02()
100 {
101 bool test __attribute__((unused)) = true;
102
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;
106
107 p1 = p;
108 VERIFY( p.get() == p1.get() );
109
110 p2 = p1;
111 VERIFY( p1.get() == p2.get() );
112 }
113
114 int
115 main()
116 {
117 test01();
118 test02();
119 return 0;
120 }