]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/align/test/aligned_allocator_adaptor_test.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / align / test / aligned_allocator_adaptor_test.cpp
CommitLineData
7c673cae 1/*
b32b8144
FG
2Copyright 2014 Glen Joseph Fernandes
3(glenjofe@gmail.com)
7c673cae 4
b32b8144
FG
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7c673cae
FG
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
14template<class T>
15class A {
16public:
17 typedef T value_type;
18 typedef T* pointer;
7c673cae
FG
19 typedef std::size_t size_type;
20 typedef std::ptrdiff_t difference_type;
b32b8144 21
7c673cae
FG
22 template<class U>
23 struct rebind {
24 typedef A<U> other;
25 };
b32b8144
FG
26
27 A(int state)
28 : state_(state) { }
29
7c673cae
FG
30 template<class U>
31 A(const A<U>& other)
b32b8144
FG
32 : state_(other.state()) { }
33
34 T* allocate(std::size_t size, const void* = 0) {
7c673cae
FG
35 return static_cast<T*>(::operator new(sizeof(T) * size));
36 }
b32b8144
FG
37
38 void deallocate(T* ptr, std::size_t) {
7c673cae
FG
39 ::operator delete(ptr);
40 }
b32b8144
FG
41
42 void construct(T* ptr, const T& value) {
7c673cae
FG
43 ::new(static_cast<void*>(ptr)) T(value);
44 }
b32b8144
FG
45
46 void destroy(T* ptr) {
7c673cae
FG
47 ptr->~T();
48 }
b32b8144
FG
49
50 int state() const {
51 return state_;
52 }
53
54private:
55 int state_;
7c673cae
FG
56};
57
b32b8144
FG
58template<class T, class U>
59inline bool
60operator==(const A<T>& a, const A<U>& b)
7c673cae 61{
b32b8144 62 return a.state() == b.state();
7c673cae
FG
63}
64
b32b8144
FG
65template<class T, class U>
66inline bool
67operator!=(const A<T>& a, const A<U>& b)
7c673cae
FG
68{
69 return !(a == b);
70}
71
72template<std::size_t Alignment>
73void test_allocate()
74{
75 {
b32b8144 76 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
7c673cae
FG
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 {
b32b8144
FG
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);
7c673cae
FG
92 }
93 {
b32b8144 94 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
7c673cae
FG
95 int* p = a.allocate(0);
96 a.deallocate(p, 0);
97 }
98}
99
100template<std::size_t Alignment>
101void test_construct()
102{
b32b8144 103 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
7c673cae
FG
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
111template<std::size_t Alignment>
112void test_constructor()
113{
114 {
b32b8144
FG
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);
7c673cae
FG
118 }
119 {
b32b8144
FG
120 A<int> a(5);
121 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> b(a);
122 BOOST_TEST(b.base() == a);
7c673cae
FG
123 }
124}
125
126template<std::size_t Alignment>
127void test_rebind()
128{
b32b8144 129 boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5);
7c673cae 130 typename boost::alignment::aligned_allocator_adaptor<A<int>,
b32b8144
FG
131 Alignment>::template rebind<int>::other b(a);
132 BOOST_TEST(b == a);
7c673cae
FG
133}
134
135template<std::size_t Alignment>
136void test()
137{
138 test_allocate<Alignment>();
139 test_construct<Alignment>();
140 test_constructor<Alignment>();
141 test_rebind<Alignment>();
142}
143
144int 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}