File: polymorphic_allocator_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (198 lines) | stat: -rw-r--r-- 5,640 bytes parent folder | download | duplicates (5)
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
191
192
193
194
195
196
197
198
//////////////////////////////////////////////////////////////////////////////
//
// (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/polymorphic_allocator.hpp>
#include <boost/container/pmr/global_resource.hpp>
#include <boost/core/lightweight_test.hpp>

#include "derived_from_memory_resource.hpp"
#include "propagation_test_allocator.hpp"

using namespace boost::container::pmr;
using namespace boost::container;

void test_default_constructor()
{
   polymorphic_allocator<int> a;
   BOOST_TEST(a.resource() == get_default_resource());
}

void test_resource_constructor()
{
   polymorphic_allocator<int> a(0);
   BOOST_TEST(a.resource() == get_default_resource());

   derived_from_memory_resource d;
   polymorphic_allocator<int> b(&d);
   BOOST_TEST(&d == b.resource());
}

void test_copy_constructor()
{
   derived_from_memory_resource d;
   polymorphic_allocator<int> b(&d);
   polymorphic_allocator<int> c(b);
   BOOST_TEST(b.resource() == c.resource());
}

void test_copy_assignment()
{
   derived_from_memory_resource d;
   polymorphic_allocator<int> b(&d);
   polymorphic_allocator<int> c;
   BOOST_TEST(c.resource() == get_default_resource());
   c = b;
   BOOST_TEST(c.resource() == b.resource());
}

void test_allocate()
{
   int dummy;
   derived_from_memory_resource d;
   polymorphic_allocator<int> p(&d);
   d.reset();
   d.do_allocate_return = &dummy;
   p.allocate(2);
   BOOST_TEST(d.do_allocate_called == true);
   BOOST_TEST(d.do_allocate_return == &dummy);
   //It shall allocate 2*sizeof(int), alignment_of<int>
   BOOST_TEST(d.do_allocate_bytes == 2*sizeof(int));
   BOOST_TEST(d.do_allocate_alignment == dtl::alignment_of<int>::value);
}

void test_deallocate()
{
   int dummy;
   derived_from_memory_resource d;
   polymorphic_allocator<int> p(&d);
   d.reset();
   p.deallocate(&dummy, 3);
   BOOST_TEST(d.do_deallocate_called == true);
   //It shall deallocate 2*sizeof(int), alignment_of<int>
   BOOST_TEST(d.do_deallocate_p == &dummy);
   BOOST_TEST(d.do_deallocate_bytes == 3*sizeof(int));
   BOOST_TEST(d.do_deallocate_alignment == dtl::alignment_of<int>::value);
}

void test_construct()
{
   //0 arg
   {
      typedef allocator_argument_tester<NotUsesAllocator, 0> value_type;
      value_type value;
      value.~value_type();
      polymorphic_allocator<int> pa;
      pa.construct(&value);
      BOOST_TEST(value.construction_type == NotUsesAllocator);
      BOOST_TEST(value.value == 0);
      value.~value_type();
   }
   {
      typedef allocator_argument_tester<ErasedTypePrefix, 0> value_type;
      value_type value;
      value.~value_type();
      polymorphic_allocator<int> pa;
      pa.construct(&value);
      BOOST_TEST(value.construction_type == ConstructiblePrefix);
      BOOST_TEST(value.value == 0);
      value.~value_type();
   }
   {
      typedef allocator_argument_tester<ErasedTypeSuffix, 0> value_type;
      value_type value;
      value.~value_type();
      polymorphic_allocator<int> pa;
      pa.construct(&value);
      BOOST_TEST(value.construction_type == ConstructibleSuffix);
      BOOST_TEST(value.value == 0);
      value.~value_type();
   }
   //1 arg
   {
      typedef allocator_argument_tester<NotUsesAllocator, 0> value_type;
      value_type value;
      value.~value_type();
      polymorphic_allocator<int> pa;
      pa.construct(&value, 2);
      BOOST_TEST(value.construction_type == NotUsesAllocator);
      BOOST_TEST(value.value == 2);
      value.~value_type();
   }
   {
      typedef allocator_argument_tester<ErasedTypePrefix, 0> value_type;
      value_type value;
      value.~value_type();
      polymorphic_allocator<int> pa;
      pa.construct(&value, 3);
      BOOST_TEST(value.construction_type == ConstructiblePrefix);
      BOOST_TEST(value.value == 3);
      value.~value_type();
   }
   {
      typedef allocator_argument_tester<ErasedTypeSuffix, 0> value_type;
      value_type value;
      value.~value_type();
      polymorphic_allocator<int> pa;
      pa.construct(&value, 4);
      BOOST_TEST(value.construction_type == ConstructibleSuffix);
      BOOST_TEST(value.value == 4);
      value.~value_type();
   }
}

struct char_holder
{
   char m_char;
   ~char_holder()
   {  destructor_called = true;  }
   static bool destructor_called;
};

bool char_holder::destructor_called = false;

void test_destroy()
{
   char_holder ch;
   polymorphic_allocator<int> p;
   BOOST_TEST(char_holder::destructor_called == false);
   p.destroy(&ch);
   BOOST_TEST(char_holder::destructor_called == true);
}

void test_select_on_container_copy_construction()
{
   //select_on_container_copy_construction shall return
   //a default constructed polymorphic_allocator
   //which uses the default resource.
   derived_from_memory_resource d;
   polymorphic_allocator<int> p(&d);
   BOOST_TEST(get_default_resource() == p.select_on_container_copy_construction().resource());
}

void test_resource()
{
   derived_from_memory_resource d;
   polymorphic_allocator<int> p(&d);
   BOOST_TEST(&d == p.resource());
}

int main()
{
   test_default_constructor();
   test_resource_constructor();
   test_copy_constructor();
   test_copy_assignment();
   test_allocate();
   test_deallocate();
   test_construct();
   test_destroy();
   test_select_on_container_copy_construction();
   test_resource();
   return ::boost::report_errors();
}