allocate_shared_array_construct_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright 2012-2015 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/config.hpp>
  8. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/smart_ptr/make_shared.hpp>
  11. struct allow { };
  12. template<class T = void>
  13. struct creator {
  14. typedef T value_type;
  15. template<class U>
  16. struct rebind {
  17. typedef creator<U> other;
  18. };
  19. creator() { }
  20. template<class U>
  21. creator(const creator<U>&) { }
  22. T* allocate(std::size_t size) {
  23. return static_cast<T*>(::operator new(sizeof(T) * size));
  24. }
  25. void deallocate(T* ptr, std::size_t) {
  26. ::operator delete(ptr);
  27. }
  28. template<class U>
  29. void construct(U* ptr) {
  30. ::new(static_cast<void*>(ptr)) U(allow());
  31. }
  32. template<class U>
  33. void destroy(U* ptr) {
  34. ptr->~U();
  35. }
  36. };
  37. template<class T, class U>
  38. inline bool
  39. operator==(const creator<T>&, const creator<U>&)
  40. {
  41. return true;
  42. }
  43. template<class T, class U>
  44. inline bool
  45. operator!=(const creator<T>&, const creator<U>&)
  46. {
  47. return false;
  48. }
  49. class type {
  50. public:
  51. static unsigned instances;
  52. explicit type(allow) {
  53. ++instances;
  54. }
  55. ~type() {
  56. --instances;
  57. }
  58. private:
  59. type(const type&);
  60. type& operator=(const type&);
  61. };
  62. unsigned type::instances = 0;
  63. int main()
  64. {
  65. {
  66. boost::shared_ptr<type[]> result =
  67. boost::allocate_shared<type[]>(creator<type>(), 3);
  68. BOOST_TEST(result.get() != 0);
  69. BOOST_TEST(result.use_count() == 1);
  70. BOOST_TEST(type::instances == 3);
  71. result.reset();
  72. BOOST_TEST(type::instances == 0);
  73. }
  74. {
  75. boost::shared_ptr<type[3]> result =
  76. boost::allocate_shared<type[3]>(creator<type>());
  77. BOOST_TEST(result.get() != 0);
  78. BOOST_TEST(result.use_count() == 1);
  79. BOOST_TEST(type::instances == 3);
  80. result.reset();
  81. BOOST_TEST(type::instances == 0);
  82. }
  83. {
  84. boost::shared_ptr<type[][2]> result =
  85. boost::allocate_shared<type[][2]>(creator<>(), 2);
  86. BOOST_TEST(result.get() != 0);
  87. BOOST_TEST(result.use_count() == 1);
  88. BOOST_TEST(type::instances == 4);
  89. result.reset();
  90. BOOST_TEST(type::instances == 0);
  91. }
  92. {
  93. boost::shared_ptr<type[2][2]> result =
  94. boost::allocate_shared<type[2][2]>(creator<>());
  95. BOOST_TEST(result.get() != 0);
  96. BOOST_TEST(result.use_count() == 1);
  97. BOOST_TEST(type::instances == 4);
  98. result.reset();
  99. BOOST_TEST(type::instances == 0);
  100. }
  101. {
  102. boost::shared_ptr<const type[]> result =
  103. boost::allocate_shared<const type[]>(creator<>(), 3);
  104. BOOST_TEST(result.get() != 0);
  105. BOOST_TEST(result.use_count() == 1);
  106. BOOST_TEST(type::instances == 3);
  107. result.reset();
  108. BOOST_TEST(type::instances == 0);
  109. }
  110. {
  111. boost::shared_ptr<const type[3]> result =
  112. boost::allocate_shared<const type[3]>(creator<>());
  113. BOOST_TEST(result.get() != 0);
  114. BOOST_TEST(result.use_count() == 1);
  115. BOOST_TEST(type::instances == 3);
  116. result.reset();
  117. BOOST_TEST(type::instances == 0);
  118. }
  119. {
  120. boost::shared_ptr<const type[][2]> result =
  121. boost::allocate_shared<const type[][2]>(creator<>(), 2);
  122. BOOST_TEST(result.get() != 0);
  123. BOOST_TEST(result.use_count() == 1);
  124. BOOST_TEST(type::instances == 4);
  125. result.reset();
  126. BOOST_TEST(type::instances == 0);
  127. }
  128. {
  129. boost::shared_ptr<const type[2][2]> result =
  130. boost::allocate_shared<const type[2][2]>(creator<>());
  131. BOOST_TEST(result.get() != 0);
  132. BOOST_TEST(result.use_count() == 1);
  133. BOOST_TEST(type::instances == 4);
  134. result.reset();
  135. BOOST_TEST(type::instances == 0);
  136. }
  137. return boost::report_errors();
  138. }
  139. #else
  140. int main()
  141. {
  142. return 0;
  143. }
  144. #endif