allocate_unique_test.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2019 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_LIBSTDCXX_VERSION) || \
  9. BOOST_LIBSTDCXX_VERSION >= 46000) && \
  10. !defined(BOOST_NO_CXX11_SMART_PTR)
  11. #include <boost/smart_ptr/allocate_unique.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. template<class T = void>
  14. struct creator {
  15. typedef T value_type;
  16. typedef T* pointer;
  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. };
  31. template<class T, class U>
  32. inline bool
  33. operator==(const creator<T>&, const creator<U>&)
  34. {
  35. return true;
  36. }
  37. template<class T, class U>
  38. inline bool
  39. operator!=(const creator<T>&, const creator<U>&)
  40. {
  41. return false;
  42. }
  43. class type {
  44. public:
  45. static unsigned instances;
  46. type() {
  47. ++instances;
  48. }
  49. ~type() {
  50. --instances;
  51. }
  52. private:
  53. type(const type&);
  54. type& operator=(const type&);
  55. };
  56. unsigned type::instances = 0;
  57. int main()
  58. {
  59. {
  60. std::unique_ptr<int,
  61. boost::alloc_deleter<int, creator<int> > > result =
  62. boost::allocate_unique<int>(creator<int>());
  63. BOOST_TEST(result.get() != 0);
  64. BOOST_TEST(*result == 0);
  65. }
  66. {
  67. std::unique_ptr<const int,
  68. boost::alloc_deleter<const int, creator<> > > result =
  69. boost::allocate_unique<const int>(creator<>());
  70. BOOST_TEST(result.get() != 0);
  71. BOOST_TEST(*result == 0);
  72. }
  73. BOOST_TEST(type::instances == 0);
  74. {
  75. std::unique_ptr<type,
  76. boost::alloc_deleter<type, creator<type> > > result =
  77. boost::allocate_unique<type>(creator<type>());
  78. BOOST_TEST(result.get() != 0);
  79. BOOST_TEST(type::instances == 1);
  80. result.reset();
  81. BOOST_TEST(type::instances == 0);
  82. }
  83. BOOST_TEST(type::instances == 0);
  84. {
  85. std::unique_ptr<const type,
  86. boost::alloc_deleter<const type, creator<> > > result =
  87. boost::allocate_unique<const type>(creator<>());
  88. BOOST_TEST(result.get() != 0);
  89. BOOST_TEST(type::instances == 1);
  90. result.reset();
  91. BOOST_TEST(type::instances == 0);
  92. }
  93. return boost::report_errors();
  94. }
  95. #else
  96. int main()
  97. {
  98. return 0;
  99. }
  100. #endif