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