Branch data 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 : 25 : explicit Help(
33 : : int i_n )
34 : 25 : : n(i_n) { ++nInstanceCount_; }
35 : 25 : ~Help() { --nInstanceCount_; }
36 : 45 : int Value() const { return n; }
37 : 55 : static int InstanceCount_() { return nInstanceCount_; }
38 : :
39 : : private:
40 : : int n;
41 : : static int nInstanceCount_;
42 : : };
43 : : int Help::nInstanceCount_ = 0;
44 : :
45 : :
46 [ - + ]: 15 : class heap_ptr_test : public CppUnit::TestFixture
47 : : {
48 : : public:
49 : 5 : void global()
50 : : {
51 : : // Construction
52 : : heap_ptr<Help>
53 : 5 : t_empty;
54 : : const heap_ptr<Help>
55 [ + - ]: 5 : t0( new Help(7000) );
56 : : heap_ptr<Help>
57 [ + - ]: 5 : t1( new Help(10) );
58 : : heap_ptr<Help>
59 [ + - ]: 5 : t2( new Help(20) );
60 : :
61 : 5 : int nHelpCount = 3;
62 : :
63 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty.is());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
64 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor2", t0.is());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
65 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor3", (*t0).Value() == 7000 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
66 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor4", t0->Value() == 7000 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
67 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor5", t0.get() == t0.operator->() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
68 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor6", t0.get() == &(*t0) );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
69 : :
70 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor7", t1.is());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
71 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor8", (*t1).Value() == 10 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
72 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor9", t1->Value() == 10 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
73 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor10", t1.get() == t1.operator->() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
74 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor11", t1.get() == &(*t1) );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
75 : :
76 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor12", t2.operator*().Value() == 20);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
77 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("ctor13", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
78 : :
79 : :
80 : : // Operator safe_bool() / bool()
81 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
82 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("bool2", t0);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
83 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("bool3", t1.is() == static_cast<bool>(t1));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
84 : :
85 : :
86 : : // Assignment, reset() and release()
87 : : // Release
88 : 5 : Help * hp = t1.release();
89 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("release1", ! t1.is() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
90 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("release2", t1.get() == 0 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
91 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("release3", t1.operator->() == 0 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
92 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
93 : :
94 : : // operator=()
95 [ + - ]: 5 : t_empty = hp;
96 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("assign1", t_empty.is() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
97 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
98 : :
99 [ + - ]: 5 : t1 = t_empty.release();
100 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("assign3", t1.is() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
101 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("assign4", ! t_empty.is() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
102 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("assign5", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
103 : :
104 : : // reset()
105 [ + - ]: 5 : hp = new Help(30);
106 : 5 : ++nHelpCount;
107 : :
108 [ + - ]: 5 : t_empty.reset(hp);
109 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("reset1", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
110 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("reset2", t_empty.is() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
111 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("reset3", t_empty.get() == hp );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
112 : :
113 : : // Ignore second assignment
114 [ + - ]: 5 : t_empty = hp;
115 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("selfassign1", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
116 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("selfassign2", t_empty.is() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
117 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("selfassign3", t_empty.get() == hp );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
118 : :
119 [ + - ]: 5 : t_empty.reset(0);
120 : 5 : hp = 0;
121 : 5 : --nHelpCount;
122 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("reset4", ! t_empty.is() );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
123 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("reset5", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
124 : :
125 : :
126 : : // swap
127 : 5 : t1.swap(t2);
128 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("swap1", t1->Value() == 20 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
129 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("swap2", t2->Value() == 10 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
130 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("swap3", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
131 : :
132 : 5 : o3tl::swap(t1,t2);
133 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("swap4", t1->Value() == 10 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
134 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("swap5", t2->Value() == 20 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
135 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("swap6", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
136 : :
137 : : // RAII
138 : : {
139 : : heap_ptr<Help>
140 [ + - ]: 5 : t_raii( new Help(55) );
141 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount + 1);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
142 : : }
143 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
144 : 5 : }
145 : :
146 : :
147 : : // These macros are needed by auto register mechanism.
148 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(heap_ptr_test);
[ + - ][ + - ]
[ # # ]
149 [ + - ][ + - ]: 5 : CPPUNIT_TEST(global);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
150 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
151 : : }; // class heap_ptr_test
152 : :
153 : : // -----------------------------------------------------------------------------
154 [ + - ][ + - ]: 15 : CPPUNIT_TEST_SUITE_REGISTRATION(heap_ptr_test);
155 : :
156 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|