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