1 /*
2 Copyright 2014 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/align/aligned_allocator_adaptor.hpp>
9 #include <boost/align/is_aligned.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <new>
12 #include <cstring>
13
14 template<class T>
15 class A {
16 public:
17 typedef T value_type;
18 typedef T* pointer;
19 typedef std::size_t size_type;
20 typedef std::ptrdiff_t difference_type;
21
22 template<class U>
23 struct rebind {
24 typedef A<U> other;
25 };
26
A(int state)27 A(int state)
28 : state_(state) { }
29
30 template<class U>
A(const A<U> & other)31 A(const A<U>& other)
32 : state_(other.state()) { }
33
allocate(std::size_t size,const void * =0)34 T* allocate(std::size_t size, const void* = 0) {
35 return static_cast<T*>(::operator new(sizeof(T) * size));
36 }
37
deallocate(T * ptr,std::size_t)38 void deallocate(T* ptr, std::size_t) {
39 ::operator delete(ptr);
40 }
41
construct(T * ptr,const T & value)42 void construct(T* ptr, const T& value) {
43 ::new(static_cast<void*>(ptr)) T(value);
44 }
45
destroy(T * ptr)46 void destroy(T* ptr) {
47 ptr->~T();
48 }
49
state() const50 int state() const {
51 return state_;
52 }
53
54 private:
55 int state_;
56 };
57
58 template<class T, class U>
59 inline bool
operator ==(const A<T> & a,const A<U> & b)60 operator==(const A<T>& a, const A<U>& b)
61 {
62 return a.state() == b.state();
63 }
64
65 template<class T, class U>
66 inline bool
operator !=(const A<T> & a,const A<U> & b)67 operator!=(const A<T>& a, const A<U>& b)
68 {
69 return !(a == b);
70 }
71
72 template<std::size_t Alignment>
test_allocate()73 void test_allocate()
74 {
75 {
76 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
77 int* p = a.allocate(1);
78 BOOST_TEST(p != 0);
79 BOOST_TEST(boost::alignment::is_aligned(p, Alignment));
80 std::memset(p, 0, 1);
81 a.deallocate(p, 1);
82 }
83 {
84 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
85 int* p = a.allocate(1);
86 int* q = a.allocate(1, p);
87 BOOST_TEST(q != 0);
88 BOOST_TEST(boost::alignment::is_aligned(q, Alignment));
89 std::memset(q, 0, 1);
90 a.deallocate(q, 1);
91 a.deallocate(p, 1);
92 }
93 {
94 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
95 int* p = a.allocate(0);
96 a.deallocate(p, 0);
97 }
98 }
99
100 template<std::size_t Alignment>
test_construct()101 void test_construct()
102 {
103 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
104 int* p = a.allocate(1);
105 a.construct(p, 1);
106 BOOST_TEST(*p == 1);
107 a.destroy(p);
108 a.deallocate(p, 1);
109 }
110
111 template<std::size_t Alignment>
test_constructor()112 void test_constructor()
113 {
114 {
115 boost::alignment::aligned_allocator_adaptor<A<char>, Alignment> a(5);
116 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> b(a);
117 BOOST_TEST(b == a);
118 }
119 {
120 A<int> a(5);
121 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> b(a);
122 BOOST_TEST(b.base() == a);
123 }
124 }
125
126 template<std::size_t Alignment>
test_rebind()127 void test_rebind()
128 {
129 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
130 typename boost::alignment::aligned_allocator_adaptor<A<int>,
131 Alignment>::template rebind<int>::other b(a);
132 BOOST_TEST(b == a);
133 }
134
135 template<std::size_t Alignment>
test()136 void test()
137 {
138 test_allocate<Alignment>();
139 test_construct<Alignment>();
140 test_constructor<Alignment>();
141 test_rebind<Alignment>();
142 }
143
main()144 int main()
145 {
146 test<1>();
147 test<2>();
148 test<4>();
149 test<8>();
150 test<16>();
151 test<32>();
152 test<64>();
153 test<128>();
154
155 return boost::report_errors();
156 }
157