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 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
BOOST_AUTO_TEST_CASE(testReadOnly)
Definition: ptr_test.cpp:44
MyStruct const * get_this() const
Definition: ptr_test.cpp:37
static bool deleted
Definition: ptr_test.cpp:31
static bool copied
Definition: ptr_test.cpp:32
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
MyStruct(MyStruct const &s)
Definition: ptr_test.cpp:35
~MyStruct()
Definition: ptr_test.cpp:36
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
MyStruct()
Definition: ptr_test.cpp:34


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:27