00001 /*************************************************************************** 00002 tag: Peter Soetens Mon Jan 10 15:59:51 CET 2005 buffers_test.cpp 00003 00004 buffers_test.cpp - description 00005 ------------------- 00006 begin : Mon January 10 2005 00007 copyright : (C) 2005 Peter Soetens 00008 email : peter.soetens@mech.kuleuven.ac.be 00009 00010 *************************************************************************** 00011 * * 00012 * This program is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU General Public License as published by * 00014 * the Free Software Foundation; either version 2 of the License, or * 00015 * (at your option) any later version. * 00016 * * 00017 ***************************************************************************/ 00018 00019 00020 #include "unit.hpp" 00021 00022 #include "ptr_test.hpp" 00023 00024 using namespace std; 00025 00026 using namespace RTT; 00027 using namespace RTT::extras; 00028 00029 struct MyStruct 00030 { 00031 static bool deleted; 00032 static bool copied; 00033 00034 MyStruct() {} 00035 MyStruct(MyStruct const& s) { copied = true; } 00036 ~MyStruct() { deleted = true; } 00037 MyStruct const* get_this() const { return this; } 00038 }; 00039 bool MyStruct::deleted = false; 00040 bool MyStruct::copied = false; 00041 00042 BOOST_FIXTURE_TEST_SUITE(PtrTestSuite, PtrTest) 00043 00044 BOOST_AUTO_TEST_CASE( testReadOnly ) 00045 { 00046 MyStruct::deleted = false; 00047 MyStruct* value = new MyStruct; 00048 { 00049 // This takes ownership 00050 ReadOnlyPointer<MyStruct> ptr1(value); 00051 00052 BOOST_CHECK(ptr1.valid()); 00053 BOOST_CHECK(&(*ptr1) == value); 00054 BOOST_CHECK(ptr1->get_this() == value); 00055 00056 // Try sharing the ownership and verify the object is not deleted 00057 { ReadOnlyPointer<MyStruct> ptr2(ptr1); } 00058 BOOST_CHECK(!MyStruct::deleted); 00059 } 00060 // No more ReadOnlyPointer on the object, it should be deleted now 00061 BOOST_CHECK(MyStruct::deleted); 00062 } 00063 00064 BOOST_AUTO_TEST_CASE( testPromotion ) 00065 { 00066 MyStruct::deleted = false; 00067 MyStruct::copied = false; 00068 MyStruct* value = new MyStruct; 00069 MyStruct* write; 00070 00071 // First test: no copy needed 00072 MyStruct::deleted = false; 00073 MyStruct::copied = false; 00074 { 00075 // This takes ownership 00076 ReadOnlyPointer<MyStruct> ptr1(value); 00077 // This should remove the ownership from ptr1, invalidate it and return 00078 // the original value 00079 write = ptr1.write_access(); 00080 00081 BOOST_CHECK(!ptr1.valid()); 00082 BOOST_CHECK(write == value); 00083 } 00084 BOOST_CHECK(!MyStruct::copied); 00085 BOOST_CHECK(!MyStruct::deleted); 00086 00087 // Second test: copy needed 00088 MyStruct::deleted = false; 00089 MyStruct::copied = false; 00090 { 00091 // This takes ownership 00092 ReadOnlyPointer<MyStruct> ptr1(value); 00093 // And this makes the ownership shared 00094 ReadOnlyPointer<MyStruct> ptr2(ptr1); 00095 // This should remove the ownership from ptr1 and invalidate it. But it 00096 // should return a copy. 00097 write = ptr1.write_access(); 00098 00099 //BOOST_CHECK(!ptr1.valid()); 00100 BOOST_CHECK(write != value); 00101 00102 BOOST_CHECK(MyStruct::copied); 00103 BOOST_CHECK(!MyStruct::deleted); 00104 } // and the original value should be deleted because of ptr2 00105 BOOST_CHECK(MyStruct::deleted); 00106 00107 delete write; 00108 } 00109 00110 BOOST_AUTO_TEST_SUITE_END()