make_unique_array_throws_test.cpp 1.5 KB

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