• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright 2019 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4 
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #include <boost/core/default_allocator.hpp>
9 #include <boost/core/lightweight_test_trait.hpp>
10 #include <vector>
11 #include <list>
12 
13 class type {
14 public:
type(double value)15     explicit type(double value)
16         : value_(value) { }
17 
18 private:
19     type(const type&);
20     type& operator=(const type&);
21 
22     double value_;
23 };
24 
test_value_type()25 void test_value_type()
26 {
27     BOOST_TEST_TRAIT_SAME(int,
28         boost::default_allocator<int>::value_type);
29     BOOST_TEST_TRAIT_SAME(type,
30         boost::default_allocator<type>::value_type);
31     BOOST_TEST_TRAIT_SAME(int[5],
32         boost::default_allocator<int[5]>::value_type);
33     BOOST_TEST_TRAIT_SAME(void,
34         boost::default_allocator<void>::value_type);
35 }
36 
test_pointer()37 void test_pointer()
38 {
39     BOOST_TEST_TRAIT_SAME(int*,
40         boost::default_allocator<int>::pointer);
41     BOOST_TEST_TRAIT_SAME(type*,
42         boost::default_allocator<type>::pointer);
43     BOOST_TEST_TRAIT_SAME(int(*)[5],
44         boost::default_allocator<int[5]>::pointer);
45     BOOST_TEST_TRAIT_SAME(void*,
46         boost::default_allocator<void>::pointer);
47 }
48 
test_const_pointer()49 void test_const_pointer()
50 {
51     BOOST_TEST_TRAIT_SAME(const int*,
52         boost::default_allocator<int>::const_pointer);
53     BOOST_TEST_TRAIT_SAME(const type*,
54         boost::default_allocator<type>::const_pointer);
55     BOOST_TEST_TRAIT_SAME(const int(*)[5],
56         boost::default_allocator<int[5]>::const_pointer);
57     BOOST_TEST_TRAIT_SAME(const void*,
58         boost::default_allocator<void>::const_pointer);
59 }
60 
test_reference()61 void test_reference()
62 {
63     BOOST_TEST_TRAIT_SAME(int&,
64         boost::default_allocator<int>::reference);
65     BOOST_TEST_TRAIT_SAME(type&,
66         boost::default_allocator<type>::reference);
67     BOOST_TEST_TRAIT_SAME(int(&)[5],
68         boost::default_allocator<int[5]>::reference);
69     BOOST_TEST_TRAIT_SAME(void,
70         boost::default_allocator<void>::reference);
71 }
72 
test_const_reference()73 void test_const_reference()
74 {
75     BOOST_TEST_TRAIT_SAME(const int&,
76         boost::default_allocator<int>::const_reference);
77     BOOST_TEST_TRAIT_SAME(const type&,
78         boost::default_allocator<type>::const_reference);
79     BOOST_TEST_TRAIT_SAME(const int(&)[5],
80         boost::default_allocator<int[5]>::const_reference);
81     BOOST_TEST_TRAIT_SAME(const void,
82         boost::default_allocator<void>::const_reference);
83 }
84 
test_size_type()85 void test_size_type()
86 {
87     BOOST_TEST_TRAIT_SAME(std::size_t,
88         boost::default_allocator<int>::size_type);
89     BOOST_TEST_TRAIT_SAME(std::size_t,
90         boost::default_allocator<type>::size_type);
91     BOOST_TEST_TRAIT_SAME(std::size_t,
92         boost::default_allocator<int[5]>::size_type);
93     BOOST_TEST_TRAIT_SAME(std::size_t,
94         boost::default_allocator<void>::size_type);
95 }
96 
test_difference_type()97 void test_difference_type()
98 {
99     BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
100         boost::default_allocator<int>::difference_type);
101     BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
102         boost::default_allocator<type>::difference_type);
103     BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
104         boost::default_allocator<int[5]>::difference_type);
105     BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
106         boost::default_allocator<void>::difference_type);
107 }
108 
test_propagate_on_container_move_assignment()109 void test_propagate_on_container_move_assignment()
110 {
111     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int>::
112         propagate_on_container_move_assignment));
113     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<type>::
114         propagate_on_container_move_assignment));
115     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int[5]>::
116         propagate_on_container_move_assignment));
117     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<void>::
118         propagate_on_container_move_assignment));
119 }
120 
test_is_always_equal()121 void test_is_always_equal()
122 {
123     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int>::is_always_equal));
124     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<type>::is_always_equal));
125     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int[5]>::is_always_equal));
126     BOOST_TEST_TRAIT_TRUE((boost::default_allocator<void>::is_always_equal));
127 }
128 
test_rebind()129 void test_rebind()
130 {
131     BOOST_TEST_TRAIT_SAME(boost::default_allocator<type>,
132         boost::default_allocator<int>::rebind<type>::other);
133     BOOST_TEST_TRAIT_SAME(boost::default_allocator<int[5]>,
134         boost::default_allocator<type>::rebind<int[5]>::other);
135     BOOST_TEST_TRAIT_SAME(boost::default_allocator<void>,
136         boost::default_allocator<int[5]>::rebind<void>::other);
137     BOOST_TEST_TRAIT_SAME(boost::default_allocator<int>,
138         boost::default_allocator<void>::rebind<int>::other);
139 }
140 
test_default_construct()141 void test_default_construct()
142 {
143     boost::default_allocator<int> a1;
144     (void)a1;
145     boost::default_allocator<type> a2;
146     (void)a2;
147     boost::default_allocator<int[5]> a3;
148     (void)a3;
149     boost::default_allocator<void> a4;
150     (void)a4;
151 }
152 
test_copy()153 void test_copy()
154 {
155     boost::default_allocator<int> a1;
156     boost::default_allocator<int> a2(a1);
157     (void)a2;
158     boost::default_allocator<int[5]> a3;
159     boost::default_allocator<int[5]> a4(a3);
160     (void)a4;
161     boost::default_allocator<void> a5;
162     boost::default_allocator<void> a6(a5);
163     (void)a6;
164 }
165 
test_construct_other()166 void test_construct_other()
167 {
168     boost::default_allocator<int> a1;
169     boost::default_allocator<type> a2(a1);
170     boost::default_allocator<int[5]> a3(a2);
171     boost::default_allocator<void> a4(a3);
172     boost::default_allocator<int> a5(a4);
173     (void)a5;
174 }
175 
176 template<class T>
max_size()177 std::size_t max_size()
178 {
179     return static_cast<std::size_t>(-1) / (2 < sizeof(T) ? sizeof(T) : 2);
180 }
181 
test_max_size()182 void test_max_size()
183 {
184     BOOST_TEST_EQ(max_size<int>(),
185         boost::default_allocator<int>().max_size());
186     BOOST_TEST_EQ(max_size<type>(),
187         boost::default_allocator<type>().max_size());
188     BOOST_TEST_EQ(max_size<int[5]>(),
189         boost::default_allocator<int[5]>().max_size());
190 }
191 
192 template<class T>
test_allocate()193 void test_allocate()
194 {
195     boost::default_allocator<T> a;
196     T* p = a.allocate(1);
197     BOOST_TEST(p != 0);
198     a.deallocate(p, 1);
199     p = a.allocate(0);
200     a.deallocate(p, 0);
201     BOOST_TEST_THROWS(a.allocate(a.max_size() + 1), std::bad_alloc);
202 }
203 
test_allocate_deallocate()204 void test_allocate_deallocate()
205 {
206     test_allocate<int>();
207     test_allocate<type>();
208     test_allocate<int[5]>();
209 }
210 
test_equals()211 void test_equals()
212 {
213     BOOST_TEST(boost::default_allocator<int>() ==
214         boost::default_allocator<type>());
215     BOOST_TEST(boost::default_allocator<type>() ==
216         boost::default_allocator<int[5]>());
217     BOOST_TEST(boost::default_allocator<int[5]>() ==
218         boost::default_allocator<void>());
219     BOOST_TEST(boost::default_allocator<void>() ==
220         boost::default_allocator<int>());
221 }
222 
test_not_equals()223 void test_not_equals()
224 {
225     BOOST_TEST(!(boost::default_allocator<int>() !=
226         boost::default_allocator<type>()));
227     BOOST_TEST(!(boost::default_allocator<type>() !=
228         boost::default_allocator<int[5]>()));
229     BOOST_TEST(!(boost::default_allocator<int[5]>() !=
230         boost::default_allocator<void>()));
231     BOOST_TEST(!(boost::default_allocator<void>() !=
232         boost::default_allocator<int>()));
233 }
234 
test_container()235 void test_container()
236 {
237     std::vector<int, boost::default_allocator<int> > v;
238     v.push_back(1);
239     BOOST_TEST(v.size() == 1);
240     BOOST_TEST(v.front() == 1);
241     std::list<int, boost::default_allocator<int> > l;
242     l.push_back(1);
243     BOOST_TEST(l.size() == 1);
244     BOOST_TEST(l.front() == 1);
245 }
246 
main()247 int main()
248 {
249     test_value_type();
250     test_pointer();
251     test_const_pointer();
252     test_reference();
253     test_const_reference();
254     test_size_type();
255     test_difference_type();
256     test_propagate_on_container_move_assignment();
257     test_is_always_equal();
258     test_rebind();
259     test_default_construct();
260     test_copy();
261     test_construct_other();
262     test_max_size();
263     test_allocate_deallocate();
264     test_equals();
265     test_not_equals();
266     test_container();
267     return boost::report_errors();
268 }
269