1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/types.h>
21 #include "cppunit/TestAssert.h"
22 #include "cppunit/TestFixture.h"
23 #include "cppunit/extensions/HelperMacros.h"
25 #include <o3tl/heap_ptr.hxx>
34 : n(i_n
) { ++nInstanceCount_
; }
35 ~Help() { --nInstanceCount_
; }
36 int Value() const { return n
; }
37 static int InstanceCount_() { return nInstanceCount_
; }
41 static int nInstanceCount_
;
43 int Help::nInstanceCount_
= 0;
46 class heap_ptr_test
: public CppUnit::TestFixture
63 CPPUNIT_ASSERT_MESSAGE("ctor1", ! t_empty
.is());
64 CPPUNIT_ASSERT_MESSAGE("ctor2", t0
.is());
65 CPPUNIT_ASSERT_MESSAGE("ctor3", (*t0
).Value() == 7000 );
66 CPPUNIT_ASSERT_MESSAGE("ctor4", t0
->Value() == 7000 );
67 CPPUNIT_ASSERT_MESSAGE("ctor5", t0
.get() == t0
.operator->() );
68 CPPUNIT_ASSERT_MESSAGE("ctor6", t0
.get() == &(*t0
) );
70 CPPUNIT_ASSERT_MESSAGE("ctor7", t1
.is());
71 CPPUNIT_ASSERT_MESSAGE("ctor8", (*t1
).Value() == 10 );
72 CPPUNIT_ASSERT_MESSAGE("ctor9", t1
->Value() == 10 );
73 CPPUNIT_ASSERT_MESSAGE("ctor10", t1
.get() == t1
.operator->() );
74 CPPUNIT_ASSERT_MESSAGE("ctor11", t1
.get() == &(*t1
) );
76 CPPUNIT_ASSERT_MESSAGE("ctor12", t2
.operator*().Value() == 20);
77 CPPUNIT_ASSERT_MESSAGE("ctor13", Help::InstanceCount_() == nHelpCount
);
80 // Operator safe_bool() / bool()
81 CPPUNIT_ASSERT_MESSAGE("bool1", ! t_empty
);
82 CPPUNIT_ASSERT_MESSAGE("bool2", t0
);
83 CPPUNIT_ASSERT_MESSAGE("bool3", t1
.is() == static_cast<bool>(t1
));
86 // Assignment, reset() and release()
88 Help
* hp
= t1
.release();
89 CPPUNIT_ASSERT_MESSAGE("release1", ! t1
.is() );
90 CPPUNIT_ASSERT_MESSAGE("release2", t1
.get() == 0 );
91 CPPUNIT_ASSERT_MESSAGE("release3", t1
.operator->() == 0 );
92 CPPUNIT_ASSERT_MESSAGE("release4", Help::InstanceCount_() == nHelpCount
);
96 CPPUNIT_ASSERT_MESSAGE("assign1", t_empty
.is() );
97 CPPUNIT_ASSERT_MESSAGE("assign2", Help::InstanceCount_() == nHelpCount
);
99 t1
= t_empty
.release();
100 CPPUNIT_ASSERT_MESSAGE("assign3", t1
.is() );
101 CPPUNIT_ASSERT_MESSAGE("assign4", ! t_empty
.is() );
102 CPPUNIT_ASSERT_MESSAGE("assign5", Help::InstanceCount_() == nHelpCount
);
109 CPPUNIT_ASSERT_MESSAGE("reset1", Help::InstanceCount_() == nHelpCount
);
110 CPPUNIT_ASSERT_MESSAGE("reset2", t_empty
.is() );
111 CPPUNIT_ASSERT_MESSAGE("reset3", t_empty
.get() == hp
);
113 // Ignore second assignment
115 CPPUNIT_ASSERT_MESSAGE("selfassign1", Help::InstanceCount_() == nHelpCount
);
116 CPPUNIT_ASSERT_MESSAGE("selfassign2", t_empty
.is() );
117 CPPUNIT_ASSERT_MESSAGE("selfassign3", t_empty
.get() == hp
);
122 CPPUNIT_ASSERT_MESSAGE("reset4", ! t_empty
.is() );
123 CPPUNIT_ASSERT_MESSAGE("reset5", Help::InstanceCount_() == nHelpCount
);
128 CPPUNIT_ASSERT_MESSAGE("swap1", t1
->Value() == 20 );
129 CPPUNIT_ASSERT_MESSAGE("swap2", t2
->Value() == 10 );
130 CPPUNIT_ASSERT_MESSAGE("swap3", Help::InstanceCount_() == nHelpCount
);
133 CPPUNIT_ASSERT_MESSAGE("swap4", t1
->Value() == 10 );
134 CPPUNIT_ASSERT_MESSAGE("swap5", t2
->Value() == 20 );
135 CPPUNIT_ASSERT_MESSAGE("swap6", Help::InstanceCount_() == nHelpCount
);
140 t_raii( new Help(55) );
141 CPPUNIT_ASSERT_MESSAGE("raii1", Help::InstanceCount_() == nHelpCount
+ 1);
143 CPPUNIT_ASSERT_MESSAGE("raii2", Help::InstanceCount_() == nHelpCount
);
147 // These macros are needed by auto register mechanism.
148 CPPUNIT_TEST_SUITE(heap_ptr_test
);
149 CPPUNIT_TEST(global
);
150 CPPUNIT_TEST_SUITE_END();
151 }; // class heap_ptr_test
153 // -----------------------------------------------------------------------------
154 CPPUNIT_TEST_SUITE_REGISTRATION(heap_ptr_test
);
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */