xref: /aoo41x/main/o3tl/qa/test-heap_ptr.cxx (revision 31682d32)
1*31682d32SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*31682d32SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*31682d32SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*31682d32SAndrew Rist  * distributed with this work for additional information
6*31682d32SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*31682d32SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*31682d32SAndrew Rist  * "License"); you may not use this file except in compliance
9*31682d32SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*31682d32SAndrew Rist  *
11*31682d32SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*31682d32SAndrew Rist  *
13*31682d32SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*31682d32SAndrew Rist  * software distributed under the License is distributed on an
15*31682d32SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*31682d32SAndrew Rist  * KIND, either express or implied.  See the License for the
17*31682d32SAndrew Rist  * specific language governing permissions and limitations
18*31682d32SAndrew Rist  * under the License.
19*31682d32SAndrew Rist  *
20*31682d32SAndrew Rist  *************************************************************/
21*31682d32SAndrew Rist 
22*31682d32SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "preextstl.h"
25cdf0e10cSrcweir #include "cppunit/TestAssert.h"
26cdf0e10cSrcweir #include "cppunit/TestFixture.h"
27cdf0e10cSrcweir #include "cppunit/extensions/HelperMacros.h"
28cdf0e10cSrcweir #include "postextstl.h"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <o3tl/heap_ptr.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using o3tl::heap_ptr;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir class Help
39cdf0e10cSrcweir {
40cdf0e10cSrcweir   public:
Help(int i_n)41cdf0e10cSrcweir     explicit            Help(
42cdf0e10cSrcweir                             int                 i_n )
43cdf0e10cSrcweir                                                 : n(i_n) { ++nInstanceCount_; }
~Help()44cdf0e10cSrcweir                         ~Help()                 { --nInstanceCount_; }
Value() const45cdf0e10cSrcweir     int                 Value() const           { return n; }
InstanceCount_()46cdf0e10cSrcweir     static int          InstanceCount_()        { return nInstanceCount_; }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir   private:
49cdf0e10cSrcweir     int                 n;
50cdf0e10cSrcweir     static int          nInstanceCount_;
51cdf0e10cSrcweir };
52cdf0e10cSrcweir int Help::nInstanceCount_ = 0;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir class heap_ptr_test : public CppUnit::TestFixture
56cdf0e10cSrcweir {
57cdf0e10cSrcweir   public:
global()58cdf0e10cSrcweir     void global()
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         // Construction
61cdf0e10cSrcweir         heap_ptr<Help>
62cdf0e10cSrcweir             t_empty;
63cdf0e10cSrcweir         const heap_ptr<Help>
64cdf0e10cSrcweir             t0( new Help(7000) );
65cdf0e10cSrcweir         heap_ptr<Help>
66cdf0e10cSrcweir             t1( new Help(10) );
67cdf0e10cSrcweir         heap_ptr<Help>
68cdf0e10cSrcweir             t2( new Help(20) );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         int nHelpCount = 3;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty.is());
73cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor2",   t0.is());
74cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor3",   (*t0).Value() == 7000 );
75cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor4",   t0->Value() == 7000 );
76cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor5",   t0.get() == t0.operator->() );
77cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor6",   t0.get() == &(*t0) );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor7",   t1.is());
80cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor8",   (*t1).Value() == 10 );
81cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor9",   t1->Value() == 10 );
82cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor10",   t1.get() == t1.operator->() );
83cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor11",   t1.get() == &(*t1) );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor12",   t2.operator*().Value() == 20);
86cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("ctor13",   Help::InstanceCount_() == nHelpCount);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         // Operator safe_bool() / bool()
90cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty);
91cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("bool2", t0);
92cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("bool3", t1.is() == static_cast<bool>(t1));
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         // Assignment, reset() and release()
96cdf0e10cSrcweir             // Release
97cdf0e10cSrcweir         Help * hp = t1.release();
98cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("release1", ! t1.is() );
99cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("release2", t1.get() == 0 );
100cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("release3", t1.operator->() == 0 );
101cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             // operator=()
104cdf0e10cSrcweir         t_empty = hp;
105cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() );
106cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         t1 = t_empty.release();
109cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("assign3",     t1.is() );
110cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("assign4",   ! t_empty.is() );
111cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("assign5",   Help::InstanceCount_() == nHelpCount);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             // reset()
114cdf0e10cSrcweir         hp = new Help(30);
115cdf0e10cSrcweir         ++nHelpCount;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         t_empty.reset(hp);
118cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("reset1",  Help::InstanceCount_() == nHelpCount);
119cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("reset2",  t_empty.is() );
120cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("reset3",  t_empty.get() == hp );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             // Ignore second assignment
123cdf0e10cSrcweir         t_empty = hp;
124cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("selfassign1",  Help::InstanceCount_() == nHelpCount);
125cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("selfassign2",  t_empty.is() );
126cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("selfassign3",  t_empty.get() == hp );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         t_empty.reset(0);
129cdf0e10cSrcweir         hp = 0;
130cdf0e10cSrcweir         --nHelpCount;
131cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("reset4",   ! t_empty.is() );
132cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("reset5",   Help::InstanceCount_() == nHelpCount);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         // swap
136cdf0e10cSrcweir         t1.swap(t2);
137cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("swap1",   t1->Value() == 20 );
138cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("swap2",   t2->Value() == 10 );
139cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("swap3",   Help::InstanceCount_() == nHelpCount);
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         o3tl::swap(t1,t2);
142cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("swap4",   t1->Value() == 10 );
143cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("swap5",   t2->Value() == 20 );
144cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("swap6",   Help::InstanceCount_() == nHelpCount);
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         // RAII
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             heap_ptr<Help>
149cdf0e10cSrcweir                 t_raii( new Help(55) );
150cdf0e10cSrcweir             CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1);
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount);
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     // These macros are needed by auto register mechanism.
157cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(heap_ptr_test);
158cdf0e10cSrcweir     CPPUNIT_TEST(global);
159cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
160cdf0e10cSrcweir }; // class heap_ptr_test
161cdf0e10cSrcweir 
162cdf0e10cSrcweir // -----------------------------------------------------------------------------
163cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(heap_ptr_test);
164