Update concepts branch to revision 131834
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / creation / alloc.cc
blob90fce24c73b4cdf910e99fd9314218efb286f486
1 // { dg-options "-std=gnu++0x" }
3 // Copyright (C) 2007, 2008 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>
25 #include <testsuite_allocator.h>
27 using __gnu_test::tracker_allocator_counter;
28 using __gnu_test::tracker_allocator;
30 struct A
32 A(int i, double d, char c = '\0') : i(i), d(d), c(c) { ++ctor_count; }
33 explicit A(int i) : i(i), d(), c() { ++ctor_count; }
34 A() : i(), d(), c() { ++ctor_count; }
35 ~A() { ++dtor_count; }
36 int i;
37 double d;
38 char c;
39 static int ctor_count;
40 static int dtor_count;
42 int A::ctor_count = 0;
43 int A::dtor_count = 0;
45 struct reset_count_struct
47 ~reset_count_struct()
49 A::ctor_count = 0;
50 A::dtor_count = 0;
51 tracker_allocator_counter::reset();
55 // 20.6.6.2.6 shared_ptr creation [util.smartptr.shared.create]
57 void
58 test01()
60 bool test __attribute__((unused)) = true;
61 reset_count_struct __attribute__((unused)) reset;
64 std::shared_ptr<A> p1 = std::allocate_shared<A>(tracker_allocator<A>());
65 VERIFY( p1.get() != 0 );
66 VERIFY( p1.use_count() == 1 );
67 VERIFY( A::ctor_count == 1 );
68 VERIFY( tracker_allocator_counter::get_allocation_count() > 0 );
70 VERIFY( A::ctor_count == A::dtor_count );
71 VERIFY( tracker_allocator_counter::get_allocation_count()
72 == tracker_allocator_counter::get_deallocation_count() );
75 void
76 test02()
78 bool test __attribute__((unused)) = true;
79 reset_count_struct __attribute__((unused)) reset;
81 std::shared_ptr<A> p1;
83 p1 = std::allocate_shared<A>(tracker_allocator<A>(), 1);
84 VERIFY( A::ctor_count == 1 );
85 VERIFY( tracker_allocator_counter::get_allocation_count() > 0 );
87 p1 = std::allocate_shared<A>(tracker_allocator<A>(), 1, 2.0);
88 VERIFY( A::ctor_count == 2 );
89 VERIFY( A::dtor_count == 1 );
90 VERIFY( tracker_allocator_counter::get_deallocation_count() > 0 );
92 p1 = std::allocate_shared<A>(tracker_allocator<A>(), 1, 2.0, '3');
93 VERIFY( A::ctor_count == 3 );
94 VERIFY( A::dtor_count == 2 );
95 VERIFY( p1->i == 1 );
96 VERIFY( p1->d == 2.0 );
97 VERIFY( p1->c == '3' );
99 p1 = std::shared_ptr<A>();
100 VERIFY( A::ctor_count == A::dtor_count );
101 VERIFY( tracker_allocator_counter::get_allocation_count()
102 == tracker_allocator_counter::get_deallocation_count() );
106 main()
108 test01();
109 test02();
110 return 0;