update dev300-m58
[ooovba.git] / o3tl / qa / test-heap_ptr.cxx
blob12520b0bc019275693d0f8ff3ea2f3e6c744e70b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test-heap_ptr.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <cppunit/simpleheader.hxx>
33 #include <o3tl/heap_ptr.hxx>
38 using o3tl::heap_ptr;
41 class Help
43 public:
44 explicit Help(
45 int i_n )
46 : n(i_n) { ++nInstanceCount_; }
47 ~Help() { --nInstanceCount_; }
48 int Value() const { return n; }
49 static int InstanceCount_() { return nInstanceCount_; }
51 private:
52 int n;
53 static int nInstanceCount_;
55 int Help::nInstanceCount_ = 0;
58 class heap_ptr_test : public CppUnit::TestFixture
60 public:
61 void global()
63 // Construction
64 heap_ptr<Help>
65 t_empty;
66 const heap_ptr<Help>
67 t0( new Help(7000) );
68 heap_ptr<Help>
69 t1( new Help(10) );
70 heap_ptr<Help>
71 t2( new Help(20) );
73 int nHelpCount = 3;
75 CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty.is());
76 CPPUNIT_ASSERT_MESSAGE("ctor2", t0.is());
77 CPPUNIT_ASSERT_MESSAGE("ctor3", (*t0).Value() == 7000 );
78 CPPUNIT_ASSERT_MESSAGE("ctor4", t0->Value() == 7000 );
79 CPPUNIT_ASSERT_MESSAGE("ctor5", t0.get() == t0.operator->() );
80 CPPUNIT_ASSERT_MESSAGE("ctor6", t0.get() == &(*t0) );
82 CPPUNIT_ASSERT_MESSAGE("ctor7", t1.is());
83 CPPUNIT_ASSERT_MESSAGE("ctor8", (*t1).Value() == 10 );
84 CPPUNIT_ASSERT_MESSAGE("ctor9", t1->Value() == 10 );
85 CPPUNIT_ASSERT_MESSAGE("ctor10", t1.get() == t1.operator->() );
86 CPPUNIT_ASSERT_MESSAGE("ctor11", t1.get() == &(*t1) );
88 CPPUNIT_ASSERT_MESSAGE("ctor12", t2.operator*().Value() == 20);
89 CPPUNIT_ASSERT_MESSAGE("ctor13", Help::InstanceCount_() == nHelpCount);
92 // Operator safe_bool() / bool()
93 CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty);
94 CPPUNIT_ASSERT_MESSAGE("bool2", t0);
95 CPPUNIT_ASSERT_MESSAGE("bool3", t1.is() == static_cast<bool>(t1));
98 // Assignment, reset() and release()
99 // Release
100 Help * hp = t1.release();
101 CPPUNIT_ASSERT_MESSAGE("release1", ! t1.is() );
102 CPPUNIT_ASSERT_MESSAGE("release2", t1.get() == 0 );
103 CPPUNIT_ASSERT_MESSAGE("release3", t1.operator->() == 0 );
104 CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount);
106 // operator=()
107 t_empty = hp;
108 CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() );
109 CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount);
111 t1 = t_empty.release();
112 CPPUNIT_ASSERT_MESSAGE("assign3", t1.is() );
113 CPPUNIT_ASSERT_MESSAGE("assign4", ! t_empty.is() );
114 CPPUNIT_ASSERT_MESSAGE("assign5", Help::InstanceCount_() == nHelpCount);
116 // reset()
117 hp = new Help(30);
118 ++nHelpCount;
120 t_empty.reset(hp);
121 CPPUNIT_ASSERT_MESSAGE("reset1", Help::InstanceCount_() == nHelpCount);
122 CPPUNIT_ASSERT_MESSAGE("reset2", t_empty.is() );
123 CPPUNIT_ASSERT_MESSAGE("reset3", t_empty.get() == hp );
125 // Ignore second assignment
126 t_empty = hp;
127 CPPUNIT_ASSERT_MESSAGE("selfassign1", Help::InstanceCount_() == nHelpCount);
128 CPPUNIT_ASSERT_MESSAGE("selfassign2", t_empty.is() );
129 CPPUNIT_ASSERT_MESSAGE("selfassign3", t_empty.get() == hp );
131 t_empty.reset(0);
132 hp = 0;
133 --nHelpCount;
134 CPPUNIT_ASSERT_MESSAGE("reset4", ! t_empty.is() );
135 CPPUNIT_ASSERT_MESSAGE("reset5", Help::InstanceCount_() == nHelpCount);
138 // swap
139 t1.swap(t2);
140 CPPUNIT_ASSERT_MESSAGE("swap1", t1->Value() == 20 );
141 CPPUNIT_ASSERT_MESSAGE("swap2", t2->Value() == 10 );
142 CPPUNIT_ASSERT_MESSAGE("swap3", Help::InstanceCount_() == nHelpCount);
144 o3tl::swap(t1,t2);
145 CPPUNIT_ASSERT_MESSAGE("swap4", t1->Value() == 10 );
146 CPPUNIT_ASSERT_MESSAGE("swap5", t2->Value() == 20 );
147 CPPUNIT_ASSERT_MESSAGE("swap6", Help::InstanceCount_() == nHelpCount);
149 // RAII
151 heap_ptr<Help>
152 t_raii( new Help(55) );
153 CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1);
155 CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount);
159 // These macros are needed by auto register mechanism.
160 CPPUNIT_TEST_SUITE(heap_ptr_test);
161 CPPUNIT_TEST(global);
162 CPPUNIT_TEST_SUITE_END();
163 }; // class heap_ptr_test
165 // -----------------------------------------------------------------------------
166 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(heap_ptr_test, "o3tltests");