Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include "cppunit/TestAssert.h"
22 : #include "cppunit/TestFixture.h"
23 : #include "cppunit/extensions/HelperMacros.h"
24 :
25 : #include <o3tl/heap_ptr.hxx>
26 :
27 : using o3tl::heap_ptr;
28 :
29 : class Help
30 : {
31 : public:
32 5 : explicit Help(
33 : int i_n )
34 5 : : n(i_n) { ++nInstanceCount_; }
35 5 : ~Help() { --nInstanceCount_; }
36 9 : int Value() const { return n; }
37 11 : static int InstanceCount_() { return nInstanceCount_; }
38 :
39 : private:
40 : int n;
41 : static int nInstanceCount_;
42 : };
43 : int Help::nInstanceCount_ = 0;
44 :
45 :
46 3 : class heap_ptr_test : public CppUnit::TestFixture
47 : {
48 : public:
49 1 : void global()
50 : {
51 : // Construction
52 : heap_ptr<Help>
53 1 : t_empty;
54 : const heap_ptr<Help>
55 1 : t0( new Help(7000) );
56 : heap_ptr<Help>
57 1 : t1( new Help(10) );
58 : heap_ptr<Help>
59 1 : t2( new Help(20) );
60 :
61 1 : int nHelpCount = 3;
62 :
63 1 : CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty.is());
64 1 : CPPUNIT_ASSERT_MESSAGE("ctor2", t0.is());
65 1 : CPPUNIT_ASSERT_MESSAGE("ctor3", (*t0).Value() == 7000 );
66 1 : CPPUNIT_ASSERT_MESSAGE("ctor4", t0->Value() == 7000 );
67 1 : CPPUNIT_ASSERT_MESSAGE("ctor5", t0.get() == t0.operator->() );
68 1 : CPPUNIT_ASSERT_MESSAGE("ctor6", t0.get() == &(*t0) );
69 :
70 1 : CPPUNIT_ASSERT_MESSAGE("ctor7", t1.is());
71 1 : CPPUNIT_ASSERT_MESSAGE("ctor8", (*t1).Value() == 10 );
72 1 : CPPUNIT_ASSERT_MESSAGE("ctor9", t1->Value() == 10 );
73 1 : CPPUNIT_ASSERT_MESSAGE("ctor10", t1.get() == t1.operator->() );
74 1 : CPPUNIT_ASSERT_MESSAGE("ctor11", t1.get() == &(*t1) );
75 :
76 1 : CPPUNIT_ASSERT_MESSAGE("ctor12", t2.operator*().Value() == 20);
77 1 : CPPUNIT_ASSERT_MESSAGE("ctor13", Help::InstanceCount_() == nHelpCount);
78 :
79 :
80 : // Operator safe_bool() / bool()
81 1 : CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty);
82 1 : CPPUNIT_ASSERT_MESSAGE("bool2", t0);
83 1 : CPPUNIT_ASSERT_MESSAGE("bool3", t1.is() == static_cast<bool>(t1));
84 :
85 :
86 : // Assignment, reset() and release()
87 : // Release
88 1 : Help * hp = t1.release();
89 1 : CPPUNIT_ASSERT_MESSAGE("release1", ! t1.is() );
90 1 : CPPUNIT_ASSERT_MESSAGE("release2", t1.get() == 0 );
91 1 : CPPUNIT_ASSERT_MESSAGE("release3", t1.operator->() == 0 );
92 1 : CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount);
93 :
94 : // operator=()
95 1 : t_empty = hp;
96 1 : CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() );
97 1 : CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount);
98 :
99 1 : t1 = t_empty.release();
100 1 : CPPUNIT_ASSERT_MESSAGE("assign3", t1.is() );
101 1 : CPPUNIT_ASSERT_MESSAGE("assign4", ! t_empty.is() );
102 1 : CPPUNIT_ASSERT_MESSAGE("assign5", Help::InstanceCount_() == nHelpCount);
103 :
104 : // reset()
105 1 : hp = new Help(30);
106 1 : ++nHelpCount;
107 :
108 1 : t_empty.reset(hp);
109 1 : CPPUNIT_ASSERT_MESSAGE("reset1", Help::InstanceCount_() == nHelpCount);
110 1 : CPPUNIT_ASSERT_MESSAGE("reset2", t_empty.is() );
111 1 : CPPUNIT_ASSERT_MESSAGE("reset3", t_empty.get() == hp );
112 :
113 : // Ignore second assignment
114 1 : t_empty = hp;
115 1 : CPPUNIT_ASSERT_MESSAGE("selfassign1", Help::InstanceCount_() == nHelpCount);
116 1 : CPPUNIT_ASSERT_MESSAGE("selfassign2", t_empty.is() );
117 1 : CPPUNIT_ASSERT_MESSAGE("selfassign3", t_empty.get() == hp );
118 :
119 1 : t_empty.reset(0);
120 1 : hp = 0;
121 1 : --nHelpCount;
122 1 : CPPUNIT_ASSERT_MESSAGE("reset4", ! t_empty.is() );
123 1 : CPPUNIT_ASSERT_MESSAGE("reset5", Help::InstanceCount_() == nHelpCount);
124 :
125 :
126 : // swap
127 1 : t1.swap(t2);
128 1 : CPPUNIT_ASSERT_MESSAGE("swap1", t1->Value() == 20 );
129 1 : CPPUNIT_ASSERT_MESSAGE("swap2", t2->Value() == 10 );
130 1 : CPPUNIT_ASSERT_MESSAGE("swap3", Help::InstanceCount_() == nHelpCount);
131 :
132 1 : o3tl::swap(t1,t2);
133 1 : CPPUNIT_ASSERT_MESSAGE("swap4", t1->Value() == 10 );
134 1 : CPPUNIT_ASSERT_MESSAGE("swap5", t2->Value() == 20 );
135 1 : CPPUNIT_ASSERT_MESSAGE("swap6", Help::InstanceCount_() == nHelpCount);
136 :
137 : // RAII
138 : {
139 : heap_ptr<Help>
140 1 : t_raii( new Help(55) );
141 1 : CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1);
142 : }
143 1 : CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount);
144 1 : }
145 :
146 :
147 : // These macros are needed by auto register mechanism.
148 2 : CPPUNIT_TEST_SUITE(heap_ptr_test);
149 1 : CPPUNIT_TEST(global);
150 2 : CPPUNIT_TEST_SUITE_END();
151 : }; // class heap_ptr_test
152 :
153 : // -----------------------------------------------------------------------------
154 3 : CPPUNIT_TEST_SUITE_REGISTRATION(heap_ptr_test);
155 :
156 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|