{{ message }}
forked from boostorg/interprocess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffset_ptr_test.cpp
More file actions
367 lines (311 loc) · 9.42 KB
/
Copy pathoffset_ptr_test.cpp
File metadata and controls
367 lines (311 loc) · 9.42 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2007-2012. 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/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/offset_ptr.hpp>
#include <boost/interprocess/detail/type_traits.hpp>
#include <boost/intrusive/pointer_traits.hpp>
#include <boost/core/lightweight_test.hpp>
using namespace boost::interprocess;
class Base
{};
class Derived
: public Base
{};
class VirtualDerived
: public virtual Base
{};
void test_types_and_conversions()
{
typedef offset_ptr<int> pint_t;
typedef offset_ptr<const int> pcint_t;
typedef offset_ptr<volatile int> pvint_t;
typedef offset_ptr<const volatile int> pcvint_t;
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pint_t::element_type, int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pcint_t::element_type, const int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pvint_t::element_type, volatile int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pcvint_t::element_type, const volatile int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pint_t::value_type, int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pcint_t::value_type, int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pvint_t::value_type, int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<pcvint_t::value_type, int>::value));
int dummy_int = 9;
{ pint_t pint(&dummy_int);
pcint_t pcint(pint);
BOOST_TEST(pcint.get() == &dummy_int);
}
{ pint_t pint(&dummy_int);
pvint_t pvint(pint);
BOOST_TEST(pvint.get() == &dummy_int);
}
{ pint_t pint(&dummy_int);
pcvint_t pcvint(pint);
BOOST_TEST(pcvint.get() == &dummy_int);
}
{ pcint_t pcint(&dummy_int);
pcvint_t pcvint(pcint);
BOOST_TEST(pcvint.get() == &dummy_int);
}
{ pvint_t pvint(&dummy_int);
pcvint_t pcvint(pvint);
BOOST_TEST(pcvint.get() == &dummy_int);
}
pint_t pint(0);
pcint_t pcint(0);
pvint_t pvint(0);
pcvint_t pcvint(0);
{
pint_t pint2 = 0;
pcint_t pcint2 = 0;
pvint_t pvint2 = 0;
pcvint_t pcvint2 = 0;
(void)pint2;
(void)pcint2;
(void)pvint2;
(void)pcvint2;
}
{
pint_t pint2 = op_nullptr_t();
pcint_t pcint2 = op_nullptr_t();
pvint_t pvint2 = op_nullptr_t();
pcvint_t pcvint2 = op_nullptr_t();
(void)pint2;
(void)pcint2;
(void)pvint2;
(void)pcvint2;
}
{
pint_t pint2((op_nullptr_t()));
pcint_t pcint2((op_nullptr_t()));
pvint_t pvint2((op_nullptr_t()));
pcvint_t pcvint2((op_nullptr_t()));
(void)pint2;
(void)pcint2;
(void)pvint2;
(void)pcvint2;
}
pint = &dummy_int;
pcint = &dummy_int;
pvint = &dummy_int;
pcvint = &dummy_int;
{ pcint = pint;
BOOST_TEST(pcint.get() == &dummy_int);
}
{ pvint = pint;
BOOST_TEST(pvint.get() == &dummy_int);
}
{ pcvint = pint;
BOOST_TEST(pcvint.get() == &dummy_int);
}
{ pcvint = pcint;
BOOST_TEST(pcvint.get() == &dummy_int);
}
{ pcvint = pvint;
BOOST_TEST(pcvint.get() == &dummy_int);
}
BOOST_TEST(pint);
pint = 0;
BOOST_TEST(!pint);
BOOST_TEST(pint == 0);
BOOST_TEST(0 == pint);
pint = &dummy_int;
BOOST_TEST(0 != pint);
pcint = &dummy_int;
BOOST_TEST( (pcint - pint) == 0);
BOOST_TEST( (pint - pcint) == 0);
typedef offset_ptr<void> pvoid_t;
typedef offset_ptr<const void> pcvoid_t;
typedef offset_ptr<volatile void> pvvoid_t;
typedef offset_ptr<const volatile void> pcvvoid_t;
{
pvoid_t pvoid = pint;
pcvoid_t pcvoid = pcint;
pvvoid_t pvvoid = pvint;
pcvvoid_t pcvvoid = pcvint;
(void)pvoid;
(void)pcvoid;
(void)pvvoid;
(void)pcvvoid;
}
{
pvoid_t pvoid(pint);
pcvoid_t pcvoid(pcint);
pvvoid_t pvvoid(pvint);
pcvvoid_t pcvvoid(pcvint);
(void)pvoid;
(void)pcvoid;
(void)pvvoid;
(void)pcvvoid;
}
{
pvoid_t pvoid;
pcvoid_t pcvoid;
pvvoid_t pvvoid;
pcvvoid_t pcvvoid;
pvoid = pint;
pcvoid = pcint;
pvvoid = pvint;
pcvvoid = pcvint;
(void)pvoid;
(void)pcvoid;
(void)pvvoid;
(void)pcvvoid;
}
}
template<class BasePtr, class DerivedPtr>
void test_base_derived_impl()
{
typename DerivedPtr::element_type d;
DerivedPtr pderi(&d);
{ BasePtr pbase2 = pderi; (void)pbase2; }
BasePtr pbase(pderi);
pbase = pderi;
BOOST_TEST(pbase == pderi);
BOOST_TEST(!(pbase != pderi));
BOOST_TEST((pbase - pderi) == 0);
BOOST_TEST(!(pbase < pderi));
BOOST_TEST(!(pbase > pderi));
BOOST_TEST(pbase <= pderi);
BOOST_TEST((pbase >= pderi));
}
void test_base_derived()
{
typedef offset_ptr<Base> pbase_t;
typedef offset_ptr<const Base> pcbas_t;
typedef offset_ptr<Derived> pderi_t;
typedef offset_ptr<VirtualDerived> pvder_t;
test_base_derived_impl<pbase_t, pderi_t>();
test_base_derived_impl<pbase_t, pvder_t>();
test_base_derived_impl<pcbas_t, pderi_t>();
test_base_derived_impl<pcbas_t, pvder_t>();
}
void test_arithmetic()
{
typedef offset_ptr<int> pint_t;
const int NumValues = 5;
int values[NumValues];
//Initialize p
pint_t p = values;
BOOST_TEST(p.get() == values);
//Initialize p + NumValues
pint_t pe = &values[NumValues];
BOOST_TEST(pe != p);
BOOST_TEST(pe.get() == &values[NumValues]);
//ptr - ptr
BOOST_TEST((pe - p) == NumValues);
//ptr - integer
BOOST_TEST((pe - NumValues) == p);
//ptr + integer
BOOST_TEST((p + NumValues) == pe);
//integer + ptr
BOOST_TEST((NumValues + p) == pe);
//indexing
BOOST_TEST(pint_t(&p[NumValues]) == pe);
BOOST_TEST(pint_t(&pe[-NumValues]) == p);
//ptr -= integer
pint_t p0 = pe;
p0-= NumValues;
BOOST_TEST(p == p0);
//ptr += integer
pint_t penew = p0;
penew += NumValues;
BOOST_TEST(penew == pe);
//++ptr
penew = p0;
for(int j = 0; j != NumValues; ++j, ++penew);
BOOST_TEST(penew == pe);
//--ptr
p0 = pe;
for(int j = 0; j != NumValues; ++j, --p0);
BOOST_TEST(p == p0);
//ptr++
penew = p0;
for(int j = 0; j != NumValues; ++j){
pint_t p_new_copy = penew;
BOOST_TEST(p_new_copy == penew++);
}
//ptr--
p0 = pe;
for(int j = 0; j != NumValues; ++j){
pint_t p0_copy = p0;
BOOST_TEST(p0_copy == p0--);
}
}
void test_comparison()
{
typedef offset_ptr<int> pint_t;
const int NumValues = 5;
int values[NumValues];
//Initialize p
pint_t p = values;
BOOST_TEST(p.get() == values);
//Initialize p + NumValues
pint_t pe = &values[NumValues];
BOOST_TEST(pe != p);
BOOST_TEST(pe.get() == &values[NumValues]);
//operators
BOOST_TEST(p != pe);
BOOST_TEST(p == p);
BOOST_TEST((p < pe));
BOOST_TEST((p <= pe));
BOOST_TEST((pe > p));
BOOST_TEST((pe >= p));
}
bool test_pointer_traits()
{
typedef offset_ptr<int> OInt;
typedef boost::intrusive::pointer_traits< OInt > PTOInt;
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<PTOInt::element_type, int>::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<PTOInt::pointer, OInt >::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<PTOInt::difference_type, OInt::difference_type >::value));
BOOST_INTERPROCESS_STATIC_ASSERT((ipcdetail::is_same<PTOInt::rebind_pointer<double>::type, offset_ptr<double> >::value));
int dummy;
OInt oi(&dummy);
if(boost::intrusive::pointer_traits<OInt>::pointer_to(dummy) != oi){
return false;
}
return true;
}
struct node
{
offset_ptr<node> next;
};
void test_pointer_plus_bits()
{
BOOST_INTERPROCESS_STATIC_ASSERT((boost::intrusive::max_pointer_plus_bits< offset_ptr<void>, boost::move_detail::alignment_of<node>::value >::value >= 1U));
typedef boost::intrusive::pointer_plus_bits< offset_ptr<node>, 1u > ptr_plus_bits;
node n, n2;
offset_ptr<node> pnode(&n);
BOOST_TEST(ptr_plus_bits::get_pointer(pnode) == &n);
BOOST_TEST(0 == ptr_plus_bits::get_bits(pnode));
ptr_plus_bits::set_bits(pnode, 1u);
BOOST_TEST(1 == ptr_plus_bits::get_bits(pnode));
BOOST_TEST(ptr_plus_bits::get_pointer(pnode) == &n);
ptr_plus_bits::set_pointer(pnode, &n2);
BOOST_TEST(ptr_plus_bits::get_pointer(pnode) == &n2);
BOOST_TEST(1 == ptr_plus_bits::get_bits(pnode));
ptr_plus_bits::set_bits(pnode, 0u);
BOOST_TEST(0 == ptr_plus_bits::get_bits(pnode));
BOOST_TEST(ptr_plus_bits::get_pointer(pnode) == &n2);
ptr_plus_bits::set_pointer(pnode, offset_ptr<node>());
BOOST_TEST(ptr_plus_bits::get_pointer(pnode) ==0);
BOOST_TEST(0 == ptr_plus_bits::get_bits(pnode));
ptr_plus_bits::set_bits(pnode, 1u);
BOOST_TEST(1 == ptr_plus_bits::get_bits(pnode));
BOOST_TEST(ptr_plus_bits::get_pointer(pnode) == 0);
}
int main()
{
test_types_and_conversions();
test_base_derived();
test_arithmetic();
test_comparison();
test_pointer_traits();
test_pointer_plus_bits();
return ::boost::report_errors();
}
You can’t perform that action at this time.
