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