intrusive_ref_counter_test.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright Andrey Semashev 2013.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file intrusive_ref_counter_test.cpp
  9. * \author Andrey Semashev
  10. * \date 31.08.2013
  11. *
  12. * This file contains tests for the \c intrusive_ref_counter base class.
  13. */
  14. #include <boost/config.hpp>
  15. #if defined(BOOST_MSVC)
  16. #pragma warning(disable: 4786) // identifier truncated in debug info
  17. #pragma warning(disable: 4710) // function not inlined
  18. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  19. #pragma warning(disable: 4514) // unreferenced inline removed
  20. #pragma warning(disable: 4355) // 'this' : used in base member initializer list
  21. #pragma warning(disable: 4511) // copy constructor could not be generated
  22. #pragma warning(disable: 4512) // assignment operator could not be generated
  23. #if (BOOST_MSVC >= 1310)
  24. #pragma warning(disable: 4675) // resolved overload found with Koenig lookup
  25. #endif
  26. #endif
  27. #include <cstddef>
  28. #include <boost/smart_ptr/intrusive_ref_counter.hpp>
  29. #include <boost/intrusive_ptr.hpp>
  30. #include <boost/detail/lightweight_test.hpp>
  31. namespace N1 {
  32. class my_class :
  33. public boost::intrusive_ref_counter< my_class >
  34. {
  35. public:
  36. static unsigned int destructor_count;
  37. ~my_class()
  38. {
  39. ++destructor_count;
  40. }
  41. };
  42. unsigned int my_class::destructor_count = 0;
  43. } // namespace N1
  44. namespace N2 {
  45. class my_class :
  46. public boost::intrusive_ref_counter< my_class, boost::thread_unsafe_counter >
  47. {
  48. public:
  49. static unsigned int destructor_count;
  50. ~my_class()
  51. {
  52. ++destructor_count;
  53. }
  54. };
  55. unsigned int my_class::destructor_count = 0;
  56. } // namespace N2
  57. namespace N3 {
  58. struct root :
  59. public boost::intrusive_ref_counter< root >
  60. {
  61. virtual ~root() {}
  62. };
  63. } // namespace N3
  64. namespace N4 {
  65. struct X :
  66. public virtual N3::root
  67. {
  68. };
  69. } // namespace N4
  70. namespace N5 {
  71. struct Y :
  72. public virtual N3::root
  73. {
  74. };
  75. } // namespace N5
  76. namespace N6 {
  77. struct Z :
  78. public N4::X,
  79. public N5::Y
  80. {
  81. static unsigned int destructor_count;
  82. ~Z()
  83. {
  84. ++destructor_count;
  85. }
  86. };
  87. unsigned int Z::destructor_count = 0;
  88. } // namespace N6
  89. int main()
  90. {
  91. // The test check that ADL works
  92. {
  93. boost::intrusive_ptr< N1::my_class > p = new N1::my_class();
  94. p = NULL;
  95. BOOST_TEST(N1::my_class::destructor_count == 1);
  96. }
  97. {
  98. boost::intrusive_ptr< N2::my_class > p = new N2::my_class();
  99. p = NULL;
  100. BOOST_TEST(N2::my_class::destructor_count == 1);
  101. }
  102. {
  103. N1::my_class* p = new N1::my_class();
  104. intrusive_ptr_add_ref(p);
  105. intrusive_ptr_release(p);
  106. BOOST_TEST(N1::my_class::destructor_count == 2);
  107. }
  108. // The test checks that destroying through the base class works
  109. {
  110. boost::intrusive_ptr< N6::Z > p1 = new N6::Z();
  111. BOOST_TEST(p1->use_count() == 1);
  112. BOOST_TEST(N6::Z::destructor_count == 0);
  113. boost::intrusive_ptr< N3::root > p2 = p1;
  114. BOOST_TEST(p1->use_count() == 2);
  115. BOOST_TEST(N6::Z::destructor_count == 0);
  116. p1 = NULL;
  117. BOOST_TEST(N6::Z::destructor_count == 0);
  118. p2 = NULL;
  119. BOOST_TEST(N6::Z::destructor_count == 1);
  120. }
  121. return boost::report_errors();
  122. }