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