allocator_utilities_test.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2018 Daniel James.
  2. // Distributed under the Boost 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/detail/allocator_utilities.hpp>
  5. #include <boost/static_assert.hpp>
  6. #include <cassert>
  7. typedef std::allocator<int> std_int_allocator;
  8. typedef boost::detail::allocator::rebind_to<std_int_allocator, char>::type char_allocator;
  9. typedef boost::detail::allocator::rebind_to<char_allocator, int>::type int_allocator;
  10. typedef boost::detail::allocator::rebind_to<int_allocator, char>::type char_allocator2;
  11. int main() {
  12. BOOST_STATIC_ASSERT((!boost::is_same<char_allocator, int_allocator>::value));
  13. BOOST_STATIC_ASSERT((boost::is_same<char_allocator, char_allocator2>::value));
  14. // Check the constructors works okay
  15. std_int_allocator a1;
  16. char_allocator a2(a1);
  17. char_allocator a2a(a2);
  18. int_allocator a3(a2);
  19. // Check allocate works okay
  20. {
  21. char_allocator::pointer p = a2.allocate(10);
  22. assert(p);
  23. a2.deallocate(p, 10);
  24. }
  25. // Try using the standalone construct/destroy
  26. {
  27. int_allocator::pointer p2 = a3.allocate(1);
  28. boost::detail::allocator::construct(p2, 25);
  29. assert(*p2 == 25);
  30. boost::detail::allocator::destroy(p2);
  31. a3.deallocate(p2, 1);
  32. }
  33. }