|
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 #include <qregexp.h> |
|
45 #include <qstringlist.h> |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 //TESTED_CLASS= |
|
52 //TESTED_FILES= |
|
53 |
|
54 class tst_QStringList : public QObject |
|
55 { |
|
56 Q_OBJECT |
|
57 |
|
58 public: |
|
59 tst_QStringList(); |
|
60 virtual ~tst_QStringList(); |
|
61 |
|
62 public slots: |
|
63 void init(); |
|
64 void cleanup(); |
|
65 private slots: |
|
66 void filter(); |
|
67 void replaceInStrings(); |
|
68 void removeDuplicates(); |
|
69 void removeDuplicates_data(); |
|
70 void contains(); |
|
71 void indexOf(); |
|
72 void lastIndexOf(); |
|
73 |
|
74 void indexOf_regExp(); |
|
75 void lastIndexOf_regExp(); |
|
76 |
|
77 void streamingOperator(); |
|
78 void join() const; |
|
79 void join_data() const; |
|
80 }; |
|
81 |
|
82 extern const char email[]; |
|
83 |
|
84 tst_QStringList::tst_QStringList() |
|
85 { |
|
86 } |
|
87 |
|
88 tst_QStringList::~tst_QStringList() |
|
89 { |
|
90 } |
|
91 |
|
92 void tst_QStringList::init() |
|
93 { |
|
94 } |
|
95 |
|
96 void tst_QStringList::cleanup() |
|
97 { |
|
98 } |
|
99 |
|
100 void tst_QStringList::indexOf_regExp() |
|
101 { |
|
102 QStringList list; |
|
103 list << "harald" << "trond" << "vohi" << "harald"; |
|
104 |
|
105 QRegExp re(".*o.*"); |
|
106 |
|
107 QCOMPARE(list.indexOf(re), 1); |
|
108 QCOMPARE(list.indexOf(re, 2), 2); |
|
109 QCOMPARE(list.indexOf(re, 3), -1); |
|
110 |
|
111 QCOMPARE(list.indexOf(QRegExp(".*x.*")), -1); |
|
112 QCOMPARE(list.indexOf(re, -1), -1); |
|
113 QCOMPARE(list.indexOf(re, -3), 1); |
|
114 QCOMPARE(list.indexOf(re, -9999), 1); |
|
115 QCOMPARE(list.indexOf(re, 9999), -1); |
|
116 } |
|
117 |
|
118 void tst_QStringList::lastIndexOf_regExp() |
|
119 { |
|
120 QStringList list; |
|
121 list << "harald" << "trond" << "vohi" << "harald"; |
|
122 |
|
123 QRegExp re(".*o.*"); |
|
124 |
|
125 QCOMPARE(list.lastIndexOf(re), 2); |
|
126 QCOMPARE(list.lastIndexOf(re, 2), 2); |
|
127 QCOMPARE(list.lastIndexOf(re, 1), 1); |
|
128 |
|
129 QCOMPARE(list.lastIndexOf(QRegExp(".*x.*")), -1); |
|
130 QCOMPARE(list.lastIndexOf(re, -1), 2); |
|
131 QCOMPARE(list.lastIndexOf(re, -3), 1); |
|
132 QCOMPARE(list.lastIndexOf(re, -9999), -1); |
|
133 QCOMPARE(list.lastIndexOf(re, 9999), 2); |
|
134 } |
|
135 |
|
136 void tst_QStringList::indexOf() |
|
137 { |
|
138 QStringList list; |
|
139 list << "harald" << "trond" << "vohi" << "harald"; |
|
140 |
|
141 QCOMPARE(list.indexOf("harald"), 0); |
|
142 QCOMPARE(list.indexOf("trond"), 1); |
|
143 QCOMPARE(list.indexOf("vohi"), 2); |
|
144 QCOMPARE(list.indexOf("harald", 1), 3); |
|
145 |
|
146 QCOMPARE(list.indexOf("hans"), -1); |
|
147 QCOMPARE(list.indexOf("trond", 2), -1); |
|
148 QCOMPARE(list.indexOf("harald", -1), 3); |
|
149 QCOMPARE(list.indexOf("vohi", -3), 2); |
|
150 } |
|
151 |
|
152 void tst_QStringList::lastIndexOf() |
|
153 { |
|
154 QStringList list; |
|
155 list << "harald" << "trond" << "vohi" << "harald"; |
|
156 |
|
157 QCOMPARE(list.lastIndexOf("harald"), 3); |
|
158 QCOMPARE(list.lastIndexOf("trond"), 1); |
|
159 QCOMPARE(list.lastIndexOf("vohi"), 2); |
|
160 QCOMPARE(list.lastIndexOf("harald", 2), 0); |
|
161 |
|
162 QCOMPARE(list.lastIndexOf("hans"), -1); |
|
163 QCOMPARE(list.lastIndexOf("vohi", 1), -1); |
|
164 QCOMPARE(list.lastIndexOf("vohi", -1), 2); |
|
165 QCOMPARE(list.lastIndexOf("vohi", -3), -1); |
|
166 } |
|
167 |
|
168 void tst_QStringList::filter() |
|
169 { |
|
170 QStringList list1, list2; |
|
171 list1 << "Bill Gates" << "Joe Blow" << "Bill Clinton"; |
|
172 list1 = list1.filter( "Bill" ); |
|
173 list2 << "Bill Gates" << "Bill Clinton"; |
|
174 QCOMPARE( list1, list2 ); |
|
175 |
|
176 QStringList list3, list4; |
|
177 list3 << "Bill Gates" << "Joe Blow" << "Bill Clinton"; |
|
178 list3 = list3.filter( QRegExp("[i]ll") ); |
|
179 list4 << "Bill Gates" << "Bill Clinton"; |
|
180 QCOMPARE( list3, list4 ); |
|
181 } |
|
182 |
|
183 void tst_QStringList::replaceInStrings() |
|
184 { |
|
185 QStringList list1, list2; |
|
186 list1 << "alpha" << "beta" << "gamma" << "epsilon"; |
|
187 list1.replaceInStrings( "a", "o" ); |
|
188 list2 << "olpho" << "beto" << "gommo" << "epsilon"; |
|
189 QCOMPARE( list1, list2 ); |
|
190 |
|
191 QStringList list3, list4; |
|
192 list3 << "alpha" << "beta" << "gamma" << "epsilon"; |
|
193 list3.replaceInStrings( QRegExp("^a"), "o" ); |
|
194 list4 << "olpha" << "beta" << "gamma" << "epsilon"; |
|
195 QCOMPARE( list3, list4 ); |
|
196 |
|
197 QStringList list5, list6; |
|
198 list5 << "Bill Clinton" << "Gates, Bill"; |
|
199 list6 << "Bill Clinton" << "Bill Gates"; |
|
200 list5.replaceInStrings( QRegExp("^(.*), (.*)$"), "\\2 \\1" ); |
|
201 QCOMPARE( list5, list6 ); |
|
202 } |
|
203 |
|
204 void tst_QStringList::contains() |
|
205 { |
|
206 QStringList list; |
|
207 list << "arthur" << "Arthur" << "arthuR" << "ARTHUR" << "Dent" << "Hans Dent"; |
|
208 |
|
209 QVERIFY(list.contains("arthur")); |
|
210 QVERIFY(!list.contains("ArthuR")); |
|
211 QVERIFY(!list.contains("Hans")); |
|
212 QVERIFY(list.contains("arthur", Qt::CaseInsensitive)); |
|
213 QVERIFY(list.contains("ArthuR", Qt::CaseInsensitive)); |
|
214 QVERIFY(list.contains("ARTHUR", Qt::CaseInsensitive)); |
|
215 QVERIFY(list.contains("dent", Qt::CaseInsensitive)); |
|
216 QVERIFY(!list.contains("hans", Qt::CaseInsensitive)); |
|
217 } |
|
218 |
|
219 void tst_QStringList::removeDuplicates_data() |
|
220 { |
|
221 QTest::addColumn<QString>("before"); |
|
222 QTest::addColumn<QString>("after"); |
|
223 QTest::addColumn<int>("count"); |
|
224 |
|
225 QTest::newRow("empty") << "Hello,Hello" << "Hello" << 1; |
|
226 QTest::newRow("empty") << "Hello,World" << "Hello,World" << 0; |
|
227 } |
|
228 |
|
229 void tst_QStringList::removeDuplicates() |
|
230 { |
|
231 QFETCH(QString, before); |
|
232 QFETCH(QString, after); |
|
233 QFETCH(int, count); |
|
234 |
|
235 QStringList lbefore = before.split(','); |
|
236 QStringList lafter = after.split(','); |
|
237 int removed = lbefore.removeDuplicates(); |
|
238 |
|
239 QCOMPARE(removed, count); |
|
240 QCOMPARE(lbefore, lafter); |
|
241 } |
|
242 |
|
243 void tst_QStringList::streamingOperator() |
|
244 { |
|
245 QStringList list; |
|
246 list << "hei"; |
|
247 list << list << "hopp" << list; |
|
248 |
|
249 QCOMPARE(list, QStringList() |
|
250 << "hei" << "hei" << "hopp" |
|
251 << "hei" << "hei" << "hopp"); |
|
252 |
|
253 QStringList list2; |
|
254 list2 << "adam"; |
|
255 |
|
256 QStringList list3; |
|
257 list3 << "eva"; |
|
258 |
|
259 QCOMPARE(list2 << list3, QStringList() << "adam" << "eva"); |
|
260 } |
|
261 |
|
262 void tst_QStringList::join() const |
|
263 { |
|
264 QFETCH(QStringList, input); |
|
265 QFETCH(QString, separator); |
|
266 QFETCH(QString, expectedResult); |
|
267 |
|
268 QCOMPARE(input.join(separator), expectedResult); |
|
269 } |
|
270 |
|
271 void tst_QStringList::join_data() const |
|
272 { |
|
273 QTest::addColumn<QStringList>("input"); |
|
274 QTest::addColumn<QString>("separator"); |
|
275 QTest::addColumn<QString>("expectedResult"); |
|
276 |
|
277 QTest::newRow("") |
|
278 << QStringList() |
|
279 << QString() |
|
280 << QString(); |
|
281 |
|
282 QTest::newRow("") |
|
283 << QStringList() |
|
284 << QString(QLatin1String("separator")) |
|
285 << QString(); |
|
286 |
|
287 QTest::newRow("") |
|
288 << QStringList("one") |
|
289 << QString(QLatin1String("separator")) |
|
290 << QString("one"); |
|
291 |
|
292 QTest::newRow("") |
|
293 << QStringList("one") |
|
294 << QString(QLatin1String("separator")) |
|
295 << QString("one"); |
|
296 |
|
297 |
|
298 QTest::newRow("") |
|
299 << (QStringList() |
|
300 << QLatin1String("a") |
|
301 << QLatin1String("b")) |
|
302 << QString(QLatin1String(" ")) |
|
303 << QString("a b"); |
|
304 |
|
305 QTest::newRow("") |
|
306 << (QStringList() |
|
307 << QLatin1String("a") |
|
308 << QLatin1String("b") |
|
309 << QLatin1String("c")) |
|
310 << QString(QLatin1String(" ")) |
|
311 << QString("a b c"); |
|
312 } |
|
313 |
|
314 QTEST_APPLESS_MAIN(tst_QStringList) |
|
315 #include "tst_qstringlist.moc" |