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/alloc_construct.hpp>
9 #include <boost/core/default_allocator.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 
12 class type {
13 public:
type(int x=0,int y=0)14     explicit type(int x = 0, int y = 0)
15         : value_(x + y) {
16         ++count;
17     }
18 
type(const type & other)19     type(const type& other)
20         : value_(other.value_) {
21         ++count;
22     }
23 
~type()24     ~type() {
25         --count;
26     }
27 
value() const28     int value() const {
29         return value_;
30     }
31 
32     static int count;
33 
34 private:
35     int value_;
36 };
37 
38 int type::count = 0;
39 
test_construct()40 void test_construct()
41 {
42     boost::default_allocator<type> a;
43     type* p = a.allocate(1);
44     boost::alloc_construct(a, p);
45     BOOST_TEST_EQ(type::count, 1);
46     BOOST_TEST_EQ(p->value(), 0);
47     boost::alloc_destroy(a, p);
48     BOOST_TEST_EQ(type::count, 0);
49     a.deallocate(p, 1);
50 }
51 
test_construct_value()52 void test_construct_value()
53 {
54     boost::default_allocator<type> a;
55     type* p = a.allocate(1);
56     boost::alloc_construct(a, p, 1);
57     BOOST_TEST_EQ(type::count, 1);
58     BOOST_TEST_EQ(p->value(), 1);
59     boost::alloc_destroy(a, p);
60     BOOST_TEST_EQ(type::count, 0);
61     a.deallocate(p, 1);
62 }
63 
64 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
65     !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
test_construct_args()66 void test_construct_args()
67 {
68     boost::default_allocator<type> a;
69     type* p = a.allocate(1);
70     boost::alloc_construct(a, p, 1, 2);
71     BOOST_TEST_EQ(type::count, 1);
72     BOOST_TEST_EQ(p->value(), 3);
73     boost::alloc_destroy(a, p);
74     BOOST_TEST_EQ(type::count, 0);
75     a.deallocate(p, 1);
76 }
77 #endif
78 
test_construct_n()79 void test_construct_n()
80 {
81     boost::default_allocator<type> a;
82     type* p = a.allocate(3);
83     boost::alloc_construct_n(a, p, 3);
84     BOOST_TEST_EQ(type::count, 3);
85     BOOST_TEST_EQ(p[0].value(), 0);
86     BOOST_TEST_EQ(p[1].value(), 0);
87     BOOST_TEST_EQ(p[2].value(), 0);
88     boost::alloc_destroy_n(a, p, 3);
89     BOOST_TEST_EQ(type::count, 0);
90     a.deallocate(p, 3);
91 }
92 
test_construct_n_list()93 void test_construct_n_list()
94 {
95     boost::default_allocator<type> a;
96     type* p = a.allocate(3);
97     type q(1);
98     boost::alloc_construct_n(a, p, 3, &q, 1);
99     BOOST_TEST_EQ(type::count, 4);
100     BOOST_TEST_EQ(p[0].value(), 1);
101     BOOST_TEST_EQ(p[1].value(), 1);
102     BOOST_TEST_EQ(p[2].value(), 1);
103     boost::alloc_destroy_n(a, p, 3);
104     BOOST_TEST_EQ(type::count, 1);
105     a.deallocate(p, 3);
106 }
107 
test_construct_n_iterator()108 void test_construct_n_iterator()
109 {
110     boost::default_allocator<type> a;
111     type* p = a.allocate(3);
112     type l[] = { type(1), type(2), type(3) };
113     boost::alloc_construct_n(a, p, 3, &l[0]);
114     BOOST_TEST_EQ(type::count, 6);
115     BOOST_TEST_EQ(p[0].value(), 1);
116     BOOST_TEST_EQ(p[1].value(), 2);
117     BOOST_TEST_EQ(p[2].value(), 3);
118     boost::alloc_destroy_n(a, p, 3);
119     BOOST_TEST_EQ(type::count, 3);
120     a.deallocate(p, 3);
121 }
122 
main()123 int main()
124 {
125     test_construct();
126     test_construct_value();
127 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
128     !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
129     test_construct_args();
130 #endif
131     test_construct_n();
132     test_construct_n_list();
133     test_construct_n_iterator();
134     return boost::report_errors();
135 }
136