-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathptr_test.cpp
110 lines (89 loc) · 3.38 KB
/
ptr_test.cpp
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
/***************************************************************************
tag: Peter Soetens Mon Jan 10 15:59:51 CET 2005 buffers_test.cpp
buffers_test.cpp - description
-------------------
begin : Mon January 10 2005
copyright : (C) 2005 Peter Soetens
email : [email protected]
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "unit.hpp"
#include "ptr_test.hpp"
using namespace std;
using namespace RTT;
using namespace RTT::extras;
struct MyStruct
{
static bool deleted;
static bool copied;
MyStruct() {}
MyStruct(MyStruct const& s) { copied = true; }
~MyStruct() { deleted = true; }
MyStruct const* get_this() const { return this; }
};
bool MyStruct::deleted = false;
bool MyStruct::copied = false;
BOOST_FIXTURE_TEST_SUITE(PtrTestSuite, PtrTest)
BOOST_AUTO_TEST_CASE( testReadOnly )
{
MyStruct::deleted = false;
MyStruct* value = new MyStruct;
{
// This takes ownership
ReadOnlyPointer<MyStruct> ptr1(value);
BOOST_CHECK(ptr1.valid());
BOOST_CHECK(&(*ptr1) == value);
BOOST_CHECK(ptr1->get_this() == value);
// Try sharing the ownership and verify the object is not deleted
{ ReadOnlyPointer<MyStruct> ptr2(ptr1); }
BOOST_CHECK(!MyStruct::deleted);
}
// No more ReadOnlyPointer on the object, it should be deleted now
BOOST_CHECK(MyStruct::deleted);
}
BOOST_AUTO_TEST_CASE( testPromotion )
{
MyStruct::deleted = false;
MyStruct::copied = false;
MyStruct* value = new MyStruct;
MyStruct* write;
// First test: no copy needed
MyStruct::deleted = false;
MyStruct::copied = false;
{
// This takes ownership
ReadOnlyPointer<MyStruct> ptr1(value);
// This should remove the ownership from ptr1, invalidate it and return
// the original value
write = ptr1.write_access();
BOOST_CHECK(!ptr1.valid());
BOOST_CHECK(write == value);
}
BOOST_CHECK(!MyStruct::copied);
BOOST_CHECK(!MyStruct::deleted);
// Second test: copy needed
MyStruct::deleted = false;
MyStruct::copied = false;
{
// This takes ownership
ReadOnlyPointer<MyStruct> ptr1(value);
// And this makes the ownership shared
ReadOnlyPointer<MyStruct> ptr2(ptr1);
// This should remove the ownership from ptr1 and invalidate it. But it
// should return a copy.
write = ptr1.write_access();
//BOOST_CHECK(!ptr1.valid());
BOOST_CHECK(write != value);
BOOST_CHECK(MyStruct::copied);
BOOST_CHECK(!MyStruct::deleted);
} // and the original value should be deleted because of ptr2
BOOST_CHECK(MyStruct::deleted);
delete write;
}
BOOST_AUTO_TEST_SUITE_END()