summaryrefslogtreecommitdiff
path: root/o3tl
diff options
context:
space:
mode:
authorIvo Hinkelmann <ihi@openoffice.org>2007-08-20 14:50:57 +0000
committerIvo Hinkelmann <ihi@openoffice.org>2007-08-20 14:50:57 +0000
commit1076f643bc03de5164f3508dcf6572cfdec8422f (patch)
tree431304088a7b023dddc46f0e0703b0fc1583aec1 /o3tl
parent3665e496c9a68fe6bba282c3a41f9066d3ada3d0 (diff)
INTEGRATION: CWS np1 (1.1.2); FILE ADDED
2007/07/16 08:56:40 np 1.1.2.2: #i76768# adding safe_bool to heap_ptr; removing unnecessary stuff from test files. 2007/04/27 16:24:18 np 1.1.2.1: #76768#
Diffstat (limited to 'o3tl')
-rw-r--r--o3tl/qa/test-heap_ptr.cxx171
1 files changed, 171 insertions, 0 deletions
diff --git a/o3tl/qa/test-heap_ptr.cxx b/o3tl/qa/test-heap_ptr.cxx
new file mode 100644
index 000000000000..0bf34bd3846c
--- /dev/null
+++ b/o3tl/qa/test-heap_ptr.cxx
@@ -0,0 +1,171 @@
+/*************************************************************************
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: test-heap_ptr.cxx,v $
+ *
+ * $Revision: 1.2 $
+ *
+ * last change: $Author: ihi $ $Date: 2007-08-20 15:50:57 $
+ *
+ * The Contents of this file are made available subject to
+ * the terms of GNU Lesser General Public License Version 2.1.
+ *
+ *
+ * GNU Lesser General Public License Version 2.1
+ * =============================================
+ * Copyright 2005 by Sun Microsystems, Inc.
+ * 901 San Antonio Road, Palo Alto, CA 94303, USA
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ ************************************************************************/
+
+#include <cppunit/simpleheader.hxx>
+
+#include <o3tl/heap_ptr.hxx>
+
+
+
+
+using o3tl::heap_ptr;
+
+
+class Help
+{
+ public:
+ explicit Help(
+ int i_n )
+ : n(i_n) { ++nInstanceCount_; }
+ ~Help() { --nInstanceCount_; }
+ int Value() const { return n; }
+ static int InstanceCount_() { return nInstanceCount_; }
+
+ private:
+ int n;
+ static int nInstanceCount_;
+};
+int Help::nInstanceCount_ = 0;
+
+
+class heap_ptr_test : public CppUnit::TestFixture
+{
+ public:
+ void global()
+ {
+ // Construction
+ heap_ptr<Help>
+ t_empty;
+ const heap_ptr<Help>
+ t0( new Help(7000) );
+ heap_ptr<Help>
+ t1( new Help(10) );
+ heap_ptr<Help>
+ t2( new Help(20) );
+
+ int nHelpCount = 3;
+
+ CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty.is());
+ CPPUNIT_ASSERT_MESSAGE("ctor2", t0.is());
+ CPPUNIT_ASSERT_MESSAGE("ctor3", (*t0).Value() == 7000 );
+ CPPUNIT_ASSERT_MESSAGE("ctor4", t0->Value() == 7000 );
+ CPPUNIT_ASSERT_MESSAGE("ctor5", t0.get() == t0.operator->() );
+ CPPUNIT_ASSERT_MESSAGE("ctor6", t0.get() == &(*t0) );
+
+ CPPUNIT_ASSERT_MESSAGE("ctor7", t1.is());
+ CPPUNIT_ASSERT_MESSAGE("ctor8", (*t1).Value() == 10 );
+ CPPUNIT_ASSERT_MESSAGE("ctor9", t1->Value() == 10 );
+ CPPUNIT_ASSERT_MESSAGE("ctor10", t1.get() == t1.operator->() );
+ CPPUNIT_ASSERT_MESSAGE("ctor11", t1.get() == &(*t1) );
+
+ CPPUNIT_ASSERT_MESSAGE("ctor12", t2.operator*().Value() == 20);
+ CPPUNIT_ASSERT_MESSAGE("ctor13", Help::InstanceCount_() == nHelpCount);
+
+
+ // Operator safe_bool() / bool()
+ CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty);
+ CPPUNIT_ASSERT_MESSAGE("bool2", t0);
+ CPPUNIT_ASSERT_MESSAGE("bool3", t1.is() == static_cast<bool>(t1));
+
+
+ // Assignment, reset() and release()
+ // Release
+ Help * hp = t1.release();
+ CPPUNIT_ASSERT_MESSAGE("release1", ! t1.is() );
+ CPPUNIT_ASSERT_MESSAGE("release2", t1.get() == 0 );
+ CPPUNIT_ASSERT_MESSAGE("release3", t1.operator->() == 0 );
+ CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount);
+
+ // operator=()
+ t_empty = hp;
+ CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() );
+ CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount);
+
+ t1 = t_empty.release();
+ CPPUNIT_ASSERT_MESSAGE("assign3", t1.is() );
+ CPPUNIT_ASSERT_MESSAGE("assign4", ! t_empty.is() );
+ CPPUNIT_ASSERT_MESSAGE("assign5", Help::InstanceCount_() == nHelpCount);
+
+ // reset()
+ hp = new Help(30);
+ ++nHelpCount;
+
+ t_empty.reset(hp);
+ CPPUNIT_ASSERT_MESSAGE("reset1", Help::InstanceCount_() == nHelpCount);
+ CPPUNIT_ASSERT_MESSAGE("reset2", t_empty.is() );
+ CPPUNIT_ASSERT_MESSAGE("reset3", t_empty.get() == hp );
+
+ // Ignore second assignment
+ t_empty = hp;
+ CPPUNIT_ASSERT_MESSAGE("selfassign1", Help::InstanceCount_() == nHelpCount);
+ CPPUNIT_ASSERT_MESSAGE("selfassign2", t_empty.is() );
+ CPPUNIT_ASSERT_MESSAGE("selfassign3", t_empty.get() == hp );
+
+ t_empty.reset(0);
+ hp = 0;
+ --nHelpCount;
+ CPPUNIT_ASSERT_MESSAGE("reset4", ! t_empty.is() );
+ CPPUNIT_ASSERT_MESSAGE("reset5", Help::InstanceCount_() == nHelpCount);
+
+
+ // swap
+ t1.swap(t2);
+ CPPUNIT_ASSERT_MESSAGE("swap1", t1->Value() == 20 );
+ CPPUNIT_ASSERT_MESSAGE("swap2", t2->Value() == 10 );
+ CPPUNIT_ASSERT_MESSAGE("swap3", Help::InstanceCount_() == nHelpCount);
+
+ o3tl::swap(t1,t2);
+ CPPUNIT_ASSERT_MESSAGE("swap4", t1->Value() == 10 );
+ CPPUNIT_ASSERT_MESSAGE("swap5", t2->Value() == 20 );
+ CPPUNIT_ASSERT_MESSAGE("swap6", Help::InstanceCount_() == nHelpCount);
+
+ // RAII
+ {
+ heap_ptr<Help>
+ t_raii( new Help(55) );
+ CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1);
+ }
+ CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount);
+ }
+
+
+ // These macros are needed by auto register mechanism.
+ CPPUNIT_TEST_SUITE(heap_ptr_test);
+ CPPUNIT_TEST(global);
+ CPPUNIT_TEST_SUITE_END();
+}; // class heap_ptr_test
+
+// -----------------------------------------------------------------------------
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(heap_ptr_test, "o3tltests");