summaryrefslogtreecommitdiff
path: root/tests/auto/qautoptr/tst_qautoptr.cpp
blob: 193effad060f8c9f367619fef0ad82bcf4843ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#include "private/qautoptr_p.h"

using namespace QPatternist;

/*!
 \class tst_QAutoPtr
 \internal
 \since 4.4
 \brief Tests class QAutoPtr.

 */
class tst_QAutoPtr : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void defaultConstructor() const;
    void copyConstructor() const;
    void assignmentOperator() const;
    void data() const;
    void dataSignature() const;
    void release() const;
    void reset() const;
    void onConstObject() const;
    void dereferenceOperator() const;
    void pointerOperator() const;
    void pointerOperatorSignature() const;
    void negationOperator() const;
    void negationOperatorSignature() const;
    void operatorBool() const;
    void operatorBoolSignature() const;
    /*
     TODO:
     - Test all the type hierarchy operators/constructors
     - No code is at all calling AutoPtr& operator=(AutoPtrRef<T> ref) currently. Is it needed?
     - No code is at all calling AutoPtrRef stuff. Is it needed?
     - Equalness/unequalness operators?
     - Test AutoPtr& operator=(AutoPtrRef<T> ref)
     */
};

void tst_QAutoPtr::defaultConstructor() const
{
    /* Check that the members, one, is correctly initialized. */
    AutoPtr<int> p;
    QCOMPARE(p.data(), static_cast<int *>(0));
}

void tst_QAutoPtr::copyConstructor() const
{
    /* Copy default constructed value. */
    {
        AutoPtr<int> p1;
        AutoPtr<int> p2(p1);
        QCOMPARE(p2.data(), static_cast<int *>(0));
    }

    /* Copy active value. */
    {
        AutoPtr<int> p1(new int(7));
        AutoPtr<int> p2(p1);
        QCOMPARE(*p2.data(), 7);
        QCOMPARE(p1.data(), static_cast<int *>(0));
    }
}

void tst_QAutoPtr::assignmentOperator() const
{
    /* Assign to self, a default constructed value. */
    {
        AutoPtr<int> p1;
        p1 = p1;
        p1 = p1;
        p1 = p1;
    }

    /* Assign to a default constructed value. */
    {
        AutoPtr<int> p1;
        AutoPtr<int> p2;
        p1 = p2;
        p1 = p2;
        p1 = p2;
    }

    /* Assign to an active value. */
    {
        AutoPtr<int> p1(new int(6));
        AutoPtr<int> p2;
        p1 = p2;
        p1 = p2;
        p1 = p2;
    }

    /* Assign from an active value. */
    {
        int *const ptr =  new int(6);
        AutoPtr<int> p1(ptr);
        AutoPtr<int> p2;
        p2 = p1;

        QCOMPARE(p2.data(), ptr);
        /* p1 should have reset. */
        QCOMPARE(p1.data(), static_cast<int *>(0));
    }
}

void tst_QAutoPtr::data() const
{
    AutoPtr<int> p;

    QCOMPARE(p.data(), static_cast<int *>(0));
}

void tst_QAutoPtr::dataSignature() const
{
    const AutoPtr<int> p;
    /* data() should be const. */
    p.data();
}

void tst_QAutoPtr::release() const
{
    /* Call release() on a default constructed value. */
    {
        AutoPtr<int> p;
        QCOMPARE(p.release(), static_cast<int *>(0));
    }

    /* Call release() on an active value, it shouldn't delete. */
    {
        int value = 3;
        AutoPtr<int> p(&value);
        p.release();
    }
}

void tst_QAutoPtr::reset() const
{
    /* Call reset() on a default constructed value. */
    {
        AutoPtr<int> p;
        p.reset();
    }

    /* Call reset() on an active value. */
    {
        AutoPtr<int> p(new int(3));
        p.reset();
    }

    /* Call reset() with a value, on an active value. */
    {
        AutoPtr<int> p(new int(3));

        int *const value = new int(9);
        p.reset(value);
        QCOMPARE(*p.data(), 9);
    }

    /* Call reset() with a value, on default constructed value. */
    {
        AutoPtr<int> p;

        int *const value = new int(9);
        p.reset(value);
        QCOMPARE(*p.data(), 9);
    }
}

void tst_QAutoPtr::onConstObject() const
{
    /* Instansiate on a const object. */
    AutoPtr<const int> p(new int(3));
    p.reset();
    p.data();
    p.release();
    p = p;
}

class AbstractClass
{
public:
    virtual ~AbstractClass()
    {
    }

    virtual int member() const = 0;
};

class SubClass : public AbstractClass
{
public:
    virtual int member() const
    {
        return 5;
    }
};

void tst_QAutoPtr::dereferenceOperator() const
{
    /* Dereference a basic value. */
    {
        int value = 5;
        AutoPtr<int> p(&value);

        const int value2 = *p;
        QCOMPARE(value2, 5);
        p.release();
    }

    /* Dereference a pointer to an abstract class. This verifies
     * that the operator returns a reference, when compiling
     * with MSVC 2005. */
    {
        AutoPtr<AbstractClass> p(new SubClass());

        QCOMPARE((*p).member(), 5);
    }
}

class AnyForm
{
public:
    int value;
};

void tst_QAutoPtr::pointerOperator() const
{
    AnyForm af;
    af.value = 5;
    AutoPtr<AnyForm> p(&af);

    QCOMPARE(p->value, 5);
    p.release();
}

void tst_QAutoPtr::pointerOperatorSignature() const
{
    /* The operator should be const. */
    const AutoPtr<AnyForm> p(new AnyForm);
    p->value = 5;

    QVERIFY(p->value);
}

void tst_QAutoPtr::negationOperator() const
{
    /* Invoke on default constructed value. */
    {
        AutoPtr<int> p;
        QVERIFY(!p);
    }
}

void tst_QAutoPtr::negationOperatorSignature() const
{
    /* The signature should be const. */
    const AutoPtr<int> p;
    QVERIFY(!p);

    /* The return value should be bool. */
    QCOMPARE(!p, true);
}

void tst_QAutoPtr::operatorBool() const
{
    /* Invoke on default constructed value. */
    {
        AutoPtr<int> p;
        QCOMPARE(bool(p), false);
    }

    /* Invoke on active value. */
    {
        AutoPtr<int> p(new int(3));
        QVERIFY(p);
    }
}

void tst_QAutoPtr::operatorBoolSignature() const
{
    /* The signature should be const. */
    const AutoPtr<int> p;
    QCOMPARE(bool(p), false);
}

QTEST_MAIN(tst_QAutoPtr)

#include "tst_qautoptr.moc"