make_shared_array_throws_test.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/detail/lightweight_test.hpp>
  8. #include <boost/smart_ptr/make_shared.hpp>
  9. class type {
  10. public:
  11. static unsigned instances;
  12. type() {
  13. if (instances == 5) {
  14. throw true;
  15. }
  16. ++instances;
  17. }
  18. ~type() {
  19. --instances;
  20. }
  21. private:
  22. type(const type&);
  23. type& operator=(const type&);
  24. };
  25. unsigned type::instances = 0;
  26. int main()
  27. {
  28. try {
  29. boost::make_shared<type[]>(6);
  30. BOOST_ERROR("make_shared did not throw");
  31. } catch (...) {
  32. BOOST_TEST(type::instances == 0);
  33. }
  34. try {
  35. boost::make_shared<type[][2]>(3);
  36. BOOST_ERROR("make_shared did not throw");
  37. } catch (...) {
  38. BOOST_TEST(type::instances == 0);
  39. }
  40. try {
  41. boost::make_shared<type[6]>();
  42. BOOST_ERROR("make_shared did not throw");
  43. } catch (...) {
  44. BOOST_TEST(type::instances == 0);
  45. }
  46. try {
  47. boost::make_shared<type[3][2]>();
  48. BOOST_ERROR("make_shared did not throw");
  49. } catch (...) {
  50. BOOST_TEST(type::instances == 0);
  51. }
  52. try {
  53. boost::make_shared_noinit<type[]>(6);
  54. BOOST_ERROR("make_shared_noinit did not throw");
  55. } catch (...) {
  56. BOOST_TEST(type::instances == 0);
  57. }
  58. try {
  59. boost::make_shared_noinit<type[][2]>(3);
  60. BOOST_ERROR("make_shared_noinit did not throw");
  61. } catch (...) {
  62. BOOST_TEST(type::instances == 0);
  63. }
  64. try {
  65. boost::make_shared_noinit<type[6]>();
  66. BOOST_ERROR("make_shared_noinit did not throw");
  67. } catch (...) {
  68. BOOST_TEST(type::instances == 0);
  69. }
  70. try {
  71. boost::make_shared_noinit<type[3][2]>();
  72. BOOST_ERROR("make_shared_noinit did not throw");
  73. } catch (...) {
  74. BOOST_TEST(type::instances == 0);
  75. }
  76. return boost::report_errors();
  77. }