alloc_construct_throws_test.cpp 873 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/core/alloc_construct.hpp>
  8. #include <boost/core/default_allocator.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. class type {
  11. public:
  12. type() {
  13. if (count == 4) {
  14. throw true;
  15. }
  16. ++count;
  17. }
  18. ~type() {
  19. --count;
  20. }
  21. static int count;
  22. private:
  23. type(const type&);
  24. type& operator=(const type&);
  25. };
  26. int type::count = 0;
  27. int main()
  28. {
  29. boost::default_allocator<type> a;
  30. type* p = a.allocate(5);
  31. try {
  32. boost::alloc_construct_n(a, p, 5);
  33. BOOST_ERROR("construct_n did not throw");
  34. } catch (...) {
  35. BOOST_TEST_EQ(type::count, 0);
  36. }
  37. a.deallocate(p, 5);
  38. return boost::report_errors();
  39. }