alloc_construct_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. explicit type(int x = 0, int y = 0)
  13. : value_(x + y) {
  14. ++count;
  15. }
  16. type(const type& other)
  17. : value_(other.value_) {
  18. ++count;
  19. }
  20. ~type() {
  21. --count;
  22. }
  23. int value() const {
  24. return value_;
  25. }
  26. static int count;
  27. private:
  28. int value_;
  29. };
  30. int type::count = 0;
  31. void test_construct()
  32. {
  33. boost::default_allocator<type> a;
  34. type* p = a.allocate(1);
  35. boost::alloc_construct(a, p);
  36. BOOST_TEST_EQ(type::count, 1);
  37. BOOST_TEST_EQ(p->value(), 0);
  38. boost::alloc_destroy(a, p);
  39. BOOST_TEST_EQ(type::count, 0);
  40. a.deallocate(p, 1);
  41. }
  42. void test_construct_value()
  43. {
  44. boost::default_allocator<type> a;
  45. type* p = a.allocate(1);
  46. boost::alloc_construct(a, p, 1);
  47. BOOST_TEST_EQ(type::count, 1);
  48. BOOST_TEST_EQ(p->value(), 1);
  49. boost::alloc_destroy(a, p);
  50. BOOST_TEST_EQ(type::count, 0);
  51. a.deallocate(p, 1);
  52. }
  53. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  54. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  55. void test_construct_args()
  56. {
  57. boost::default_allocator<type> a;
  58. type* p = a.allocate(1);
  59. boost::alloc_construct(a, p, 1, 2);
  60. BOOST_TEST_EQ(type::count, 1);
  61. BOOST_TEST_EQ(p->value(), 3);
  62. boost::alloc_destroy(a, p);
  63. BOOST_TEST_EQ(type::count, 0);
  64. a.deallocate(p, 1);
  65. }
  66. #endif
  67. void test_construct_n()
  68. {
  69. boost::default_allocator<type> a;
  70. type* p = a.allocate(3);
  71. boost::alloc_construct_n(a, p, 3);
  72. BOOST_TEST_EQ(type::count, 3);
  73. BOOST_TEST_EQ(p[0].value(), 0);
  74. BOOST_TEST_EQ(p[1].value(), 0);
  75. BOOST_TEST_EQ(p[2].value(), 0);
  76. boost::alloc_destroy_n(a, p, 3);
  77. BOOST_TEST_EQ(type::count, 0);
  78. a.deallocate(p, 3);
  79. }
  80. void test_construct_n_list()
  81. {
  82. boost::default_allocator<type> a;
  83. type* p = a.allocate(3);
  84. type q(1);
  85. boost::alloc_construct_n(a, p, 3, &q, 1);
  86. BOOST_TEST_EQ(type::count, 4);
  87. BOOST_TEST_EQ(p[0].value(), 1);
  88. BOOST_TEST_EQ(p[1].value(), 1);
  89. BOOST_TEST_EQ(p[2].value(), 1);
  90. boost::alloc_destroy_n(a, p, 3);
  91. BOOST_TEST_EQ(type::count, 1);
  92. a.deallocate(p, 3);
  93. }
  94. void test_construct_n_iterator()
  95. {
  96. boost::default_allocator<type> a;
  97. type* p = a.allocate(3);
  98. type l[] = { type(1), type(2), type(3) };
  99. boost::alloc_construct_n(a, p, 3, &l[0]);
  100. BOOST_TEST_EQ(type::count, 6);
  101. BOOST_TEST_EQ(p[0].value(), 1);
  102. BOOST_TEST_EQ(p[1].value(), 2);
  103. BOOST_TEST_EQ(p[2].value(), 3);
  104. boost::alloc_destroy_n(a, p, 3);
  105. BOOST_TEST_EQ(type::count, 3);
  106. a.deallocate(p, 3);
  107. }
  108. int main()
  109. {
  110. test_construct();
  111. test_construct_value();
  112. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  113. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  114. test_construct_args();
  115. #endif
  116. test_construct_n();
  117. test_construct_n_list();
  118. test_construct_n_iterator();
  119. return boost::report_errors();
  120. }