| 1 | |
| 2 | // Copyright 2011 Daniel James. |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include "../objects/test.hpp" |
| 7 | #include <boost/core/lightweight_test.hpp> |
| 8 | #include <boost/static_assert.hpp> |
| 9 | #include <boost/type_traits/is_same.hpp> |
| 10 | #include <boost/unordered/detail/implementation.hpp> |
| 11 | |
| 12 | template <class Tp> struct SimpleAllocator |
| 13 | { |
| 14 | typedef Tp value_type; |
| 15 | |
| 16 | SimpleAllocator() {} |
| 17 | |
| 18 | template <class T> SimpleAllocator(const SimpleAllocator<T>&) {} |
| 19 | |
| 20 | Tp* allocate(std::size_t n) |
| 21 | { |
| 22 | return static_cast<Tp*>(::operator new(n * sizeof(Tp))); |
| 23 | } |
| 24 | |
| 25 | void deallocate(Tp* p, std::size_t) { ::operator delete((void*)p); } |
| 26 | }; |
| 27 | |
| 28 | template <typename T> void test_simple_allocator() |
| 29 | { |
| 30 | test::check_instances check_; |
| 31 | |
| 32 | typedef boost::unordered::detail::allocator_traits<SimpleAllocator<T> > |
| 33 | traits; |
| 34 | |
| 35 | BOOST_STATIC_ASSERT((boost::is_same<typename traits::allocator_type, |
| 36 | SimpleAllocator<T> >::value)); |
| 37 | |
| 38 | BOOST_STATIC_ASSERT((boost::is_same<typename traits::value_type, T>::value)); |
| 39 | |
| 40 | BOOST_STATIC_ASSERT((boost::is_same<typename traits::pointer, T*>::value)); |
| 41 | BOOST_STATIC_ASSERT( |
| 42 | (boost::is_same<typename traits::const_pointer, T const*>::value)); |
| 43 | // BOOST_STATIC_ASSERT((boost::is_same<typename traits::void_pointer, void* |
| 44 | // >::value)); |
| 45 | // BOOST_STATIC_ASSERT((boost::is_same<typename traits::const_void_pointer, |
| 46 | // void const*>::value)); |
| 47 | |
| 48 | BOOST_STATIC_ASSERT( |
| 49 | (boost::is_same<typename traits::difference_type, std::ptrdiff_t>::value)); |
| 50 | |
| 51 | BOOST_STATIC_ASSERT((boost::is_same<typename traits::size_type, |
| 52 | std::make_unsigned<std::ptrdiff_t>::type>::value)); |
| 53 | |
| 54 | BOOST_TEST(!traits::propagate_on_container_copy_assignment::value); |
| 55 | BOOST_TEST(!traits::propagate_on_container_move_assignment::value); |
| 56 | BOOST_TEST(!traits::propagate_on_container_swap::value); |
| 57 | |
| 58 | // rebind_alloc |
| 59 | // rebind_traits |
| 60 | |
| 61 | SimpleAllocator<T> a; |
| 62 | |
| 63 | T* ptr1 = traits::allocate(a, 1); |
| 64 | // T* ptr2 = traits::allocate(a, 1, static_cast<void const*>(ptr1)); |
| 65 | |
| 66 | traits::construct(a, ptr1, T(10)); |
| 67 | // traits::construct(a, ptr2, T(30), ptr1); |
| 68 | |
| 69 | BOOST_TEST(*ptr1 == T(10)); |
| 70 | // BOOST_TEST(*ptr2 == T(30)); |
| 71 | |
| 72 | traits::destroy(a, ptr1); |
| 73 | // traits::destroy(a, ptr2); |
| 74 | |
| 75 | // traits::deallocate(a, ptr2, 1); |
| 76 | traits::deallocate(a, ptr1, 1); |
| 77 | |
| 78 | traits::max_size(a); |
| 79 | } |
| 80 | |
| 81 | int main() |
| 82 | { |
| 83 | test_simple_allocator<int>(); |
| 84 | test_simple_allocator<test::object>(); |
| 85 | |
| 86 | return boost::report_errors(); |
| 87 | } |
| 88 | |