|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <QtCore/QSharedData> |
|
45 |
|
46 /*! |
|
47 \class tst_QExplicitlySharedDataPointer |
|
48 \internal |
|
49 \since 4.4 |
|
50 \brief Tests class QExplicitlySharedDataPointer. |
|
51 |
|
52 */ |
|
53 class tst_QExplicitlySharedDataPointer : public QObject |
|
54 { |
|
55 Q_OBJECT |
|
56 |
|
57 private Q_SLOTS: |
|
58 void pointerOperatorOnConst() const; |
|
59 void pointerOperatorOnMutable() const; |
|
60 void copyConstructor() const; |
|
61 void clone() const; |
|
62 void data() const; |
|
63 void reset() const; |
|
64 void swap() const; |
|
65 }; |
|
66 |
|
67 class MyClass : public QSharedData |
|
68 { |
|
69 public: |
|
70 void mutating() |
|
71 { |
|
72 } |
|
73 |
|
74 void notMutating() const |
|
75 { |
|
76 } |
|
77 |
|
78 MyClass &operator=(const MyClass &) |
|
79 { |
|
80 return *this; |
|
81 } |
|
82 }; |
|
83 |
|
84 class Base : public QSharedData |
|
85 { |
|
86 public: |
|
87 virtual ~Base() { } |
|
88 virtual Base *clone() { return new Base(*this); } |
|
89 virtual bool isBase() const { return true; } |
|
90 }; |
|
91 |
|
92 class Derived : public Base |
|
93 { |
|
94 public: |
|
95 virtual Base *clone() { return new Derived(*this); } |
|
96 virtual bool isBase() const { return false; } |
|
97 }; |
|
98 |
|
99 QT_BEGIN_NAMESPACE |
|
100 template<> Base *QExplicitlySharedDataPointer<Base>::clone() |
|
101 { |
|
102 return d->clone(); |
|
103 } |
|
104 QT_END_NAMESPACE |
|
105 |
|
106 void tst_QExplicitlySharedDataPointer::pointerOperatorOnConst() const |
|
107 { |
|
108 /* Pointer itself is const. */ |
|
109 { |
|
110 const QExplicitlySharedDataPointer<const MyClass> pointer(new MyClass()); |
|
111 pointer->notMutating(); |
|
112 } |
|
113 |
|
114 /* Pointer itself is mutable. */ |
|
115 { |
|
116 QExplicitlySharedDataPointer<const MyClass> pointer(new MyClass()); |
|
117 pointer->notMutating(); |
|
118 } |
|
119 } |
|
120 |
|
121 void tst_QExplicitlySharedDataPointer::pointerOperatorOnMutable() const |
|
122 { |
|
123 /* Pointer itself is const. */ |
|
124 { |
|
125 const QExplicitlySharedDataPointer<MyClass> pointer(new MyClass()); |
|
126 pointer->notMutating(); |
|
127 pointer->mutating(); |
|
128 *pointer = MyClass(); |
|
129 } |
|
130 |
|
131 /* Pointer itself is mutable. */ |
|
132 { |
|
133 const QExplicitlySharedDataPointer<MyClass> pointer(new MyClass()); |
|
134 pointer->notMutating(); |
|
135 pointer->mutating(); |
|
136 *pointer = MyClass(); |
|
137 } |
|
138 } |
|
139 |
|
140 void tst_QExplicitlySharedDataPointer::copyConstructor() const |
|
141 { |
|
142 const QExplicitlySharedDataPointer<const MyClass> pointer(new MyClass()); |
|
143 const QExplicitlySharedDataPointer<const MyClass> copy(pointer); |
|
144 } |
|
145 |
|
146 void tst_QExplicitlySharedDataPointer::clone() const |
|
147 { |
|
148 /* holding a base element */ |
|
149 { |
|
150 QExplicitlySharedDataPointer<Base> pointer(new Base); |
|
151 QVERIFY(pointer->isBase()); |
|
152 |
|
153 QExplicitlySharedDataPointer<Base> copy(pointer); |
|
154 pointer.detach(); |
|
155 QVERIFY(pointer->isBase()); |
|
156 } |
|
157 |
|
158 /* holding a derived element */ |
|
159 { |
|
160 QExplicitlySharedDataPointer<Base> pointer(new Derived); |
|
161 QVERIFY(!pointer->isBase()); |
|
162 |
|
163 QExplicitlySharedDataPointer<Base> copy(pointer); |
|
164 pointer.detach(); |
|
165 QVERIFY(!pointer->isBase()); |
|
166 } |
|
167 } |
|
168 |
|
169 void tst_QExplicitlySharedDataPointer::data() const |
|
170 { |
|
171 /* Check default value. */ |
|
172 { |
|
173 QExplicitlySharedDataPointer<const MyClass> pointer; |
|
174 QCOMPARE(pointer.data(), static_cast<const MyClass *>(0)); |
|
175 } |
|
176 |
|
177 /* On const pointer. Must not mutate the pointer. */ |
|
178 { |
|
179 const QExplicitlySharedDataPointer<const MyClass> pointer(new MyClass()); |
|
180 pointer.data(); |
|
181 |
|
182 /* Check that this cast is possible. */ |
|
183 static_cast<const MyClass *>(pointer.data()); |
|
184 } |
|
185 |
|
186 /* On mutatable pointer. Must not mutate the pointer. */ |
|
187 { |
|
188 QExplicitlySharedDataPointer<const MyClass> pointer(new MyClass()); |
|
189 pointer.data(); |
|
190 |
|
191 /* Check that this cast is possible. */ |
|
192 static_cast<const MyClass *>(pointer.data()); |
|
193 } |
|
194 |
|
195 /* Must not mutate the pointer. */ |
|
196 { |
|
197 const QExplicitlySharedDataPointer<MyClass> pointer(new MyClass()); |
|
198 pointer.data(); |
|
199 |
|
200 /* Check that these casts are possible. */ |
|
201 static_cast<MyClass *>(pointer.data()); |
|
202 static_cast<const MyClass *>(pointer.data()); |
|
203 } |
|
204 |
|
205 /* Must not mutate the pointer. */ |
|
206 { |
|
207 QExplicitlySharedDataPointer<MyClass> pointer(new MyClass()); |
|
208 pointer.data(); |
|
209 |
|
210 /* Check that these casts are possible. */ |
|
211 static_cast<MyClass *>(pointer.data()); |
|
212 static_cast<const MyClass *>(pointer.data()); |
|
213 } |
|
214 } |
|
215 |
|
216 void tst_QExplicitlySharedDataPointer::reset() const |
|
217 { |
|
218 /* Do reset on a single ref count. */ |
|
219 { |
|
220 QExplicitlySharedDataPointer<MyClass> pointer(new MyClass()); |
|
221 QVERIFY(pointer.data() != 0); |
|
222 |
|
223 pointer.reset(); |
|
224 QCOMPARE(pointer.data(), static_cast<MyClass *>(0)); |
|
225 } |
|
226 |
|
227 /* Do reset on a default constructed object. */ |
|
228 { |
|
229 QExplicitlySharedDataPointer<MyClass> pointer; |
|
230 QCOMPARE(pointer.data(), static_cast<MyClass *>(0)); |
|
231 |
|
232 pointer.reset(); |
|
233 QCOMPARE(pointer.data(), static_cast<MyClass *>(0)); |
|
234 } |
|
235 } |
|
236 |
|
237 void tst_QExplicitlySharedDataPointer::swap() const |
|
238 { |
|
239 QExplicitlySharedDataPointer<MyClass> p1(0), p2(new MyClass()); |
|
240 QVERIFY(!p1.data()); |
|
241 QVERIFY(p2.data()); |
|
242 |
|
243 p1.swap(p2); |
|
244 QVERIFY(p1.data()); |
|
245 QVERIFY(!p2.data()); |
|
246 |
|
247 p1.swap(p2); |
|
248 QVERIFY(!p1.data()); |
|
249 QVERIFY(p2.data()); |
|
250 |
|
251 qSwap(p1, p2); |
|
252 QVERIFY(p1.data()); |
|
253 QVERIFY(!p2.data()); |
|
254 } |
|
255 |
|
256 QTEST_MAIN(tst_QExplicitlySharedDataPointer) |
|
257 |
|
258 #include "tst_qexplicitlyshareddatapointer.moc" |
|
259 // vim: et:ts=4:sw=4:sts=4 |