summaryrefslogtreecommitdiff
path: root/libs/unordered/test/unordered/allocator_traits.cpp
blob: ef7f61fe291442bf7673af4ff83bf4c6cd07cae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

// Copyright 2011 Daniel James.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/unordered/detail/allocator_helpers.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/limits.hpp>

// Boilerplate

#define ALLOCATOR_METHODS(name)                                             \
    template <typename U> struct rebind {                                   \
        typedef name<U> other;                                              \
    };                                                                      \
                                                                            \
    name() {}                                                               \
    template <typename Y> name(name<Y> const&) {}                           \
    T* address(T& r) { return &r;}                                          \
    T const* address(T const& r) { return &r; }                             \
    T* allocate(std::size_t n)                                              \
    { return static_cast<T*>(::operator new(n * sizeof(T))); }              \
    T* allocate(std::size_t n, void const* u)                               \
    { return static_cast<T*>(::operator new(n * sizeof(T))); }              \
    void deallocate(T* p, std::size_t n) { ::operator delete((void*) p); }  \
    void construct(T* p, T const& t) { new(p) T(t); }                       \
    void destroy(T* p) { p->~T(); }                                         \
    std::size_t max_size() const                                            \
    { return (std::numeric_limits<std::size_t>::max)(); }                   \
    bool operator==(name<T> const&) { return true; }                        \
    bool operator!=(name<T> const&) { return false; }                       \
/**/

#define ALLOCATOR_METHODS_TYPEDEFS(name)                                    \
    template <typename U> struct rebind {                                   \
        typedef name<U> other;                                              \
    };                                                                      \
                                                                            \
    name() {}                                                               \
    template <typename Y> name(name<Y> const&) {}                           \
    pointer address(T& r) { return &r;}                                     \
    const_pointer address(T const& r) { return &r; }                        \
    pointer allocate(std::size_t n)                                         \
    { return pointer(::operator new(n * sizeof(T))); }                      \
    pointer allocate(std::size_t n, void const* u)                          \
    { return pointer(::operator new(n * sizeof(T))); }                      \
    void deallocate(pointer p, std::size_t n)                               \
    { ::operator delete((void*) p); }                                       \
    void construct(T* p, T const& t) { new(p) T(t); }                       \
    void destroy(T* p) { p->~T(); }                                         \
    size_type max_size() const                                              \
    { return (std::numeric_limits<size_type>::max)(); }                     \
    bool operator==(name<T> const&) { return true; }                        \
    bool operator!=(name<T> const&) { return false; }                       \
/**/

struct yes_type { enum { value = true }; };
struct no_type { enum { value = false }; };

// For tracking calls...

static int selected;
void reset() {
    selected = 0;
}

template <typename Allocator>
int call_select()
{
    typedef boost::unordered::detail::allocator_traits<Allocator> traits;
    Allocator a;

    reset();
    BOOST_TEST(traits::select_on_container_copy_construction(a) == a);
    return selected;
}

// Empty allocator test

template <typename T>
struct empty_allocator
{
    typedef T value_type;
    ALLOCATOR_METHODS(empty_allocator)
};

void test_empty_allocator()
{
    typedef empty_allocator<int> allocator;
    typedef boost::unordered::detail::allocator_traits<allocator> traits;
#if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS
    BOOST_MPL_ASSERT((boost::is_same<traits::size_type,
        std::make_unsigned<std::ptrdiff_t>::type>));
#else
    BOOST_MPL_ASSERT((boost::is_same<traits::size_type, std::size_t>));
#endif
    BOOST_MPL_ASSERT((boost::is_same<traits::difference_type, std::ptrdiff_t>));
    BOOST_MPL_ASSERT((boost::is_same<traits::pointer, int*>));
    BOOST_MPL_ASSERT((boost::is_same<traits::const_pointer, int const*>));
    BOOST_MPL_ASSERT((boost::is_same<traits::value_type, int>));
    BOOST_TEST(!traits::propagate_on_container_copy_assignment::value);
    BOOST_TEST(!traits::propagate_on_container_move_assignment::value);
    BOOST_TEST(!traits::propagate_on_container_swap::value);
    BOOST_TEST(call_select<allocator>() == 0);
}

// allocator 1

template <typename T>
struct allocator1
{
    typedef T value_type;
    ALLOCATOR_METHODS(allocator1)
    
    typedef yes_type propagate_on_container_copy_assignment;
    typedef yes_type propagate_on_container_move_assignment;
    typedef yes_type propagate_on_container_swap;
    
    allocator1<T> select_on_container_copy_construction() const {
        ++selected;
        return allocator1<T>();
    }
};

