ac596d63694616ea56d159f99098da38b7f07dec
[profile/ivi/qtxmlpatterns.git] / tests / auto / qautoptr / tst_qautoptr.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44
45 #include "private/qautoptr_p.h"
46
47 using namespace QPatternist;
48
49 /*!
50  \class tst_QAutoPtr
51  \internal
52  \since 4.4
53  \brief Tests class QAutoPtr.
54
55  */
56 class tst_QAutoPtr : public QObject
57 {
58     Q_OBJECT
59
60 private Q_SLOTS:
61     void defaultConstructor() const;
62     void copyConstructor() const;
63     void assignmentOperator() const;
64     void data() const;
65     void dataSignature() const;
66     void release() const;
67     void reset() const;
68     void onConstObject() const;
69     void dereferenceOperator() const;
70     void pointerOperator() const;
71     void pointerOperatorSignature() const;
72     void negationOperator() const;
73     void negationOperatorSignature() const;
74     void operatorBool() const;
75     void operatorBoolSignature() const;
76     /*
77      TODO:
78      - Test all the type hierarchy operators/constructors
79      - No code is at all calling AutoPtr& operator=(AutoPtrRef<T> ref) currently. Is it needed?
80      - No code is at all calling AutoPtrRef stuff. Is it needed?
81      - Equalness/unequalness operators?
82      - Test AutoPtr& operator=(AutoPtrRef<T> ref)
83      */
84 };
85
86 void tst_QAutoPtr::defaultConstructor() const
87 {
88     /* Check that the members, one, is correctly initialized. */
89     AutoPtr<int> p;
90     QCOMPARE(p.data(), static_cast<int *>(0));
91 }
92
93 void tst_QAutoPtr::copyConstructor() const
94 {
95     /* Copy default constructed value. */
96     {
97         AutoPtr<int> p1;
98         AutoPtr<int> p2(p1);
99         QCOMPARE(p2.data(), static_cast<int *>(0));
100     }
101
102     /* Copy active value. */
103     {
104         AutoPtr<int> p1(new int(7));
105         AutoPtr<int> p2(p1);
106         QCOMPARE(*p2.data(), 7);
107         QCOMPARE(p1.data(), static_cast<int *>(0));
108     }
109 }
110
111 void tst_QAutoPtr::assignmentOperator() const
112 {
113     /* Assign to self, a default constructed value. */
114     {
115         AutoPtr<int> p1;
116         p1 = p1;
117         p1 = p1;
118         p1 = p1;
119     }
120
121     /* Assign to a default constructed value. */
122     {
123         AutoPtr<int> p1;
124         AutoPtr<int> p2;
125         p1 = p2;
126         p1 = p2;
127         p1 = p2;
128     }
129
130     /* Assign to an active value. */
131     {
132         AutoPtr<int> p1(new int(6));
133         AutoPtr<int> p2;
134         p1 = p2;
135         p1 = p2;
136         p1 = p2;
137     }
138
139     /* Assign from an active value. */
140     {
141         int *const ptr =  new int(6);
142         AutoPtr<int> p1(ptr);
143         AutoPtr<int> p2;
144         p2 = p1;
145
146         QCOMPARE(p2.data(), ptr);
147         /* p1 should have reset. */
148         QCOMPARE(p1.data(), static_cast<int *>(0));
149     }
150 }
151
152 void tst_QAutoPtr::data() const
153 {
154     AutoPtr<int> p;
155
156     QCOMPARE(p.data(), static_cast<int *>(0));
157 }
158
159 void tst_QAutoPtr::dataSignature() const
160 {
161     const AutoPtr<int> p;
162     /* data() should be const. */
163     p.data();
164 }
165
166 void tst_QAutoPtr::release() const
167 {
168     /* Call release() on a default constructed value. */
169     {
170         AutoPtr<int> p;
171         QCOMPARE(p.release(), static_cast<int *>(0));
172     }
173
174     /* Call release() on an active value, it shouldn't delete. */
175     {
176         int value = 3;
177         AutoPtr<int> p(&value);
178         p.release();
179     }
180 }
181
182 void tst_QAutoPtr::reset() const
183 {
184     /* Call reset() on a default constructed value. */
185     {
186         AutoPtr<int> p;
187         p.reset();
188     }
189
190     /* Call reset() on an active value. */
191     {
192         AutoPtr<int> p(new int(3));
193         p.reset();
194     }
195
196     /* Call reset() with a value, on an active value. */
197     {
198         AutoPtr<int> p(new int(3));
199
200         int *const value = new int(9);
201         p.reset(value);
202         QCOMPARE(*p.data(), 9);
203     }
204
205     /* Call reset() with a value, on default constructed value. */
206     {
207         AutoPtr<int> p;
208
209         int *const value = new int(9);
210         p.reset(value);
211         QCOMPARE(*p.data(), 9);
212     }
213 }
214
215 void tst_QAutoPtr::onConstObject() const
216 {
217     /* Instansiate on a const object. */
218     AutoPtr<const int> p(new int(3));
219     p.reset();
220     p.data();
221     p.release();
222     p = p;
223 }
224
225 class AbstractClass
226 {
227 public:
228     virtual ~AbstractClass()
229     {
230     }
231
232     virtual int member() const = 0;
233 };
234
235 class SubClass : public AbstractClass
236 {
237 public:
238     virtual int member() const
239     {
240         return 5;
241     }
242 };
243
244 void tst_QAutoPtr::dereferenceOperator() const
245 {
246     /* Dereference a basic value. */
247     {
248         int value = 5;
249         AutoPtr<int> p(&value);
250
251         const int value2 = *p;
252         QCOMPARE(value2, 5);
253         p.release();
254     }
255
256     /* Dereference a pointer to an abstract class. This verifies
257      * that the operator returns a reference, when compiling
258      * with MSVC 2005. */
259     {
260         AutoPtr<AbstractClass> p(new SubClass());
261
262         QCOMPARE((*p).member(), 5);
263     }
264 }
265
266 class AnyForm
267 {
268 public:
269     int value;
270 };
271
272 void tst_QAutoPtr::pointerOperator() const
273 {
274     AnyForm af;
275     af.value = 5;
276     AutoPtr<AnyForm> p(&af);
277
278     QCOMPARE(p->value, 5);
279     p.release();
280 }
281
282 void tst_QAutoPtr::pointerOperatorSignature() const
283 {
284     /* The operator should be const. */
285     const AutoPtr<AnyForm> p(new AnyForm);
286     p->value = 5;
287
288     QVERIFY(p->value);
289 }
290
291 void tst_QAutoPtr::negationOperator() const
292 {
293     /* Invoke on default constructed value. */
294     {
295         AutoPtr<int> p;
296         QVERIFY(!p);
297     }
298 }
299
300 void tst_QAutoPtr::negationOperatorSignature() const
301 {
302     /* The signature should be const. */
303     const AutoPtr<int> p;
304     QVERIFY(!p);
305
306     /* The return value should be bool. */
307     QCOMPARE(!p, true);
308 }
309
310 void tst_QAutoPtr::operatorBool() const
311 {
312     /* Invoke on default constructed value. */
313     {
314         AutoPtr<int> p;
315         QCOMPARE(bool(p), false);
316     }
317
318     /* Invoke on active value. */
319     {
320         AutoPtr<int> p(new int(3));
321         QVERIFY(p);
322     }
323 }
324
325 void tst_QAutoPtr::operatorBoolSignature() const
326 {
327     /* The signature should be const. */
328     const AutoPtr<int> p;
329     QCOMPARE(bool(p), false);
330 }
331
332 QTEST_MAIN(tst_QAutoPtr)
333
334 #include "tst_qautoptr.moc"