destroy_test.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright David Abrahams 2004. Distributed under the Boost
  2. // Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/python/detail/destroy.hpp>
  5. #include <boost/detail/lightweight_test.hpp>
  6. int count;
  7. int marks[] = {
  8. -1
  9. , -1, -1
  10. , -1, -1, -1, -1
  11. , -1
  12. };
  13. int* kills = marks;
  14. struct foo
  15. {
  16. foo() : n(count++) {}
  17. ~foo()
  18. {
  19. *kills++ = n;
  20. }
  21. int n;
  22. // This used to cause compiler errors with MSVC 9.0.
  23. foo& operator~();
  24. foo& T();
  25. };
  26. void assert_destructions(int n)
  27. {
  28. for (int i = 0; i < n; ++i)
  29. BOOST_TEST(marks[i] == i);
  30. BOOST_TEST(marks[n] == -1);
  31. }
  32. int main()
  33. {
  34. assert_destructions(0);
  35. foo* f1 = new foo;
  36. boost::python::detail::destroy_referent<foo const volatile&>(f1);
  37. assert_destructions(1);
  38. foo* f2 = new foo[2];
  39. typedef foo x[2];
  40. boost::python::detail::destroy_referent<x const&>(f2);
  41. assert_destructions(3);
  42. typedef foo y[2][2];
  43. x* f3 = new y;
  44. boost::python::detail::destroy_referent<y&>(f3);
  45. assert_destructions(7);
  46. return boost::report_errors();
  47. }