Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / alias.cc
1 // { dg-options "-std=gnu++0x" }
2
3 // Copyright (C) 2007-2013 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 // 20.6.6.2 Template class shared_ptr [util.smartptr.shared]
21
22 #include <memory>
23 #include <testsuite_hooks.h>
24
25 struct A
26 {
27   A() : i() { }
28   virtual ~A() { }
29   int i;
30 };
31
32 struct B : A
33 {
34   B() : A(), a() { }
35   virtual ~B() { }
36   A a;
37 };
38
39 void deletefunc(A* p) { delete p; }
40
41 // 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
42
43 // Aliasing constructors
44
45 int test01()
46 {
47   bool test __attribute__((unused)) = 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   return 0;
60 }
61
62 int
63 test02()
64 {
65   bool test __attribute__((unused)) = true;
66
67   std::shared_ptr<A> a(new A);
68   std::shared_ptr<int> i1(a, &a->i);
69   VERIFY( i1.use_count() == 2 );
70
71   std::shared_ptr<int> i2(i1);
72   VERIFY( i2.use_count() == 3 );
73   VERIFY( i2.get() == &a->i );
74
75   return 0;
76 }
77
78 int
79 test03()
80 {
81   bool test __attribute__((unused)) = true;
82
83   std::shared_ptr<B> b(new B);
84   std::shared_ptr<A> a1(b, b.get());
85   std::shared_ptr<A> a2(b, &b->a);
86   VERIFY( a2.use_count() == 3 );
87   VERIFY( a1 == b );
88   VERIFY( a2 != b );
89   VERIFY( a1.get() != a2.get() );
90
91   std::shared_ptr<A> a3(a1);
92   VERIFY( a3 == b );
93
94   a3 = a2;
95   VERIFY( a3.get() == &b->a );
96
97   return 0;
98 }
99
100 int
101 main()
102 {
103   test01();
104   test02();
105   test03();
106   return 0;
107 }