1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Howard Hinnant 2009 4 // (C) Copyright Ion Gaztanaga 2014-2014. 5 // 6 // Distributed under the Boost Software License, Version 1.0. 7 // (See accompanying file LICENSE_1_0.txt or copy at 8 // http://www.boost.org/LICENSE_1_0.txt) 9 // 10 // See http://www.boost.org/libs/move for documentation. 11 // 12 ////////////////////////////////////////////////////////////////////////////// 13 #include <boost/move/default_delete.hpp> 14 #include <boost/core/lightweight_test.hpp> 15 16 ////////////////////////////////////////////// 17 // 18 // The initial implementation of these tests 19 // was written by Howard Hinnant. 20 // 21 // These test were later refactored grouping 22 // and porting them to Boost.Move. 23 // 24 // Many thanks to Howard for releasing his C++03 25 // unique_ptr implementation with such detailed 26 // test cases. 27 // 28 ////////////////////////////////////////////// 29 30 //////////////////////////////// 31 // unique_ptr_dltr_dflt_convert_ctor 32 //////////////////////////////// 33 34 namespace bml = ::boost::movelib; 35 36 struct A 37 { 38 static int count; AA39 A() {++count;} AA40 A(const A&) {++count;} ~AA41 virtual ~A() {--count;} 42 }; 43 44 int A::count = 0; 45 46 struct B 47 : public A 48 { 49 static int count; BB50 B() : A() {++count;} BB51 B(const B&) : A() {++count;} ~BB52 virtual ~B() {--count;} 53 }; 54 55 int B::count = 0; 56 reset_counters()57void reset_counters() 58 { A::count = B::count = 0; } 59 60 namespace unique_ptr_dltr_dflt_convert_ctor{ 61 test()62void test() 63 { 64 //Single element deleter 65 { 66 reset_counters(); 67 bml::default_delete<B> d2; 68 bml::default_delete<A> d1 = d2; 69 A* p = new B; 70 BOOST_TEST(A::count == 1); 71 BOOST_TEST(B::count == 1); 72 d1(p); 73 BOOST_TEST(A::count == 0); 74 BOOST_TEST(B::count == 0); 75 } 76 //Array element deleter 77 { 78 reset_counters(); 79 bml::default_delete<A[]> d2; 80 bml::default_delete<const A[]> d1 = d2; 81 const A* p = new const A[2]; 82 BOOST_TEST(A::count == 2); 83 d1(p); 84 BOOST_TEST(A::count == 0); 85 } 86 //Bounded array element deleter 87 { 88 reset_counters(); 89 bml::default_delete<A[2]> d2; 90 bml::default_delete<const A[2]> d1 = d2; 91 const A* p = new const A[2]; 92 BOOST_TEST(A::count == 2); 93 d1(p); 94 bml::default_delete<const A[]> d0 = d1; 95 d0(0); 96 BOOST_TEST(A::count == 0); 97 } 98 } 99 100 } //namespace unique_ptr_dltr_dflt_convert_ctor{ 101 102 //////////////////////////////// 103 // unique_ptr_dltr_dflt_convert_assign 104 //////////////////////////////// 105 106 namespace unique_ptr_dltr_dflt_convert_assign{ 107 test()108void test() 109 { 110 //Single element deleter 111 { 112 reset_counters(); 113 bml::default_delete<B> d2; 114 bml::default_delete<A> d1; 115 d1 = d2; 116 A* p = new B; 117 BOOST_TEST(A::count == 1); 118 BOOST_TEST(B::count == 1); 119 d1(p); 120 BOOST_TEST(A::count == 0); 121 BOOST_TEST(B::count == 0); 122 } 123 //Array element deleter 124 { 125 reset_counters(); 126 bml::default_delete<A[]> d2; 127 bml::default_delete<const A[]> d1; 128 d1 = d2; 129 const A* p = new const A[2]; 130 BOOST_TEST(A::count == 2); 131 d1(p); 132 BOOST_TEST(A::count == 0); 133 } 134 //Bounded array element deleter 135 { 136 reset_counters(); 137 bml::default_delete<A[2]> d2; 138 bml::default_delete<const A[2]> d1; 139 d1 = d2; 140 const A* p = new const A[2]; 141 BOOST_TEST(A::count == 2); 142 d1(p); 143 bml::default_delete<const A[]> d0; 144 d0 = d1; 145 d0(0); 146 BOOST_TEST(A::count == 0); 147 } 148 } 149 150 } //namespace unique_ptr_dltr_dflt_convert_assign{ 151 152 153 //////////////////////////////// 154 // unique_ptr_dltr_dflt_default 155 //////////////////////////////// 156 157 namespace unique_ptr_dltr_dflt_default{ 158 test()159void test() 160 { 161 { 162 //Single element deleter 163 reset_counters(); 164 bml::default_delete<A> d; 165 A* p = new A; 166 BOOST_TEST(A::count == 1); 167 d(p); 168 BOOST_TEST(A::count == 0); 169 } 170 { 171 //Array element deleter 172 reset_counters(); 173 bml::default_delete<A[]> d; 174 A* p = new A[2]; 175 BOOST_TEST(A::count == 2); 176 d(p); 177 BOOST_TEST(A::count == 0); 178 } 179 180 { 181 //Bounded Array element deleter 182 reset_counters(); 183 bml::default_delete<A[10]> d; 184 A* p = new A[10]; 185 BOOST_TEST(A::count == 10); 186 d(p); 187 BOOST_TEST(A::count == 0); 188 } 189 } 190 191 } //namespace unique_ptr_dltr_dflt_default{ 192 193 //////////////////////////////// 194 // main 195 //////////////////////////////// main()196int main() 197 { 198 unique_ptr_dltr_dflt_convert_ctor::test(); 199 unique_ptr_dltr_dflt_convert_assign::test(); 200 unique_ptr_dltr_dflt_default::test(); 201 202 //Test results 203 return boost::report_errors(); 204 } 205