allocate_unique_throws_test.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if (instances == 0) {
  48. throw true;
  49. }
  50. ++instances;
  51. }
  52. ~type() {
  53. --instances;
  54. }
  55. private:
  56. type(const type&);
  57. type& operator=(const type&);
  58. };
  59. unsigned type::instances = 0;
  60. int main()
  61. {
  62. try {
  63. boost::allocate_unique<type>(creator<type>());
  64. BOOST_ERROR("allocate_unique did not throw");
  65. } catch (...) {
  66. BOOST_TEST(type::instances == 0);
  67. }
  68. try {
  69. boost::allocate_unique<const type>(creator<>());
  70. BOOST_ERROR("allocate_unique did not throw");
  71. } catch (...) {
  72. BOOST_TEST(type::instances == 0);
  73. }
  74. return boost::report_errors();
  75. }
  76. #else
  77. int main()
  78. {
  79. return 0;
  80. }
  81. #endif