void test_allocator1()
{
    typedef allocator1<int> allocator;
    typedef boost::unordered::detail::allocator_traits<allocator> traits;
#if BOOST_UNORDERED_USE_ALLOCATOR_TRAITS
    BOOST_MPL_ASSERT((boost::is_same<typename traits::size_type,
        std::make_unsigned<std::ptrdiff_t>::type>));
#else
    BOOST_MPL_ASSERT((boost::is_same<traits::size_type, std::size_t>));
#endif
    BOOST_MPL_ASSERT((boost::is_same<traits::difference_type, std::ptrdiff_t>));
    BOOST_MPL_ASSERT((boost::is_same<traits::pointer, int*>));
    BOOST_MPL_ASSERT((boost::is_same<traits::const_pointer, int const*>));
    BOOST_MPL_ASSERT((boost::is_same<traits::value_type, int>));
    BOOST_TEST(traits::propagate_on_container_copy_assignment::value);
    BOOST_TEST(traits::propagate_on_container_move_assignment::value);
    BOOST_TEST(traits::propagate_on_container_swap::value);
    BOOST_TEST(call_select<allocator>() == 1);
}

// allocator 2

template <typename Alloc>
struct allocator2_base
{
    Alloc select_on_container_copy_construction() const {
        ++selected;
        return Alloc();
    }
};

template <typename T>
struct allocator2 : allocator2_base<allocator2<T> >
{
    typedef T value_type;
    typedef T* pointer;
    typedef T const* const_pointer;
    typedef std::size_t size_type;
    
    ALLOCATOR_METHODS(allocator2)
    
    typedef no_type propagate_on_container_copy_assignment;
    typedef no_type propagate_on_container_move_assignment;
    typedef no_type propagate_on_container_swap;
};

void test_allocator2()
{
    typedef allocator2<int> allocator;
    typedef boost::unordered::detail::allocator_traits<allocator> traits;
    BOOST_MPL_ASSERT((boost::is_same<traits::size_type, std::size_t>));
    BOOST_MPL_ASSERT((boost::is_same<traits::difference_type, std::ptrdiff_t>));
    BOOST_MPL_ASSERT((boost::is_same<traits::pointer, int*>));
    BOOST_MPL_ASSERT((boost::is_same<traits::const_pointer, int const*>));
    BOOST_MPL_ASSERT((boost::is_same<traits::value_type, int>));
    BOOST_TEST(!traits::propagate_on_container_copy_assignment::value);
    BOOST_TEST(!traits::propagate_on_container_move_assignment::value);
    BOOST_TEST(!traits::propagate_on_container_swap::value);
    BOOST_TEST(call_select<allocator>() == 1);
}

// allocator 3

template <typename T>
struct ptr
{
    T* value_;
    
    ptr(void* v) : value_((T*) v) {}
    T& operator*() const { return *value_; }
};

template <>
struct ptr<void>
{
    void* value_;
    ptr(void* v) : value_(v) {}
};

template <>
struct ptr<const void>
{
    void const* value_;
    ptr(void const* v) : value_(v) {}
};

template <typename T>
struct allocator3
{
    typedef T value_type;
    typedef ptr<T> pointer;
    typedef ptr<T const> const_pointer;
    typedef unsigned short size_type;

    ALLOCATOR_METHODS_TYPEDEFS(allocator3)
    
    typedef yes_type propagate_on_container_copy_assignment;
    typedef no_type propagate_on_container_move_assignment;

    allocator3<T> select_on_container_copy_construction() const {
        ++selected;
        return allocator3<T>();
    }
};

void test_allocator3()
{
    typedef allocator3<int> allocator;
    typedef boost::unordered::detail::allocator_traits<allocator> traits;
    BOOST_MPL_ASSERT((boost::is_same<traits::size_type, unsigned short>));
    BOOST_MPL_ASSERT((boost::is_same<traits::difference_type, std::ptrdiff_t>));
    BOOST_MPL_ASSERT((boost::is_same<traits::pointer, ptr<int> >));
    BOOST_MPL_ASSERT((boost::is_same<traits::const_pointer, ptr<int const> >));
    BOOST_MPL_ASSERT((boost::is_same<traits::value_type, int>));
    BOOST_TEST(traits::propagate_on_container_copy_assignment::value);
    BOOST_TEST(!traits::propagate_on_container_move_assignment::value);
    BOOST_TEST(!traits::propagate_on_container_swap::value);
    BOOST_TEST(call_select<allocator>() == 1);
}

int main()
{
    test_empty_allocator();
    test_allocator1();
    test_allocator2();
    test_allocator3();
    return boost::report_errors();
}