1. //
  2. // AutoPtrTest.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/testsuite/src/AutoPtrTest.cpp#1 $
  5. //
  6. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  7. // and Contributors.
  8. //
  9. // Permission is hereby granted, free of charge, to any person or organization
  10. // obtaining a copy of the software and accompanying documentation covered by
  11. // this license (the "Software") to use, reproduce, display, distribute,
  12. // execute, and transmit the Software, and to prepare derivative works of the
  13. // Software, and to permit third-parties to whom the Software is furnished to
  14. // do so, all subject to the following:
  15. //
  16. // The copyright notices in the Software and this entire statement, including
  17. // the above license grant, this restriction and the following disclaimer,
  18. // must be included in all copies of the Software, in whole or in part, and
  19. // all derivative works of the Software, unless such copies or derivative
  20. // works are solely in the form of machine-executable object code generated by
  21. // a source language processor.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  26. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  27. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  28. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. //
  31.  
  32.  
  33. #include "AutoPtrTest.h"
  34. #include "CppUnit/TestCaller.h"
  35. #include "CppUnit/TestSuite.h"
  36. #include "Poco/AutoPtr.h"
  37. #include "Poco/Exception.h"
  38.  
  39.  
  40. using Poco::AutoPtr;
  41. using Poco::NullPointerException;
  42.  
  43.  
  44. namespace
  45. {
  46. class TestObj
  47. {
  48. public:
  49. TestObj(): _rc(1), _data(0)
  50. {
  51. ++_count;
  52. }
  53.  
  54. void duplicate() const
  55. {
  56. ++_rc;
  57. }
  58.  
  59. void release() const
  60. {
  61. if (--_rc == 0)
  62. delete this;
  63. }
  64.  
  65. int rc() const
  66. {
  67. return _rc;
  68. }
  69.  
  70. static int count()
  71. {
  72. return _count;
  73. }
  74. void setData(int x)
  75. {
  76. _data = x;
  77. }
  78. int getData() const
  79. {
  80. return _data;
  81. }
  82. protected:
  83. virtual ~TestObj()
  84. {
  85. --_count;
  86. }
  87.  
  88. private:
  89. mutable int _rc;
  90. static int _count;
  91. int _data;
  92. };
  93.  
  94. int TestObj::_count = 0;
  95. }
  96.  
  97.  
  98. AutoPtrTest::AutoPtrTest(const std::string& name): CppUnit::TestCase(name)
  99. {
  100. }
  101.  
  102.  
  103. AutoPtrTest::~AutoPtrTest()
  104. {
  105. }
  106.  
  107.  
  108. void AutoPtrTest::testAutoPtr()
  109. {
  110. {
  111. AutoPtr<TestObj> ptr = new TestObj;
  112. assert (ptr->rc() == 1);
  113. AutoPtr<TestObj> ptr2 = ptr;
  114. assert (ptr->rc() == 2);
  115. ptr2 = new TestObj;
  116. assert (ptr->rc() == 1);
  117. AutoPtr<TestObj> ptr3;
  118. ptr3 = ptr2;
  119. assert (ptr2->rc() == 2);
  120. ptr3 = new TestObj;
  121. assert (ptr2->rc() == 1);
  122. ptr3 = ptr2;
  123. assert (ptr2->rc() == 2);
  124. assert (TestObj::count() > 0);
  125. }
  126. assert (TestObj::count() == 0);
  127. }
  128.  
  129. void AutoPtrTest::testOps()
  130. {
  131. AutoPtr<TestObj> ptr1;
  132. assertNull(ptr1.get());
  133. TestObj* pTO1 = new TestObj;
  134. TestObj* pTO2 = new TestObj;
  135. if (pTO2 < pTO1)
  136. {
  137. TestObj* pTmp = pTO1;
  138. pTO1 = pTO2;
  139. pTO2 = pTmp;
  140. }
  141. assert (pTO1 < pTO2);
  142. ptr1 = pTO1;
  143. AutoPtr<TestObj> ptr2 = pTO2;
  144. AutoPtr<TestObj> ptr3 = ptr1;
  145. AutoPtr<TestObj> ptr4;
  146. assert (ptr1.get() == pTO1);
  147. assert (ptr1 == pTO1);
  148. assert (ptr2.get() == pTO2);
  149. assert (ptr2 == pTO2);
  150. assert (ptr3.get() == pTO1);
  151. assert (ptr3 == pTO1);
  152.  
  153. assert (ptr1 == pTO1);
  154. assert (ptr1 != pTO2);
  155. assert (ptr1 < pTO2);
  156. assert (ptr1 <= pTO2);
  157. assert (ptr2 > pTO1);
  158. assert (ptr2 >= pTO1);
  159.  
  160. assert (ptr1 == ptr3);
  161. assert (ptr1 != ptr2);
  162. assert (ptr1 < ptr2);
  163. assert (ptr1 <= ptr2);
  164. assert (ptr2 > ptr1);
  165. assert (ptr2 >= ptr1);
  166.  
  167. ptr1 = pTO1;
  168. ptr2 = pTO2;
  169. ptr1.swap(ptr2);
  170. assert (ptr2.get() == pTO1);
  171. assert (ptr1.get() == pTO2);
  172.  
  173. try
  174. {
  175. assert (ptr4->rc() > 0);
  176. fail ("must throw NullPointerException");
  177. }
  178. catch (NullPointerException&)
  179. {
  180. }
  181.  
  182. assert (!(ptr4 == ptr1));
  183. assert (!(ptr4 == ptr2));
  184. assert (ptr4 != ptr1);
  185. assert (ptr4 != ptr2);
  186.  
  187. ptr4 = ptr2;
  188. assert (ptr4 == ptr2);
  189. assert (!(ptr4 != ptr2));
  190.  
  191. assert (!(!ptr1));
  192. ptr1 = 0;
  193. assert (!ptr1);
  194.  
  195. assert (pTO1 == ptr1);
  196. assert (pTO2 != ptr1);
  197. assert (pTO2 > ptr1);
  198. assert (pTO2 >= ptr1);
  199. assert (pTO1 < ptr2);
  200. assert (pTO1 <= ptr2);
  201.  
  202. assert (0 != ptr1);
  203.  
  204. //AutoPtr<const TestObj> cptr1 = ptr1;
  205. //AutoPtr<const
  206. }
  207.  
  208. namespace {
  209. template <class T>
  210. bool isConstPtr(T* p)
  211. {
  212. return false;
  213. }
  214. template <class T>
  215. bool isConstPtr(const T* p)
  216. {
  217. return true;
  218. }
  219. }
  220.  
  221. void AutoPtrTest::testConst()
  222. {
  223. const TestObj* cp1 = new TestObj;
  224. AutoPtr<const TestObj> csp1 = cp1; // csp1 is similar to "const TestObject*"
  225. assert (isConstPtr(csp1.get()));
  226.  
  227. // It is only the object that is points to that is const,
  228. // not what object it points to.
  229. csp1 = new TestObj();
  230.  
  231. const AutoPtr<TestObj> spc1 = new TestObj; // spc1 is similar to "TestObj* const"
  232.  
  233. // should compile because we do reassign spc1 to some other object
  234. spc1->setData(5);
  235. }
  236.  
  237. namespace
  238. {
  239. class DerivedTestObj : public TestObj
  240. {
  241. public:
  242. };
  243. }
  244.  
  245. void AutoPtrTest::testInheritance()
  246. {
  247. AutoPtr<DerivedTestObj> d(new DerivedTestObj);
  248.  
  249. if (AutoPtr<const TestObj> b = d) {
  250. assert (b == d);
  251. }
  252. else
  253. {
  254. assert(0);
  255. }
  256. }
  257.  
  258. void AutoPtrTest::setUp()
  259. {
  260. }
  261.  
  262.  
  263. void AutoPtrTest::tearDown()
  264. {
  265. }
  266.  
  267.  
  268. CppUnit::Test* AutoPtrTest::suite()
  269. {
  270. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AutoPtrTest");
  271.  
  272. CppUnit_addTest(pSuite, AutoPtrTest, testAutoPtr);
  273. CppUnit_addTest(pSuite, AutoPtrTest, testOps);
  274. CppUnit_addTest(pSuite, AutoPtrTest, testConst);
  275. CppUnit_addTest(pSuite, AutoPtrTest, testInheritance);
  276.  
  277. return pSuite;
  278. }