memory_resource_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/container/pmr/memory_resource.hpp>
  11. #include <boost/core/lightweight_test.hpp>
  12. #include <boost/core/no_exceptions_support.hpp>
  13. #include "derived_from_memory_resource.hpp"
  14. #include <cstdlib>
  15. using namespace boost::container;
  16. using namespace boost::container::pmr;
  17. void test_allocate()
  18. {
  19. derived_from_memory_resource d;
  20. memory_resource &mr = d;
  21. d.reset();
  22. BOOST_TEST(d.do_allocate_called == false);
  23. BOOST_TEST(d.do_allocate_bytes == 0);
  24. BOOST_TEST(d.do_allocate_alignment == 0);
  25. mr.allocate(2, 4);
  26. BOOST_TEST(d.do_allocate_called == true);
  27. BOOST_TEST(d.do_allocate_bytes == 2);
  28. BOOST_TEST(d.do_allocate_alignment == 4);
  29. }
  30. void test_deallocate()
  31. {
  32. derived_from_memory_resource d;
  33. memory_resource &mr = d;
  34. d.reset();
  35. BOOST_TEST(d.do_deallocate_called == false);
  36. BOOST_TEST(d.do_deallocate_p == 0);
  37. BOOST_TEST(d.do_allocate_bytes == 0);
  38. BOOST_TEST(d.do_allocate_alignment == 0);
  39. mr.deallocate(&d, 2, 4);
  40. BOOST_TEST(d.do_deallocate_called == true);
  41. BOOST_TEST(d.do_deallocate_p == &d);
  42. BOOST_TEST(d.do_deallocate_bytes == 2);
  43. BOOST_TEST(d.do_deallocate_alignment == 4);
  44. }
  45. void test_destructor()
  46. {
  47. {
  48. derived_from_memory_resource d;
  49. d.reset();
  50. BOOST_TEST(derived_from_memory_resource::destructor_called == false);
  51. }
  52. BOOST_TEST(derived_from_memory_resource::destructor_called == true);
  53. }
  54. void test_is_equal()
  55. {
  56. derived_from_memory_resource d;
  57. memory_resource &mr = d;
  58. d.reset();
  59. BOOST_TEST(d.do_is_equal_called == false);
  60. BOOST_TEST(d.do_is_equal_other == 0);
  61. mr.is_equal(d);
  62. BOOST_TEST(d.do_is_equal_called == true);
  63. BOOST_TEST(d.do_is_equal_other == &d);
  64. }
  65. void test_equality_operator()
  66. {
  67. derived_from_memory_resource d;
  68. memory_resource &mr = d;
  69. d.reset();
  70. BOOST_TEST(d.do_is_equal_called == false);
  71. BOOST_TEST(d.do_is_equal_other == 0);
  72. //equal addresses are shorcircuited
  73. BOOST_TEST((mr == mr) == true);
  74. BOOST_TEST(d.do_is_equal_called == false);
  75. BOOST_TEST(d.do_is_equal_other == 0);
  76. //unequal addresses are dispatched to is_equal which in turn calls do_is_equal
  77. derived_from_memory_resource d2(1);
  78. d.reset();
  79. d2.reset();
  80. memory_resource &mr2 = d2;
  81. BOOST_TEST((mr == mr2) == false);
  82. BOOST_TEST(d.do_is_equal_called == true);
  83. BOOST_TEST(d.do_is_equal_other == &d2);
  84. }
  85. void test_inequality_operator()
  86. {
  87. derived_from_memory_resource d;
  88. memory_resource &mr = d;
  89. d.reset();
  90. BOOST_TEST(d.do_is_equal_called == false);
  91. BOOST_TEST(d.do_is_equal_other == 0);
  92. //equal addresses are shorcircuited
  93. BOOST_TEST((mr != mr) == false);
  94. BOOST_TEST(d.do_is_equal_called == false);
  95. BOOST_TEST(d.do_is_equal_other == 0);
  96. //unequal addresses are dispatched to is_equal which in turn calls do_is_equal
  97. derived_from_memory_resource d2(1);
  98. d.reset();
  99. d2.reset();
  100. memory_resource &mr2 = d2;
  101. BOOST_TEST((mr != mr2) == true);
  102. BOOST_TEST(d.do_is_equal_called == true);
  103. BOOST_TEST(d.do_is_equal_other == &d2);
  104. }
  105. int main()
  106. {
  107. test_destructor();
  108. test_allocate();
  109. test_deallocate();
  110. test_is_equal();
  111. test_equality_operator();
  112. test_inequality_operator();
  113. return ::boost::report_errors();
  114. }