allocate_local_shared_array_construct_test.cpp 4.2 KB

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