Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / alias.cc
bloba707740c9ec98105e4ce95e18a2939a23c5e1a03
1 // { dg-options "-std=gnu++0x" }
3 // Copyright (C) 2007 Free Software Foundation
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 2, or (at your option)
9 // any later version.
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 COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
23 #include <memory>
24 #include <testsuite_hooks.h>
26 struct A
28 A() : i() { }
29 virtual ~A() { }
30 int i;
33 struct B : A
35 B() : A(), a() { }
36 virtual ~B() { }
37 A a;
40 void deletefunc(A* p) { delete p; }
42 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
44 // Aliasing constructors
46 int test01()
48 bool test __attribute__((unused)) = true;
50 std::shared_ptr<A> a;
51 std::shared_ptr<bool> b1(a, &test);
52 VERIFY( b1.use_count() == 0 );
53 VERIFY( a.get() == 0 );
54 VERIFY( b1.get() == &test );
56 std::shared_ptr<bool> b2(b1);
57 VERIFY( b2.use_count() == 0 );
58 VERIFY( b1.get() == b2.get() );
60 return 0;
63 int
64 test02()
66 bool test __attribute__((unused)) = true;
68 std::shared_ptr<A> a(new A);
69 std::shared_ptr<int> i1(a, &a->i);
70 VERIFY( i1.use_count() == 2 );
72 std::shared_ptr<int> i2(i1);
73 VERIFY( i2.use_count() == 3 );
74 VERIFY( i2.get() == &a->i );
76 return 0;
79 int
80 test03()
82 bool test __attribute__((unused)) = true;
84 std::shared_ptr<B> b(new B);
85 std::shared_ptr<A> a1(b, b.get());
86 std::shared_ptr<A> a2(b, &b->a);
87 VERIFY( a2.use_count() == 3 );
88 VERIFY( a1 == b );
89 VERIFY( a2 != b );
90 VERIFY( a1.get() != a2.get() );
92 std::shared_ptr<A> a3(a1);
93 VERIFY( a3 == b );
95 a3 = a2;
96 VERIFY( a3.get() == &b->a );
98 return 0;
102 main()
104 test01();
105 test02();
106 test03();
107 return 0;