allocate_shared_array_throws_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/core/lightweight_test.hpp>
  8. #include <boost/smart_ptr/make_shared.hpp>
  9. template<class T = void>
  10. struct creator {
  11. typedef T value_type;
  12. template<class U>
  13. struct rebind {
  14. typedef creator<U> other;
  15. };
  16. creator() { }
  17. template<class U>
  18. creator(const creator<U>&) { }
  19. T* allocate(std::size_t size) {
  20. return static_cast<T*>(::operator new(sizeof(T) * size));
  21. }
  22. void deallocate(T* ptr, std::size_t) {
  23. ::operator delete(ptr);
  24. }
  25. };
  26. template<class T, class U>
  27. inline bool
  28. operator==(const creator<T>&, const creator<U>&)
  29. {
  30. return true;
  31. }
  32. template<class T, class U>
  33. inline bool
  34. operator!=(const creator<T>&, const creator<U>&)
  35. {
  36. return false;
  37. }
  38. class type {
  39. public:
  40. static unsigned instances;
  41. type() {
  42. if (instances == 5) {
  43. throw true;
  44. }
  45. ++instances;
  46. }
  47. ~type() {
  48. --instances;
  49. }
  50. private:
  51. type(const type&);
  52. type& operator=(const type&);
  53. };
  54. unsigned type::instances = 0;
  55. int main()
  56. {
  57. try {
  58. boost::allocate_shared<type[]>(creator<type>(), 6);
  59. BOOST_ERROR("allocate_shared did not throw");
  60. } catch (...) {
  61. BOOST_TEST(type::instances == 0);
  62. }
  63. try {
  64. boost::allocate_shared<type[][2]>(creator<type>(), 3);
  65. BOOST_ERROR("allocate_shared did not throw");
  66. } catch (...) {
  67. BOOST_TEST(type::instances == 0);
  68. }
  69. try {
  70. boost::allocate_shared<type[6]>(creator<>());
  71. BOOST_ERROR("allocate_shared did not throw");
  72. } catch (...) {
  73. BOOST_TEST(type::instances == 0);
  74. }
  75. try {
  76. boost::allocate_shared<type[3][2]>(creator<>());
  77. BOOST_ERROR("allocate_shared did not throw");
  78. } catch (...) {
  79. BOOST_TEST(type::instances == 0);
  80. }
  81. try {
  82. boost::allocate_shared_noinit<type[]>(creator<>(), 6);
  83. BOOST_ERROR("allocate_shared_noinit did not throw");
  84. } catch (...) {
  85. BOOST_TEST(type::instances == 0);
  86. }
  87. try {
  88. boost::allocate_shared_noinit<type[][2]>(creator<>(), 3);
  89. BOOST_ERROR("allocate_shared_noinit did not throw");
  90. } catch (...) {
  91. BOOST_TEST(type::instances == 0);
  92. }
  93. try {
  94. boost::allocate_shared_noinit<type[6]>(creator<>());
  95. BOOST_ERROR("allocate_shared_noinit did not throw");
  96. } catch (...) {
  97. BOOST_TEST(type::instances == 0);
  98. }
  99. try {
  100. boost::allocate_shared_noinit<type[3][2]>(creator<>());
  101. BOOST_ERROR("allocate_shared_noinit did not throw");
  102. } catch (...) {
  103. BOOST_TEST(type::instances == 0);
  104. }
  105. return boost::report_errors();
  106. }