1/*
2Copyright 2019 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7*/
8#include <boost/core/noinit_adaptor.hpp>
9#include <boost/core/default_allocator.hpp>
10#include <boost/core/lightweight_test.hpp>
11#include <vector>
12
13template<class T>
14class creator
15 : public boost::default_allocator<T> {
16public:
17 template<class U>
18 struct rebind {
19 typedef creator<U> other;
20 };
21
22 creator(int state)
23 : state_(state) { }
24
25 template<class U>
26 creator(const creator<U>& other)
27 : state_(other.state()) { }
28
29 template<class U, class V>
30 void construct(U*, const V&) {
31 BOOST_ERROR("construct");
32 }
33
34 template<class U>
35 void destroy(U*) {
36 BOOST_ERROR("destroy");
37 }
38
39 int state() const {
40 return state_;
41 }
42
43private:
44 int state_;
45};
46
47template<class T, class U>
48inline bool
49operator==(const creator<T>& lhs, const creator<U>& rhs)
50{
51 return lhs.state() == rhs.state();
52}
53
54template<class T, class U>
55inline bool
56operator!=(const creator<T>& lhs, const creator<U>& rhs)
57{
58 return !(lhs == rhs);
59}
60
61class type {
62public:
63 type() { }
64
65 type(int value)
66 : value_(value) { }
67
68 int value() const {
69 return value_;
70 }
71
72private:
73 int value_;
74};
75
76inline bool
77operator==(const type& lhs, const type& rhs)
78{
79 return lhs.value() == rhs.value();
80}
81
82template<class A>
83void test(const A& allocator)
84{
85 std::vector<typename A::value_type, A> v(allocator);
86 v.push_back(1);
87 BOOST_TEST(v.front() == 1);
88 v.clear();
89 v.resize(5);
90 v.front() = 1;
91}
92
93int main()
94{
95 test(allocator: boost::noinit_adaptor<creator<int> >(1));
96 test(allocator: boost::noinit_adaptor<creator<type> >(2));
97 test(allocator: boost::noinit_adapt(a: creator<int>(3)));
98 test(allocator: boost::noinit_adapt(a: creator<type>(4)));
99 return boost::report_errors();
100}
101

source code of boost/libs/core/test/noinit_adaptor_test.cpp