android: Update app-specific/MIME type icons
[LibreOffice.git] / sal / qa / rtl / ref / rtl_ref.cxx
blob2bf7c9803f3d85c54ed2cb94529dc60a3f17d1ee
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 namespace {
21 class MoveTestClass
23 private:
24 bool m_bIncFlag;
25 long m_nRef;
26 public:
27 MoveTestClass(): m_bIncFlag(false), m_nRef(0) { }
29 // There should never be more than two references to this class as it
30 // is used as a test class for move functions. One reference being the
31 // original reference and the second being the test reference
32 void acquire()
34 if(m_bIncFlag)
36 ++m_nRef;
37 m_bIncFlag = false;
39 else
40 CPPUNIT_FAIL("RC was incremented when in should not have been");
43 void release() { --m_nRef; }
45 long use_count() { return m_nRef; }
47 void set_inc_flag() { m_bIncFlag = true; }
52 static rtl::Reference< MoveTestClass > get_reference( MoveTestClass* pcTestClass )
54 // constructor will increment the reference count
55 pcTestClass->set_inc_flag();
56 rtl::Reference< MoveTestClass > tmp(pcTestClass);
57 return tmp;
60 class TestReferenceRefCounting : public CppUnit::TestFixture
62 void testMove()
64 MoveTestClass cTestClass;
66 // constructor will increment the reference count
67 cTestClass.set_inc_flag();
68 rtl::Reference< MoveTestClass > test1( &cTestClass );
70 // move should not increment the reference count
71 rtl::Reference< MoveTestClass > test2( std::move(test1) );
72 CPPUNIT_ASSERT_EQUAL_MESSAGE("test2.use_count() == 1",
73 static_cast<long>(1), test2->use_count());
75 // test1 now contains a null pointer
76 CPPUNIT_ASSERT_MESSAGE("!test1.is()",
77 !test1.is()); // NOLINT(bugprone-use-after-move)
79 // function return should move the reference
80 test2 = get_reference( &cTestClass );
81 CPPUNIT_ASSERT_EQUAL_MESSAGE("test2.use_count() == 1",
82 static_cast<long>(1), test2->use_count());
84 // normal copy
85 test2->set_inc_flag();
86 test1 = test2;
87 CPPUNIT_ASSERT_EQUAL_MESSAGE("test2.use_count() == 2",
88 static_cast<long>(2), test2->use_count());
90 // use count should decrement
91 test2 = rtl::Reference< MoveTestClass >();
92 CPPUNIT_ASSERT_EQUAL_MESSAGE("test1.use_count() == 1",
93 static_cast<long>(1), test1->use_count());
95 // move of a null pointer should not cause an error
96 test1 = std::move(test2);
98 CPPUNIT_ASSERT_MESSAGE("!test1.is()",
99 !test1.is());
100 CPPUNIT_ASSERT_MESSAGE("!test2.is()",
101 !test2.is()); // NOLINT(bugprone-use-after-move)
103 CPPUNIT_ASSERT_EQUAL_MESSAGE("cTestClass.use_count() == 0",
104 static_cast<long>(0), cTestClass.use_count());
107 CPPUNIT_TEST_SUITE(TestReferenceRefCounting);
108 CPPUNIT_TEST(testMove);
109 CPPUNIT_TEST_SUITE_END();
112 } // namespace rtl_ref
113 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_ref::TestReferenceRefCounting);
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */