Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sal / qa / rtl / ref / rtl_ref.cxx
blobd8dc59e6583927748d0d25c7603f9f7eb9219ed5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <rtl/ref.hxx>
11 #include <sal/types.h>
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/extensions/HelperMacros.h>
14 #include <cppunit/plugin/TestPlugIn.h>
16 namespace rtl_ref
19 class MoveTestClass
21 private:
22 bool m_bIncFlag;
23 long m_nRef;
24 public:
25 MoveTestClass(): m_bIncFlag(false), m_nRef(0) { }
27 // There should never be more than two references to this class as it
28 // is used as a test class for move functions. One reference being the
29 // original reference and the second being the test reference
30 void acquire()
32 if(m_bIncFlag)
34 ++m_nRef;
35 m_bIncFlag = false;
37 else
38 CPPUNIT_FAIL("RC was incremented when in should not have been");
41 void release() { --m_nRef; }
43 long use_count() { return m_nRef; }
45 void set_inc_flag() { m_bIncFlag = true; }
48 rtl::Reference< MoveTestClass > get_reference( MoveTestClass* pcTestClass )
50 // constructor will increment the reference count
51 pcTestClass->set_inc_flag();
52 rtl::Reference< MoveTestClass > tmp(pcTestClass);
53 return tmp;
56 class TestReferenceRefCounting : public CppUnit::TestFixture
58 void testMove()
60 MoveTestClass cTestClass;
62 // constructor will increment the reference count
63 cTestClass.set_inc_flag();
64 rtl::Reference< MoveTestClass > test1( &cTestClass );
66 // move should not increment the reference count
67 rtl::Reference< MoveTestClass > test2( std::move(test1) );
68 CPPUNIT_ASSERT_EQUAL_MESSAGE("test2.use_count() == 1",
69 static_cast<long>(1), test2->use_count());
71 // test1 now contains a null pointer
72 CPPUNIT_ASSERT_MESSAGE("!test1.is()",
73 !test1.is());
75 // function return should move the reference
76 test2 = get_reference( &cTestClass );
77 CPPUNIT_ASSERT_EQUAL_MESSAGE("test2.use_count() == 1",
78 static_cast<long>(1), test2->use_count());
80 // normal copy
81 test2->set_inc_flag();
82 test1 = test2;
83 CPPUNIT_ASSERT_EQUAL_MESSAGE("test2.use_count() == 2",
84 static_cast<long>(2), test2->use_count());
86 // use count should decrement
87 test2 = rtl::Reference< MoveTestClass >(nullptr);
88 CPPUNIT_ASSERT_EQUAL_MESSAGE("test1.use_count() == 1",
89 static_cast<long>(1), test1->use_count());
91 // move of a null pointer should not cause an error
92 test1 = std::move(test2);
94 CPPUNIT_ASSERT_MESSAGE("!test1.is()",
95 !test1.is());
96 CPPUNIT_ASSERT_MESSAGE("!test2.is()",
97 !test2.is());
99 CPPUNIT_ASSERT_EQUAL_MESSAGE("cTestClass.use_count() == 0",
100 static_cast<long>(0), cTestClass.use_count());
103 CPPUNIT_TEST_SUITE(TestReferenceRefCounting);
104 CPPUNIT_TEST(testMove);
105 CPPUNIT_TEST_SUITE_END();
108 } // namespace rtl_ref
109 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_ref::TestReferenceRefCounting);
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */