Debian Codegraph @

libstdc++-v3/testsuite/20_util/shared_ptr/cons/alias.cc

raw
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
3
4 // Copyright (C) 2007-2025 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
22
23 #include <memory>
24 #include <testsuite_hooks.h>
25
26 struct A
27 {
28 A() : i() { }
29 virtual ~A() { }
30 int i;
31 };
32
33 struct B : A
34 {
35 B() : A(), a() { }
36 virtual ~B() { }
37 A a;
38 };
39
40 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
41
42 // Aliasing constructors
43
44 void
45 test01()
46 {
47 bool test = true;
48
49 std::shared_ptr<A> a;
50 std::shared_ptr<bool> b1(a, &test);
51 VERIFY( b1.use_count() == 0 );
52 VERIFY( a.get() == 0 );
53 VERIFY( b1.get() == &test );
54
55 std::shared_ptr<bool> b2(b1);
56 VERIFY( b2.use_count() == 0 );
57 VERIFY( b1.get() == b2.get() );
58 }
59
60 void
61 test02()
62 {
63 std::shared_ptr<A> a(new A);
64 std::shared_ptr<int> i1(a, &a->i);
65 VERIFY( i1.use_count() == 2 );
66
67 std::shared_ptr<int> i2(i1);
68 VERIFY( i2.use_count() == 3 );
69 VERIFY( i2.get() == &a->i );
70 }
71
72 void
73 test03()
74 {
75 std::shared_ptr<B> b(new B);
76 std::shared_ptr<A> a1(b, b.get());
77 std::shared_ptr<A> a2(b, &b->a);
78 VERIFY( a2.use_count() == 3 );
79 VERIFY( a1 == b );
80 VERIFY( a2 != b );
81 VERIFY( a1.get() != a2.get() );
82
83 std::shared_ptr<A> a3(a1);
84 VERIFY( a3 == b );
85
86 a3 = a2;
87 VERIFY( a3.get() == &b->a );
88 }
89
90 int
91 main()
92 {
93 test01();
94 test02();
95 test03();
96 }