1//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
4// Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// See http://www.boost.org/libs/container for documentation.
8//
9//////////////////////////////////////////////////////////////////////////////
10#include <boost/container/pmr/memory_resource.hpp>
11#include <boost/core/lightweight_test.hpp>
12#include "derived_from_memory_resource.hpp"
13
14#include <cstdlib>
15
16using namespace boost::container;
17using namespace boost::container::pmr;
18
19void test_allocate()
20{
21 derived_from_memory_resource d;
22 memory_resource &mr = d;
23
24 d.reset();
25 BOOST_TEST(d.do_allocate_called == false);
26 BOOST_TEST(d.do_allocate_bytes == 0);
27 BOOST_TEST(d.do_allocate_alignment == 0);
28
29 mr.allocate(bytes: 2, alignment: 4);
30 BOOST_TEST(d.do_allocate_called == true);
31 BOOST_TEST(d.do_allocate_bytes == 2);
32 BOOST_TEST(d.do_allocate_alignment == 4);
33}
34
35void test_deallocate()
36{
37 derived_from_memory_resource d;
38 memory_resource &mr = d;
39
40 d.reset();
41 BOOST_TEST(d.do_deallocate_called == false);
42 BOOST_TEST(d.do_deallocate_p == 0);
43 BOOST_TEST(d.do_allocate_bytes == 0);
44 BOOST_TEST(d.do_allocate_alignment == 0);
45
46 mr.deallocate(p: &d, bytes: 2, alignment: 4);
47 BOOST_TEST(d.do_deallocate_called == true);
48 BOOST_TEST(d.do_deallocate_p == &d);
49 BOOST_TEST(d.do_deallocate_bytes == 2);
50 BOOST_TEST(d.do_deallocate_alignment == 4);
51}
52
53void test_destructor()
54{
55 {
56 derived_from_memory_resource d;
57 d.reset();
58 BOOST_TEST(derived_from_memory_resource::destructor_called == false);
59 }
60 BOOST_TEST(derived_from_memory_resource::destructor_called == true);
61}
62
63void test_is_equal()
64{
65 derived_from_memory_resource d;
66 memory_resource &mr = d;
67
68 d.reset();
69 BOOST_TEST(d.do_is_equal_called == false);
70 BOOST_TEST(d.do_is_equal_other == 0);
71
72 mr.is_equal(other: d);
73 BOOST_TEST(d.do_is_equal_called == true);
74 BOOST_TEST(d.do_is_equal_other == &d);
75}
76
77void test_equality_operator()
78{
79 derived_from_memory_resource d;
80 memory_resource &mr = d;
81
82 d.reset();
83 BOOST_TEST(d.do_is_equal_called == false);
84 BOOST_TEST(d.do_is_equal_other == 0);
85
86 //equal addresses are shorcircuited
87 BOOST_TEST((mr == mr) == true);
88 BOOST_TEST(d.do_is_equal_called == false);
89 BOOST_TEST(d.do_is_equal_other == 0);
90
91 //unequal addresses are dispatched to is_equal which in turn calls do_is_equal
92 derived_from_memory_resource d2(1);
93 d.reset();
94 d2.reset();
95 memory_resource &mr2 = d2;
96 BOOST_TEST((mr == mr2) == false);
97 BOOST_TEST(d.do_is_equal_called == true);
98 BOOST_TEST(d.do_is_equal_other == &d2);
99}
100
101void test_inequality_operator()
102{
103 derived_from_memory_resource d;
104 memory_resource &mr = d;
105
106 d.reset();
107 BOOST_TEST(d.do_is_equal_called == false);
108 BOOST_TEST(d.do_is_equal_other == 0);
109
110 //equal addresses are shorcircuited
111 BOOST_TEST((mr != mr) == false);
112 BOOST_TEST(d.do_is_equal_called == false);
113 BOOST_TEST(d.do_is_equal_other == 0);
114
115 //unequal addresses are dispatched to is_equal which in turn calls do_is_equal
116 derived_from_memory_resource d2(1);
117 d.reset();
118 d2.reset();
119 memory_resource &mr2 = d2;
120 BOOST_TEST((mr != mr2) == true);
121 BOOST_TEST(d.do_is_equal_called == true);
122 BOOST_TEST(d.do_is_equal_other == &d2);
123}
124
125int main()
126{
127 test_destructor();
128 test_allocate();
129 test_deallocate();
130 test_is_equal();
131 test_equality_operator();
132 test_inequality_operator();
133 return ::boost::report_errors();
134}
135

source code of boost/libs/container/test/memory_resource_test.cpp