xref: /aoo41x/main/o3tl/qa/test-heap_ptr.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include "preextstl.h"
29 #include "cppunit/TestAssert.h"
30 #include "cppunit/TestFixture.h"
31 #include "cppunit/extensions/HelperMacros.h"
32 #include "postextstl.h"
33 
34 #include <o3tl/heap_ptr.hxx>
35 
36 
37 
38 
39 using o3tl::heap_ptr;
40 
41 
42 class Help
43 {
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_; }
51 
52   private:
53     int                 n;
54     static int          nInstanceCount_;
55 };
56 int Help::nInstanceCount_ = 0;
57 
58 
59 class heap_ptr_test : public CppUnit::TestFixture
60 {
61   public:
62     void global()
63     {
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) );
73 
74         int nHelpCount = 3;
75 
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) );
82 
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) );
88 
89         CPPUNIT_ASSERT_MESSAGE("ctor12",   t2.operator*().Value() == 20);
90         CPPUNIT_ASSERT_MESSAGE("ctor13",   Help::InstanceCount_() == nHelpCount);
91 
92 
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));
97 
98 
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);
106 
107             // operator=()
108         t_empty = hp;
109         CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() );
110         CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount);
111 
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);
116 
117             // reset()
118         hp = new Help(30);
119         ++nHelpCount;
120 
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 );
125 
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 );
131 
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);
137 
138 
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);
144 
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);
149 
150         // RAII
151         {
152             heap_ptr<Help>
153                 t_raii( new Help(55) );
154             CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1);
155         }
156         CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount);
157     }
158 
159 
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
165 
166 // -----------------------------------------------------------------------------
167 CPPUNIT_TEST_SUITE_REGISTRATION(heap_ptr_test);
168