tdf#36709 Add loext:text-indent supporting font-relative units
[libreoffice.git] / o3tl / qa / test-cow_wrapper.cxx
blobb5d4936d5d7d65c9f1d4dedcdcad967ea63fa24f
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/.
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>
24 #include <cppunit/plugin/TestPlugIn.h>
26 #include "cow_wrapper_clients.hxx"
28 using namespace ::o3tl;
29 using namespace ::o3tltests;
32 class cow_wrapper_test : public CppUnit::TestFixture
34 public:
35 template< class T > void test( T& rTestObj1, T& rTestObj2, T& rTestObj3 )
37 CPPUNIT_ASSERT_MESSAGE("rTestObj1 is unique",
38 rTestObj1.is_unique() );
39 CPPUNIT_ASSERT_MESSAGE("rTestObj2 is unique",
40 rTestObj2.is_unique() );
41 CPPUNIT_ASSERT_MESSAGE("rTestObj3 is unique",
42 rTestObj3.is_unique() );
44 CPPUNIT_ASSERT_MESSAGE("rTestObj1 != rTestObj2",
45 rTestObj1 != rTestObj2 );
46 CPPUNIT_ASSERT_MESSAGE("rTestObj2 != rTestObj3",
47 rTestObj2 != rTestObj3 );
48 CPPUNIT_ASSERT_MESSAGE("rTestObj1 != rTestObj3",
49 rTestObj1 != rTestObj3 );
50 CPPUNIT_ASSERT_MESSAGE("rTestObj1 < rTestObj2",
51 rTestObj1 < rTestObj2 );
52 CPPUNIT_ASSERT_MESSAGE("rTestObj2 < rTestObj3",
53 rTestObj2 < rTestObj3 );
55 rTestObj2 = rTestObj1;
56 rTestObj3 = rTestObj1;
57 CPPUNIT_ASSERT_MESSAGE("rTestObj1 == rTestObj2",
58 rTestObj1 == rTestObj2 );
59 CPPUNIT_ASSERT_MESSAGE("rTestObj1 == rTestObj3",
60 rTestObj1 == rTestObj3 );
61 CPPUNIT_ASSERT_MESSAGE("rTestObj1.use_count() == 3",
62 rTestObj1.use_count() == 3 );
63 CPPUNIT_ASSERT_MESSAGE("rTestObj2.use_count() == 3",
64 rTestObj2.use_count() == 3 );
65 CPPUNIT_ASSERT_MESSAGE("rTestObj3.use_count() == 3",
66 rTestObj3.use_count() == 3 );
68 rTestObj2.makeUnique();
69 CPPUNIT_ASSERT_MESSAGE("rTestObj1 == rTestObj2",
70 rTestObj1 == rTestObj2 );
71 CPPUNIT_ASSERT_MESSAGE("rTestObj1 == rTestObj3",
72 rTestObj1 == rTestObj3 );
73 CPPUNIT_ASSERT_MESSAGE("rTestObj1.use_count() == 2",
74 rTestObj1.use_count() == 2 );
75 CPPUNIT_ASSERT_MESSAGE("rTestObj2.use_count() == 1",
76 rTestObj2.use_count() == 1 );
77 CPPUNIT_ASSERT_MESSAGE("rTestObj2.is_unique()",
78 rTestObj2.is_unique() );
79 CPPUNIT_ASSERT_MESSAGE("rTestObj3.use_count() == 2",
80 rTestObj3.use_count() == 2 );
82 rTestObj2.swap( rTestObj3 );
83 CPPUNIT_ASSERT_MESSAGE("rTestObj1 == rTestObj2",
84 rTestObj1 == rTestObj2 );
85 CPPUNIT_ASSERT_MESSAGE("rTestObj1 == rTestObj3",
86 rTestObj1 == rTestObj3 );
87 CPPUNIT_ASSERT_MESSAGE("rTestObj1.use_count() == 2",
88 rTestObj1.use_count() == 2 );
89 CPPUNIT_ASSERT_MESSAGE("rTestObj2.use_count() == 2",
90 rTestObj2.use_count() == 2 );
91 CPPUNIT_ASSERT_MESSAGE("rTestObj3.use_count() == 1",
92 rTestObj3.use_count() == 1 );
93 CPPUNIT_ASSERT_MESSAGE("rTestObj3.is_unique()",
94 rTestObj3.is_unique() );
97 void testCowWrapper()
99 // setup
100 cow_wrapper_client1 aTestObj1;
101 cow_wrapper_client1 aTestObj2;
102 cow_wrapper_client1 aTestObj3;
104 cow_wrapper_client2 aTestObj4;
105 cow_wrapper_client2 aTestObj5;
106 cow_wrapper_client2 aTestObj6;
108 cow_wrapper_client3 aTestObj7;
109 cow_wrapper_client3 aTestObj8;
110 cow_wrapper_client3 aTestObj9;
113 aTestObj1 = cow_wrapper_client1( 1 );
114 CPPUNIT_ASSERT_EQUAL(1, aTestObj1.queryUnmodified());
115 aTestObj2.modify( 2 );
116 CPPUNIT_ASSERT_EQUAL(2, aTestObj2.queryUnmodified());
117 aTestObj3.modify( 3 );
118 CPPUNIT_ASSERT_EQUAL(3, aTestObj3.queryUnmodified());
120 aTestObj4 = cow_wrapper_client2( 4 );
121 CPPUNIT_ASSERT_EQUAL(4, aTestObj4.queryUnmodified());
122 aTestObj5.modify( 5 );
123 CPPUNIT_ASSERT_EQUAL(5, aTestObj5.queryUnmodified());
124 aTestObj6.modify( 6 );
125 CPPUNIT_ASSERT_EQUAL(6, aTestObj6.queryUnmodified());
127 aTestObj7 = cow_wrapper_client3( 7 );
128 CPPUNIT_ASSERT_EQUAL(7, aTestObj7.queryUnmodified());
129 aTestObj8.modify( 8 );
130 CPPUNIT_ASSERT_EQUAL(8, aTestObj8.queryUnmodified());
131 aTestObj9.modify( 9 );
132 CPPUNIT_ASSERT_EQUAL(9, aTestObj9.queryUnmodified());
134 // all three temporaries are dead now
136 // test
137 test( aTestObj1, aTestObj2, aTestObj3 );
138 test( aTestObj4, aTestObj5, aTestObj6 );
139 test( aTestObj7, aTestObj8, aTestObj9 );
142 void testStaticDefault()
144 cow_wrapper_client4 aTestObj1;
145 cow_wrapper_client4 aTestObj2;
146 cow_wrapper_client4 aTestObj3(4);
148 CPPUNIT_ASSERT_MESSAGE("aTestObj1.is_default()",
149 aTestObj1.is_default() );
150 CPPUNIT_ASSERT_MESSAGE("aTestObj2.is_default()",
151 aTestObj2.is_default() );
152 CPPUNIT_ASSERT_MESSAGE("!aTestObj3.is_default()",
153 !aTestObj3.is_default() );
154 aTestObj1 = aTestObj2;
155 CPPUNIT_ASSERT_MESSAGE("aTestObj1.is_default() #2",
156 aTestObj1.is_default() );
157 CPPUNIT_ASSERT_MESSAGE("aTestObj2.is_default() #2",
158 aTestObj2.is_default() );
159 aTestObj1 = aTestObj3;
160 CPPUNIT_ASSERT_MESSAGE("!aTestObj1.is_default()",
161 !aTestObj1.is_default() );
164 void testRefCounting()
166 // add scope to ensure appropriate number of calls to
167 // the reference counting policy have been made
169 // if any incrementing/decrementing occurs a failure
170 // will occur
171 cow_wrapper_client5 aTestObj1(1);
172 cow_wrapper_client5 aTestObj2( std::move( aTestObj1 ) );
173 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj2.use_count()",
174 static_cast<sal_uInt32>(1), aTestObj2.use_count() );
176 // the following should increment
177 BogusRefCountPolicy::s_bShouldIncrement = true;
178 cow_wrapper_client5 aTestObj3( aTestObj2 );
179 CPPUNIT_ASSERT_MESSAGE("s_bShouldIncrement == 0",
180 !BogusRefCountPolicy::s_bShouldIncrement );
182 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj3.use_count()",
183 static_cast<sal_uInt32>(2), aTestObj3.use_count() );
185 cow_wrapper_client5 aTestObj4;
186 // the following should decrement the lvalue and then increment the rvalue
187 BogusRefCountPolicy::s_bShouldIncrement = true;
188 BogusRefCountPolicy::s_bShouldDecrement = true;
189 aTestObj4 = aTestObj2;
190 CPPUNIT_ASSERT_MESSAGE("s_bShouldIncrement == 0",
191 !BogusRefCountPolicy::s_bShouldIncrement );
192 CPPUNIT_ASSERT_MESSAGE("s_bShouldDecrement == 0",
193 !BogusRefCountPolicy::s_bShouldDecrement );
195 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj2.use_count()",
196 static_cast<sal_uInt32>(3), aTestObj2.use_count() );
197 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj3.use_count()",
198 static_cast<sal_uInt32>(3), aTestObj3.use_count() );
199 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj4.use_count()",
200 static_cast<sal_uInt32>(3), aTestObj4.use_count() );
201 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj2 == aTestObj3",
202 aTestObj3, aTestObj2 );
203 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj3 == aTestObj4",
204 aTestObj4, aTestObj3 );
205 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj2 == aTestObj4",
206 aTestObj4, aTestObj2 );
208 // only decrement the lvalue before assignment
209 BogusRefCountPolicy::s_bShouldDecrement = true;
210 aTestObj4 = cow_wrapper_client5( 4 );
211 CPPUNIT_ASSERT_MESSAGE("s_bShouldIncrement == 0",
212 !BogusRefCountPolicy::s_bShouldIncrement );
214 // only one call should be made to the ref counting policy's
215 // decrementing function at the end of the scope
216 BogusRefCountPolicy::s_bShouldDecrement = true;
218 CPPUNIT_ASSERT_MESSAGE("s_bShouldDecrement == 0",
219 !BogusRefCountPolicy::s_bShouldDecrement );
221 // self assignment
222 // aTestObj2 is defunct afterwards, one decrement happens
223 BogusRefCountPolicy::s_bShouldDecrement = true;
224 aTestObj3 = std::move( aTestObj2 );
225 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj2.use_count()",
226 static_cast<sal_uInt32>(0), aTestObj2.use_count() ); // NOLINT(bugprone-use-after-move)
227 CPPUNIT_ASSERT_EQUAL_MESSAGE("aTestObj3.use_count()",
228 static_cast<sal_uInt32>(1), aTestObj3.use_count() );
230 cow_wrapper_client5 aTestObj5;
232 // only decrement the lvalue before assignment
233 BogusRefCountPolicy::s_bShouldDecrement = true;
234 aTestObj3 = std::move( aTestObj5 );
235 CPPUNIT_ASSERT_MESSAGE("s_bShouldDecrement == 0",
236 !BogusRefCountPolicy::s_bShouldDecrement);
238 // one call should be made to the ref-counting policy's
239 // decrementing function at the end of the scope. Only
240 // aTestObj3 still holds a valid instance
241 BogusRefCountPolicy::s_nEndOfScope = 1;
243 CPPUNIT_ASSERT_EQUAL_MESSAGE("s_EndOfScope",
244 static_cast<sal_uInt32>(0), BogusRefCountPolicy::s_nEndOfScope );
247 // Change the following lines only, if you add, remove or rename
248 // member functions of the current class,
249 // because these macros are need by auto register mechanism.
251 CPPUNIT_TEST_SUITE(cow_wrapper_test);
252 CPPUNIT_TEST(testCowWrapper);
253 CPPUNIT_TEST(testStaticDefault);
254 CPPUNIT_TEST(testRefCounting);
255 CPPUNIT_TEST_SUITE_END();
259 CPPUNIT_TEST_SUITE_REGISTRATION(cow_wrapper_test);
261 CPPUNIT_PLUGIN_IMPLEMENT();
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */