allocate_local_shared_array_throws_test.cpp 3.0 KB

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