|
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 |
|
45 #ifdef QTEST_XMLPATTERNS |
|
46 |
|
47 #include "private/qautoptr_p.h" |
|
48 |
|
49 using namespace QPatternist; |
|
50 |
|
51 /*! |
|
52 \class tst_QAutoPtr |
|
53 \internal |
|
54 \since 4.4 |
|
55 \brief Tests class QAutoPtr. |
|
56 |
|
57 */ |
|
58 class tst_QAutoPtr : public QObject |
|
59 { |
|
60 Q_OBJECT |
|
61 |
|
62 private Q_SLOTS: |
|
63 void defaultConstructor() const; |
|
64 void copyConstructor() const; |
|
65 void assignmentOperator() const; |
|
66 void data() const; |
|
67 void dataSignature() const; |
|
68 void release() const; |
|
69 void reset() const; |
|
70 void onConstObject() const; |
|
71 void dereferenceOperator() const; |
|
72 void pointerOperator() const; |
|
73 void pointerOperatorSignature() const; |
|
74 void negationOperator() const; |
|
75 void negationOperatorSignature() const; |
|
76 void operatorBool() const; |
|
77 void operatorBoolSignature() const; |
|
78 /* |
|
79 TODO: |
|
80 - Test all the type hierarchy operators/constructors |
|
81 - No code is at all calling AutoPtr& operator=(AutoPtrRef<T> ref) currently. Is it needed? |
|
82 - No code is at all calling AutoPtrRef stuff. Is it needed? |
|
83 - Equalness/unequalness operators? |
|
84 - Test AutoPtr& operator=(AutoPtrRef<T> ref) |
|
85 */ |
|
86 }; |
|
87 |
|
88 void tst_QAutoPtr::defaultConstructor() const |
|
89 { |
|
90 /* Check that the members, one, is correctly initialized. */ |
|
91 AutoPtr<int> p; |
|
92 QCOMPARE(p.data(), static_cast<int *>(0)); |
|
93 } |
|
94 |
|
95 void tst_QAutoPtr::copyConstructor() const |
|
96 { |
|
97 /* Copy default constructed value. */ |
|
98 { |
|
99 AutoPtr<int> p1; |
|
100 AutoPtr<int> p2(p1); |
|
101 QCOMPARE(p2.data(), static_cast<int *>(0)); |
|
102 } |
|
103 |
|
104 /* Copy active value. */ |
|
105 { |
|
106 AutoPtr<int> p1(new int(7)); |
|
107 AutoPtr<int> p2(p1); |
|
108 QCOMPARE(*p2.data(), 7); |
|
109 QCOMPARE(p1.data(), static_cast<int *>(0)); |
|
110 } |
|
111 } |
|
112 |
|
113 void tst_QAutoPtr::assignmentOperator() const |
|
114 { |
|
115 /* Assign to self, a default constructed value. */ |
|
116 { |
|
117 AutoPtr<int> p1; |
|
118 p1 = p1; |
|
119 p1 = p1; |
|
120 p1 = p1; |
|
121 } |
|
122 |
|
123 /* Assign to a default constructed value. */ |
|
124 { |
|
125 AutoPtr<int> p1; |
|
126 AutoPtr<int> p2; |
|
127 p1 = p2; |
|
128 p1 = p2; |
|
129 p1 = p2; |
|
130 } |
|
131 |
|
132 /* Assign to an active value. */ |
|
133 { |
|
134 AutoPtr<int> p1(new int(6)); |
|
135 AutoPtr<int> p2; |
|
136 p1 = p2; |
|
137 p1 = p2; |
|
138 p1 = p2; |
|
139 } |
|
140 |
|
141 /* Assign from an active value. */ |
|
142 { |
|
143 int *const ptr = new int(6); |
|
144 AutoPtr<int> p1(ptr); |
|
145 AutoPtr<int> p2; |
|
146 p2 = p1; |
|
147 |
|
148 QCOMPARE(p2.data(), ptr); |
|
149 /* p1 should have reset. */ |
|
150 QCOMPARE(p1.data(), static_cast<int *>(0)); |
|
151 } |
|
152 } |
|
153 |
|
154 void tst_QAutoPtr::data() const |
|
155 { |
|
156 AutoPtr<int> p; |
|
157 |
|
158 QCOMPARE(p.data(), static_cast<int *>(0)); |
|
159 } |
|
160 |
|
161 void tst_QAutoPtr::dataSignature() const |
|
162 { |
|
163 const AutoPtr<int> p; |
|
164 /* data() should be const. */ |
|
165 p.data(); |
|
166 } |
|
167 |
|
168 void tst_QAutoPtr::release() const |
|
169 { |
|
170 /* Call release() on a default constructed value. */ |
|
171 { |
|
172 AutoPtr<int> p; |
|
173 QCOMPARE(p.release(), static_cast<int *>(0)); |
|
174 } |
|
175 |
|
176 /* Call release() on an active value, it shouldn't delete. */ |
|
177 { |
|
178 int value = 3; |
|
179 AutoPtr<int> p(&value); |
|
180 p.release(); |
|
181 } |
|
182 } |
|
183 |
|
184 void tst_QAutoPtr::reset() const |
|
185 { |
|
186 /* Call reset() on a default constructed value. */ |
|
187 { |
|
188 AutoPtr<int> p; |
|
189 p.reset(); |
|
190 } |
|
191 |
|
192 /* Call reset() on an active value. */ |
|
193 { |
|
194 AutoPtr<int> p(new int(3)); |
|
195 p.reset(); |
|
196 } |
|
197 |
|
198 /* Call reset() with a value, on an active value. */ |
|
199 { |
|
200 AutoPtr<int> p(new int(3)); |
|
201 |
|
202 int *const value = new int(9); |
|
203 p.reset(value); |
|
204 QCOMPARE(*p.data(), 9); |
|
205 } |
|
206 |
|
207 /* Call reset() with a value, on default constructed value. */ |
|
208 { |
|
209 AutoPtr<int> p; |
|
210 |
|
211 int *const value = new int(9); |
|
212 p.reset(value); |
|
213 QCOMPARE(*p.data(), 9); |
|
214 } |
|
215 } |
|
216 |
|
217 void tst_QAutoPtr::onConstObject() const |
|
218 { |
|
219 /* Instansiate on a const object. */ |
|
220 AutoPtr<const int> p(new int(3)); |
|
221 p.reset(); |
|
222 p.data(); |
|
223 p.release(); |
|
224 p = p; |
|
225 } |
|
226 |
|
227 class AbstractClass |
|
228 { |
|
229 public: |
|
230 virtual ~AbstractClass() |
|
231 { |
|
232 } |
|
233 |
|
234 virtual int member() const = 0; |
|
235 }; |
|
236 |
|
237 class SubClass : public AbstractClass |
|
238 { |
|
239 public: |
|
240 virtual int member() const |
|
241 { |
|
242 return 5; |
|
243 } |
|
244 }; |
|
245 |
|
246 void tst_QAutoPtr::dereferenceOperator() const |
|
247 { |
|
248 /* Dereference a basic value. */ |
|
249 { |
|
250 int value = 5; |
|
251 AutoPtr<int> p(&value); |
|
252 |
|
253 const int value2 = *p; |
|
254 QCOMPARE(value2, 5); |
|
255 p.release(); |
|
256 } |
|
257 |
|
258 /* Dereference a pointer to an abstract class. This verifies |
|
259 * that the operator returns a reference, when compiling |
|
260 * with MSVC 2005. */ |
|
261 { |
|
262 AutoPtr<AbstractClass> p(new SubClass()); |
|
263 |
|
264 QCOMPARE((*p).member(), 5); |
|
265 } |
|
266 } |
|
267 |
|
268 class AnyForm |
|
269 { |
|
270 public: |
|
271 int value; |
|
272 }; |
|
273 |
|
274 void tst_QAutoPtr::pointerOperator() const |
|
275 { |
|
276 AnyForm af; |
|
277 af.value = 5; |
|
278 AutoPtr<AnyForm> p(&af); |
|
279 |
|
280 QCOMPARE(p->value, 5); |
|
281 p.release(); |
|
282 } |
|
283 |
|
284 void tst_QAutoPtr::pointerOperatorSignature() const |
|
285 { |
|
286 /* The operator should be const. */ |
|
287 const AutoPtr<AnyForm> p(new AnyForm); |
|
288 p->value = 5; |
|
289 |
|
290 QVERIFY(p->value); |
|
291 } |
|
292 |
|
293 void tst_QAutoPtr::negationOperator() const |
|
294 { |
|
295 /* Invoke on default constructed value. */ |
|
296 { |
|
297 AutoPtr<int> p; |
|
298 QVERIFY(!p); |
|
299 } |
|
300 } |
|
301 |
|
302 void tst_QAutoPtr::negationOperatorSignature() const |
|
303 { |
|
304 /* The signature should be const. */ |
|
305 const AutoPtr<int> p; |
|
306 QVERIFY(!p); |
|
307 |
|
308 /* The return value should be bool. */ |
|
309 QCOMPARE(!p, true); |
|
310 } |
|
311 |
|
312 void tst_QAutoPtr::operatorBool() const |
|
313 { |
|
314 /* Invoke on default constructed value. */ |
|
315 { |
|
316 AutoPtr<int> p; |
|
317 QCOMPARE(bool(p), false); |
|
318 } |
|
319 |
|
320 /* Invoke on active value. */ |
|
321 { |
|
322 AutoPtr<int> p(new int(3)); |
|
323 QVERIFY(p); |
|
324 } |
|
325 } |
|
326 |
|
327 void tst_QAutoPtr::operatorBoolSignature() const |
|
328 { |
|
329 /* The signature should be const. */ |
|
330 const AutoPtr<int> p; |
|
331 QCOMPARE(bool(p), false); |
|
332 } |
|
333 |
|
334 QTEST_MAIN(tst_QAutoPtr) |
|
335 |
|
336 #include "tst_qautoptr.moc" |
|
337 #else |
|
338 QTEST_NOOP_MAIN |
|
339 #endif |