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
SimpleAllocatorSimpleAllocator16 SimpleAllocator() {}
17
SimpleAllocatorSimpleAllocator18 template <class T> SimpleAllocator(const SimpleAllocator<T>&) {}
19
allocateSimpleAllocator20 Tp* allocate(std::size_t n)
21 {
22 return static_cast<Tp*>(::operator new(n * sizeof(Tp)));
23 }
24
deallocateSimpleAllocator25 void deallocate(Tp* p, std::size_t) { ::operator delete((void*)p); }
26 };
27
test_simple_allocator()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 #if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS == 1
52 BOOST_STATIC_ASSERT((boost::is_same<typename traits::size_type,
53 std::make_unsigned<std::ptrdiff_t>::type>::value));
54 #else
55 BOOST_STATIC_ASSERT(
56 (boost::is_same<typename traits::size_type, std::size_t>::value));
57 #endif
58
59 BOOST_TEST(!traits::propagate_on_container_copy_assignment::value);
60 BOOST_TEST(!traits::propagate_on_container_move_assignment::value);
61 BOOST_TEST(!traits::propagate_on_container_swap::value);
62
63 // rebind_alloc
64 // rebind_traits
65
66 SimpleAllocator<T> a;
67
68 T* ptr1 = traits::allocate(a, 1);
69 // T* ptr2 = traits::allocate(a, 1, static_cast<void const*>(ptr1));
70
71 traits::construct(a, ptr1, T(10));
72 // traits::construct(a, ptr2, T(30), ptr1);
73
74 BOOST_TEST(*ptr1 == T(10));
75 // BOOST_TEST(*ptr2 == T(30));
76
77 traits::destroy(a, ptr1);
78 // traits::destroy(a, ptr2);
79
80 // traits::deallocate(a, ptr2, 1);
81 traits::deallocate(a, ptr1, 1);
82
83 traits::max_size(a);
84 }
85
main()86 int main()
87 {
88 test_simple_allocator<int>();
89 test_simple_allocator<test::object>();
90
91 return boost::report_errors();
92 }
93