1
/****************************************************************************
2
**
3
** Copyright (C) 2012 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
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
43
#include <QtTest/QtTest>
44
45
46
#include <q3strlist.h>
47
48
49
50
51
#include <qdatetime.h>
52
53
//TESTED_CLASS=
54
//TESTED_FILES=
55
56
class tst_Q3PtrList : public QObject
57
{
58
Q_OBJECT
59
60
public:
61
    tst_Q3PtrList();
62
    virtual ~tst_Q3PtrList();
63
64
65
public slots:
66
    void init();
67
    void cleanup();
68
private slots:
69
    void replace();
70
    void replaceStrDeep();
71
    void replaceStrShallow();
72
    void take();
73
    void removeType();
74
};
75
76
tst_Q3PtrList::tst_Q3PtrList()
77
{
78
}
79
80
tst_Q3PtrList::~tst_Q3PtrList()
81
{
82
83
}
84
85
void tst_Q3PtrList::init()
86
{
87
}
88
89
void tst_Q3PtrList::cleanup()
90
{
91
}
92
93
void tst_Q3PtrList::replace()
94
{
95
    Q3PtrList<int> list;
96
    int foo = 4;
97
    list.setAutoDelete( TRUE );
98
    QCOMPARE( list.insert(0, new int(1)), (bool)TRUE );
99
    QCOMPARE( list.insert(1, new int(2)), (bool)TRUE );
100
    QCOMPARE( list.insert(2, new int(4)), (bool)TRUE );
101
102
    QCOMPARE( *(list.at(2)), 4 );
103
    QCOMPARE( list.replace(2, new int(3)), (bool)TRUE );
104
    QCOMPARE( *(list.at(2)), 3 );
105
    uint count = list.count();
106
    QCOMPARE( list.replace(3, &foo), (bool)FALSE );
107
    QCOMPARE( list.count(), count );
108
109
    int *p = new int(7);
110
    QCOMPARE( list.insert(2, p), (bool)TRUE );
111
    QCOMPARE( list.replace(2, p), (bool)TRUE );
112
}
113
114
void tst_Q3PtrList::replaceStrDeep()
115
{
116
    Q3StrList list;
117
    const char *str;
118
119
    QCOMPARE( list.insert(0, "This is string 1"), (bool)TRUE );
120
    QCOMPARE( list.insert(1, "This is string 2"), (bool)TRUE );
121
    QCOMPARE( list.insert(2, "This is string 3"), (bool)TRUE );
122
123
    QCOMPARE( strcmp(list.at(2), "This is string 3"), 0 );
124
    QCOMPARE( list.replace(2, "Replaced String"), (bool)TRUE );
125
    QCOMPARE( strcmp(list.at(2), "Replaced String"), 0 );
126
    uint count = list.count();
127
128
    str = "TEST";
129
    QCOMPARE( list.replace(3, str), (bool)FALSE );
130
    QCOMPARE( list.count(), count );
131
132
    QCOMPARE( list.insert(2, str), (bool)TRUE );
133
    QCOMPARE( list.replace(2, str), (bool)TRUE );
134
}
135
136
void tst_Q3PtrList::replaceStrShallow()
137
{
138
    Q3StrList list( FALSE );
139
    char str1[] = "This is string 1";
140
    char str2[] = "This is string 2";
141
    char str3[] = "This is string 3";
142
    char str4[] = "Replace";
143
144
    QCOMPARE( list.insert(0, str1), (bool)TRUE );
145
    QCOMPARE( list.insert(1, str2), (bool)TRUE );
146
    QCOMPARE( list.insert(2, str3), (bool)TRUE );
147
148
    QCOMPARE( strcmp(list.at(2), str3), 0 );
149
    QCOMPARE( list.replace(2, str4), (bool)TRUE );
150
    QCOMPARE( strcmp(list.at(2), str4), 0 );
151
    uint count = list.count();
152
153
    char str[] = "TEST";
154
    QCOMPARE( list.replace(3, str), (bool)FALSE );
155
    QCOMPARE( list.count(), count );
156
157
    QCOMPARE( list.insert(2, str), (bool)TRUE );
158
    QCOMPARE( list.replace(2, str), (bool)TRUE );
159
}
160
161
void tst_Q3PtrList::take()
162
{
163
    Q3PtrList<int> list;
164
    QVERIFY(list.take(0) == 0);
165
    QVERIFY(list.take(list.count()) == 0);
166
}
167
168
void tst_Q3PtrList::removeType()
169
{
170
    Q3PtrList<QString> items;
171
    items.append(new QString("first"));
172
    QString *second = new QString("second");
173
    items.append(second);
174
    QString *third = new QString("third");
175
    items.append(third);
176
    QString *fourth = new QString("fourth");
177
    items.append(fourth);
178
179
    QVERIFY(items.current() == fourth);
180
    items.setAutoDelete(FALSE);
181
182
    // this test an undocumented feature of remove( NULL )
183
    // in QGList::remove if the ptr is 0 it removes the current item
184
    // ie. it removes the fourth item from the list in this case
185
    QString *nullPointer = NULL;
186
    items.remove( nullPointer );
187
    QVERIFY(items.count() == 3);
188
    QVERIFY(items.current() == third);
189
190
    // this tests that remove updates the current item also
191
    // when it removes the _end_ item in the list
192
    items.remove(third);
193
    QVERIFY(items.current() == second);
194
195
    // test that the removed items are not in the list, then deletes them
196
    QVERIFY(third && items.find(third) == -1 );
197
    QVERIFY(fourth && items.find(fourth) == -1);
198
    delete third;
199
    delete fourth;
200
    fourth = third = 0;
201
202
    items.setAutoDelete(TRUE);
203
}
204
205
QTEST_APPLESS_MAIN(tst_Q3PtrList)
206
#include "tst_q3ptrlist.moc"