allocate_unique_array_throws_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 >= 48000) && \
  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 == 5) {
  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>(), 6);
  64. BOOST_ERROR("allocate_unique did not throw");
  65. } catch (...) {
  66. BOOST_TEST(type::instances == 0);
  67. }
  68. try {
  69. boost::allocate_unique<type[][2]>(creator<type>(), 3);
  70. BOOST_ERROR("allocate_unique did not throw");
  71. } catch (...) {
  72. BOOST_TEST(type::instances == 0);
  73. }
  74. try {
  75. boost::allocate_unique<type[6]>(creator<>());
  76. BOOST_ERROR("allocate_unique did not throw");
  77. } catch (...) {
  78. BOOST_TEST(type::instances == 0);
  79. }
  80. try {
  81. boost::allocate_unique<type[3][2]>(creator<>());
  82. BOOST_ERROR("allocate_unique did not throw");
  83. } catch (...) {
  84. BOOST_TEST(type::instances == 0);
  85. }
  86. try {
  87. boost::allocate_unique_noinit<type[]>(creator<>(), 6);
  88. BOOST_ERROR("allocate_unique_noinit did not throw");
  89. } catch (...) {
  90. BOOST_TEST(type::instances == 0);
  91. }
  92. try {
  93. boost::allocate_unique_noinit<type[][2]>(creator<>(), 3);
  94. BOOST_ERROR("allocate_unique_noinit did not throw");
  95. } catch (...) {
  96. BOOST_TEST(type::instances == 0);
  97. }
  98. try {
  99. boost::allocate_unique_noinit<type[6]>(creator<>());
  100. BOOST_ERROR("allocate_unique_noinit did not throw");
  101. } catch (...) {
  102. BOOST_TEST(type::instances == 0);
  103. }
  104. try {
  105. boost::allocate_unique_noinit<type[3][2]>(creator<>());
  106. BOOST_ERROR("allocate_unique_noinit did not throw");
  107. } catch (...) {
  108. BOOST_TEST(type::instances == 0);
  109. }
  110. return boost::report_errors();
  111. }
  112. #else
  113. int main()
  114. {
  115. return 0;
  116. }
  117. #endif