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.hpp>
9 #include <boost/align/is_aligned.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <cstring>
12 
13 template<std::size_t Alignment>
test_allocate()14 void test_allocate()
15 {
16     {
17         boost::alignment::aligned_allocator<int, Alignment> a;
18         int* p = a.allocate(1);
19         BOOST_TEST(p != 0);
20         BOOST_TEST(boost::alignment::is_aligned(p, Alignment));
21         std::memset(p, 0, 1);
22         a.deallocate(p, 1);
23     }
24     {
25         boost::alignment::aligned_allocator<int, Alignment> a;
26         int* p = a.allocate(0);
27         a.deallocate(p, 0);
28     }
29 }
30 
31 template<std::size_t Alignment>
test_construct()32 void test_construct()
33 {
34     boost::alignment::aligned_allocator<int, Alignment> a;
35     int* p = a.allocate(1);
36     a.construct(p, 1);
37     BOOST_TEST(*p == 1);
38     a.destroy(p);
39     a.deallocate(p, 1);
40 }
41 
42 template<std::size_t Alignment>
test_constructor()43 void test_constructor()
44 {
45     {
46         boost::alignment::aligned_allocator<char, Alignment> a1;
47         boost::alignment::aligned_allocator<int, Alignment> a2(a1);
48         BOOST_TEST(a2 == a1);
49     }
50     {
51         boost::alignment::aligned_allocator<char, Alignment> a1;
52         boost::alignment::aligned_allocator<void, Alignment> a2(a1);
53         BOOST_TEST(a2 == a1);
54     }
55     {
56         boost::alignment::aligned_allocator<void, Alignment> a1;
57         boost::alignment::aligned_allocator<char, Alignment> a2(a1);
58         BOOST_TEST(a2 == a1);
59     }
60 }
61 
62 template<std::size_t Alignment>
test_rebind()63 void test_rebind()
64 {
65     {
66         boost::alignment::aligned_allocator<char, Alignment> a1;
67         typename boost::alignment::aligned_allocator<char,
68             Alignment>::template rebind<int>::other a2(a1);
69         BOOST_TEST(a2 == a1);
70     }
71     {
72         boost::alignment::aligned_allocator<char, Alignment> a1;
73         typename boost::alignment::aligned_allocator<char,
74             Alignment>::template rebind<void>::other a2(a1);
75         BOOST_TEST(a2 == a1);
76     }
77     {
78         boost::alignment::aligned_allocator<void, Alignment> a1;
79         typename boost::alignment::aligned_allocator<void,
80             Alignment>::template rebind<char>::other a2(a1);
81         BOOST_TEST(a2 == a1);
82     }
83 }
84 
85 template<std::size_t Alignment>
test()86 void test()
87 {
88     test_allocate<Alignment>();
89     test_construct<Alignment>();
90     test_constructor<Alignment>();
91     test_rebind<Alignment>();
92 }
93 
main()94 int main()
95 {
96     test<1>();
97     test<2>();
98     test<4>();
99     test<8>();
100     test<16>();
101     test<32>();
102     test<64>();
103     test<128>();
104 
105     return boost::report_errors();
106 }
107