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