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