CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / o3tl / qa / test-heap_ptr.cxx
blob63ec692efe5c243ae15b58f033fa34641338256e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "preextstl.h"
29 #include "cppunit/TestAssert.h"
30 #include "cppunit/TestFixture.h"
31 #include "cppunit/extensions/HelperMacros.h"
32 #include "postextstl.h"
34 #include <o3tl/heap_ptr.hxx>
39 using o3tl::heap_ptr;
42 class Help
44 public:
45 explicit Help(
46 int i_n )
47 : n(i_n) { ++nInstanceCount_; }
48 ~Help() { --nInstanceCount_; }
49 int Value() const { return n; }
50 static int InstanceCount_() { return nInstanceCount_; }
52 private:
53 int n;
54 static int nInstanceCount_;
56 int Help::nInstanceCount_ = 0;
59 class heap_ptr_test : public CppUnit::TestFixture
61 public:
62 void global()
64 // Construction
65 heap_ptr<Help>
66 t_empty;
67 const heap_ptr<Help>
68 t0( new Help(7000) );
69 heap_ptr<Help>
70 t1( new Help(10) );
71 heap_ptr<Help>
72 t2( new Help(20) );
74 int nHelpCount = 3;
76 CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty.is());
77 CPPUNIT_ASSERT_MESSAGE("ctor2", t0.is());
78 CPPUNIT_ASSERT_MESSAGE("ctor3", (*t0).Value() == 7000 );
79 CPPUNIT_ASSERT_MESSAGE("ctor4", t0->Value() == 7000 );
80 CPPUNIT_ASSERT_MESSAGE("ctor5", t0.get() == t0.operator->() );
81 CPPUNIT_ASSERT_MESSAGE("ctor6", t0.get() == &(*t0) );
83 CPPUNIT_ASSERT_MESSAGE("ctor7", t1.is());
84 CPPUNIT_ASSERT_MESSAGE("ctor8", (*t1).Value() == 10 );
85 CPPUNIT_ASSERT_MESSAGE("ctor9", t1->Value() == 10 );
86 CPPUNIT_ASSERT_MESSAGE("ctor10", t1.get() == t1.operator->() );
87 CPPUNIT_ASSERT_MESSAGE("ctor11", t1.get() == &(*t1) );
89 CPPUNIT_ASSERT_MESSAGE("ctor12", t2.operator*().Value() == 20);
90 CPPUNIT_ASSERT_MESSAGE("ctor13", Help::InstanceCount_() == nHelpCount);
93 // Operator safe_bool() / bool()
94 CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty);
95 CPPUNIT_ASSERT_MESSAGE("bool2", t0);
96 CPPUNIT_ASSERT_MESSAGE("bool3", t1.is() == static_cast<bool>(t1));
99 // Assignment, reset() and release()
100 // Release
101 Help * hp = t1.release();
102 CPPUNIT_ASSERT_MESSAGE("release1", ! t1.is() );
103 CPPUNIT_ASSERT_MESSAGE("release2", t1.get() == 0 );
104 CPPUNIT_ASSERT_MESSAGE("release3", t1.operator->() == 0 );
105 CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount);
107 // operator=()
108 t_empty = hp;
109 CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() );
110 CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount);
112 t1 = t_empty.release();
113 CPPUNIT_ASSERT_MESSAGE("assign3", t1.is() );
114 CPPUNIT_ASSERT_MESSAGE("assign4", ! t_empty.is() );
115 CPPUNIT_ASSERT_MESSAGE("assign5", Help::InstanceCount_() == nHelpCount);
117 // reset()
118 hp = new Help(30);
119 ++nHelpCount;
121 t_empty.reset(hp);
122 CPPUNIT_ASSERT_MESSAGE("reset1", Help::InstanceCount_() == nHelpCount);
123 CPPUNIT_ASSERT_MESSAGE("reset2", t_empty.is() );
124 CPPUNIT_ASSERT_MESSAGE("reset3", t_empty.get() == hp );
126 // Ignore second assignment
127 t_empty = hp;
128 CPPUNIT_ASSERT_MESSAGE("selfassign1", Help::InstanceCount_() == nHelpCount);
129 CPPUNIT_ASSERT_MESSAGE("selfassign2", t_empty.is() );
130 CPPUNIT_ASSERT_MESSAGE("selfassign3", t_empty.get() == hp );
132 t_empty.reset(0);
133 hp = 0;
134 --nHelpCount;
135 CPPUNIT_ASSERT_MESSAGE("reset4", ! t_empty.is() );
136 CPPUNIT_ASSERT_MESSAGE("reset5", Help::InstanceCount_() == nHelpCount);
139 // swap
140 t1.swap(t2);
141 CPPUNIT_ASSERT_MESSAGE("swap1", t1->Value() == 20 );
142 CPPUNIT_ASSERT_MESSAGE("swap2", t2->Value() == 10 );
143 CPPUNIT_ASSERT_MESSAGE("swap3", Help::InstanceCount_() == nHelpCount);
145 o3tl::swap(t1,t2);
146 CPPUNIT_ASSERT_MESSAGE("swap4", t1->Value() == 10 );
147 CPPUNIT_ASSERT_MESSAGE("swap5", t2->Value() == 20 );
148 CPPUNIT_ASSERT_MESSAGE("swap6", Help::InstanceCount_() == nHelpCount);
150 // RAII
152 heap_ptr<Help>
153 t_raii( new Help(55) );
154 CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1);
156 CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount);
160 // These macros are needed by auto register mechanism.
161 CPPUNIT_TEST_SUITE(heap_ptr_test);
162 CPPUNIT_TEST(global);
163 CPPUNIT_TEST_SUITE_END();
164 }; // class heap_ptr_test
166 // -----------------------------------------------------------------------------
167 CPPUNIT_TEST_SUITE_REGISTRATION(heap_ptr_test);