1 /* 2 * Copyright (c) 2012-2014 Glen Joseph Fernandes 3 * glenfe at live dot com 4 * 5 * Distributed under the Boost Software License, 6 * Version 1.0. (See accompanying file LICENSE_1_0.txt 7 * or copy at http://boost.org/LICENSE_1_0.txt) 8 */ 9 #include <boost/detail/lightweight_test.hpp> 10 #include <boost/smart_ptr/allocate_shared_array.hpp> 11 #include <boost/smart_ptr/weak_ptr.hpp> 12 #include <boost/type_traits/alignment_of.hpp> 13 14 class type { 15 public: 16 static unsigned int instances; 17 type()18 explicit type() { 19 instances++; 20 } 21 ~type()22 ~type() { 23 instances--; 24 } 25 26 private: 27 type(const type&); 28 type& operator=(const type&); 29 }; 30 31 unsigned int type::instances = 0; 32 main()33int main() { 34 { 35 boost::shared_ptr<int[]> a1 = boost::allocate_shared<int[]>(std::allocator<int>(), 3); 36 int* a2 = a1.get(); 37 BOOST_TEST(a1.use_count() == 1); 38 BOOST_TEST(a2 != 0); 39 BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0); 40 BOOST_TEST(a1[0] == 0); 41 BOOST_TEST(a1[1] == 0); 42 BOOST_TEST(a1[2] == 0); 43 } 44 45 { 46 boost::shared_ptr<int[3]> a1 = boost::allocate_shared<int[3]>(std::allocator<int>()); 47 int* a2 = a1.get(); 48 BOOST_TEST(a1.use_count() == 1); 49 BOOST_TEST(a2 != 0); 50 BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0); 51 BOOST_TEST(a1[0] == 0); 52 BOOST_TEST(a1[1] == 0); 53 BOOST_TEST(a1[2] == 0); 54 } 55 56 { 57 boost::shared_ptr<int[][2]> a1 = boost::allocate_shared<int[][2]>(std::allocator<int>(), 2); 58 BOOST_TEST(a1.get() != 0); 59 BOOST_TEST(a1.use_count() == 1); 60 BOOST_TEST(a1[0][0] == 0); 61 BOOST_TEST(a1[0][1] == 0); 62 BOOST_TEST(a1[1][0] == 0); 63 BOOST_TEST(a1[1][1] == 0); 64 } 65 66 { 67 boost::shared_ptr<int[2][2]> a1 = boost::allocate_shared<int[2][2]>(std::allocator<int>()); 68 BOOST_TEST(a1.get() != 0); 69 BOOST_TEST(a1.use_count() == 1); 70 BOOST_TEST(a1[0][0] == 0); 71 BOOST_TEST(a1[0][1] == 0); 72 BOOST_TEST(a1[1][0] == 0); 73 BOOST_TEST(a1[1][1] == 0); 74 } 75 76 { 77 boost::shared_ptr<const int[]> a1 = boost::allocate_shared<const int[]>(std::allocator<int>(), 3); 78 const int* a2 = a1.get(); 79 BOOST_TEST(a1.use_count() == 1); 80 BOOST_TEST(a2 != 0); 81 BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0); 82 BOOST_TEST(a1[0] == 0); 83 BOOST_TEST(a1[1] == 0); 84 BOOST_TEST(a1[2] == 0); 85 } 86 87 { 88 boost::shared_ptr<const int[3]> a1 = boost::allocate_shared<const int[3]>(std::allocator<int>()); 89 const int* a2 = a1.get(); 90 BOOST_TEST(a1.use_count() == 1); 91 BOOST_TEST(a2 != 0); 92 BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0); 93 BOOST_TEST(a1[0] == 0); 94 BOOST_TEST(a1[1] == 0); 95 BOOST_TEST(a1[2] == 0); 96 } 97 98 { 99 boost::shared_ptr<const int[][2]> a1 = boost::allocate_shared<const int[][2]>(std::allocator<int>(), 2); 100 BOOST_TEST(a1.get() != 0); 101 BOOST_TEST(a1.use_count() == 1); 102 BOOST_TEST(a1[0][0] == 0); 103 BOOST_TEST(a1[0][1] == 0); 104 BOOST_TEST(a1[1][0] == 0); 105 BOOST_TEST(a1[1][1] == 0); 106 } 107 108 { 109 boost::shared_ptr<const int[2][2]> a1 = boost::allocate_shared<const int[2][2]>(std::allocator<int>()); 110 BOOST_TEST(a1.get() != 0); 111 BOOST_TEST(a1.use_count() == 1); 112 BOOST_TEST(a1[0][0] == 0); 113 BOOST_TEST(a1[0][1] == 0); 114 BOOST_TEST(a1[1][0] == 0); 115 BOOST_TEST(a1[1][1] == 0); 116 } 117 118 BOOST_TEST(type::instances == 0); 119 { 120 boost::shared_ptr<type[]> a1 = boost::allocate_shared<type[]>(std::allocator<type>(), 3); 121 type* a2 = a1.get(); 122 BOOST_TEST(a1.use_count() == 1); 123 BOOST_TEST(a2 != 0); 124 BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0); 125 BOOST_TEST(type::instances == 3); 126 boost::weak_ptr<type[]> w1 = a1; 127 a1.reset(); 128 BOOST_TEST(type::instances == 0); 129 } 130 131 BOOST_TEST(type::instances == 0); 132 { 133 boost::shared_ptr<type[3]> a1 = boost::allocate_shared<type[3]>(std::allocator<type>()); 134 type* a2 = a1.get(); 135 BOOST_TEST(a1.use_count() == 1); 136 BOOST_TEST(a2 != 0); 137 BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0); 138 BOOST_TEST(type::instances == 3); 139 boost::weak_ptr<type[3]> w1 = a1; 140 a1.reset(); 141 BOOST_TEST(type::instances == 0); 142 } 143 144 BOOST_TEST(type::instances == 0); 145 { 146 boost::shared_ptr<type[][2]> a1 = boost::allocate_shared<type[][2]>(std::allocator<type>(), 2); 147 BOOST_TEST(a1.get() != 0); 148 BOOST_TEST(a1.use_count() == 1); 149 BOOST_TEST(type::instances == 4); 150 a1.reset(); 151 BOOST_TEST(type::instances == 0); 152 } 153 154 BOOST_TEST(type::instances == 0); 155 { 156 boost::shared_ptr<type[2][2]> a1 = boost::allocate_shared<type[2][2]>(std::allocator<type>()); 157 BOOST_TEST(a1.get() != 0); 158 BOOST_TEST(a1.use_count() == 1); 159 BOOST_TEST(type::instances == 4); 160 a1.reset(); 161 BOOST_TEST(type::instances == 0); 162 } 163 164 BOOST_TEST(type::instances == 0); 165 { 166 boost::shared_ptr<const type[]> a1 = boost::allocate_shared<const type[]>(std::allocator<type>(), 3); 167 const type* a2 = a1.get(); 168 BOOST_TEST(a1.use_count() == 1); 169 BOOST_TEST(a2 != 0); 170 BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0); 171 BOOST_TEST(type::instances == 3); 172 a1.reset(); 173 BOOST_TEST(type::instances == 0); 174 } 175 176 BOOST_TEST(type::instances == 0); 177 { 178 boost::shared_ptr<const type[3]> a1 = boost::allocate_shared<const type[3]>(std::allocator<type>()); 179 const type* a2 = a1.get(); 180 BOOST_TEST(a1.use_count() == 1); 181 BOOST_TEST(a2 != 0); 182 BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0); 183 BOOST_TEST(type::instances == 3); 184 a1.reset(); 185 BOOST_TEST(type::instances == 0); 186 } 187 188 BOOST_TEST(type::instances == 0); 189 { 190 boost::shared_ptr<const type[][2]> a1 = boost::allocate_shared<const type[][2]>(std::allocator<type>(), 2); 191 BOOST_TEST(a1.get() != 0); 192 BOOST_TEST(a1.use_count() == 1); 193 BOOST_TEST(type::instances == 4); 194 a1.reset(); 195 BOOST_TEST(type::instances == 0); 196 } 197 198 BOOST_TEST(type::instances == 0); 199 { 200 boost::shared_ptr<const type[2][2]> a1 = boost::allocate_shared<const type[2][2]>(std::allocator<type>()); 201 BOOST_TEST(a1.get() != 0); 202 BOOST_TEST(a1.use_count() == 1); 203 BOOST_TEST(type::instances == 4); 204 a1.reset(); 205 BOOST_TEST(type::instances == 0); 206 } 207 208 return boost::report_errors(); 209 } 210