File: resource_adaptor_test.cpp

package info (click to toggle)
boost1.62 1.62.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 686,420 kB
  • sloc: cpp: 2,609,004; xml: 972,558; ansic: 53,674; python: 32,437; sh: 8,829; asm: 3,071; cs: 2,121; makefile: 964; perl: 859; yacc: 472; php: 132; ruby: 94; f90: 55; sql: 13; csh: 6
file content (190 lines) | stat: -rw-r--r-- 5,595 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/container/pmr/resource_adaptor.hpp>
#include <boost/core/lightweight_test.hpp>
#include "propagation_test_allocator.hpp"
#include "derived_from_memory_resource.hpp"

using namespace boost::container::pmr;

void test_default_constructor()
{
   typedef propagation_test_allocator<char, 0> alloc_t;
   resource_adaptor<alloc_t> ra;
   BOOST_TEST(ra.get_allocator().m_default_contructed == true);
}

void test_copy_constructor()
{
   typedef propagation_test_allocator<char, 0> alloc_t;
   resource_adaptor<alloc_t> ra;
   BOOST_TEST(ra.get_allocator().m_default_contructed == true);
   resource_adaptor<alloc_t> rb(ra);
   BOOST_TEST(rb.get_allocator().m_default_contructed == false);
   BOOST_TEST(rb.get_allocator().m_move_contructed == false);
}

void test_move_constructor()
{
   typedef propagation_test_allocator<char, 0> alloc_t;
   resource_adaptor<alloc_t> ra;
   BOOST_TEST(ra.get_allocator().m_default_contructed == true);
   resource_adaptor<alloc_t> rb(::boost::move(ra));
   BOOST_TEST(rb.get_allocator().m_default_contructed == false);
   BOOST_TEST(rb.get_allocator().m_move_contructed == true);
}

void test_lvalue_alloc_constructor()
{
   typedef propagation_test_allocator<char, 0> alloc_t;
   alloc_t a;
   resource_adaptor<alloc_t> ra(a);
   BOOST_TEST(ra.get_allocator().m_default_contructed == false);
   BOOST_TEST(ra.get_allocator().m_move_contructed == false);
}

void test_rvalue_alloc_constructor()
{
   typedef propagation_test_allocator<char, 0> alloc_t;
   alloc_t a;
   resource_adaptor<alloc_t> ra(::boost::move(a));
   BOOST_TEST(ra.get_allocator().m_default_contructed == false);
   BOOST_TEST(ra.get_allocator().m_move_contructed == true);
}

void test_copy_assign()
{
   typedef propagation_test_allocator<char, 0> alloc_t;
   resource_adaptor<alloc_t> ra;
   BOOST_TEST(ra.get_allocator().m_default_contructed == true);
   resource_adaptor<alloc_t> rb;
   BOOST_TEST(ra.get_allocator().m_default_contructed == true);
   rb = ra;
   BOOST_TEST(rb.get_allocator().m_move_contructed == false);
   BOOST_TEST(rb.get_allocator().m_move_assigned == false);
}

void test_move_assign()
{
   typedef propagation_test_allocator<char, 0> alloc_t;
   resource_adaptor<alloc_t> ra;
   BOOST_TEST(ra.get_allocator().m_default_contructed == true);
   resource_adaptor<alloc_t> rb;
   BOOST_TEST(ra.get_allocator().m_default_contructed == true);
   rb = ::boost::move(ra);
   BOOST_TEST(rb.get_allocator().m_move_contructed == false);
   BOOST_TEST(rb.get_allocator().m_move_assigned == true);
}

struct stateful
{
   public:
   typedef char value_type;

   template<class U>
   struct rebind
   {  typedef stateful other; };

   stateful()
      : m_u(0u)
   {}

   char *allocate(std::size_t n)
   {  allocate_size = n;   return allocate_return;  }

   void deallocate(char *p, std::size_t n)
   {  deallocate_p = p;    deallocate_size = n; }

   friend bool operator==(const stateful &l, const stateful &r)
   {  return l.m_u == r.m_u;   }

   friend bool operator!=(const stateful &l, const stateful &r)
   {  return l.m_u != r.m_u;   }

   public:
   unsigned m_u;
   std::size_t allocate_size;
   char *allocate_return;
   std::size_t deallocate_size;
   char *deallocate_p;
};

void test_get_allocator()
{
   stateful a;
   a.m_u = 999;
   resource_adaptor<stateful> ra(a);
   const resource_adaptor<stateful> & cra = ra;
   BOOST_TEST( ra.get_allocator().m_u == 999);
   BOOST_TEST(cra.get_allocator().m_u == 999);
}

typedef resource_adaptor<stateful> stateful_resource_adaptor_t;

struct derived_from_resource_adaptor_stateful
   : public stateful_resource_adaptor_t
{
   public:
   typedef stateful_resource_adaptor_t base_t;
   using base_t::do_allocate;
   using base_t::do_deallocate;
   using base_t::do_is_equal;
};

void test_do_allocate()
{
   derived_from_resource_adaptor_stateful dra;
   char dummy = 0;
   dra.get_allocator().allocate_return = &dummy;
   void *allocate_ret = dra.do_allocate(998, 1234);
   BOOST_TEST(allocate_ret == &dummy);
   BOOST_TEST(dra.get_allocator().allocate_size == 998);
}

void test_do_deallocate()
{
   derived_from_resource_adaptor_stateful dra;
   char dummy = 0;
   dra.do_deallocate(&dummy, 1234, 753);
   BOOST_TEST(dra.get_allocator().deallocate_p == &dummy);
   BOOST_TEST(dra.get_allocator().deallocate_size == 1234);
}

void test_do_is_equal()
{
   derived_from_resource_adaptor_stateful dra;
   derived_from_memory_resource dmr;
   //Different dynamic type must return false
   BOOST_TEST(dra.do_is_equal(dmr) == false);

   //Same dynamic type with same state must return true
   derived_from_resource_adaptor_stateful dra2;
   BOOST_TEST(dra.do_is_equal(dra2) == true);

   //Same dynamic type with different state must return false
   dra2.get_allocator().m_u = 1234;
   BOOST_TEST(dra.do_is_equal(dra2) == false);
}

int main()
{
   test_default_constructor();
   test_copy_constructor();
   test_move_constructor();
   test_lvalue_alloc_constructor();
   test_rvalue_alloc_constructor();
   test_copy_assign();
   test_move_assign();
   test_get_allocator();
   test_do_allocate();
   test_do_deallocate();
   test_do_is_equal();
   return ::boost::report_errors();
}