Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
tests
ptr_test.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
tag: Peter Soetens Mon Jan 10 15:59:51 CET 2005 buffers_test.cpp
3
4
buffers_test.cpp - description
5
-------------------
6
begin : Mon January 10 2005
7
copyright : (C) 2005 Peter Soetens
8
email : peter.soetens@mech.kuleuven.ac.be
9
10
***************************************************************************
11
* *
12
* This program is free software; you can redistribute it and/or modify *
13
* it under the terms of the GNU General Public License as published by *
14
* the Free Software Foundation; either version 2 of the License, or *
15
* (at your option) any later version. *
16
* *
17
***************************************************************************/
18
19
20
#include "
unit.hpp
"
21
22
#include "
ptr_test.hpp
"
23
24
using namespace
std
;
25
26
using namespace
RTT
;
27
using namespace
RTT::extras
;
28
29
struct
MyStruct
30
{
31
static
bool
deleted
;
32
static
bool
copied
;
33
34
MyStruct
() {}
35
MyStruct
(
MyStruct
const
& s) { copied =
true
; }
36
~MyStruct
() { deleted =
true
; }
37
MyStruct
const
*
get_this
()
const
{
return
this
; }
38
};
39
bool
MyStruct::deleted
=
false
;
40
bool
MyStruct::copied
=
false
;
41
42
BOOST_FIXTURE_TEST_SUITE
(PtrTestSuite,
PtrTest
)
43
44
BOOST_AUTO_TEST_CASE
( testReadOnly )
45
{
46
MyStruct::deleted
=
false
;
47
MyStruct
* value =
new
MyStruct
;
48
{
49
// This takes ownership
50
ReadOnlyPointer<MyStruct>
ptr1(value);
51
52
BOOST_CHECK(ptr1.
valid
());
53
BOOST_CHECK(&(*ptr1) == value);
54
BOOST_CHECK(ptr1->get_this() == value);
55
56
// Try sharing the ownership and verify the object is not deleted
57
{
ReadOnlyPointer<MyStruct>
ptr2(ptr1); }
58
BOOST_CHECK(!
MyStruct::deleted
);
59
}
60
// No more ReadOnlyPointer on the object, it should be deleted now
61
BOOST_CHECK(
MyStruct::deleted
);
62
}
63
64
BOOST_AUTO_TEST_CASE
( testPromotion )
65
{
66
MyStruct::deleted
=
false
;
67
MyStruct::copied
=
false
;
68
MyStruct
* value =
new
MyStruct
;
69
MyStruct
* write;
70
71
// First test: no copy needed
72
MyStruct::deleted
=
false
;
73
MyStruct::copied
=
false
;
74
{
75
// This takes ownership
76
ReadOnlyPointer<MyStruct>
ptr1(value);
77
// This should remove the ownership from ptr1, invalidate it and return
78
// the original value
79
write = ptr1.
write_access
();
80
81
BOOST_CHECK(!ptr1.
valid
());
82
BOOST_CHECK(write == value);
83
}
84
BOOST_CHECK(!
MyStruct::copied
);
85
BOOST_CHECK(!
MyStruct::deleted
);
86
87
// Second test: copy needed
88
MyStruct::deleted
=
false
;
89
MyStruct::copied
=
false
;
90
{
91
// This takes ownership
92
ReadOnlyPointer<MyStruct>
ptr1(value);
93
// And this makes the ownership shared
94
ReadOnlyPointer<MyStruct>
ptr2(ptr1);
95
// This should remove the ownership from ptr1 and invalidate it. But it
96
// should return a copy.
97
write = ptr1.
write_access
();
98
99
//BOOST_CHECK(!ptr1.valid());
100
BOOST_CHECK(write != value);
101
102
BOOST_CHECK(
MyStruct::copied
);
103
BOOST_CHECK(!
MyStruct::deleted
);
104
}
// and the original value should be deleted because of ptr2
105
BOOST_CHECK(
MyStruct::deleted
);
106
107
delete
write;
108
}
109
110
BOOST_AUTO_TEST_SUITE_END
()
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
Definition:
unit_test_suite.hpp:53
MyStruct
Definition:
ptr_test.cpp:29
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testReadOnly)
Definition:
ptr_test.cpp:44
MyStruct::get_this
MyStruct const * get_this() const
Definition:
ptr_test.cpp:37
MyStruct::deleted
static bool deleted
Definition:
ptr_test.cpp:31
MyStruct::copied
static bool copied
Definition:
ptr_test.cpp:32
BOOST_AUTO_TEST_SUITE_END
#define BOOST_AUTO_TEST_SUITE_END()
Definition:
unit_test_suite.hpp:62
std
Definition:
mystd.hpp:163
RTT::extras::ReadOnlyPointer::valid
bool valid() const
Definition:
ReadOnlyPointer.hpp:112
RTT::extras::ReadOnlyPointer::write_access
T * write_access()
Definition:
ReadOnlyPointer.hpp:212
unit.hpp
MyStruct::MyStruct
MyStruct(MyStruct const &s)
Definition:
ptr_test.cpp:35
PtrTest
Definition:
ptr_test.hpp:25
ptr_test.hpp
MyStruct::~MyStruct
~MyStruct()
Definition:
ptr_test.cpp:36
RTT::extras
Definition:
FileDescriptorActivity.hpp:46
RTT::extras::ReadOnlyPointer
Definition:
ReadOnlyPointer.hpp:98
RTT
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition:
Activity.cpp:53
MyStruct::MyStruct
MyStruct()
Definition:
ptr_test.cpp:34
rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:34