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"
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
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() );
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(aTestObj1
.queryUnmodified(), 1);
115 aTestObj2
.modify( 2 );
116 CPPUNIT_ASSERT_EQUAL(aTestObj2
.queryUnmodified(), 2);
117 aTestObj3
.modify( 3 );
118 CPPUNIT_ASSERT_EQUAL(aTestObj3
.queryUnmodified(), 3);
120 aTestObj4
= cow_wrapper_client2( 4 );
121 CPPUNIT_ASSERT_EQUAL(aTestObj4
.queryUnmodified(), 4);
122 aTestObj5
.modify( 5 );
123 CPPUNIT_ASSERT_EQUAL(aTestObj5
.queryUnmodified(), 5);
124 aTestObj6
.modify( 6 );
125 CPPUNIT_ASSERT_EQUAL(aTestObj6
.queryUnmodified(), 6);
127 aTestObj7
= cow_wrapper_client3( 7 );
128 CPPUNIT_ASSERT_EQUAL(aTestObj7
.queryUnmodified(), 7);
129 aTestObj8
.modify( 8 );
130 CPPUNIT_ASSERT_EQUAL(aTestObj8
.queryUnmodified(), 8);
131 aTestObj9
.modify( 9 );
132 CPPUNIT_ASSERT_EQUAL(aTestObj9
.queryUnmodified(), 9);
134 // all three temporaries are dead now
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 // Change the following lines only, if you add, remove or rename
165 // member functions of the current class,
166 // because these macros are need by auto register mechanism.
168 CPPUNIT_TEST_SUITE(cow_wrapper_test
);
169 CPPUNIT_TEST(testCowWrapper
);
170 CPPUNIT_TEST(testStaticDefault
);
171 CPPUNIT_TEST_SUITE_END();
174 // -----------------------------------------------------------------------------
175 CPPUNIT_TEST_SUITE_REGISTRATION(cow_wrapper_test
);
177 CPPUNIT_PLUGIN_IMPLEMENT();
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */