0
|
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 |
#define QT_STRICT_ITERATORS
|
|
43 |
|
|
44 |
#include <QtTest/QtTest>
|
|
45 |
#include <QDebug>
|
|
46 |
|
|
47 |
|
|
48 |
#include <qmap.h>
|
|
49 |
|
|
50 |
//TESTED_CLASS=
|
|
51 |
//TESTED_FILES=
|
|
52 |
|
|
53 |
class tst_QMap : public QObject
|
|
54 |
{
|
|
55 |
Q_OBJECT
|
|
56 |
|
|
57 |
public:
|
|
58 |
tst_QMap();
|
|
59 |
|
|
60 |
public slots:
|
|
61 |
void init();
|
|
62 |
private slots:
|
|
63 |
void count();
|
|
64 |
void clear();
|
|
65 |
void beginEnd();
|
|
66 |
void key();
|
|
67 |
|
|
68 |
void operator_eq();
|
|
69 |
|
|
70 |
void empty();
|
|
71 |
void contains();
|
|
72 |
void find();
|
|
73 |
void constFind();
|
|
74 |
void lowerUpperBound();
|
|
75 |
void mergeCompare();
|
|
76 |
void take();
|
|
77 |
|
|
78 |
void iterators();
|
|
79 |
void keys_values_uniqueKeys();
|
|
80 |
void qmultimap_specific();
|
|
81 |
};
|
|
82 |
|
|
83 |
tst_QMap::tst_QMap()
|
|
84 |
{
|
|
85 |
}
|
|
86 |
|
|
87 |
typedef QMap<QString, QString> StringMap;
|
|
88 |
|
|
89 |
class MyClass
|
|
90 |
{
|
|
91 |
public:
|
|
92 |
MyClass() {
|
|
93 |
++count;
|
|
94 |
// qDebug("creating MyClass count=%d", count);
|
|
95 |
}
|
|
96 |
MyClass( const QString& c) {
|
|
97 |
count++; str = c;
|
|
98 |
// qDebug("creating MyClass '%s' count = %d", str.latin1(), count);
|
|
99 |
}
|
|
100 |
~MyClass() {
|
|
101 |
count--;
|
|
102 |
// qDebug("deleting MyClass '%s' count = %d", str.latin1(), count);
|
|
103 |
}
|
|
104 |
MyClass( const MyClass& c ) {
|
|
105 |
count++; str = c.str;
|
|
106 |
// qDebug("creating MyClass '%s' count = %d", str.latin1(), count);
|
|
107 |
}
|
|
108 |
MyClass &operator =(const MyClass &o) {
|
|
109 |
// qDebug("copying MyClass '%s'", o.str.latin1());
|
|
110 |
str = o.str; return *this;
|
|
111 |
}
|
|
112 |
|
|
113 |
QString str;
|
|
114 |
static int count;
|
|
115 |
};
|
|
116 |
|
|
117 |
int MyClass::count = 0;
|
|
118 |
|
|
119 |
typedef QMap<QString, MyClass> MyMap;
|
|
120 |
|
|
121 |
void tst_QMap::init()
|
|
122 |
{
|
|
123 |
MyClass::count = 0;
|
|
124 |
}
|
|
125 |
|
|
126 |
void tst_QMap::count()
|
|
127 |
{
|
|
128 |
{
|
|
129 |
MyMap map;
|
|
130 |
MyMap map2( map );
|
|
131 |
QCOMPARE( map.count(), 0 );
|
|
132 |
QCOMPARE( map2.count(), 0 );
|
|
133 |
QCOMPARE( MyClass::count, int(0) );
|
|
134 |
// detach
|
|
135 |
map2["Hallo"] = MyClass( "Fritz" );
|
|
136 |
QCOMPARE( map.count(), 0 );
|
|
137 |
QCOMPARE( map2.count(), 1 );
|
|
138 |
#ifndef Q_CC_SUN
|
|
139 |
QCOMPARE( MyClass::count, 1 );
|
|
140 |
#endif
|
|
141 |
}
|
|
142 |
QCOMPARE( MyClass::count, int(0) );
|
|
143 |
|
|
144 |
{
|
|
145 |
typedef QMap<QString, MyClass> Map;
|
|
146 |
Map map;
|
|
147 |
QCOMPARE( map.count(), 0);
|
|
148 |
map.insert( "Torben", MyClass("Weis") );
|
|
149 |
QCOMPARE( map.count(), 1 );
|
|
150 |
map.insert( "Claudia", MyClass("Sorg") );
|
|
151 |
QCOMPARE( map.count(), 2 );
|
|
152 |
map.insert( "Lars", MyClass("Linzbach") );
|
|
153 |
map.insert( "Matthias", MyClass("Ettrich") );
|
|
154 |
map.insert( "Sue", MyClass("Paludo") );
|
|
155 |
map.insert( "Eirik", MyClass("Eng") );
|
|
156 |
map.insert( "Haavard", MyClass("Nord") );
|
|
157 |
map.insert( "Arnt", MyClass("Gulbrandsen") );
|
|
158 |
map.insert( "Paul", MyClass("Tvete") );
|
|
159 |
QCOMPARE( map.count(), 9 );
|
|
160 |
map.insert( "Paul", MyClass("Tvete 1") );
|
|
161 |
map.insert( "Paul", MyClass("Tvete 2") );
|
|
162 |
map.insert( "Paul", MyClass("Tvete 3") );
|
|
163 |
map.insert( "Paul", MyClass("Tvete 4") );
|
|
164 |
map.insert( "Paul", MyClass("Tvete 5") );
|
|
165 |
map.insert( "Paul", MyClass("Tvete 6") );
|
|
166 |
|
|
167 |
QCOMPARE( map.count(), 9 );
|
|
168 |
#ifndef Q_CC_SUN
|
|
169 |
QCOMPARE( MyClass::count, 9 );
|
|
170 |
#endif
|
|
171 |
|
|
172 |
Map map2( map );
|
|
173 |
QVERIFY( map2.count() == 9 );
|
|
174 |
#ifndef Q_CC_SUN
|
|
175 |
QCOMPARE( MyClass::count, 9 );
|
|
176 |
#endif
|
|
177 |
|
|
178 |
map2.insert( "Kay", MyClass("Roemer") );
|
|
179 |
QVERIFY( map2.count() == 10 );
|
|
180 |
QVERIFY( map.count() == 9 );
|
|
181 |
#ifndef Q_CC_SUN
|
|
182 |
QCOMPARE( MyClass::count, 19 );
|
|
183 |
#endif
|
|
184 |
|
|
185 |
map2 = map;
|
|
186 |
QVERIFY( map.count() == 9 );
|
|
187 |
QVERIFY( map2.count() == 9 );
|
|
188 |
#ifndef Q_CC_SUN
|
|
189 |
QCOMPARE( MyClass::count, 9 );
|
|
190 |
#endif
|
|
191 |
|
|
192 |
map2.insert( "Kay", MyClass("Roemer") );
|
|
193 |
QVERIFY( map2.count() == 10 );
|
|
194 |
#ifndef Q_CC_SUN
|
|
195 |
QCOMPARE( MyClass::count, 19 );
|
|
196 |
#endif
|
|
197 |
|
|
198 |
map2.clear();
|
|
199 |
QVERIFY( map.count() == 9 );
|
|
200 |
QVERIFY( map2.count() == 0 );
|
|
201 |
#ifndef Q_CC_SUN
|
|
202 |
QCOMPARE( MyClass::count, 9 );
|
|
203 |
#endif
|
|
204 |
|
|
205 |
map2 = map;
|
|
206 |
QVERIFY( map.count() == 9 );
|
|
207 |
QVERIFY( map2.count() == 9 );
|
|
208 |
#ifndef Q_CC_SUN
|
|
209 |
QCOMPARE( MyClass::count, 9 );
|
|
210 |
#endif
|
|
211 |
|
|
212 |
map2.clear();
|
|
213 |
QVERIFY( map.count() == 9 );
|
|
214 |
QVERIFY( map2.count() == 0 );
|
|
215 |
#ifndef Q_CC_SUN
|
|
216 |
QCOMPARE( MyClass::count, 9 );
|
|
217 |
#endif
|
|
218 |
|
|
219 |
map.remove( "Lars" );
|
|
220 |
QVERIFY( map.count() == 8 );
|
|
221 |
QVERIFY( map2.count() == 0 );
|
|
222 |
#ifndef Q_CC_SUN
|
|
223 |
QCOMPARE( MyClass::count, 8 );
|
|
224 |
#endif
|
|
225 |
|
|
226 |
map.remove( "Mist" );
|
|
227 |
QVERIFY( map.count() == 8 );
|
|
228 |
QVERIFY( map2.count() == 0 );
|
|
229 |
#ifndef Q_CC_SUN
|
|
230 |
QCOMPARE( MyClass::count, 8 );
|
|
231 |
#endif
|
|
232 |
}
|
|
233 |
QVERIFY( MyClass::count == 0 );
|
|
234 |
|
|
235 |
{
|
|
236 |
typedef QMap<QString,MyClass> Map;
|
|
237 |
Map map;
|
|
238 |
map["Torben"] = MyClass("Weis");
|
|
239 |
#ifndef Q_CC_SUN
|
|
240 |
QVERIFY( MyClass::count == 1 );
|
|
241 |
#endif
|
|
242 |
QVERIFY( map.count() == 1 );
|
|
243 |
|
|
244 |
(void)map["Torben"].str;
|
|
245 |
(void)map["Lars"].str;
|
|
246 |
#ifndef Q_CC_SUN
|
|
247 |
QVERIFY( MyClass::count == 2 );
|
|
248 |
#endif
|
|
249 |
QVERIFY( map.count() == 2 );
|
|
250 |
|
|
251 |
const Map& cmap = map;
|
|
252 |
(void)cmap["Depp"].str;
|
|
253 |
#ifndef Q_CC_SUN
|
|
254 |
QVERIFY( MyClass::count == 2 );
|
|
255 |
#endif
|
|
256 |
QVERIFY( map.count() == 2 );
|
|
257 |
QVERIFY( cmap.count() == 2 );
|
|
258 |
}
|
|
259 |
QCOMPARE( MyClass::count, 0 );
|
|
260 |
{
|
|
261 |
for ( int i = 0; i < 100; ++i )
|
|
262 |
{
|
|
263 |
QMap<int, MyClass> map;
|
|
264 |
for (int j = 0; j < i; ++j)
|
|
265 |
map.insert(j, MyClass(QString::number(j)));
|
|
266 |
}
|
|
267 |
QCOMPARE( MyClass::count, 0 );
|
|
268 |
}
|
|
269 |
QCOMPARE( MyClass::count, 0 );
|
|
270 |
}
|
|
271 |
|
|
272 |
void tst_QMap::clear()
|
|
273 |
{
|
|
274 |
{
|
|
275 |
MyMap map;
|
|
276 |
map.clear();
|
|
277 |
QVERIFY( map.isEmpty() );
|
|
278 |
map.insert( "key", MyClass( "value" ) );
|
|
279 |
map.clear();
|
|
280 |
QVERIFY( map.isEmpty() );
|
|
281 |
map.insert( "key0", MyClass( "value0" ) );
|
|
282 |
map.insert( "key0", MyClass( "value1" ) );
|
|
283 |
map.insert( "key1", MyClass( "value2" ) );
|
|
284 |
map.clear();
|
|
285 |
QVERIFY( map.isEmpty() );
|
|
286 |
}
|
|
287 |
QCOMPARE( MyClass::count, int(0) );
|
|
288 |
}
|
|
289 |
|
|
290 |
void tst_QMap::beginEnd()
|
|
291 |
{
|
|
292 |
StringMap m0;
|
|
293 |
QVERIFY( m0.begin() == m0.end() );
|
|
294 |
QVERIFY( m0.begin() == m0.begin() );
|
|
295 |
|
|
296 |
// sample string->string map
|
|
297 |
StringMap map;
|
|
298 |
QVERIFY( map.constBegin() == map.constEnd() );
|
|
299 |
map.insert( "0", "a" );
|
|
300 |
map.insert( "1", "b" );
|
|
301 |
|
|
302 |
// make a copy. const function shouldn't detach
|
|
303 |
StringMap map2 = map;
|
|
304 |
QVERIFY( map.constBegin() == map2.constBegin() );
|
|
305 |
QVERIFY( map.constEnd() == map2.constEnd() );
|
|
306 |
|
|
307 |
// test iteration
|
|
308 |
QString result;
|
|
309 |
for ( StringMap::ConstIterator it = map.constBegin();
|
|
310 |
it != map.constEnd(); ++it )
|
|
311 |
result += *it;
|
|
312 |
QCOMPARE( result, QString( "ab" ) );
|
|
313 |
|
|
314 |
// maps should still be identical
|
|
315 |
QVERIFY( map.constBegin() == map2.constBegin() );
|
|
316 |
QVERIFY( map.constEnd() == map2.constEnd() );
|
|
317 |
|
|
318 |
// detach
|
|
319 |
map2.insert( "2", "c" );
|
|
320 |
QVERIFY( map.constBegin() == map.constBegin() );
|
|
321 |
QVERIFY( map.constBegin() != map2.constBegin() );
|
|
322 |
}
|
|
323 |
|
|
324 |
void tst_QMap::key()
|
|
325 |
{
|
|
326 |
{
|
|
327 |
QString def("default value");
|
|
328 |
|
|
329 |
QMap<QString, int> map1;
|
|
330 |
QCOMPARE(map1.key(1), QString());
|
|
331 |
QCOMPARE(map1.key(1, def), def);
|
|
332 |
|
|
333 |
map1.insert("one", 1);
|
|
334 |
QCOMPARE(map1.key(1), QString("one"));
|
|
335 |
QCOMPARE(map1.key(1, def), QString("one"));
|
|
336 |
QCOMPARE(map1.key(2), QString());
|
|
337 |
QCOMPARE(map1.key(2, def), def);
|
|
338 |
|
|
339 |
map1.insert("two", 2);
|
|
340 |
QCOMPARE(map1.key(1), QString("one"));
|
|
341 |
QCOMPARE(map1.key(1, def), QString("one"));
|
|
342 |
QCOMPARE(map1.key(2), QString("two"));
|
|
343 |
QCOMPARE(map1.key(2, def), QString("two"));
|
|
344 |
QCOMPARE(map1.key(3), QString());
|
|
345 |
QCOMPARE(map1.key(3, def), def);
|
|
346 |
|
|
347 |
map1.insert("deux", 2);
|
|
348 |
QCOMPARE(map1.key(1), QString("one"));
|
|
349 |
QCOMPARE(map1.key(1, def), QString("one"));
|
|
350 |
QVERIFY(map1.key(2) == "deux" || map1.key(2) == "two");
|
|
351 |
QVERIFY(map1.key(2, def) == "deux" || map1.key(2, def) == "two");
|
|
352 |
QCOMPARE(map1.key(3), QString());
|
|
353 |
QCOMPARE(map1.key(3, def), def);
|
|
354 |
}
|
|
355 |
|
|
356 |
{
|
|
357 |
int def = 666;
|
|
358 |
|
|
359 |
QMap<int, QString> map2;
|
|
360 |
QCOMPARE(map2.key("one"), 0);
|
|
361 |
QCOMPARE(map2.key("one", def), def);
|
|
362 |
|
|
363 |
map2.insert(1, "one");
|
|
364 |
QCOMPARE(map2.key("one"), 1);
|
|
365 |
QCOMPARE(map2.key("one", def), 1);
|
|
366 |
QCOMPARE(map2.key("two"), 0);
|
|
367 |
QCOMPARE(map2.key("two", def), def);
|
|
368 |
|
|
369 |
map2.insert(2, "two");
|
|
370 |
QCOMPARE(map2.key("one"), 1);
|
|
371 |
QCOMPARE(map2.key("one", def), 1);
|
|
372 |
QCOMPARE(map2.key("two"), 2);
|
|
373 |
QCOMPARE(map2.key("two", def), 2);
|
|
374 |
QCOMPARE(map2.key("three"), 0);
|
|
375 |
QCOMPARE(map2.key("three", def), def);
|
|
376 |
|
|
377 |
map2.insert(3, "two");
|
|
378 |
QCOMPARE(map2.key("one"), 1);
|
|
379 |
QCOMPARE(map2.key("one", def), 1);
|
|
380 |
QCOMPARE(map2.key("two"), 2);
|
|
381 |
QCOMPARE(map2.key("two", def), 2);
|
|
382 |
QCOMPARE(map2.key("three"), 0);
|
|
383 |
QCOMPARE(map2.key("three", def), def);
|
|
384 |
|
|
385 |
map2.insert(-1, "two");
|
|
386 |
QCOMPARE(map2.key("two"), -1);
|
|
387 |
QCOMPARE(map2.key("two", def), -1);
|
|
388 |
|
|
389 |
map2.insert(0, "zero");
|
|
390 |
QCOMPARE(map2.key("zero"), 0);
|
|
391 |
QCOMPARE(map2.key("zero", def), 0);
|
|
392 |
}
|
|
393 |
}
|
|
394 |
|
|
395 |
void tst_QMap::operator_eq()
|
|
396 |
{
|
|
397 |
{
|
|
398 |
// compare for equality:
|
|
399 |
QMap<int, int> a;
|
|
400 |
QMap<int, int> b;
|
|
401 |
|
|
402 |
QVERIFY(a == b);
|
|
403 |
QVERIFY(!(a != b));
|
|
404 |
|
|
405 |
a.insert(1,1);
|
|
406 |
b.insert(1,1);
|
|
407 |
QVERIFY(a == b);
|
|
408 |
QVERIFY(!(a != b));
|
|
409 |
|
|
410 |
a.insert(0,1);
|
|
411 |
b.insert(0,1);
|
|
412 |
QVERIFY(a == b);
|
|
413 |
QVERIFY(!(a != b));
|
|
414 |
|
|
415 |
// compare for inequality:
|
|
416 |
a.insert(42,0);
|
|
417 |
QVERIFY(a != b);
|
|
418 |
QVERIFY(!(a == b));
|
|
419 |
|
|
420 |
a.insert(65, -1);
|
|
421 |
QVERIFY(a != b);
|
|
422 |
QVERIFY(!(a == b));
|
|
423 |
|
|
424 |
b.insert(-1, -1);
|
|
425 |
QVERIFY(a != b);
|
|
426 |
QVERIFY(!(a == b));
|
|
427 |
}
|
|
428 |
|
|
429 |
{
|
|
430 |
// a more complex map
|
|
431 |
QMap<QString, QString> a;
|
|
432 |
QMap<QString, QString> b;
|
|
433 |
|
|
434 |
QVERIFY(a == b);
|
|
435 |
QVERIFY(!(a != b));
|
|
436 |
|
|
437 |
a.insert("Hello", "World");
|
|
438 |
QVERIFY(a != b);
|
|
439 |
QVERIFY(!(a == b));
|
|
440 |
|
|
441 |
b.insert("Hello", "World");
|
|
442 |
QVERIFY(a == b);
|
|
443 |
QVERIFY(!(a != b));
|
|
444 |
|
|
445 |
a.insert("Goodbye", "cruel world");
|
|
446 |
QVERIFY(a != b);
|
|
447 |
QVERIFY(!(a == b));
|
|
448 |
|
|
449 |
b.insert("Goodbye", "cruel world");
|
|
450 |
|
|
451 |
// what happens if we insert nulls?
|
|
452 |
a.insert(QString(), QString());
|
|
453 |
QVERIFY(a != b);
|
|
454 |
QVERIFY(!(a == b));
|
|
455 |
|
|
456 |
// empty keys and null keys match:
|
|
457 |
b.insert(QString(""), QString());
|
|
458 |
QVERIFY(a == b);
|
|
459 |
QVERIFY(!(a != b));
|
|
460 |
}
|
|
461 |
|
|
462 |
{
|
|
463 |
// task 102658
|
|
464 |
|
|
465 |
QMap<QString, int> a;
|
|
466 |
QMap<QString, int> b;
|
|
467 |
|
|
468 |
a.insert("otto", 1);
|
|
469 |
b.insert("willy", 1);
|
|
470 |
QVERIFY(a != b);
|
|
471 |
QVERIFY(!(a == b));
|
|
472 |
}
|
|
473 |
}
|
|
474 |
|
|
475 |
void tst_QMap::empty()
|
|
476 |
{
|
|
477 |
QMap<int, QString> map1;
|
|
478 |
|
|
479 |
QVERIFY(map1.isEmpty());
|
|
480 |
|
|
481 |
map1.insert(1, "one");
|
|
482 |
QVERIFY(!map1.isEmpty());
|
|
483 |
|
|
484 |
map1.clear();
|
|
485 |
QVERIFY(map1.isEmpty());
|
|
486 |
|
|
487 |
}
|
|
488 |
|
|
489 |
void tst_QMap::contains()
|
|
490 |
{
|
|
491 |
QMap<int, QString> map1;
|
|
492 |
int i;
|
|
493 |
|
|
494 |
map1.insert(1, "one");
|
|
495 |
QVERIFY(map1.contains(1));
|
|
496 |
|
|
497 |
for(i=2; i < 100; ++i)
|
|
498 |
map1.insert(i, "teststring");
|
|
499 |
for(i=99; i > 1; --i)
|
|
500 |
QVERIFY(map1.contains(i));
|
|
501 |
|
|
502 |
map1.remove(43);
|
|
503 |
QVERIFY(!map1.contains(43));
|
|
504 |
}
|
|
505 |
|
|
506 |
void tst_QMap::find()
|
|
507 |
{
|
|
508 |
QMap<int, QString> map1;
|
|
509 |
QString testString="Teststring %0";
|
|
510 |
QString compareString;
|
|
511 |
int i,count=0;
|
|
512 |
|
|
513 |
QVERIFY(map1.find(1) == map1.end());
|
|
514 |
|
|
515 |
map1.insert(1,"Mensch");
|
|
516 |
map1.insert(1,"Mayer");
|
|
517 |
map1.insert(2,"Hej");
|
|
518 |
|
|
519 |
QVERIFY(map1.find(1).value() == "Mayer");
|
|
520 |
QVERIFY(map1.find(2).value() == "Hej");
|
|
521 |
|
|
522 |
for(i = 3; i < 10; ++i) {
|
|
523 |
compareString = testString.arg(i);
|
|
524 |
map1.insertMulti(4, compareString);
|
|
525 |
}
|
|
526 |
|
|
527 |
QMap<int, QString>::const_iterator it=map1.constFind(4);
|
|
528 |
|
|
529 |
for(i = 9; i > 2 && it != map1.constEnd() && it.key() == 4; --i) {
|
|
530 |
compareString = testString.arg(i);
|
|
531 |
QVERIFY(it.value() == compareString);
|
|
532 |
++it;
|
|
533 |
++count;
|
|
534 |
}
|
|
535 |
QCOMPARE(count, 7);
|
|
536 |
}
|
|
537 |
|
|
538 |
void tst_QMap::constFind()
|
|
539 |
{
|
|
540 |
QMap<int, QString> map1;
|
|
541 |
QString testString="Teststring %0";
|
|
542 |
QString compareString;
|
|
543 |
int i,count=0;
|
|
544 |
|
|
545 |
QVERIFY(map1.constFind(1) == map1.constEnd());
|
|
546 |
|
|
547 |
map1.insert(1,"Mensch");
|
|
548 |
map1.insert(1,"Mayer");
|
|
549 |
map1.insert(2,"Hej");
|
|
550 |
|
|
551 |
QVERIFY(map1.constFind(4) == map1.constEnd());
|
|
552 |
|
|
553 |
QVERIFY(map1.constFind(1).value() == "Mayer");
|
|
554 |
QVERIFY(map1.constFind(2).value() == "Hej");
|
|
555 |
|
|
556 |
for(i = 3; i < 10; ++i) {
|
|
557 |
compareString = testString.arg(i);
|
|
558 |
map1.insertMulti(4, compareString);
|
|
559 |
}
|
|
560 |
|
|
561 |
QMap<int, QString>::const_iterator it=map1.constFind(4);
|
|
562 |
|
|
563 |
for(i = 9; i > 2 && it != map1.constEnd() && it.key() == 4; --i) {
|
|
564 |
compareString = testString.arg(i);
|
|
565 |
QVERIFY(it.value() == compareString);
|
|
566 |
++it;
|
|
567 |
++count;
|
|
568 |
}
|
|
569 |
QCOMPARE(count, 7);
|
|
570 |
}
|
|
571 |
|
|
572 |
void tst_QMap::lowerUpperBound()
|
|
573 |
{
|
|
574 |
QMap<int, QString> map1;
|
|
575 |
|
|
576 |
map1.insert(1, "one");
|
|
577 |
map1.insert(5, "five");
|
|
578 |
map1.insert(10, "ten");
|
|
579 |
|
|
580 |
|
|
581 |
//Copied from documentation
|
|
582 |
|
|
583 |
QCOMPARE(map1.upperBound(0).key(), 1); // returns iterator to (1, "one")
|
|
584 |
QCOMPARE(map1.upperBound(1).key(), 5); // returns iterator to (5, "five")
|
|
585 |
QCOMPARE(map1.upperBound(2).key(), 5); // returns iterator to (5, "five")
|
|
586 |
QVERIFY(map1.upperBound(10) == map1.end()); // returns end()
|
|
587 |
QVERIFY(map1.upperBound(999) == map1.end()); // returns end()
|
|
588 |
|
|
589 |
QCOMPARE(map1.lowerBound(0).key(), 1); // returns iterator to (1, "one")
|
|
590 |
QCOMPARE(map1.lowerBound(1).key(), 1); // returns iterator to (1, "one")
|
|
591 |
QCOMPARE(map1.lowerBound(2).key(), 5); // returns iterator to (5, "five")
|
|
592 |
QCOMPARE(map1.lowerBound(10).key(), 10); // returns iterator to (10, "ten")
|
|
593 |
QVERIFY(map1.lowerBound(999) == map1.end()); // returns end()
|
|
594 |
}
|
|
595 |
|
|
596 |
void tst_QMap::mergeCompare()
|
|
597 |
{
|
|
598 |
QMap<int, QString> map1, map2, map3;
|
|
599 |
|
|
600 |
map1.insert(1,"ett");
|
|
601 |
map1.insert(3,"tre");
|
|
602 |
map1.insert(5,"fem");
|
|
603 |
|
|
604 |
map2.insert(2,"tvo");
|
|
605 |
map2.insert(4,"fyra");
|
|
606 |
|
|
607 |
map1.unite(map2);
|
|
608 |
|
|
609 |
QVERIFY(map1.value(1) == "ett");
|
|
610 |
QVERIFY(map1.value(2) == "tvo");
|
|
611 |
QVERIFY(map1.value(3) == "tre");
|
|
612 |
QVERIFY(map1.value(4) == "fyra");
|
|
613 |
QVERIFY(map1.value(5) == "fem");
|
|
614 |
|
|
615 |
map3.insert(1, "ett");
|
|
616 |
map3.insert(2, "tvo");
|
|
617 |
map3.insert(3, "tre");
|
|
618 |
map3.insert(4, "fyra");
|
|
619 |
map3.insert(5, "fem");
|
|
620 |
|
|
621 |
QVERIFY(map1 == map3);
|
|
622 |
}
|
|
623 |
|
|
624 |
void tst_QMap::take()
|
|
625 |
{
|
|
626 |
QMap<int, QString> map;
|
|
627 |
|
|
628 |
map.insert(2, "zwei");
|
|
629 |
map.insert(3, "drei");
|
|
630 |
|
|
631 |
QVERIFY(map.take(3) == "drei");
|
|
632 |
QVERIFY(!map.contains(3));
|
|
633 |
}
|
|
634 |
|
|
635 |
void tst_QMap::iterators()
|
|
636 |
{
|
|
637 |
QMap<int, QString> map;
|
|
638 |
QString testString="Teststring %1";
|
|
639 |
int i;
|
|
640 |
|
|
641 |
for(i = 1; i < 100; ++i)
|
|
642 |
map.insert(i, testString.arg(i));
|
|
643 |
|
|
644 |
//STL-Style iterators
|
|
645 |
|
|
646 |
QMap<int, QString>::iterator stlIt = map.begin();
|
|
647 |
QVERIFY(stlIt.value() == "Teststring 1");
|
|
648 |
|
|
649 |
stlIt+=5;
|
|
650 |
QVERIFY(stlIt.value() == "Teststring 6");
|
|
651 |
|
|
652 |
stlIt++;
|
|
653 |
QVERIFY(stlIt.value() == "Teststring 7");
|
|
654 |
|
|
655 |
stlIt-=3;
|
|
656 |
QVERIFY(stlIt.value() == "Teststring 4");
|
|
657 |
|
|
658 |
stlIt--;
|
|
659 |
QVERIFY(stlIt.value() == "Teststring 3");
|
|
660 |
|
|
661 |
for(stlIt = map.begin(), i = 1; stlIt != map.end(), i < 100; ++stlIt, ++i)
|
|
662 |
QVERIFY(stlIt.value() == testString.arg(i));
|
|
663 |
|
|
664 |
//STL-Style const-iterators
|
|
665 |
|
|
666 |
QMap<int, QString>::const_iterator cstlIt = map.constBegin();
|
|
667 |
QVERIFY(cstlIt.value() == "Teststring 1");
|
|
668 |
|
|
669 |
cstlIt+=5;
|
|
670 |
QVERIFY(cstlIt.value() == "Teststring 6");
|
|
671 |
|
|
672 |
cstlIt++;
|
|
673 |
QVERIFY(cstlIt.value() == "Teststring 7");
|
|
674 |
|
|
675 |
cstlIt-=3;
|
|
676 |
QVERIFY(cstlIt.value() == "Teststring 4");
|
|
677 |
|
|
678 |
cstlIt--;
|
|
679 |
QVERIFY(cstlIt.value() == "Teststring 3");
|
|
680 |
|
|
681 |
for(cstlIt = map.constBegin(), i = 1; cstlIt != map.constEnd(), i < 100; ++cstlIt, ++i)
|
|
682 |
QVERIFY(cstlIt.value() == testString.arg(i));
|
|
683 |
|
|
684 |
//Java-Style iterators
|
|
685 |
|
|
686 |
QMapIterator<int, QString> javaIt(map);
|
|
687 |
|
|
688 |
i = 0;
|
|
689 |
while(javaIt.hasNext()) {
|
|
690 |
++i;
|
|
691 |
javaIt.next();
|
|
692 |
QVERIFY(javaIt.value() == testString.arg(i));
|
|
693 |
}
|
|
694 |
|
|
695 |
++i;
|
|
696 |
while(javaIt.hasPrevious()) {
|
|
697 |
--i;
|
|
698 |
javaIt.previous();
|
|
699 |
QVERIFY(javaIt.value() == testString.arg(i));
|
|
700 |
}
|
|
701 |
|
|
702 |
/*
|
|
703 |
I've removed findNextKey() and findPreviousKey() from the API
|
|
704 |
for Qt 4.0 beta 1.
|
|
705 |
*/
|
|
706 |
|
|
707 |
#if 0
|
|
708 |
QVERIFY(javaIt.findNextKey(50));
|
|
709 |
QVERIFY(javaIt.value() == "Teststring 50");
|
|
710 |
#endif
|
|
711 |
|
|
712 |
i = 51;
|
|
713 |
while(javaIt.hasPrevious()) {
|
|
714 |
--i;
|
|
715 |
javaIt.previous();
|
|
716 |
QVERIFY(javaIt.value() == testString.arg(i));
|
|
717 |
}
|
|
718 |
|
|
719 |
#if 0
|
|
720 |
QVERIFY(javaIt.findNextKey(50));
|
|
721 |
QVERIFY(javaIt.value() == "Teststring 50");
|
|
722 |
|
|
723 |
QVERIFY(javaIt.hasPrevious());
|
|
724 |
QVERIFY(javaIt.findPreviousKey(20));
|
|
725 |
QCOMPARE(javaIt.value(), QString("Teststring 20"));
|
|
726 |
#endif
|
|
727 |
}
|
|
728 |
|
|
729 |
void tst_QMap::keys_values_uniqueKeys()
|
|
730 |
{
|
|
731 |
QMap<QString, int> map;
|
|
732 |
QVERIFY(map.uniqueKeys().isEmpty());
|
|
733 |
QVERIFY(map.keys().isEmpty());
|
|
734 |
QVERIFY(map.values().isEmpty());
|
|
735 |
|
|
736 |
map.insertMulti("alpha", 1);
|
|
737 |
QVERIFY(map.keys() == (QList<QString>() << "alpha"));
|
|
738 |
QVERIFY(map.uniqueKeys() == map.keys());
|
|
739 |
QVERIFY(map.values() == (QList<int>() << 1));
|
|
740 |
|
|
741 |
map.insertMulti("beta", -2);
|
|
742 |
QVERIFY(map.keys() == (QList<QString>() << "alpha" << "beta"));
|
|
743 |
QVERIFY(map.keys() == map.uniqueKeys());
|
|
744 |
QVERIFY(map.values() == (QList<int>() << 1 << -2));
|
|
745 |
|
|
746 |
map.insertMulti("alpha", 2);
|
|
747 |
QVERIFY(map.uniqueKeys() == (QList<QString>() << "alpha" << "beta"));
|
|
748 |
QVERIFY(map.keys() == (QList<QString>() << "alpha" << "alpha" << "beta"));
|
|
749 |
QVERIFY(map.values() == (QList<int>() << 2 << 1 << -2));
|
|
750 |
|
|
751 |
map.insertMulti("beta", 4);
|
|
752 |
QVERIFY(map.uniqueKeys() == (QList<QString>() << "alpha" << "beta"));
|
|
753 |
QVERIFY(map.keys() == (QList<QString>() << "alpha" << "alpha" << "beta" << "beta"));
|
|
754 |
QVERIFY(map.values() == (QList<int>() << 2 << 1 << 4 << -2));
|
|
755 |
}
|
|
756 |
|
|
757 |
void tst_QMap::qmultimap_specific()
|
|
758 |
{
|
|
759 |
QMultiMap<int, int> map1;
|
|
760 |
for (int i = 1; i <= 9; ++i) {
|
|
761 |
for (int j = 1; j <= i; ++j) {
|
|
762 |
int k = i * 10 + j;
|
|
763 |
QVERIFY(!map1.contains(i, k));
|
|
764 |
map1.insert(i, k);
|
|
765 |
QVERIFY(map1.contains(i, k));
|
|
766 |
}
|
|
767 |
}
|
|
768 |
|
|
769 |
for (int i = 1; i <= 9; ++i) {
|
|
770 |
for (int j = 1; j <= i; ++j) {
|
|
771 |
int k = i * 10 + j;
|
|
772 |
QVERIFY(map1.contains(i, k));
|
|
773 |
}
|
|
774 |
}
|
|
775 |
|
|
776 |
QVERIFY(map1.contains(9, 99));
|
|
777 |
QCOMPARE(map1.count(), 45);
|
|
778 |
map1.remove(9, 99);
|
|
779 |
QVERIFY(!map1.contains(9, 99));
|
|
780 |
QCOMPARE(map1.count(), 44);
|
|
781 |
|
|
782 |
map1.remove(9, 99);
|
|
783 |
QVERIFY(!map1.contains(9, 99));
|
|
784 |
QCOMPARE(map1.count(), 44);
|
|
785 |
|
|
786 |
map1.remove(1, 99);
|
|
787 |
QCOMPARE(map1.count(), 44);
|
|
788 |
|
|
789 |
map1.insert(1, 99);
|
|
790 |
map1.insert(1, 99);
|
|
791 |
|
|
792 |
QCOMPARE(map1.count(), 46);
|
|
793 |
map1.remove(1, 99);
|
|
794 |
QCOMPARE(map1.count(), 44);
|
|
795 |
map1.remove(1, 99);
|
|
796 |
QCOMPARE(map1.count(), 44);
|
|
797 |
|
|
798 |
{
|
|
799 |
QMultiMap<int, int>::const_iterator i = map1.constFind(1, 11);
|
|
800 |
QVERIFY(i.key() == 1);
|
|
801 |
QVERIFY(i.value() == 11);
|
|
802 |
|
|
803 |
i = map1.constFind(2, 22);
|
|
804 |
QVERIFY(i.key() == 2);
|
|
805 |
QVERIFY(i.value() == 22);
|
|
806 |
|
|
807 |
i = map1.constFind(9, 98);
|
|
808 |
QVERIFY(i.key() == 9);
|
|
809 |
QVERIFY(i.value() == 98);
|
|
810 |
}
|
|
811 |
|
|
812 |
{
|
|
813 |
const QMultiMap<int, int> map2(map1);
|
|
814 |
QMultiMap<int, int>::const_iterator i = map2.find(1, 11);
|
|
815 |
QVERIFY(i.key() == 1);
|
|
816 |
QVERIFY(i.value() == 11);
|
|
817 |
|
|
818 |
i = map2.find(2, 22);
|
|
819 |
QVERIFY(i.key() == 2);
|
|
820 |
QVERIFY(i.value() == 22);
|
|
821 |
|
|
822 |
i = map2.find(9, 98);
|
|
823 |
QVERIFY(i.key() == 9);
|
|
824 |
QVERIFY(i.value() == 98);
|
|
825 |
}
|
|
826 |
|
|
827 |
{
|
|
828 |
QMultiMap<int, int>::iterator i = map1.find(1, 11);
|
|
829 |
QVERIFY(i.key() == 1);
|
|
830 |
QVERIFY(i.value() == 11);
|
|
831 |
|
|
832 |
i = map1.find(2, 22);
|
|
833 |
QVERIFY(i.key() == 2);
|
|
834 |
QVERIFY(i.value() == 22);
|
|
835 |
|
|
836 |
i = map1.find(9, 98);
|
|
837 |
QVERIFY(i.key() == 9);
|
|
838 |
QVERIFY(i.value() == 98);
|
|
839 |
}
|
|
840 |
}
|
|
841 |
|
|
842 |
QTEST_APPLESS_MAIN(tst_QMap)
|
|
843 |
#include "tst_qmap.moc"
|