Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AutoPtrTest.cpp
- //
- // $Id: //poco/1.4/Foundation/testsuite/src/AutoPtrTest.cpp#1 $
- //
- // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // Permission is hereby granted, free of charge, to any person or organization
- // obtaining a copy of the software and accompanying documentation covered by
- // this license (the "Software") to use, reproduce, display, distribute,
- // execute, and transmit the Software, and to prepare derivative works of the
- // Software, and to permit third-parties to whom the Software is furnished to
- // do so, all subject to the following:
- //
- // The copyright notices in the Software and this entire statement, including
- // the above license grant, this restriction and the following disclaimer,
- // must be included in all copies of the Software, in whole or in part, and
- // all derivative works of the Software, unless such copies or derivative
- // works are solely in the form of machine-executable object code generated by
- // a source language processor.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
- // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
- // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
- // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- // DEALINGS IN THE SOFTWARE.
- //
- #include "AutoPtrTest.h"
- #include "CppUnit/TestCaller.h"
- #include "CppUnit/TestSuite.h"
- #include "Poco/AutoPtr.h"
- #include "Poco/Exception.h"
- using Poco::AutoPtr;
- using Poco::NullPointerException;
- namespace
- {
- class TestObj
- {
- public:
- TestObj(): _rc(1), _data(0)
- {
- ++_count;
- }
- void duplicate() const
- {
- ++_rc;
- }
- void release() const
- {
- if (--_rc == 0)
- delete this;
- }
- int rc() const
- {
- return _rc;
- }
- static int count()
- {
- return _count;
- }
- void setData(int x)
- {
- _data = x;
- }
- int getData() const
- {
- return _data;
- }
- protected:
- virtual ~TestObj()
- {
- --_count;
- }
- private:
- mutable int _rc;
- static int _count;
- int _data;
- };
- int TestObj::_count = 0;
- }
- AutoPtrTest::AutoPtrTest(const std::string& name): CppUnit::TestCase(name)
- {
- }
- AutoPtrTest::~AutoPtrTest()
- {
- }
- void AutoPtrTest::testAutoPtr()
- {
- {
- AutoPtr<TestObj> ptr = new TestObj;
- assert (ptr->rc() == 1);
- AutoPtr<TestObj> ptr2 = ptr;
- assert (ptr->rc() == 2);
- ptr2 = new TestObj;
- assert (ptr->rc() == 1);
- AutoPtr<TestObj> ptr3;
- ptr3 = ptr2;
- assert (ptr2->rc() == 2);
- ptr3 = new TestObj;
- assert (ptr2->rc() == 1);
- ptr3 = ptr2;
- assert (ptr2->rc() == 2);
- assert (TestObj::count() > 0);
- }
- assert (TestObj::count() == 0);
- }
- void AutoPtrTest::testOps()
- {
- AutoPtr<TestObj> ptr1;
- assertNull(ptr1.get());
- TestObj* pTO1 = new TestObj;
- TestObj* pTO2 = new TestObj;
- if (pTO2 < pTO1)
- {
- TestObj* pTmp = pTO1;
- pTO1 = pTO2;
- pTO2 = pTmp;
- }
- assert (pTO1 < pTO2);
- ptr1 = pTO1;
- AutoPtr<TestObj> ptr2 = pTO2;
- AutoPtr<TestObj> ptr3 = ptr1;
- AutoPtr<TestObj> ptr4;
- assert (ptr1.get() == pTO1);
- assert (ptr1 == pTO1);
- assert (ptr2.get() == pTO2);
- assert (ptr2 == pTO2);
- assert (ptr3.get() == pTO1);
- assert (ptr3 == pTO1);
- assert (ptr1 == pTO1);
- assert (ptr1 != pTO2);
- assert (ptr1 < pTO2);
- assert (ptr1 <= pTO2);
- assert (ptr2 > pTO1);
- assert (ptr2 >= pTO1);
- assert (ptr1 == ptr3);
- assert (ptr1 != ptr2);
- assert (ptr1 < ptr2);
- assert (ptr1 <= ptr2);
- assert (ptr2 > ptr1);
- assert (ptr2 >= ptr1);
- ptr1 = pTO1;
- ptr2 = pTO2;
- ptr1.swap(ptr2);
- assert (ptr2.get() == pTO1);
- assert (ptr1.get() == pTO2);
- try
- {
- assert (ptr4->rc() > 0);
- fail ("must throw NullPointerException");
- }
- catch (NullPointerException&)
- {
- }
- assert (!(ptr4 == ptr1));
- assert (!(ptr4 == ptr2));
- assert (ptr4 != ptr1);
- assert (ptr4 != ptr2);
- ptr4 = ptr2;
- assert (ptr4 == ptr2);
- assert (!(ptr4 != ptr2));
- assert (!(!ptr1));
- ptr1 = 0;
- assert (!ptr1);
- assert (pTO1 == ptr1);
- assert (pTO2 != ptr1);
- assert (pTO2 > ptr1);
- assert (pTO2 >= ptr1);
- assert (pTO1 < ptr2);
- assert (pTO1 <= ptr2);
- assert (0 != ptr1);
- //AutoPtr<const TestObj> cptr1 = ptr1;
- //AutoPtr<const
- }
- namespace {
- template <class T>
- bool isConstPtr(T* p)
- {
- return false;
- }
- template <class T>
- bool isConstPtr(const T* p)
- {
- return true;
- }
- }
- void AutoPtrTest::testConst()
- {
- const TestObj* cp1 = new TestObj;
- AutoPtr<const TestObj> csp1 = cp1; // csp1 is similar to "const TestObject*"
- assert (isConstPtr(csp1.get()));
- // It is only the object that is points to that is const,
- // not what object it points to.
- csp1 = new TestObj();
- const AutoPtr<TestObj> spc1 = new TestObj; // spc1 is similar to "TestObj* const"
- // should compile because we do reassign spc1 to some other object
- spc1->setData(5);
- }
- namespace
- {
- class DerivedTestObj : public TestObj
- {
- public:
- };
- }
- void AutoPtrTest::testInheritance()
- {
- AutoPtr<DerivedTestObj> d(new DerivedTestObj);
- if (AutoPtr<const TestObj> b = d) {
- assert (b == d);
- }
- else
- {
- assert(0);
- }
- }
- void AutoPtrTest::setUp()
- {
- }
- void AutoPtrTest::tearDown()
- {
- }
- CppUnit::Test* AutoPtrTest::suite()
- {
- CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AutoPtrTest");
- CppUnit_addTest(pSuite, AutoPtrTest, testAutoPtr);
- CppUnit_addTest(pSuite, AutoPtrTest, testOps);
- CppUnit_addTest(pSuite, AutoPtrTest, testConst);
- CppUnit_addTest(pSuite, AutoPtrTest, testInheritance);
- return pSuite;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement