|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 // test the container forwards |
|
44 #include <QtContainerFwd> |
|
45 |
|
46 static QCache<int, int> *cacheX; |
|
47 static QHash<int, int> *hashX; |
|
48 static QLinkedList<int> *linkedListX; |
|
49 static QList<int> *listX; |
|
50 static QMap<int, int> *mapX; |
|
51 static QMultiHash<int, int> *multiHashX; |
|
52 static QMultiMap<int, int> *multiMapX; |
|
53 static QPair<int, int> *pairX; |
|
54 static QQueue<int> *queueX; |
|
55 static QSet<int> *setX; |
|
56 static QStack<int> *stackX; |
|
57 static QVarLengthArray<int> *varLengthArrayX; |
|
58 static QVarLengthArray<int, 512> *varLengthArrayY; |
|
59 static QVector<int> *vectorX; |
|
60 |
|
61 void foo() |
|
62 { |
|
63 cacheX = 0; |
|
64 hashX = 0; |
|
65 linkedListX = 0; |
|
66 listX = 0; |
|
67 mapX = 0; |
|
68 multiHashX = 0; |
|
69 multiMapX = 0; |
|
70 pairX = 0; |
|
71 queueX = 0; |
|
72 setX = 0; |
|
73 stackX = 0; |
|
74 varLengthArrayX = 0; |
|
75 varLengthArrayY = 0; |
|
76 vectorX = 0; |
|
77 } |
|
78 |
|
79 #include <QtTest/QtTest> |
|
80 |
|
81 #ifndef QT_NO_STL |
|
82 # include <algorithm> |
|
83 #endif |
|
84 |
|
85 #include "qalgorithms.h" |
|
86 #include "qbitarray.h" |
|
87 #include "qbytearray.h" |
|
88 #include "qcache.h" |
|
89 #include "qhash.h" |
|
90 #include "qlinkedlist.h" |
|
91 #include "qlist.h" |
|
92 #include "qmap.h" |
|
93 #include "qpair.h" |
|
94 #include "qregexp.h" |
|
95 #include "qset.h" |
|
96 #include "qstack.h" |
|
97 #include "qstring.h" |
|
98 #include "qstringlist.h" |
|
99 #include "qvarlengtharray.h" |
|
100 #include "qvector.h" |
|
101 #include "qqueue.h" |
|
102 |
|
103 #ifdef QT3_SUPPORT |
|
104 #include "q3cleanuphandler.h" |
|
105 #endif |
|
106 |
|
107 // Do not test initialization of pods on msvc6 and msvc 2002 |
|
108 // This is a known issue |
|
109 #if defined Q_CC_MSVC && _MSC_VER < 1310 |
|
110 # define NOPODINITIALIZATION |
|
111 #endif |
|
112 |
|
113 |
|
114 template class QList<int>; |
|
115 |
|
116 //TESTED_FILES= |
|
117 |
|
118 class tst_Collections : public QObject |
|
119 { |
|
120 Q_OBJECT |
|
121 |
|
122 public: |
|
123 tst_Collections(); |
|
124 ~tst_Collections(); |
|
125 |
|
126 public slots: |
|
127 void init(); |
|
128 void cleanup(); |
|
129 private slots: |
|
130 void typeinfo(); |
|
131 void qstring(); |
|
132 void list(); |
|
133 void linkedList(); |
|
134 void vector(); |
|
135 void byteArray(); |
|
136 void stack(); |
|
137 void hash(); |
|
138 void map(); |
|
139 void bitArray(); |
|
140 void cache(); |
|
141 void regexp(); |
|
142 void pair(); |
|
143 void cleanupHandler(); |
|
144 void sharableQList(); |
|
145 void sharableQLinkedList(); |
|
146 void sharableQVector(); |
|
147 void sharableQMap(); |
|
148 void sharableQHash(); |
|
149 void q_foreach(); |
|
150 void conversions(); |
|
151 void javaStyleIterators(); |
|
152 void constAndNonConstStlIterators(); |
|
153 #ifndef QT_NO_STL |
|
154 void vector_stl_data(); |
|
155 void vector_stl(); |
|
156 void list_stl_data(); |
|
157 void list_stl(); |
|
158 void linkedlist_stl_data(); |
|
159 void linkedlist_stl(); |
|
160 #endif |
|
161 void q_init(); |
|
162 void pointersize(); |
|
163 void containerInstantiation(); |
|
164 void qtimerList(); |
|
165 void containerTypedefs(); |
|
166 void forwardDeclared(); |
|
167 void alignment(); |
|
168 }; |
|
169 |
|
170 struct LargeStatic { |
|
171 static int count; |
|
172 LargeStatic():c(count) { ++count; } |
|
173 LargeStatic(const LargeStatic& o):c(o.c) { ++count; } |
|
174 ~LargeStatic() { --count; } |
|
175 int c; |
|
176 int data[8]; |
|
177 }; |
|
178 |
|
179 int LargeStatic::count = 0; |
|
180 |
|
181 struct Movable { |
|
182 static int count; |
|
183 Movable():c(count) { ++count; } |
|
184 Movable(const Movable& o):c(o.c) { ++count; } |
|
185 ~Movable() { --count; } |
|
186 int c; |
|
187 }; |
|
188 |
|
189 int Movable::count = 0; |
|
190 QT_BEGIN_NAMESPACE |
|
191 Q_DECLARE_TYPEINFO(Movable, Q_MOVABLE_TYPE); |
|
192 QT_END_NAMESPACE |
|
193 |
|
194 |
|
195 struct Pod { |
|
196 int i1, i2; |
|
197 |
|
198 #if defined NOPODINITIALIZATION |
|
199 Pod() : i1(0), i2(0) { } |
|
200 #endif |
|
201 }; |
|
202 |
|
203 tst_Collections::tst_Collections() |
|
204 { |
|
205 } |
|
206 |
|
207 tst_Collections::~tst_Collections() |
|
208 { |
|
209 |
|
210 } |
|
211 |
|
212 void tst_Collections::init() |
|
213 { |
|
214 } |
|
215 |
|
216 void tst_Collections::cleanup() |
|
217 { |
|
218 } |
|
219 |
|
220 void tst_Collections::typeinfo() |
|
221 { |
|
222 QVERIFY(QTypeInfo<int*>::isPointer); |
|
223 QVERIFY(!QTypeInfo<int>::isPointer); |
|
224 QVERIFY(QTypeInfo<QString>::isComplex); |
|
225 QVERIFY(!QTypeInfo<int>::isComplex); |
|
226 } |
|
227 |
|
228 void tst_Collections::list() |
|
229 { |
|
230 { |
|
231 QList<int> list; |
|
232 QVERIFY(list.isEmpty()); |
|
233 list.append(1); |
|
234 QVERIFY(list.size() == 1); |
|
235 |
|
236 QVERIFY(*list.begin() == 1); |
|
237 |
|
238 list.push_back(2); |
|
239 list += (3); |
|
240 list << 4 << 5 << 6; |
|
241 QVERIFY(!list.isEmpty()); |
|
242 QVERIFY(list.size() == 6); |
|
243 QVERIFY(list.end() - list.begin() == list.size()); |
|
244 |
|
245 #if !defined(QT_NO_STL) && !defined(Q_CC_MSVC) && !defined(Q_CC_SUN) |
|
246 QVERIFY(std::binary_search(list.begin(), list.end(), 2) == true); |
|
247 QVERIFY(std::binary_search(list.begin(), list.end(), 9) == false); |
|
248 #endif |
|
249 QVERIFY(qBinaryFind(list.begin(), list.end(), 2) == list.begin() + 1); |
|
250 QVERIFY(qLowerBound(list.begin(), list.end(), 2) == list.begin() + 1); |
|
251 QVERIFY(qUpperBound(list.begin(), list.end(), 2) == list.begin() + 2); |
|
252 QVERIFY(qBinaryFind(list.begin(), list.end(), 9) == list.end()); |
|
253 QVERIFY(qLowerBound(list.begin(), list.end(), 9) == list.end()); |
|
254 QVERIFY(qUpperBound(list.begin(), list.end(), 9) == list.end()); |
|
255 { |
|
256 int sum = 0; |
|
257 QListIterator<int> i(list); |
|
258 while (i.hasNext()) |
|
259 sum += i.next(); |
|
260 QVERIFY(sum == 21); |
|
261 } |
|
262 |
|
263 { |
|
264 QList<int> list1; |
|
265 list1 << 1 << 2 << 3 << 5 << 7 << 8 << 9; |
|
266 QList<int> list2 = list1; |
|
267 |
|
268 QMutableListIterator<int> i1(list1); |
|
269 while (i1.hasNext()) { |
|
270 if (i1.next() % 2 != 0) |
|
271 i1.remove(); |
|
272 } |
|
273 |
|
274 QMutableListIterator<int> i2(list2); |
|
275 i2.toBack(); |
|
276 while (i2.hasPrevious()) { |
|
277 if (i2.previous() % 2 != 0) |
|
278 i2.remove(); |
|
279 } |
|
280 QVERIFY(list1.size() == 2); |
|
281 QVERIFY(list2.size() == 2); |
|
282 QVERIFY(list1 == list2); |
|
283 } |
|
284 |
|
285 { |
|
286 int sum = 0; |
|
287 for (int i = 0; i < list.size(); ++i) |
|
288 sum += list[i]; |
|
289 QVERIFY(sum == 21); |
|
290 } |
|
291 { |
|
292 int sum = 0; |
|
293 QList<int>::const_iterator i = list.begin(); |
|
294 while (i != list.end()) |
|
295 sum += *i++; |
|
296 QVERIFY(sum == 21); |
|
297 } |
|
298 { |
|
299 int sum = 0; |
|
300 QList<int>::ConstIterator i = list.begin(); |
|
301 while (i != list.end()) |
|
302 sum += *i++; |
|
303 QVERIFY(sum == 21); |
|
304 } |
|
305 { |
|
306 QList<int>::Iterator i = list.begin(); |
|
307 i += 2; |
|
308 QCOMPARE(*i, 3); |
|
309 i -= 1; |
|
310 QCOMPARE(*i, 2); |
|
311 } |
|
312 { |
|
313 QList<int>::ConstIterator i = list.begin(); |
|
314 i += 2; |
|
315 QCOMPARE(*i, 3); |
|
316 i -= 1; |
|
317 QCOMPARE(*i, 2); |
|
318 } |
|
319 { |
|
320 int sum = 0; |
|
321 int i; |
|
322 for (i = 0; i < list.size(); ++i) |
|
323 list[i] = list[i] +1; |
|
324 for (i = 0; i < list.size(); ++i) |
|
325 sum += list[i]; |
|
326 QVERIFY(sum == 21 + list.size()); |
|
327 } |
|
328 { |
|
329 int sum = 0; |
|
330 int i; |
|
331 for (i = 0; i < list.size(); ++i) |
|
332 --list[i]; |
|
333 for (i = 0; i < list.size(); ++i) |
|
334 sum += list[i]; |
|
335 QVERIFY(sum == 21); |
|
336 } |
|
337 { |
|
338 QMutableListIterator<int> i(list); |
|
339 while (i.hasNext()) |
|
340 i.setValue(2*i.next()); |
|
341 } |
|
342 { |
|
343 int sum = 0; |
|
344 QListIterator<int> i(list); |
|
345 i.toBack(); |
|
346 while (i.hasPrevious()) |
|
347 sum += i.previous(); |
|
348 QVERIFY(sum == 2*21); |
|
349 } |
|
350 { |
|
351 QMutableListIterator<int> i(list); |
|
352 i.toBack(); |
|
353 while (i.hasPrevious()) |
|
354 i.setValue(2*i.previous()); |
|
355 } |
|
356 { |
|
357 int sum = 0; |
|
358 QListIterator<int> i(list); |
|
359 i.toBack(); |
|
360 while (i.hasPrevious()) |
|
361 sum += i.previous(); |
|
362 QVERIFY(sum == 2*2*21); |
|
363 } |
|
364 { |
|
365 QMutableListIterator<int> i(list); |
|
366 while (i.hasNext()) { |
|
367 int a = i.next(); |
|
368 i.insert(a); |
|
369 } |
|
370 } |
|
371 { |
|
372 int sum = 0; |
|
373 QList<int>::iterator i = list.begin(); |
|
374 while (i != list.end()) |
|
375 sum += *i++; |
|
376 QVERIFY(sum == 2*2*2*21); |
|
377 } |
|
378 { |
|
379 int duplicates = 0; |
|
380 QListIterator<int> i(list); |
|
381 while (i.hasNext()) { |
|
382 int a = i.next(); |
|
383 if (i.hasNext() && a == i.peekNext()) |
|
384 duplicates++; |
|
385 } |
|
386 QVERIFY(duplicates == 6); |
|
387 } |
|
388 { |
|
389 int duplicates = 0; |
|
390 QListIterator<int> i(list); |
|
391 i.toBack(); |
|
392 while (i.hasPrevious()) { |
|
393 int a = i.previous(); |
|
394 if (i.hasPrevious() && a == i.peekPrevious()) |
|
395 duplicates++; |
|
396 } |
|
397 QVERIFY(duplicates == 6); |
|
398 } |
|
399 { |
|
400 QMutableListIterator<int> i(list); |
|
401 while (i.hasNext()) { |
|
402 int a = i.next(); |
|
403 if (i.hasNext() && |
|
404 i.peekNext() == a) |
|
405 i.remove(); |
|
406 } |
|
407 } |
|
408 { |
|
409 int duplicates = 0; |
|
410 QMutableListIterator<int> i = list; |
|
411 i.toBack(); |
|
412 while (i.hasPrevious()) { |
|
413 int a = i.previous(); |
|
414 if (i.hasPrevious() && a == i.peekPrevious()) |
|
415 duplicates++; |
|
416 } |
|
417 QVERIFY(duplicates == 0); |
|
418 } |
|
419 { |
|
420 QVERIFY(list.size() == 6); |
|
421 QMutableListIterator<int> i = list; |
|
422 while (i.hasNext()) { |
|
423 int a = i.peekNext(); |
|
424 i.insert(42); |
|
425 QVERIFY(i.peekPrevious() == 42 && i.peekNext() == a); |
|
426 i.next(); |
|
427 } |
|
428 QVERIFY(list.size() == 12); |
|
429 i.toFront(); |
|
430 while (i.findNext(42)) |
|
431 i.remove(); |
|
432 } |
|
433 { |
|
434 QList<int> l; |
|
435 l << 4 << 8 << 12 << 16 << 20 << 24; |
|
436 QVERIFY(l == list); |
|
437 QList<int> copy = list; |
|
438 list += list; |
|
439 QVERIFY(l != list && l.size() == list.size()/2 && l == copy); |
|
440 l += copy; |
|
441 QVERIFY(l == list); |
|
442 list = copy; |
|
443 } |
|
444 { |
|
445 QList<int> copy = list; |
|
446 list << 8; |
|
447 QVERIFY(list.indexOf(8) == 1); |
|
448 QVERIFY(list.indexOf(8, list.indexOf(8)+1) == 6); |
|
449 int a = list.indexOf(8); |
|
450 QVERIFY(list.count(8) == 2); |
|
451 int r = list.removeAll(8); |
|
452 QVERIFY(r == 2); |
|
453 list.insert(a, 8); |
|
454 QVERIFY(list == copy); |
|
455 } |
|
456 { |
|
457 QList<QString> list; |
|
458 list << "one" << "two" << "three" << "four" << "five" << "six"; |
|
459 while (!list.isEmpty()) |
|
460 list.removeAll(list.first()); |
|
461 } |
|
462 { |
|
463 QList<QString> list; |
|
464 list << "one" << "two" << "one" << "two"; |
|
465 QVERIFY(!list.removeOne("three")); |
|
466 QVERIFY(list.removeOne("two")); |
|
467 QCOMPARE(list, QList<QString>() << "one" << "one" << "two");; |
|
468 QVERIFY(list.removeOne("two")); |
|
469 QCOMPARE(list, QList<QString>() << "one" << "one"); |
|
470 QVERIFY(!list.removeOne("two")); |
|
471 QCOMPARE(list, QList<QString>() << "one" << "one"); |
|
472 QVERIFY(list.removeOne("one")); |
|
473 QCOMPARE(list, QList<QString>() << "one"); |
|
474 QVERIFY(list.removeOne("one")); |
|
475 QVERIFY(list.isEmpty()); |
|
476 QVERIFY(!list.removeOne("one")); |
|
477 QVERIFY(list.isEmpty()); |
|
478 } |
|
479 { |
|
480 QList<int> copy = list; |
|
481 list << 8; |
|
482 QVERIFY(list.lastIndexOf(8) == 6); |
|
483 QVERIFY(list.lastIndexOf(8, list.lastIndexOf(8)-1) == 1); |
|
484 list = copy; |
|
485 } |
|
486 { |
|
487 QList<int> copy = list; |
|
488 list.insert(3, 999); |
|
489 QVERIFY(list[3] == 999); |
|
490 list.replace(3, 222); |
|
491 QVERIFY(list[3] == 222); |
|
492 QVERIFY(list.contains(222) && ! list.contains(999)); |
|
493 list.removeAt(3); |
|
494 list = copy; |
|
495 QVERIFY(list == copy); |
|
496 } |
|
497 { |
|
498 list.clear(); |
|
499 QVERIFY(list.isEmpty()); |
|
500 QVERIFY(list.begin() == list.end()); |
|
501 QListIterator<int> i(list); |
|
502 QVERIFY(!i.hasNext() && !i.hasPrevious()); |
|
503 } |
|
504 { |
|
505 QList<int> l1; |
|
506 QList<int> l2; |
|
507 l1 << 1 << 2 << 3; |
|
508 l2 << 4 << 5 << 6; |
|
509 QList<int> l3 = l1 + l2; |
|
510 l1 += l2; |
|
511 QVERIFY(l3 == l1); |
|
512 } |
|
513 { |
|
514 QList<int> list; |
|
515 QVERIFY(list.isEmpty()); |
|
516 list.append(1); |
|
517 QList<int> list2; |
|
518 list2 = list; |
|
519 list2.clear(); |
|
520 QVERIFY(list2.size() == 0); |
|
521 QVERIFY(list.size() == 1); |
|
522 } |
|
523 { |
|
524 QList<int> list; |
|
525 list.append(1); |
|
526 list = list; |
|
527 QVERIFY(list.size() == 1); |
|
528 } |
|
529 } |
|
530 { |
|
531 QList<void*> list; |
|
532 list.append(0); |
|
533 list.append((void*)42); |
|
534 QCOMPARE(list.size(), 2); |
|
535 QCOMPARE(list.at(0), (void*)0); |
|
536 QCOMPARE(list.at(1), (void*)42); |
|
537 } |
|
538 |
|
539 { |
|
540 QVector<QString> vector(5); |
|
541 vector[0] = "99"; |
|
542 vector[4] ="100"; |
|
543 QList<QString> list = vector.toList(); |
|
544 |
|
545 QVERIFY(list.size() == 5); |
|
546 QVERIFY(list.at(0) == "99"); |
|
547 QVERIFY(list.at(4) == "100"); |
|
548 list[0] = "10"; |
|
549 QVERIFY(list.at(0) == "10"); |
|
550 QVERIFY(vector.at(0) == "99"); |
|
551 |
|
552 } |
|
553 |
|
554 { |
|
555 QList<QString> list; |
|
556 list.append("Hello"); |
|
557 |
|
558 QList<QString>::iterator it = list.begin(); |
|
559 QVERIFY((*it)[0] == QChar('H')); |
|
560 QVERIFY(it->constData()[0] == QChar('H')); |
|
561 it->replace(QChar('H'), QChar('X')); |
|
562 QVERIFY(list.first() == "Xello"); |
|
563 |
|
564 QList<QString>::const_iterator cit = list.constBegin(); |
|
565 QVERIFY((*cit).toLower() == "xello"); |
|
566 QVERIFY(cit->toUpper() == "XELLO"); |
|
567 } |
|
568 |
|
569 { |
|
570 QList<int *> list; |
|
571 QVERIFY(list.value(0) == 0); |
|
572 int i; |
|
573 list.append(&i); |
|
574 QVERIFY(list.value(0) == &i); |
|
575 } |
|
576 { |
|
577 QList<const int *> list; |
|
578 QVERIFY(list.value(0) == 0); |
|
579 int i; |
|
580 list.append(&i); |
|
581 QVERIFY(list.value(0) == &i); |
|
582 } |
|
583 { |
|
584 QList<int> list; |
|
585 QVERIFY(list.value(0) == 0); |
|
586 list.append(10); |
|
587 QVERIFY(list.value(0) == 10); |
|
588 } |
|
589 { |
|
590 QList<Pod> list; |
|
591 QCOMPARE(list.value(0).i1, 0); |
|
592 QCOMPARE(list.value(0).i2, 0); |
|
593 } |
|
594 |
|
595 { |
|
596 QList<QString> list; |
|
597 list << "alpha" << "beta"; |
|
598 list += list; |
|
599 QVERIFY(list.size() == 4); |
|
600 QVERIFY(list.at(0) == "alpha"); |
|
601 QVERIFY(list.at(1) == "beta"); |
|
602 QVERIFY(list.at(2) == "alpha"); |
|
603 QVERIFY(list.at(3) == "beta"); |
|
604 } |
|
605 |
|
606 // test endcases for inserting into a qlist |
|
607 { |
|
608 QList<QString> list; |
|
609 list << "foo" << "bar"; |
|
610 QVERIFY(!list.isEmpty()); |
|
611 |
|
612 list.insert(-1, "lessthanzero"); |
|
613 QCOMPARE(list.at(0), QString("lessthanzero")); |
|
614 |
|
615 list.insert(0, "atzero"); |
|
616 QCOMPARE(list.at(0), QString("atzero")); |
|
617 |
|
618 int listCount = list.count(); |
|
619 list.insert(listCount, "atcount"); |
|
620 QCOMPARE(list.at(listCount), QString("atcount")); |
|
621 |
|
622 listCount = list.count(); |
|
623 list.insert(listCount + 1, "beyondcount"); |
|
624 QCOMPARE(list.at(listCount), QString("beyondcount")); |
|
625 } |
|
626 |
|
627 { |
|
628 QList<int> list1; |
|
629 list1 << 0 << 1 << 2 << 3; |
|
630 list1.removeFirst(); |
|
631 |
|
632 list1.swap(0, 0); |
|
633 QVERIFY(list1 == QList<int>() << 1 << 2 << 3); |
|
634 |
|
635 list1.swap(1, 1); |
|
636 QVERIFY(list1 == QList<int>() << 1 << 2 << 3); |
|
637 |
|
638 list1.swap(2, 2); |
|
639 QVERIFY(list1 == QList<int>() << 1 << 2 << 3); |
|
640 |
|
641 list1.swap(0, 1); |
|
642 QVERIFY(list1 == QList<int>() << 2 << 1 << 3); |
|
643 |
|
644 list1.swap(0, 2); |
|
645 QVERIFY(list1 == QList<int>() << 3 << 1 << 2); |
|
646 |
|
647 list1.swap(1, 2); |
|
648 QVERIFY(list1 == QList<int>() << 3 << 2 << 1); |
|
649 |
|
650 list1.swap(1, 2); |
|
651 QVERIFY(list1 == QList<int>() << 3 << 1 << 2); |
|
652 |
|
653 QList<QString> list2; |
|
654 list2 << "1" << "2" << "3"; |
|
655 |
|
656 list2.swap(0, 0); |
|
657 QVERIFY(list2 == QList<QString>() << "1" << "2" << "3"); |
|
658 |
|
659 list2.swap(1, 1); |
|
660 QVERIFY(list2 == QList<QString>() << "1" << "2" << "3"); |
|
661 |
|
662 list2.swap(2, 2); |
|
663 QVERIFY(list2 == QList<QString>() << "1" << "2" << "3"); |
|
664 |
|
665 list2.swap(0, 1); |
|
666 QVERIFY(list2 == QList<QString>() << "2" << "1" << "3"); |
|
667 |
|
668 list2.swap(0, 2); |
|
669 QVERIFY(list2 == QList<QString>() << "3" << "1" << "2"); |
|
670 |
|
671 list2.swap(1, 2); |
|
672 QVERIFY(list2 == QList<QString>() << "3" << "2" << "1"); |
|
673 |
|
674 list2.swap(1, 2); |
|
675 QVERIFY(list2 == QList<QString>() << "3" << "1" << "2"); |
|
676 |
|
677 QList<double> list3; |
|
678 list3 << 1.0 << 2.0 << 3.0; |
|
679 |
|
680 list3.swap(0, 0); |
|
681 QVERIFY(list3 == QList<double>() << 1.0 << 2.0 << 3.0); |
|
682 |
|
683 list3.swap(1, 1); |
|
684 QVERIFY(list3 == QList<double>() << 1.0 << 2.0 << 3.0); |
|
685 |
|
686 list3.swap(2, 2); |
|
687 QVERIFY(list3 == QList<double>() << 1.0 << 2.0 << 3.0); |
|
688 |
|
689 list3.swap(0, 1); |
|
690 QVERIFY(list3 == QList<double>() << 2.0 << 1.0 << 3.0); |
|
691 |
|
692 list3.swap(0, 2); |
|
693 QVERIFY(list3 == QList<double>() << 3.0 << 1.0 << 2.0); |
|
694 |
|
695 list3.swap(1, 2); |
|
696 QVERIFY(list3 == QList<double>() << 3.0 << 2.0 << 1.0); |
|
697 |
|
698 list3.swap(1, 2); |
|
699 QVERIFY(list3 == QList<double>() << 3.0 << 1.0 << 2.0); |
|
700 } |
|
701 |
|
702 // Check what happens when using references to own items. |
|
703 // Ideally we should run valgrind on this. |
|
704 { |
|
705 int i; |
|
706 |
|
707 QList<void *> list1; |
|
708 list1.append(reinterpret_cast<void *>(50)); |
|
709 |
|
710 for (i = 1; i < 100; ++i) { |
|
711 list1.append(list1.at(i - 1)); |
|
712 list1.prepend(list1.at(i)); |
|
713 list1.insert(i, list1.at(i - 1)); |
|
714 list1.insert(i, list1.at(i)); |
|
715 list1.insert(i, list1.at(i + 1)); |
|
716 list1.replace(i, list1.at(i - 1)); |
|
717 list1.replace(i, list1.at(i)); |
|
718 list1.replace(i, list1.at(i + 1)); |
|
719 } |
|
720 QCOMPARE(list1.size(), 496); |
|
721 for (i = 0; i < list1.size(); ++i) { |
|
722 QCOMPARE(list1.at(i), reinterpret_cast<void *>(50)); |
|
723 } |
|
724 |
|
725 QList<QString> list2; |
|
726 list2.append("50"); |
|
727 |
|
728 for (i = 1; i < 100; ++i) { |
|
729 list2.append(list2.at(i - 1)); |
|
730 list2.prepend(list2.at(i)); |
|
731 list2.insert(i, list2.at(i - 1)); |
|
732 list2.insert(i, list2.at(i)); |
|
733 list2.insert(i, list2.at(i + 1)); |
|
734 list2.replace(i, list2.at(i - 1)); |
|
735 list2.replace(i, list2.at(i)); |
|
736 list2.replace(i, list2.at(i + 1)); |
|
737 } |
|
738 QCOMPARE(list2.size(), 496); |
|
739 for (i = 0; i < list2.size(); ++i) { |
|
740 QCOMPARE(list2.at(i), QString::fromLatin1("50")); |
|
741 } |
|
742 |
|
743 QList<double> list3; |
|
744 list3.append(50.0); |
|
745 |
|
746 for (i = 1; i < 100; ++i) { |
|
747 list3.append(list3.at(i - 1)); |
|
748 list3.prepend(list3.at(i)); |
|
749 list3.insert(i, list3.at(i - 1)); |
|
750 list3.insert(i, list3.at(i)); |
|
751 list3.insert(i, list3.at(i + 1)); |
|
752 list3.replace(i, list3.at(i - 1)); |
|
753 list3.replace(i, list3.at(i)); |
|
754 list3.replace(i, list3.at(i + 1)); |
|
755 } |
|
756 QCOMPARE(list3.size(), 496); |
|
757 for (i = 0; i < list3.size(); ++i) { |
|
758 QCOMPARE(list3.at(i), 50.0); |
|
759 } |
|
760 |
|
761 QList<QTime> list4; |
|
762 list4.append(QTime(12, 34, 56)); |
|
763 |
|
764 for (i = 1; i < 100; ++i) { |
|
765 list4.append(list4.at(i - 1)); |
|
766 list4.prepend(list4.at(i)); |
|
767 list4.insert(i, list4.at(i - 1)); |
|
768 list4.insert(i, list4.at(i)); |
|
769 list4.insert(i, list4.at(i + 1)); |
|
770 list4.replace(i, list4.at(i - 1)); |
|
771 list4.replace(i, list4.at(i)); |
|
772 list4.replace(i, list4.at(i + 1)); |
|
773 } |
|
774 QCOMPARE(list4.size(), 496); |
|
775 for (i = 0; i < list4.size(); ++i) { |
|
776 QVERIFY(list4.at(i) == QTime(12, 34, 56)); |
|
777 } |
|
778 |
|
779 } |
|
780 { |
|
781 QList<int> a; |
|
782 QCOMPARE(a.startsWith(1), false); |
|
783 QCOMPARE(a.endsWith(1), false); |
|
784 a.append(1); |
|
785 QCOMPARE(a.startsWith(1), true); |
|
786 QCOMPARE(a.startsWith(2), false); |
|
787 QCOMPARE(a.endsWith(1), true); |
|
788 QCOMPARE(a.endsWith(2), false); |
|
789 a.append(2); |
|
790 QCOMPARE(a.startsWith(1), true); |
|
791 QCOMPARE(a.startsWith(2), false); |
|
792 QCOMPARE(a.endsWith(1), false); |
|
793 QCOMPARE(a.endsWith(2), true); |
|
794 } |
|
795 } |
|
796 |
|
797 void tst_Collections::linkedList() |
|
798 { |
|
799 { |
|
800 QLinkedList<int> list; |
|
801 QVERIFY(list.isEmpty()); |
|
802 list.append(1); |
|
803 list.push_back(2); |
|
804 list += (3); |
|
805 list << 4 << 5 << 6; |
|
806 QVERIFY(!list.isEmpty()); |
|
807 QVERIFY(list.size() == 6); |
|
808 { |
|
809 int sum = 0; |
|
810 QLinkedListIterator<int> i = list; |
|
811 while (i.hasNext()) { |
|
812 sum += i.next(); |
|
813 } |
|
814 QVERIFY(sum == 21); |
|
815 } |
|
816 { |
|
817 int sum = 0; |
|
818 QLinkedList<int>::const_iterator i = list.begin(); |
|
819 while (i != list.end()) |
|
820 sum += *i++; |
|
821 QVERIFY(sum == 21); |
|
822 } |
|
823 { |
|
824 QMutableLinkedListIterator<int> i = list; |
|
825 while (i.hasNext()) |
|
826 i.setValue(2*i.next()); |
|
827 } |
|
828 { |
|
829 int sum = 0; |
|
830 QLinkedListIterator<int> i = list; |
|
831 i.toBack(); |
|
832 while (i.hasPrevious()) |
|
833 sum += i.previous(); |
|
834 QVERIFY(sum == 2*21); |
|
835 } |
|
836 { |
|
837 QMutableLinkedListIterator<int> i = list; |
|
838 i.toBack(); |
|
839 while (i.hasPrevious()) |
|
840 i.setValue(2*i.previous()); |
|
841 } |
|
842 { |
|
843 int sum = 0; |
|
844 QLinkedListIterator<int> i = list; |
|
845 i.toBack(); |
|
846 while (i.hasPrevious()) |
|
847 sum += i.previous(); |
|
848 QVERIFY(sum == 2*2*21); |
|
849 } |
|
850 { |
|
851 QMutableLinkedListIterator<int> i = list; |
|
852 while (i.hasNext()) { |
|
853 int a = i.next(); |
|
854 i.insert(a); |
|
855 } |
|
856 } |
|
857 { |
|
858 int sum = 0; |
|
859 QLinkedList<int>::iterator i = list.begin(); |
|
860 while (i != list.end()) |
|
861 sum += *i++; |
|
862 QVERIFY(sum == 2*2*2*21); |
|
863 } |
|
864 { |
|
865 int duplicates = 0; |
|
866 QLinkedListIterator<int> i = list; |
|
867 while (i.hasNext()) { |
|
868 int a = i.next(); |
|
869 if (i.hasNext() && a == i.peekNext()) |
|
870 duplicates++; |
|
871 } |
|
872 QVERIFY(duplicates == 6); |
|
873 } |
|
874 { |
|
875 int duplicates = 0; |
|
876 QLinkedListIterator<int> i = list; |
|
877 i.toBack(); |
|
878 while (i.hasPrevious()) { |
|
879 int a = i.previous(); |
|
880 if (i.hasPrevious() && a == i.peekPrevious()) |
|
881 duplicates++; |
|
882 } |
|
883 QVERIFY(duplicates == 6); |
|
884 } |
|
885 { |
|
886 QMutableLinkedListIterator<int> i = list; |
|
887 while (i.hasNext()) { |
|
888 int a = i.next(); |
|
889 if (i.hasNext() && |
|
890 i.peekNext() == a) |
|
891 i.remove(); |
|
892 } |
|
893 } |
|
894 { |
|
895 int duplicates = 0; |
|
896 QMutableLinkedListIterator<int> i = list; |
|
897 i.toBack(); |
|
898 while (i.hasPrevious()) { |
|
899 int a = i.previous(); |
|
900 if (i.hasPrevious() && a == i.peekPrevious()) |
|
901 duplicates++; |
|
902 } |
|
903 QVERIFY(duplicates == 0); |
|
904 } |
|
905 { |
|
906 QVERIFY(list.size() == 6); |
|
907 QMutableLinkedListIterator<int> i = list; |
|
908 while (i.hasNext()) { |
|
909 int a = i.peekNext(); |
|
910 i.insert(42); |
|
911 QVERIFY(i.peekPrevious() == 42 && i.peekNext() == a); |
|
912 i.next(); |
|
913 } |
|
914 QVERIFY(list.size() == 12); |
|
915 i.toFront(); |
|
916 while (i.findNext(42)) |
|
917 i.remove(); |
|
918 } |
|
919 { |
|
920 QLinkedList<int> l; |
|
921 l << 4 << 8 << 12 << 16 << 20 << 24; |
|
922 QVERIFY(l == list); |
|
923 QLinkedList<int> copy = list; |
|
924 list += list; |
|
925 QVERIFY(l != list && l.size() == list.size()/2 && l == copy); |
|
926 l += copy; |
|
927 QVERIFY(l == list); |
|
928 list = copy; |
|
929 } |
|
930 { |
|
931 QLinkedList<int> copy = list; |
|
932 list.prepend(999); |
|
933 list.append(999); |
|
934 QVERIFY(list.contains(999)); |
|
935 QVERIFY(list.count(999) == 2); |
|
936 list.removeAll(999); |
|
937 QVERIFY(list == copy); |
|
938 } |
|
939 { |
|
940 QLinkedList<QString> list; |
|
941 list << "one" << "two" << "three" << "four" << "five" << "six"; |
|
942 while (!list.isEmpty()) |
|
943 list.removeAll(list.first()); |
|
944 } |
|
945 { |
|
946 QLinkedList<QString> list; |
|
947 list << "one" << "two" << "one" << "two"; |
|
948 QVERIFY(!list.removeOne("three")); |
|
949 QVERIFY(list.removeOne("two")); |
|
950 QCOMPARE(list, QLinkedList<QString>() << "one" << "one" << "two");; |
|
951 QVERIFY(list.removeOne("two")); |
|
952 QCOMPARE(list, QLinkedList<QString>() << "one" << "one"); |
|
953 QVERIFY(!list.removeOne("two")); |
|
954 QCOMPARE(list, QLinkedList<QString>() << "one" << "one"); |
|
955 QVERIFY(list.removeOne("one")); |
|
956 QCOMPARE(list, QLinkedList<QString>() << "one"); |
|
957 QVERIFY(list.removeOne("one")); |
|
958 QVERIFY(list.isEmpty()); |
|
959 QVERIFY(!list.removeOne("one")); |
|
960 QVERIFY(list.isEmpty()); |
|
961 } |
|
962 { |
|
963 list.clear(); |
|
964 QVERIFY(list.isEmpty()); |
|
965 QVERIFY(list.begin() == list.end()); |
|
966 QLinkedListIterator<int> i(list); |
|
967 QVERIFY(!i.hasNext() && !i.hasPrevious()); |
|
968 } |
|
969 } |
|
970 |
|
971 { |
|
972 QLinkedList<QString> list; |
|
973 list.append("Hello"); |
|
974 |
|
975 QLinkedList<QString>::iterator it = list.begin(); |
|
976 QVERIFY((*it)[0] == QChar('H')); |
|
977 QVERIFY(it->constData()[0] == QChar('H')); |
|
978 it->replace(QChar('H'), QChar('X')); |
|
979 QVERIFY(list.first() == "Xello"); |
|
980 |
|
981 QLinkedList<QString>::const_iterator cit = list.constBegin(); |
|
982 QVERIFY((*cit).toLower() == "xello"); |
|
983 QVERIFY(cit->toUpper() == "XELLO"); |
|
984 } |
|
985 |
|
986 { |
|
987 QLinkedList<QString> list; |
|
988 list << "alpha" << "beta"; |
|
989 list += list; |
|
990 QVERIFY(list.size() == 4); |
|
991 QVERIFY(*list.begin() == "alpha"); |
|
992 QVERIFY(*(list.begin() + 1) == "beta"); |
|
993 QVERIFY(*(list.begin() + 2) == "alpha"); |
|
994 QVERIFY(*(list.begin() + 3) == "beta"); |
|
995 } |
|
996 |
|
997 { |
|
998 QLinkedList<int> a; |
|
999 QCOMPARE(a.startsWith(1), false); |
|
1000 QCOMPARE(a.endsWith(1), false); |
|
1001 a.append(1); |
|
1002 QCOMPARE(a.startsWith(1), true); |
|
1003 QCOMPARE(a.startsWith(2), false); |
|
1004 QCOMPARE(a.endsWith(1), true); |
|
1005 QCOMPARE(a.endsWith(2), false); |
|
1006 a.append(2); |
|
1007 QCOMPARE(a.startsWith(1), true); |
|
1008 QCOMPARE(a.startsWith(2), false); |
|
1009 QCOMPARE(a.endsWith(1), false); |
|
1010 QCOMPARE(a.endsWith(2), true); |
|
1011 } |
|
1012 }; |
|
1013 |
|
1014 |
|
1015 void tst_Collections::vector() |
|
1016 { |
|
1017 QVector<int> v1; |
|
1018 v1 << 1 << 2 << 3; |
|
1019 QVector<int> v2; |
|
1020 v2 << 4 << 5; |
|
1021 QVector<int> v3; |
|
1022 v3 << 1 << 2 << 3 << 4 << 5; |
|
1023 QVERIFY(v1 + v2 == v3); |
|
1024 |
|
1025 QVector<int> emptyVector; |
|
1026 QCOMPARE(emptyVector.size(), 0); |
|
1027 #if 0 |
|
1028 // this should trigger an assert |
|
1029 emptyVector.remove(3, -3); |
|
1030 QCOMPARE(emptyVector.size(), 0); |
|
1031 #endif |
|
1032 emptyVector.remove(0, 0); |
|
1033 QCOMPARE(emptyVector.size(), 0); |
|
1034 |
|
1035 QVector<int> v4; |
|
1036 v4 << 1 << 2 << 3; |
|
1037 QCOMPARE(v4.size(), 3); |
|
1038 v4.remove(1, 0); |
|
1039 QCOMPARE(v4.size(), 3); |
|
1040 |
|
1041 QVector<int> v; |
|
1042 v.append(2); |
|
1043 QVERIFY(*v.begin() == 2); |
|
1044 v.prepend(1); |
|
1045 |
|
1046 v << 3 << 4 << 5 << 6; |
|
1047 #if !defined(QT_NO_STL) |
|
1048 QVERIFY(std::binary_search(v.begin(), v.end(), 2) == true); |
|
1049 QVERIFY(std::binary_search(v.begin(), v.end(), 9) == false); |
|
1050 #endif |
|
1051 QVERIFY(qBinaryFind(v.begin(), v.end(), 2) == v.begin() + 1); |
|
1052 QVERIFY(qLowerBound(v.begin(), v.end(), 2) == v.begin() + 1); |
|
1053 QVERIFY(qUpperBound(v.begin(), v.end(), 2) == v.begin() + 2); |
|
1054 QVERIFY(qBinaryFind(v.begin(), v.end(), 9) == v.end()); |
|
1055 QVERIFY(qLowerBound(v.begin(), v.end(), 9) == v.end()); |
|
1056 QVERIFY(qUpperBound(v.begin(), v.end(), 9) == v.end()); |
|
1057 |
|
1058 v.clear(); |
|
1059 v << 1 << 2 << 3; |
|
1060 v.insert(v.begin(), 0); |
|
1061 v.insert(v.end(), 4); |
|
1062 v.insert(v.begin()+2, 9); |
|
1063 |
|
1064 QVector<int> result; |
|
1065 result << 0 << 1 << 9 << 2 << 3 << 4; |
|
1066 |
|
1067 QVERIFY( v == result ); |
|
1068 |
|
1069 v.clear(); |
|
1070 v << 1 << 2 << 3; |
|
1071 v.insert(0, 0); |
|
1072 v.insert(4, 4); |
|
1073 v.insert(2, 9); |
|
1074 |
|
1075 QVERIFY( v == result ); |
|
1076 |
|
1077 QVector<QString> vec; |
|
1078 vec << "foo" << "bar"; |
|
1079 vec.reserve( 512 ); |
|
1080 QVERIFY(vec[0] == "foo"); |
|
1081 QVERIFY(vec[1] == "bar"); |
|
1082 |
|
1083 int initialLargeStaticCount = LargeStatic::count; |
|
1084 { |
|
1085 QVector<LargeStatic> vector; |
|
1086 vector.append(LargeStatic()); |
|
1087 vector.resize(0); |
|
1088 } |
|
1089 QCOMPARE(LargeStatic::count, initialLargeStaticCount); |
|
1090 |
|
1091 { |
|
1092 QVector<QString> vector; |
|
1093 vector << "alpha" << "beta"; |
|
1094 vector += vector; |
|
1095 QVERIFY(vector.size() == 4); |
|
1096 QVERIFY(vector.at(0) == "alpha"); |
|
1097 QVERIFY(vector.at(1) == "beta"); |
|
1098 QVERIFY(vector.at(2) == "alpha"); |
|
1099 QVERIFY(vector.at(3) == "beta"); |
|
1100 } |
|
1101 |
|
1102 int originalLargeStaticCount = LargeStatic::count; |
|
1103 { |
|
1104 QVector<LargeStatic> vector(5); |
|
1105 } |
|
1106 QVERIFY(LargeStatic::count == originalLargeStaticCount); |
|
1107 { |
|
1108 QVector<LargeStatic> vector(5); |
|
1109 QList<LargeStatic> list = vector.toList(); |
|
1110 } |
|
1111 QVERIFY(LargeStatic::count == originalLargeStaticCount); |
|
1112 { |
|
1113 QVector<LargeStatic> vector; |
|
1114 LargeStatic *dummy = 0; |
|
1115 for (int i = 0; i < 10000; ++i) { |
|
1116 delete dummy; |
|
1117 dummy = new LargeStatic; |
|
1118 vector.append(LargeStatic()); |
|
1119 } |
|
1120 delete dummy; |
|
1121 } |
|
1122 QVERIFY(LargeStatic::count == originalLargeStaticCount); |
|
1123 |
|
1124 int originalMovableCount = Movable::count; |
|
1125 { |
|
1126 QVector<Movable> vector(5); |
|
1127 } |
|
1128 QVERIFY(Movable::count == originalMovableCount); |
|
1129 { |
|
1130 QVector<Movable> vector(5); |
|
1131 QList<Movable> list = vector.toList(); |
|
1132 } |
|
1133 QVERIFY(Movable::count == originalMovableCount); |
|
1134 { |
|
1135 QVector<Movable> vector; |
|
1136 Movable *dummy = 0; |
|
1137 for (int i = 0; i < 10000; ++i) { |
|
1138 delete dummy; |
|
1139 dummy = new Movable; |
|
1140 vector.append(Movable()); |
|
1141 } |
|
1142 delete dummy; |
|
1143 } |
|
1144 QVERIFY(Movable::count == originalMovableCount); |
|
1145 |
|
1146 // Check what happens when using references to own items. |
|
1147 // Ideally we should run valgrind on this. |
|
1148 { |
|
1149 int i; |
|
1150 |
|
1151 QVector<void *> vect1; |
|
1152 vect1.append(reinterpret_cast<void *>(50)); |
|
1153 |
|
1154 for (i = 1; i < 100; ++i) { |
|
1155 vect1.append(vect1.at(i - 1)); |
|
1156 vect1.prepend(vect1.at(i)); |
|
1157 vect1.insert(i, vect1.at(i - 1)); |
|
1158 vect1.insert(i, vect1.at(i)); |
|
1159 vect1.insert(i, vect1.at(i + 1)); |
|
1160 vect1.replace(i, vect1.at(i - 1)); |
|
1161 vect1.replace(i, vect1.at(i)); |
|
1162 vect1.replace(i, vect1.at(i + 1)); |
|
1163 } |
|
1164 QCOMPARE(vect1.size(), 496); |
|
1165 for (i = 0; i < vect1.size(); ++i) { |
|
1166 QCOMPARE(vect1.at(i), reinterpret_cast<void *>(50)); |
|
1167 } |
|
1168 |
|
1169 QVector<QString> vect2; |
|
1170 vect2.append("50"); |
|
1171 |
|
1172 for (i = 1; i < 100; ++i) { |
|
1173 vect2.append(vect2.at(i - 1)); |
|
1174 vect2.prepend(vect2.at(i)); |
|
1175 vect2.insert(i, vect2.at(i - 1)); |
|
1176 vect2.insert(i, vect2.at(i)); |
|
1177 vect2.insert(i, vect2.at(i + 1)); |
|
1178 vect2.replace(i, vect2.at(i - 1)); |
|
1179 vect2.replace(i, vect2.at(i)); |
|
1180 vect2.replace(i, vect2.at(i + 1)); |
|
1181 } |
|
1182 QCOMPARE(vect2.size(), 496); |
|
1183 for (i = 0; i < vect2.size(); ++i) { |
|
1184 QCOMPARE(vect2.at(i), QString::fromLatin1("50")); |
|
1185 } |
|
1186 |
|
1187 QVector<double> vect3; |
|
1188 vect3.append(50.0); |
|
1189 |
|
1190 for (i = 1; i < 100; ++i) { |
|
1191 vect3.append(vect3.at(i - 1)); |
|
1192 vect3.prepend(vect3.at(i)); |
|
1193 vect3.insert(i, vect3.at(i - 1)); |
|
1194 vect3.insert(i, vect3.at(i)); |
|
1195 vect3.insert(i, vect3.at(i + 1)); |
|
1196 vect3.replace(i, vect3.at(i - 1)); |
|
1197 vect3.replace(i, vect3.at(i)); |
|
1198 vect3.replace(i, vect3.at(i + 1)); |
|
1199 } |
|
1200 QCOMPARE(vect3.size(), 496); |
|
1201 for (i = 0; i < vect3.size(); ++i) { |
|
1202 QCOMPARE(vect3.at(i), 50.0); |
|
1203 } |
|
1204 |
|
1205 QVector<QTime> vect4; |
|
1206 vect4.append(QTime(12, 34, 56)); |
|
1207 |
|
1208 for (i = 1; i < 100; ++i) { |
|
1209 vect4.append(vect4.at(i - 1)); |
|
1210 vect4.prepend(vect4.at(i)); |
|
1211 vect4.insert(i, vect4.at(i - 1)); |
|
1212 vect4.insert(i, vect4.at(i)); |
|
1213 vect4.insert(i, vect4.at(i + 1)); |
|
1214 vect4.replace(i, vect4.at(i - 1)); |
|
1215 vect4.replace(i, vect4.at(i)); |
|
1216 vect4.replace(i, vect4.at(i + 1)); |
|
1217 } |
|
1218 QCOMPARE(vect4.size(), 496); |
|
1219 for (i = 0; i < vect4.size(); ++i) { |
|
1220 QVERIFY(vect4.at(i) == QTime(12, 34, 56)); |
|
1221 } |
|
1222 } |
|
1223 |
|
1224 // this used to trigger an unitialized read in valgrind |
|
1225 QVector<char> foo; |
|
1226 foo.resize(144); |
|
1227 |
|
1228 { |
|
1229 QVector<int> a; |
|
1230 QCOMPARE(a.startsWith(1), false); |
|
1231 QCOMPARE(a.endsWith(1), false); |
|
1232 a.append(1); |
|
1233 QCOMPARE(a.startsWith(1), true); |
|
1234 QCOMPARE(a.startsWith(2), false); |
|
1235 QCOMPARE(a.endsWith(1), true); |
|
1236 QCOMPARE(a.endsWith(2), false); |
|
1237 a.append(2); |
|
1238 QCOMPARE(a.startsWith(1), true); |
|
1239 QCOMPARE(a.startsWith(2), false); |
|
1240 QCOMPARE(a.endsWith(1), false); |
|
1241 QCOMPARE(a.endsWith(2), true); |
|
1242 } |
|
1243 } |
|
1244 |
|
1245 void tst_Collections::byteArray() |
|
1246 { |
|
1247 QByteArray hello = "hello"; |
|
1248 QByteArray ello = "ello"; |
|
1249 QByteArray World = "World"; |
|
1250 QByteArray Wor = "Wor"; |
|
1251 QByteArray helloWorld = "helloWorld"; |
|
1252 QVERIFY(hello + World == helloWorld); |
|
1253 QVERIFY(hello + "World" == helloWorld); |
|
1254 QVERIFY("hello" + World == helloWorld); |
|
1255 |
|
1256 |
|
1257 QByteArray l; |
|
1258 QVERIFY('h' + ello == hello); |
|
1259 QVERIFY(Wor + 'l' + 'd' == "World"); |
|
1260 QVERIFY(hello + World == "helloWorld"); |
|
1261 QVERIFY(hello + "World" == "helloWorld"); |
|
1262 QVERIFY("hello" + World == "helloWorld"); |
|
1263 QVERIFY('h' + ello == "hello"); |
|
1264 QVERIFY(Wor + 'l' + 'd' == "World"); |
|
1265 QVERIFY("helloWorld" == hello + World); |
|
1266 QVERIFY("helloWorld" == hello + "World"); |
|
1267 QVERIFY("helloWorld" == "hello" + World); |
|
1268 QVERIFY("hello" == 'h' + ello); |
|
1269 QVERIFY("World" == Wor + 'l' + 'd'); |
|
1270 |
|
1271 QVERIFY(hello.contains('e')); |
|
1272 QVERIFY (true == hello.contains('e')); |
|
1273 QVERIFY (hello.contains('e') != false); |
|
1274 |
|
1275 QVERIFY(hello.indexOf('e') == 1); |
|
1276 QVERIFY(hello.indexOf('e', -10) == 1); |
|
1277 QVERIFY(hello.indexOf('l') == 2); |
|
1278 QVERIFY(hello.indexOf('l',2) == 2); |
|
1279 QVERIFY(hello.indexOf('l',3) == 3); |
|
1280 |
|
1281 QByteArray large = "000 100 200 300 400 500 600 700 800 900"; |
|
1282 |
|
1283 QVERIFY(large.indexOf("700") == 28); |
|
1284 QVERIFY(large.indexOf("700", 28) == 28); |
|
1285 QVERIFY(large.indexOf("700", 29) == -1); |
|
1286 QVERIFY(large.lastIndexOf("700") == 28); |
|
1287 QVERIFY(large.lastIndexOf("700", 28) == 28); |
|
1288 QVERIFY(large.lastIndexOf("700", 27) == -1); |
|
1289 |
|
1290 QVERIFY(large.contains("200")); |
|
1291 QVERIFY(!large.contains("201")); |
|
1292 QVERIFY(large.contains('3')); |
|
1293 QVERIFY(!large.contains('a')); |
|
1294 |
|
1295 QVERIFY(large.count("00") == 11); |
|
1296 QVERIFY(large.count('3') == 1); |
|
1297 QVERIFY(large.count('0') == 21); |
|
1298 QVERIFY(large.count("0") == 21); |
|
1299 QVERIFY(large.count("200") == 1); |
|
1300 QVERIFY(large.count("201") == 0); |
|
1301 |
|
1302 QVERIFY(hello.left(0) == ""); |
|
1303 QVERIFY(!hello.left(0).isNull()); |
|
1304 QVERIFY(hello.left(1) == "h"); |
|
1305 QVERIFY(hello.left(2) == "he"); |
|
1306 QVERIFY(hello.left(200) == "hello"); |
|
1307 QVERIFY(hello.left(hello.size()) == hello); |
|
1308 QVERIFY(hello.left(hello.size()+1) == hello); |
|
1309 |
|
1310 QVERIFY(hello.right(0) == ""); |
|
1311 QVERIFY(!hello.right(0).isNull()); |
|
1312 QVERIFY(hello.right(1) == "o"); |
|
1313 QVERIFY(hello.right(2) == "lo"); |
|
1314 QVERIFY(hello.right(200) == "hello"); |
|
1315 QVERIFY(hello.right(hello.size()) == hello); |
|
1316 QVERIFY(hello.right(hello.size()+1) == hello); |
|
1317 |
|
1318 QVERIFY(!hello.mid(0, 0).isNull()); |
|
1319 QVERIFY(hello.mid(0, 1) == "h"); |
|
1320 QVERIFY(hello.mid(0, 2) == "he"); |
|
1321 QVERIFY(hello.mid(0, 200) == "hello"); |
|
1322 QVERIFY(hello.mid(0) == "hello"); |
|
1323 QVERIFY(hello.mid(0, hello.size()) == hello); |
|
1324 QVERIFY(hello.mid(0, hello.size()+1) == hello); |
|
1325 |
|
1326 QVERIFY(hello.mid(hello.size()-0) == ""); |
|
1327 |
|
1328 QVERIFY(hello.mid(hello.size()-0).isNull()); // weird but valid 3.x semantics |
|
1329 QVERIFY(hello.mid(hello.size()-1) == "o"); |
|
1330 QVERIFY(hello.mid(hello.size()-2) == "lo"); |
|
1331 QVERIFY(hello.mid(hello.size()-200) == "hello"); |
|
1332 |
|
1333 QByteArray null; |
|
1334 QByteArray nonNull = ""; |
|
1335 QVERIFY(null.left(10).isNull()); |
|
1336 QVERIFY(null.mid(0).isNull()); |
|
1337 |
|
1338 #if 0 |
|
1339 QVERIFY(null == QByteArray::null); |
|
1340 QVERIFY(QByteArray::null == null); |
|
1341 QVERIFY(nonNull != QByteArray::null); |
|
1342 QVERIFY(QByteArray::null != nonNull); |
|
1343 QVERIFY(null == nonNull); |
|
1344 QVERIFY(QByteArray::null == QByteArray::null); |
|
1345 #endif |
|
1346 |
|
1347 QByteArray str = "Hello"; |
|
1348 QByteArray cstr(str.data(), str.size()); |
|
1349 QVERIFY(str == "Hello"); |
|
1350 QVERIFY(cstr == "Hello"); |
|
1351 cstr.clear(); |
|
1352 QVERIFY(str == "Hello"); |
|
1353 QVERIFY(cstr.isEmpty()); |
|
1354 |
|
1355 { |
|
1356 QByteArray ba1("Foo"); |
|
1357 ba1.prepend(ba1); |
|
1358 QCOMPARE(ba1, QByteArray("FooFoo")); |
|
1359 ba1.append(ba1); |
|
1360 QCOMPARE(ba1, QByteArray("FooFooFooFoo")); |
|
1361 ba1.insert(2, ba1); |
|
1362 QCOMPARE(ba1, QByteArray("FoFooFooFooFoooFooFooFoo")); |
|
1363 ba1.replace(3, 3, ba1); |
|
1364 QCOMPARE(ba1, QByteArray("FoFFoFooFooFooFoooFooFooFooooFooFoooFooFooFoo")); |
|
1365 ba1 = "FooFoo"; |
|
1366 ba1.replace(char('F'), ba1); |
|
1367 QCOMPARE(ba1, QByteArray("FooFooooFooFoooo")); |
|
1368 ba1 = "FooFoo"; |
|
1369 ba1.replace(char('o'), ba1); |
|
1370 QCOMPARE(ba1, QByteArray("FFooFooFooFooFFooFooFooFoo")); |
|
1371 |
|
1372 ba1.replace(ba1, "xxx"); |
|
1373 QCOMPARE(ba1, QByteArray("xxx")); |
|
1374 ba1.replace(ba1, QByteArray("yyy")); |
|
1375 QCOMPARE(ba1, QByteArray("yyy")); |
|
1376 ba1 += ba1; |
|
1377 QCOMPARE(ba1, QByteArray("yyyyyy")); |
|
1378 |
|
1379 ba1.remove(1, -1); // do nothing |
|
1380 QCOMPARE(ba1, QByteArray("yyyyyy")); |
|
1381 |
|
1382 ba1.replace(0, -1, "ZZZ"); |
|
1383 QCOMPARE(ba1, QByteArray("ZZZyyyyyy")); |
|
1384 } |
|
1385 }; |
|
1386 |
|
1387 void tst_Collections::stack() |
|
1388 { |
|
1389 QStack<int> stack; |
|
1390 stack.push(1); |
|
1391 stack.push(2); |
|
1392 stack.push(3); |
|
1393 QVectorIterator<int> i = stack; |
|
1394 i.toBack(); |
|
1395 int sum = 0; |
|
1396 while (i.hasPrevious()) |
|
1397 sum += i.previous(); |
|
1398 QVERIFY(sum == 6); |
|
1399 |
|
1400 sum = 0; |
|
1401 for (QStack<int>::iterator i = stack.begin(); i != stack.end(); ++i) |
|
1402 sum += *i; |
|
1403 QVERIFY(sum == 6); |
|
1404 |
|
1405 while (!stack.isEmpty()) |
|
1406 sum -= stack.pop(); |
|
1407 QVERIFY(sum == 0); |
|
1408 } |
|
1409 |
|
1410 void tst_Collections::hash() |
|
1411 { |
|
1412 const char *hello = "hello"; |
|
1413 const char *world = "world"; |
|
1414 const char *allo = "allo"; |
|
1415 const char *monde = "monde"; |
|
1416 |
|
1417 { |
|
1418 typedef QHash<QString, QString> Hash; |
|
1419 Hash hash; |
|
1420 QString key; |
|
1421 for (int i = 0; i < 10; ++i) { |
|
1422 key[0] = i + '0'; |
|
1423 for (int j = 0; j < 10; ++j) { |
|
1424 key[1] = j + '0'; |
|
1425 hash.insert(key, "V" + key); |
|
1426 } |
|
1427 } |
|
1428 |
|
1429 for (int i = 0; i < 10; ++i) { |
|
1430 key[0] = i + '0'; |
|
1431 for (int j = 0; j < 10; ++j) { |
|
1432 key[1] = j + '0'; |
|
1433 hash.remove(key); |
|
1434 } |
|
1435 } |
|
1436 } |
|
1437 |
|
1438 { |
|
1439 typedef QHash<int, const char *> Hash; |
|
1440 Hash hash; |
|
1441 hash.insert(1, hello); |
|
1442 hash.insert(2, world); |
|
1443 |
|
1444 QVERIFY(hash.size() == 2); |
|
1445 QVERIFY(!hash.isEmpty()); |
|
1446 |
|
1447 { |
|
1448 Hash hash2 = hash; |
|
1449 hash2 = hash; |
|
1450 hash = hash2; |
|
1451 hash2 = hash2; |
|
1452 hash = hash; |
|
1453 hash2.clear(); |
|
1454 hash2 = hash2; |
|
1455 QVERIFY(hash2.size() == 0); |
|
1456 QVERIFY(hash2.isEmpty()); |
|
1457 } |
|
1458 QVERIFY(hash.size() == 2); |
|
1459 |
|
1460 { |
|
1461 Hash hash2 = hash; |
|
1462 hash2[1] = allo; |
|
1463 hash2[2] = monde; |
|
1464 |
|
1465 QVERIFY(hash2[1] == allo); |
|
1466 QVERIFY(hash2[2] == monde); |
|
1467 QVERIFY(hash[1] == hello); |
|
1468 QVERIFY(hash[2] == world); |
|
1469 |
|
1470 hash2[1] = hash[1]; |
|
1471 hash2[2] = hash[2]; |
|
1472 |
|
1473 QVERIFY(hash2[1] == hello); |
|
1474 QVERIFY(hash2[2] == world); |
|
1475 |
|
1476 hash[1] = hash[1]; |
|
1477 QVERIFY(hash[1] == hello); |
|
1478 } |
|
1479 |
|
1480 { |
|
1481 Hash hash2 = hash; |
|
1482 hash2.detach(); |
|
1483 hash2.remove(1); |
|
1484 QVERIFY(hash2.size() == 1); |
|
1485 hash2.remove(1); |
|
1486 QVERIFY(hash2.size() == 1); |
|
1487 hash2.remove(0); |
|
1488 QVERIFY(hash2.size() == 1); |
|
1489 hash2.remove(2); |
|
1490 QVERIFY(hash2.size() == 0); |
|
1491 QVERIFY(hash.size() == 2); |
|
1492 } |
|
1493 |
|
1494 hash.detach(); |
|
1495 |
|
1496 { |
|
1497 Hash::iterator it1 = hash.find(1); |
|
1498 QVERIFY(it1 != hash.end()); |
|
1499 |
|
1500 Hash::iterator it2 = hash.find(0); |
|
1501 QVERIFY(it2 != hash.begin()); |
|
1502 QVERIFY(it2 == hash.end()); |
|
1503 |
|
1504 *it1 = monde; |
|
1505 QVERIFY(*it1 == monde); |
|
1506 QVERIFY(hash[1] == monde); |
|
1507 |
|
1508 *it1 = hello; |
|
1509 QVERIFY(*it1 == hello); |
|
1510 QVERIFY(hash[1] == hello); |
|
1511 |
|
1512 hash[1] = monde; |
|
1513 QVERIFY(it1.key() == 1); |
|
1514 QVERIFY(it1.value() == monde); |
|
1515 QVERIFY(*it1 == monde); |
|
1516 QVERIFY(hash[1] == monde); |
|
1517 |
|
1518 hash[1] = hello; |
|
1519 QVERIFY(*it1 == hello); |
|
1520 QVERIFY(hash[1] == hello); |
|
1521 } |
|
1522 |
|
1523 { |
|
1524 const Hash hash2 = hash; |
|
1525 |
|
1526 Hash::const_iterator it1 = hash2.find(1); |
|
1527 QVERIFY(it1 != hash2.end()); |
|
1528 QVERIFY(it1.key() == 1); |
|
1529 QVERIFY(it1.value() == hello); |
|
1530 QVERIFY(*it1 == hello); |
|
1531 |
|
1532 Hash::const_iterator it2 = hash2.find(2); |
|
1533 QVERIFY(it1 != it2); |
|
1534 QVERIFY(it1 != hash2.end()); |
|
1535 QVERIFY(it2 != hash2.end()); |
|
1536 |
|
1537 int count = 0; |
|
1538 it1 = hash2.begin(); |
|
1539 while (it1 != hash2.end()) { |
|
1540 count++; |
|
1541 ++it1; |
|
1542 } |
|
1543 QVERIFY(count == 2); |
|
1544 |
|
1545 count = 0; |
|
1546 it1 = hash.begin(); |
|
1547 while (it1 != hash.end()) { |
|
1548 count++; |
|
1549 ++it1; |
|
1550 } |
|
1551 QVERIFY(count == 2); |
|
1552 } |
|
1553 |
|
1554 { |
|
1555 QVERIFY(hash.contains(1)); |
|
1556 QVERIFY(hash.contains(2)); |
|
1557 QVERIFY(!hash.contains(0)); |
|
1558 QVERIFY(!hash.contains(3)); |
|
1559 } |
|
1560 |
|
1561 { |
|
1562 QVERIFY(hash.value(1) == hello); |
|
1563 QVERIFY(hash.value(2) == world); |
|
1564 QVERIFY(hash.value(3) == 0); |
|
1565 QVERIFY(hash.value(1, allo) == hello); |
|
1566 QVERIFY(hash.value(2, allo) == world); |
|
1567 QVERIFY(hash.value(3, allo) == allo); |
|
1568 QVERIFY(hash.value(0, monde) == monde); |
|
1569 } |
|
1570 |
|
1571 { |
|
1572 QHash<int,LargeStatic> hash; |
|
1573 for (int i = 0; i < 10; i++) |
|
1574 hash.insert(i, LargeStatic()); |
|
1575 QVERIFY(LargeStatic::count == 10); |
|
1576 hash.remove(7); |
|
1577 QVERIFY(LargeStatic::count == 9); |
|
1578 |
|
1579 } |
|
1580 QVERIFY(LargeStatic::count == 0); |
|
1581 { |
|
1582 QHash<int, int*> hash; |
|
1583 QVERIFY(((const QHash<int,int*>*) &hash)->operator[](7) == 0); |
|
1584 } |
|
1585 |
|
1586 { |
|
1587 /* |
|
1588 This test relies on a certain implementation of |
|
1589 QHash. If you change the way QHash works internally, |
|
1590 change this test as well. |
|
1591 */ |
|
1592 QHash<int, int> hash; |
|
1593 for (int i = 0; i < 1000; ++i) |
|
1594 hash.insert(i, i); |
|
1595 QVERIFY(hash.capacity() == 1031); |
|
1596 hash.squeeze(); |
|
1597 QVERIFY(hash.capacity() == 521); |
|
1598 |
|
1599 hash.insert(12345, 12345); |
|
1600 QVERIFY(hash.capacity() == 1031); |
|
1601 |
|
1602 for (int j = 0; j < 900; ++j) |
|
1603 hash.remove(j); |
|
1604 QVERIFY(hash.capacity() == 257); |
|
1605 hash.squeeze(); |
|
1606 QVERIFY(hash.capacity() == 67); |
|
1607 hash.reserve(0); |
|
1608 } |
|
1609 } |
|
1610 |
|
1611 { |
|
1612 QHash<int, QString> hash; |
|
1613 hash.insert(0, "Hello"); |
|
1614 |
|
1615 QHash<int, QString>::iterator it = hash.begin(); |
|
1616 QVERIFY((*it)[0] == QChar('H')); |
|
1617 QVERIFY(it->constData()[0] == QChar('H')); |
|
1618 it->replace(QChar('H'), QChar('X')); |
|
1619 QVERIFY(*hash.begin() == "Xello"); |
|
1620 |
|
1621 QHash<int, QString>::const_iterator cit = hash.constBegin(); |
|
1622 QVERIFY((*cit).toLower() == "xello"); |
|
1623 QVERIFY(cit->toUpper() == "XELLO"); |
|
1624 } |
|
1625 |
|
1626 { |
|
1627 QHash<int, QString> hash1, hash2; |
|
1628 hash1.insertMulti(1, "Alpha"); |
|
1629 hash1.insertMulti(1, "Gamma"); |
|
1630 hash2.insertMulti(1, "Beta"); |
|
1631 hash2.insertMulti(1, "Gamma"); |
|
1632 hash2.insertMulti(1, "Gamma"); |
|
1633 |
|
1634 hash1.unite(hash2); |
|
1635 QCOMPARE(hash1.size(), 5); |
|
1636 QCOMPARE(hash1.values(), |
|
1637 (QList<QString>() << "Gamma" << "Gamma" << "Beta" << "Gamma" << "Alpha")); |
|
1638 |
|
1639 hash2 = hash1; |
|
1640 hash2.unite(hash2); |
|
1641 QCOMPARE(hash2.size(), 10); |
|
1642 QCOMPARE(hash2.values(), hash1.values() + hash1.values()); |
|
1643 } |
|
1644 |
|
1645 #if 0 |
|
1646 { |
|
1647 QMultiHash<QString, int> hash1; |
|
1648 hash1.insert("1", 2); |
|
1649 hash1.insert("1", 1); |
|
1650 hash1.insert("a", 3); |
|
1651 hash1.insert("a", 2); |
|
1652 hash1.insert("a", 1); |
|
1653 hash1.insert("b", 2); |
|
1654 hash1.insert("b", 1); |
|
1655 |
|
1656 // hash1: [ "1" -> 1, "1" -> 2, "a" -> 1, "a" -> 2, "a" -> 3, "b" -> 1, "b" -> 2 ] |
|
1657 |
|
1658 QHashIterator<QString, int> i1(hash1); |
|
1659 i1.toBack(); |
|
1660 |
|
1661 bool found; |
|
1662 |
|
1663 found = i1.findPreviousKey("1"); |
|
1664 QVERIFY(found && i1.value() == 2); |
|
1665 |
|
1666 found = i1.findPreviousKey("1"); |
|
1667 QVERIFY(found && i1.value() == 1); |
|
1668 |
|
1669 found = i1.findPreviousKey("1"); |
|
1670 QVERIFY(!found); |
|
1671 |
|
1672 i1.toFront(); |
|
1673 found = i1.findNextKey("1"); |
|
1674 QVERIFY(found && i1.value() == 1); |
|
1675 |
|
1676 found = i1.findNextKey("1"); |
|
1677 QVERIFY(found && i1.value() == 2); |
|
1678 |
|
1679 found = i1.findNextKey("1"); |
|
1680 QVERIFY(!found); |
|
1681 |
|
1682 i1.toBack(); |
|
1683 found = i1.findPreviousKey("b"); |
|
1684 QVERIFY(found && i1.value() == 2); |
|
1685 |
|
1686 found = i1.findPreviousKey("b"); |
|
1687 QVERIFY(found && i1.value() == 1); |
|
1688 |
|
1689 found = i1.findPreviousKey("b"); |
|
1690 QVERIFY(!found); |
|
1691 |
|
1692 i1.toFront(); |
|
1693 found = i1.findNextKey("b"); |
|
1694 QVERIFY(found && i1.value() == 1); |
|
1695 |
|
1696 found = i1.findNextKey("b"); |
|
1697 QVERIFY(found && i1.value() == 2); |
|
1698 |
|
1699 found = i1.findNextKey("b"); |
|
1700 QVERIFY(!found); |
|
1701 } |
|
1702 #endif |
|
1703 } |
|
1704 |
|
1705 void tst_Collections::map() |
|
1706 { |
|
1707 const char *hello = "hello"; |
|
1708 const char *world = "world"; |
|
1709 const char *allo = "allo"; |
|
1710 const char *monde = "monde"; |
|
1711 |
|
1712 { |
|
1713 typedef QMap<int, const char *> Map; |
|
1714 Map map; |
|
1715 map.insert(1, hello); |
|
1716 map.insert(2, world); |
|
1717 |
|
1718 QVERIFY(*map.begin() == hello); |
|
1719 |
|
1720 QVERIFY(map.size() == 2); |
|
1721 QVERIFY(!map.isEmpty()); |
|
1722 |
|
1723 { |
|
1724 Map map2 = map; |
|
1725 map2 = map; |
|
1726 map = map2; |
|
1727 map2 = map2; |
|
1728 map = map; |
|
1729 map2.clear(); |
|
1730 map2 = map2; |
|
1731 QVERIFY(map2.size() == 0); |
|
1732 QVERIFY(map2.isEmpty()); |
|
1733 } |
|
1734 QVERIFY(map.size() == 2); |
|
1735 |
|
1736 { |
|
1737 Map map2 = map; |
|
1738 map2[1] = allo; |
|
1739 map2[2] = monde; |
|
1740 |
|
1741 QVERIFY(map2[1] == allo); |
|
1742 QVERIFY(map2[2] == monde); |
|
1743 QVERIFY(map[1] == hello); |
|
1744 QVERIFY(map[2] == world); |
|
1745 |
|
1746 map2[1] = map[1]; |
|
1747 map2[2] = map[2]; |
|
1748 |
|
1749 QVERIFY(map2[1] == hello); |
|
1750 QVERIFY(map2[2] == world); |
|
1751 |
|
1752 map[1] = map[1]; |
|
1753 QVERIFY(map[1] == hello); |
|
1754 } |
|
1755 |
|
1756 { |
|
1757 Map map2 = map; |
|
1758 map2.detach(); |
|
1759 map2.remove(1); |
|
1760 QVERIFY(map2.size() == 1); |
|
1761 map2.remove(1); |
|
1762 QVERIFY(map2.size() == 1); |
|
1763 map2.remove(0); |
|
1764 QVERIFY(map2.size() == 1); |
|
1765 map2.remove(2); |
|
1766 QVERIFY(map2.size() == 0); |
|
1767 QVERIFY(map.size() == 2); |
|
1768 } |
|
1769 |
|
1770 map.detach(); |
|
1771 |
|
1772 { |
|
1773 Map::iterator it1 = map.find(1); |
|
1774 QVERIFY(it1 == map.begin()); |
|
1775 QVERIFY(it1 != map.end()); |
|
1776 |
|
1777 Map::iterator it2 = map.find(0); |
|
1778 QVERIFY(it2 != map.begin()); |
|
1779 QVERIFY(it2 == map.end()); |
|
1780 |
|
1781 *it1 = monde; |
|
1782 QVERIFY(*it1 == monde); |
|
1783 QVERIFY(map[1] == monde); |
|
1784 |
|
1785 *it1 = hello; |
|
1786 QVERIFY(*it1 == hello); |
|
1787 QVERIFY(map[1] == hello); |
|
1788 |
|
1789 map[1] = monde; |
|
1790 QVERIFY(it1.key() == 1); |
|
1791 QVERIFY(it1.value() == monde); |
|
1792 QVERIFY(*it1 == monde); |
|
1793 QVERIFY(map[1] == monde); |
|
1794 |
|
1795 map[1] = hello; |
|
1796 QVERIFY(*it1 == hello); |
|
1797 QVERIFY(map[1] == hello); |
|
1798 |
|
1799 *++it1 = allo; |
|
1800 QVERIFY(*it1 == allo); |
|
1801 QVERIFY(map[2] == allo); |
|
1802 *it1 = world; |
|
1803 |
|
1804 ++it1; |
|
1805 QVERIFY(it1 == map.end()); |
|
1806 |
|
1807 int count = 0; |
|
1808 it1 = map.begin(); |
|
1809 while (it1 != map.end()) { |
|
1810 count++; |
|
1811 ++it1; |
|
1812 } |
|
1813 QVERIFY(count == 2); |
|
1814 } |
|
1815 |
|
1816 { |
|
1817 const Map map2 = map; |
|
1818 |
|
1819 Map::const_iterator it1 = map2.find(1); |
|
1820 QVERIFY(it1 != map2.end()); |
|
1821 QVERIFY(it1.key() == 1); |
|
1822 QVERIFY(it1.value() == hello); |
|
1823 QVERIFY(*it1 == hello); |
|
1824 ++it1; |
|
1825 |
|
1826 Map::const_iterator it2 = map2.find(2); |
|
1827 QVERIFY(it1 == it2); |
|
1828 ++it1; |
|
1829 QVERIFY(it1 == map2.end()); |
|
1830 QVERIFY(it2 != map2.end()); |
|
1831 QVERIFY(it1 != it2); |
|
1832 |
|
1833 int count = 0; |
|
1834 it1 = map2.begin(); |
|
1835 while (it1 != map2.end()) { |
|
1836 count++; |
|
1837 ++it1; |
|
1838 } |
|
1839 QVERIFY(count == 2); |
|
1840 |
|
1841 count = 0; |
|
1842 it1 = map.begin(); |
|
1843 while (it1 != map.end()) { |
|
1844 count++; |
|
1845 ++it1; |
|
1846 } |
|
1847 QVERIFY(count == 2); |
|
1848 } |
|
1849 |
|
1850 { |
|
1851 QVERIFY(map.contains(1)); |
|
1852 QVERIFY(map.contains(2)); |
|
1853 QVERIFY(!map.contains(0)); |
|
1854 QVERIFY(!map.contains(3)); |
|
1855 } |
|
1856 |
|
1857 { |
|
1858 QVERIFY(map.value(1) == hello); |
|
1859 QVERIFY(map.value(2) == world); |
|
1860 QVERIFY(map.value(3) == 0); |
|
1861 QVERIFY(map.value(1, allo) == hello); |
|
1862 QVERIFY(map.value(2, allo) == world); |
|
1863 QVERIFY(map.value(3, allo) == allo); |
|
1864 QVERIFY(map.value(0, monde) == monde); |
|
1865 } |
|
1866 int originalLargeStaticCount = LargeStatic::count; |
|
1867 { |
|
1868 QMap<int,LargeStatic> map; |
|
1869 for (int i = 0; i < 10; i++) |
|
1870 map.insert(i, LargeStatic()); |
|
1871 QVERIFY(LargeStatic::count == (originalLargeStaticCount + 10)); |
|
1872 map.remove(7); |
|
1873 QVERIFY(LargeStatic::count == (originalLargeStaticCount + 9)); |
|
1874 |
|
1875 } |
|
1876 QVERIFY(LargeStatic::count == originalLargeStaticCount); |
|
1877 { |
|
1878 QMap<int, int*> map; |
|
1879 QVERIFY(((const QMap<int,int*>*) &map)->operator[](7) == 0); |
|
1880 } |
|
1881 |
|
1882 { |
|
1883 QMap<int, int> map; |
|
1884 map[0] = 1; |
|
1885 map[1] = 2; |
|
1886 map[2] = 4; |
|
1887 map[3] = 8; |
|
1888 int sum = 0; |
|
1889 int sumkey = 0; |
|
1890 QMapIterator<int,int> i = map; |
|
1891 while (i.hasNext()) { |
|
1892 sum += i.next().value(); |
|
1893 sumkey += i.key(); |
|
1894 } |
|
1895 QVERIFY(sum == 15); |
|
1896 QVERIFY(sumkey == 6); |
|
1897 } |
|
1898 { |
|
1899 QMap<int, int> map; |
|
1900 map[0] = 1; |
|
1901 map[1] = 2; |
|
1902 map[2] = 4; |
|
1903 map[3] = 8; |
|
1904 int sum = 0; |
|
1905 QMutableMapIterator<int,int> i = map; |
|
1906 while(i.hasNext()) |
|
1907 if (i.next().key() == 2) |
|
1908 i.remove(); |
|
1909 i.toFront(); |
|
1910 while(i.hasNext()) { |
|
1911 sum += i.next().value(); |
|
1912 i.setValue(10); |
|
1913 i.value() += 22; |
|
1914 QVERIFY(i.value() == 32); |
|
1915 } |
|
1916 QVERIFY(sum == 11); |
|
1917 } |
|
1918 { |
|
1919 QMap<int, int> map; |
|
1920 map[0] = 1; |
|
1921 QMutableMapIterator<int,int> i(map); |
|
1922 i.toBack(); |
|
1923 while (i.hasPrevious()) { |
|
1924 i.previous(); |
|
1925 QCOMPARE(i.key(), 0); |
|
1926 QCOMPARE(i.value(), 1); |
|
1927 } |
|
1928 } |
|
1929 } |
|
1930 |
|
1931 { |
|
1932 QMultiMap<QString, int> map1; |
|
1933 map1.insert("1", 2); |
|
1934 map1.insert("1", 1); |
|
1935 map1.insert("a", 3); |
|
1936 map1.insert("a", 2); |
|
1937 map1.insert("a", 1); |
|
1938 map1.insert("b", 2); |
|
1939 map1.insert("b", 1); |
|
1940 |
|
1941 // map1: [ "1" -> 1, "1" -> 2, "a" -> 1, "a" -> 2, "a" -> 3, "b" -> 1, "b" -> 2 ] |
|
1942 |
|
1943 #if 0 |
|
1944 QMapIterator<QString, int> i1(map1); |
|
1945 i1.toBack(); |
|
1946 |
|
1947 bool found; |
|
1948 found = i1.findPreviousKey("1"); |
|
1949 QVERIFY(found && i1.value() == 2); |
|
1950 |
|
1951 found = i1.findPreviousKey("1"); |
|
1952 QVERIFY(found && i1.value() == 1); |
|
1953 |
|
1954 found = i1.findPreviousKey("1"); |
|
1955 QVERIFY(!found); |
|
1956 |
|
1957 i1.toFront(); |
|
1958 found = i1.findNextKey("1"); |
|
1959 QVERIFY(found && i1.value() == 1); |
|
1960 |
|
1961 found = i1.findNextKey("1"); |
|
1962 QVERIFY(found && i1.value() == 2); |
|
1963 |
|
1964 found = i1.findNextKey("1"); |
|
1965 QVERIFY(!found); |
|
1966 |
|
1967 i1.toBack(); |
|
1968 found = i1.findPreviousKey("b"); |
|
1969 QVERIFY(found && i1.value() == 2); |
|
1970 |
|
1971 found = i1.findPreviousKey("b"); |
|
1972 QVERIFY(found && i1.value() == 1); |
|
1973 |
|
1974 found = i1.findPreviousKey("b"); |
|
1975 QVERIFY(!found); |
|
1976 |
|
1977 i1.toFront(); |
|
1978 found = i1.findNextKey("b"); |
|
1979 QVERIFY(found && i1.value() == 1); |
|
1980 |
|
1981 found = i1.findNextKey("b"); |
|
1982 QVERIFY(found && i1.value() == 2); |
|
1983 |
|
1984 found = i1.findNextKey("b"); |
|
1985 QVERIFY(!found); |
|
1986 #endif |
|
1987 |
|
1988 QMultiMap<QString, int>::iterator j1, k1; |
|
1989 |
|
1990 j1 = map1.lowerBound("0"); k1 = map1.upperBound("0"); |
|
1991 QVERIFY(j1 == map1.begin() && k1 == j1); |
|
1992 j1 = map1.lowerBound("00"); k1 = map1.upperBound("00"); |
|
1993 QVERIFY(j1 == map1.find("1") && k1 == j1); |
|
1994 j1 = map1.lowerBound("1"); k1 = map1.upperBound("1"); |
|
1995 QVERIFY(j1 == map1.find("1") && --(--k1) == j1); |
|
1996 j1 = map1.lowerBound("11"); k1 = map1.upperBound("11"); |
|
1997 QVERIFY(j1 == map1.find("a") && k1 == j1); |
|
1998 j1 = map1.lowerBound("a"); k1 = map1.upperBound("a"); |
|
1999 QVERIFY(j1 == map1.find("a") && k1 == map1.find("b")); |
|
2000 QVERIFY(j1.value() == 1 && j1.value() == 1); |
|
2001 j1 = map1.lowerBound("aa"); k1 = map1.upperBound("aa"); |
|
2002 QVERIFY(j1 == map1.find("b") && k1 == j1); |
|
2003 QVERIFY(j1.value() == 1); |
|
2004 j1 = map1.lowerBound("b"); k1 = map1.upperBound("b"); |
|
2005 QVERIFY(j1 == map1.find("b") && k1 == map1.end()); |
|
2006 QVERIFY(j1.value() == 1); |
|
2007 j1 = map1.lowerBound("bb"); k1 = map1.upperBound("bb"); |
|
2008 QVERIFY(j1 == map1.end() && k1 == j1); |
|
2009 |
|
2010 const QMultiMap<QString, int> map2 = map1; |
|
2011 QMultiMap<QString, int>::const_iterator j2, k2; |
|
2012 |
|
2013 j2 = map2.lowerBound("0"); k2 = map2.upperBound("0"); |
|
2014 QVERIFY(j2 == map2.begin() && k2 == j2); |
|
2015 j2 = map2.lowerBound("00"); k2 = map2.upperBound("00"); |
|
2016 QVERIFY(j2 == map2.find("1") && k2 == j2); |
|
2017 j2 = map2.lowerBound("1"); k2 = map2.upperBound("1"); |
|
2018 QVERIFY(j2 == map2.find("1") && --(--k2) == j2); |
|
2019 j2 = map2.lowerBound("11"); k2 = map2.upperBound("11"); |
|
2020 QVERIFY(j2 == map2.find("a") && k2 == j2); |
|
2021 j2 = map2.lowerBound("a"); k2 = map2.upperBound("a"); |
|
2022 QVERIFY(j2 == map2.find("a") && k2 == map2.find("b")); |
|
2023 QVERIFY(j2.value() == 1 && j2.value() == 1); |
|
2024 j2 = map2.lowerBound("aa"); k2 = map2.upperBound("aa"); |
|
2025 QVERIFY(j2 == map2.find("b") && k2 == j2); |
|
2026 QVERIFY(j2.value() == 1); |
|
2027 j2 = map2.lowerBound("b"); k2 = map2.upperBound("b"); |
|
2028 QVERIFY(j2 == map2.find("b") && k2 == map2.end()); |
|
2029 QVERIFY(j2.value() == 1); |
|
2030 j2 = map2.lowerBound("bb"); k2 = map2.upperBound("bb"); |
|
2031 QVERIFY(j2 == map2.end() && k2 == j2); |
|
2032 } |
|
2033 |
|
2034 { |
|
2035 QMap<int, QString> map; |
|
2036 map.insert(0, "Hello"); |
|
2037 |
|
2038 QMap<int, QString>::iterator it = map.begin(); |
|
2039 QVERIFY((*it)[0] == QChar('H')); |
|
2040 QVERIFY(it->constData()[0] == QChar('H')); |
|
2041 it->replace(QChar('H'), QChar('X')); |
|
2042 QVERIFY(*map.begin() == "Xello"); |
|
2043 |
|
2044 QMap<int, QString>::const_iterator cit = map.constBegin(); |
|
2045 QVERIFY((*cit).toLower() == "xello"); |
|
2046 QVERIFY(cit->toUpper() == "XELLO"); |
|
2047 } |
|
2048 |
|
2049 { |
|
2050 QMap<int, QString> map1, map2; |
|
2051 map1.insertMulti(1, "Alpha"); |
|
2052 map1.insertMulti(1, "Gamma"); |
|
2053 map2.insertMulti(1, "Beta"); |
|
2054 map2.insertMulti(1, "Gamma"); |
|
2055 map2.insertMulti(1, "Gamma"); |
|
2056 |
|
2057 map1.unite(map2); |
|
2058 QCOMPARE(map1.size(), 5); |
|
2059 QCOMPARE(static_cast<QStringList>(map1.values()), |
|
2060 (QStringList() << "Gamma" << "Gamma" << "Beta" << "Gamma" << "Alpha")); |
|
2061 |
|
2062 map2 = map1; |
|
2063 map2.unite(map2); |
|
2064 QCOMPARE(map2.size(), 10); |
|
2065 QCOMPARE(map2.values(), map1.values() + map1.values()); |
|
2066 } |
|
2067 } |
|
2068 |
|
2069 void tst_Collections::qstring() |
|
2070 { |
|
2071 QString hello = "hello"; |
|
2072 QString ello = "ello"; |
|
2073 QString World = "World"; |
|
2074 QString Wor = "Wor"; |
|
2075 QString helloWorld = "helloWorld"; |
|
2076 |
|
2077 QString s = hello + "World"; |
|
2078 QVERIFY(hello + World == helloWorld); |
|
2079 QVERIFY(hello + "World" == helloWorld); |
|
2080 QVERIFY("hello" + World == helloWorld); |
|
2081 |
|
2082 |
|
2083 QString l; |
|
2084 QVERIFY('h' + ello == hello); |
|
2085 QVERIFY(Wor + 'l' + 'd' == "World"); |
|
2086 QVERIFY(hello + World == "helloWorld"); |
|
2087 QVERIFY(hello + "World" == "helloWorld"); |
|
2088 QVERIFY("hello" + World == "helloWorld"); |
|
2089 QVERIFY('h' + ello == "hello"); |
|
2090 QVERIFY(Wor + 'l' + 'd' == "World"); |
|
2091 QVERIFY("helloWorld" == hello + World); |
|
2092 QVERIFY("helloWorld" == hello + "World"); |
|
2093 QVERIFY("helloWorld" == "hello" + World); |
|
2094 QVERIFY("hello" == 'h' + ello); |
|
2095 QVERIFY("World" == Wor + 'l' + 'd'); |
|
2096 |
|
2097 QVERIFY(hello.contains('e')); |
|
2098 QVERIFY (true == hello.contains('e')); |
|
2099 QVERIFY (hello.contains('e') != false); |
|
2100 |
|
2101 QVERIFY(hello.indexOf('e') == 1); |
|
2102 QVERIFY(hello.indexOf('e', -10) == 1); |
|
2103 QVERIFY(hello.indexOf('l') == 2); |
|
2104 QVERIFY(hello.indexOf('l',2) == 2); |
|
2105 QVERIFY(hello.indexOf('l',3) == 3); |
|
2106 |
|
2107 QString large = "000 100 200 300 400 500 600 700 800 900"; |
|
2108 |
|
2109 QVERIFY(large.indexOf("700") == 28); |
|
2110 QVERIFY(large.indexOf("700", 28) == 28); |
|
2111 QVERIFY(large.indexOf("700", 29) == -1); |
|
2112 QVERIFY(large.lastIndexOf("700") == 28); |
|
2113 QVERIFY(large.lastIndexOf("700", 28) == 28); |
|
2114 QVERIFY(large.lastIndexOf("700", 27) == -1); |
|
2115 |
|
2116 QVERIFY(large.contains("200")); |
|
2117 QVERIFY(!large.contains("201")); |
|
2118 QVERIFY(large.contains('3')); |
|
2119 QVERIFY(!large.contains('a')); |
|
2120 |
|
2121 QVERIFY(large.count("00") == 11); |
|
2122 QVERIFY(large.count('3') == 1); |
|
2123 QVERIFY(large.count('0') == 21); |
|
2124 QVERIFY(large.count("0") == 21); |
|
2125 QVERIFY(large.count("200") == 1); |
|
2126 QVERIFY(large.count("201") == 0); |
|
2127 |
|
2128 QVERIFY(hello.left(0) == ""); |
|
2129 QVERIFY(!hello.left(0).isNull()); |
|
2130 QVERIFY(hello.left(1) == "h"); |
|
2131 QVERIFY(hello.left(2) == "he"); |
|
2132 QVERIFY(hello.left(200) == "hello"); |
|
2133 QVERIFY(hello.left(hello.size()) == hello); |
|
2134 QVERIFY(hello.left(hello.size()+1) == hello); |
|
2135 |
|
2136 QVERIFY(hello.right(0) == ""); |
|
2137 QVERIFY(!hello.right(0).isNull()); |
|
2138 QVERIFY(hello.right(1) == "o"); |
|
2139 QVERIFY(hello.right(2) == "lo"); |
|
2140 QVERIFY(hello.right(200) == "hello"); |
|
2141 QVERIFY(hello.right(hello.size()) == hello); |
|
2142 QVERIFY(hello.right(hello.size()+1) == hello); |
|
2143 |
|
2144 QVERIFY(!hello.mid(0, 0).isNull()); |
|
2145 QVERIFY(hello.mid(0, 1) == "h"); |
|
2146 QVERIFY(hello.mid(0, 2) == "he"); |
|
2147 QVERIFY(hello.mid(0, 200) == "hello"); |
|
2148 QVERIFY(hello.mid(0) == "hello"); |
|
2149 QVERIFY(hello.mid(0, hello.size()) == hello); |
|
2150 QVERIFY(hello.mid(0, hello.size()+1) == hello); |
|
2151 |
|
2152 QVERIFY(hello.mid(hello.size()-0) == ""); |
|
2153 |
|
2154 QVERIFY(hello.mid(hello.size()-0).isNull()); |
|
2155 QVERIFY(hello.mid(hello.size()-1) == "o"); |
|
2156 QVERIFY(hello.mid(hello.size()-2) == "lo"); |
|
2157 QVERIFY(hello.mid(hello.size()-200) == "hello"); |
|
2158 |
|
2159 QString null; |
|
2160 QString nonNull = ""; |
|
2161 QVERIFY(null.left(10).isNull()); |
|
2162 QVERIFY(null.mid(0).isNull()); |
|
2163 |
|
2164 QVERIFY(null == QString::null); |
|
2165 QVERIFY(QString::null == null); |
|
2166 QVERIFY(nonNull != QString::null); |
|
2167 QVERIFY(QString::null != nonNull); |
|
2168 QVERIFY(null == nonNull); |
|
2169 QVERIFY(QString::null == QString::null); |
|
2170 |
|
2171 QString fill = "123"; |
|
2172 fill.fill('a'); |
|
2173 QVERIFY(fill == "aaa"); |
|
2174 |
|
2175 s.clear(); |
|
2176 s = hello; |
|
2177 s.append(World); |
|
2178 QVERIFY(s == helloWorld); |
|
2179 s.clear(); |
|
2180 s = World; |
|
2181 s.insert(0,hello); |
|
2182 QVERIFY(s == helloWorld); |
|
2183 s = "012345"; |
|
2184 s.insert(3, 'E'); |
|
2185 QVERIFY(s == "012E345"); |
|
2186 s.insert(3, "INSID"); |
|
2187 QVERIFY(s == "012INSIDE345"); |
|
2188 s = "short"; |
|
2189 s.insert(7, 'E'); |
|
2190 QVERIFY(s == "short E"); |
|
2191 s = "short"; |
|
2192 s.insert(7, "END"); |
|
2193 QVERIFY(s == "short END"); |
|
2194 |
|
2195 QVERIFY(QString::fromLatin1("hello") == "hello"); |
|
2196 |
|
2197 s = "first"; |
|
2198 QVERIFY(s.toLatin1() == "first"); |
|
2199 s = "second"; |
|
2200 QVERIFY(s.toLatin1() == "second"); |
|
2201 #ifdef QT3_SUPPORT |
|
2202 const char* cache = s.latin1(); |
|
2203 QVERIFY(cache == s.latin1()); |
|
2204 #endif |
|
2205 s.clear(); |
|
2206 #ifdef QT3_SUPPORT |
|
2207 QVERIFY(*s.latin1() == '\0'); |
|
2208 #endif |
|
2209 QVERIFY(s.isNull()); |
|
2210 QVERIFY(s.toLatin1().size() == 0); |
|
2211 QVERIFY(s.toLatin1().isEmpty()); |
|
2212 QVERIFY(s.toLatin1().isNull()); |
|
2213 |
|
2214 s = "first-utf8"; |
|
2215 QVERIFY(s.toUtf8() == "first-utf8"); |
|
2216 s = "second-utf8"; |
|
2217 QVERIFY(s.toUtf8() == "second-utf8"); |
|
2218 #ifdef QT3_SUPPORT |
|
2219 cache = s.utf8(); |
|
2220 QVERIFY(cache == s.utf8()); |
|
2221 #endif |
|
2222 s.clear(); |
|
2223 #ifdef QT3_SUPPORT |
|
2224 QVERIFY(*s.utf8() == '\0'); |
|
2225 #endif |
|
2226 QVERIFY(s.isNull()); |
|
2227 QVERIFY(s.toUtf8().size() == 0); |
|
2228 QVERIFY(s.toUtf8().isEmpty()); |
|
2229 QVERIFY(s.toUtf8().isNull()); |
|
2230 |
|
2231 s = "first-utf8"; |
|
2232 QVERIFY(s.toUtf8() == "first-utf8"); |
|
2233 s = "second-utf8"; |
|
2234 QVERIFY(s.toUtf8() == "second-utf8"); |
|
2235 #ifdef QT3_SUPPORT |
|
2236 cache = s.utf8(); |
|
2237 QVERIFY(cache == s.utf8()); |
|
2238 #endif |
|
2239 s.clear(); |
|
2240 #ifdef QT3_SUPPORT |
|
2241 QVERIFY(*s.utf8() == '\0'); |
|
2242 #endif |
|
2243 QVERIFY(s.isNull()); |
|
2244 QVERIFY(s.toUtf8().size() == 0); |
|
2245 QVERIFY(s.toUtf8().isEmpty()); |
|
2246 QVERIFY(s.toUtf8().isNull()); |
|
2247 |
|
2248 s = "first-local8Bit"; |
|
2249 QVERIFY(s.toLocal8Bit() == "first-local8Bit"); |
|
2250 s = "second-local8Bit"; |
|
2251 QVERIFY(s.toLocal8Bit() == "second-local8Bit"); |
|
2252 #ifdef QT3_SUPPORT |
|
2253 cache = s.local8Bit(); |
|
2254 QVERIFY(cache == s.local8Bit()); |
|
2255 #endif |
|
2256 s.clear(); |
|
2257 #ifdef QT3_SUPPORT |
|
2258 QVERIFY(*s.local8Bit() == '\0'); |
|
2259 #endif |
|
2260 QVERIFY(s.isNull()); |
|
2261 QVERIFY(s.toLocal8Bit().size() == 0); |
|
2262 QVERIFY(s.toLocal8Bit().isEmpty()); |
|
2263 QVERIFY(!s.toLocal8Bit().isNull()); |
|
2264 |
|
2265 s = "first-ascii"; |
|
2266 QVERIFY(s.toAscii() == "first-ascii"); |
|
2267 s = "second-ascii"; |
|
2268 QVERIFY(s.toAscii() == "second-ascii"); |
|
2269 #ifdef QT3_SUPPORT |
|
2270 cache = s.ascii(); |
|
2271 QVERIFY(cache == s.ascii()); |
|
2272 #endif |
|
2273 s.clear(); |
|
2274 #ifdef QT3_SUPPORT |
|
2275 QVERIFY(*s.ascii() == '\0'); |
|
2276 #endif |
|
2277 QVERIFY(s.isNull()); |
|
2278 QVERIFY(s.toAscii().size() == 0); |
|
2279 QVERIFY(s.toAscii().isEmpty()); |
|
2280 QVERIFY(s.toAscii().isNull()); |
|
2281 |
|
2282 s = "ascii"; |
|
2283 s += (uchar) 0xb0; |
|
2284 QVERIFY(s.toUtf8() != s.toLatin1()); |
|
2285 QString sa = s.toLatin1().constData(); |
|
2286 QVERIFY(sa[sa.length()-1] == (ushort) 0xb0); |
|
2287 QVERIFY(sa.left(sa.length()-1) == "ascii"); |
|
2288 |
|
2289 QVERIFY(s == QString::fromUtf8(s.toUtf8().constData())); |
|
2290 |
|
2291 s = "12"; |
|
2292 s.append('3'); |
|
2293 s += '4'; |
|
2294 QVERIFY(s == "1234"); |
|
2295 |
|
2296 s = "repend"; |
|
2297 s.prepend('p'); |
|
2298 QVERIFY(s == "prepend"); |
|
2299 s.prepend("abc "); |
|
2300 QVERIFY(s == "abc prepend"); |
|
2301 |
|
2302 s = " whitespace "; |
|
2303 QVERIFY(s.trimmed() == "whitespace"); |
|
2304 s = " lots of stuff "; |
|
2305 QVERIFY(s.simplified() == "lots of stuff"); |
|
2306 |
|
2307 s = "a hat, a stick, a ski"; |
|
2308 QVERIFY(s[2] == 'h'); |
|
2309 QVERIFY(s[1] < 'b'); |
|
2310 |
|
2311 |
|
2312 s = "12223"; |
|
2313 s.remove(1, 2); |
|
2314 QVERIFY(s == "123"); |
|
2315 |
|
2316 s = "(%1)(%2)"; |
|
2317 s = s.arg("foo").arg(7); |
|
2318 QVERIFY(s == "(foo)(7)"); |
|
2319 |
|
2320 |
|
2321 #if 0 |
|
2322 s = "stl rocks"; |
|
2323 std::string stl_string = s; |
|
2324 s = stl_string; |
|
2325 QVERIFY(s == "stl rocks"); |
|
2326 #endif |
|
2327 |
|
2328 { |
|
2329 QString str("Bananas"); |
|
2330 QVERIFY(str.startsWith("Ban")); |
|
2331 QVERIFY(false == str.startsWith("Car")); |
|
2332 } |
|
2333 { |
|
2334 QString str("Bananas"); |
|
2335 QVERIFY(str.endsWith("anas")); |
|
2336 QVERIFY(false == str.endsWith("pple")); |
|
2337 } |
|
2338 |
|
2339 |
|
2340 QString str = "Hello"; |
|
2341 QString cstr = QString::fromRawData(str.unicode(), str.length()); |
|
2342 QVERIFY(str == "Hello"); |
|
2343 QVERIFY(cstr == "Hello"); |
|
2344 cstr.clear(); |
|
2345 QVERIFY(str == "Hello"); |
|
2346 QVERIFY(cstr.isEmpty()); |
|
2347 |
|
2348 { |
|
2349 QString str1("Foo"); |
|
2350 str1.prepend(str1); |
|
2351 QCOMPARE(str1, QString("FooFoo")); |
|
2352 str1.append(str1); |
|
2353 QCOMPARE(str1, QString("FooFooFooFoo")); |
|
2354 str1.insert(2, str1); |
|
2355 QCOMPARE(str1, QString("FoFooFooFooFoooFooFooFoo")); |
|
2356 str1.replace(3, 3, str1); |
|
2357 QCOMPARE(str1, QString("FoFFoFooFooFooFoooFooFooFooooFooFoooFooFooFoo")); |
|
2358 str1 = "FooFoo"; |
|
2359 str1.replace(char('F'), str1); |
|
2360 QCOMPARE(str1, QString("FooFooooFooFoooo")); |
|
2361 str1 = "FooFoo"; |
|
2362 str1.replace(char('o'), str1); |
|
2363 QCOMPARE(str1, QString("FFooFooFooFooFFooFooFooFoo")); |
|
2364 |
|
2365 str1 = "Foo"; |
|
2366 str1.replace("Foo", str1); |
|
2367 QCOMPARE(str1, QString("Foo")); |
|
2368 str1.replace(str1, str1); |
|
2369 QCOMPARE(str1, QString("Foo")); |
|
2370 |
|
2371 str1 = "Foo"; |
|
2372 str1.replace("Foo", str1, Qt::CaseInsensitive); |
|
2373 QCOMPARE(str1, QString("Foo")); |
|
2374 str1.replace(str1, str1); |
|
2375 QCOMPARE(str1, QString("Foo")); |
|
2376 |
|
2377 str1 = "FooFoo"; |
|
2378 str1.reserve(100); |
|
2379 str1.replace("oo", str1); |
|
2380 QCOMPARE(str1, QString("FFooFooFFooFoo")); |
|
2381 |
|
2382 str1 = "Bar"; |
|
2383 str1.replace("FooFoo", str1); |
|
2384 QCOMPARE(str1, QString("Bar")); |
|
2385 |
|
2386 str1.replace(str1, "xxx"); |
|
2387 QCOMPARE(str1, QString("xxx")); |
|
2388 str1.replace(str1, QString("yyy")); |
|
2389 QCOMPARE(str1, QString("yyy")); |
|
2390 str1 += str1; |
|
2391 QCOMPARE(str1, QString("yyyyyy")); |
|
2392 } |
|
2393 } |
|
2394 |
|
2395 |
|
2396 void tst_Collections::bitArray() |
|
2397 { |
|
2398 QBitArray ba(20); |
|
2399 QVERIFY(ba.testBit(17) == false); |
|
2400 ba.setBit(17); |
|
2401 QVERIFY(ba.size() == 20); |
|
2402 QVERIFY(ba.testBit(17)==true); |
|
2403 QVERIFY(!ba.testBit(16)); |
|
2404 ba[4] = true; |
|
2405 QVERIFY(ba.testBit(4)); |
|
2406 QVERIFY(ba[4]); |
|
2407 int sum = 0; |
|
2408 for(int i = 0; i < 20; i++) |
|
2409 sum += ba.testBit(i) ? 1 : 0; |
|
2410 QVERIFY(sum == 2); |
|
2411 |
|
2412 ba = QBitArray(7, true); |
|
2413 QVERIFY(ba.size() == 7); |
|
2414 QVERIFY(ba[5]); |
|
2415 |
|
2416 ba = QBitArray(3); |
|
2417 ba[0] = ba[2] = true; |
|
2418 |
|
2419 QBitArray nba(3); |
|
2420 nba[1] = true; |
|
2421 |
|
2422 QVERIFY(~ba == nba); |
|
2423 |
|
2424 }; |
|
2425 |
|
2426 struct CacheFoo |
|
2427 { |
|
2428 CacheFoo(int v):val(v) { counter++; } |
|
2429 ~CacheFoo() { counter--; } |
|
2430 int val; |
|
2431 static int counter; |
|
2432 bool isDetached() const { return val != 2; } |
|
2433 }; |
|
2434 |
|
2435 int CacheFoo::counter = 0; |
|
2436 |
|
2437 void tst_Collections::cache() |
|
2438 { |
|
2439 { |
|
2440 CacheFoo* cf; |
|
2441 QCache<int, CacheFoo> cache(120); |
|
2442 int i; |
|
2443 for (i = 0; i < 30; i++) { |
|
2444 cf = cache.object(10); |
|
2445 cache.insert(i, new CacheFoo(i), i); |
|
2446 } |
|
2447 |
|
2448 QVERIFY(cache.contains(10)); |
|
2449 QVERIFY(!cache.contains(1)); |
|
2450 QVERIFY(!cache.contains(2)); |
|
2451 delete cache.take(10); |
|
2452 } |
|
2453 { |
|
2454 QCache<int, QString> cache(120); |
|
2455 int i; |
|
2456 QString two; |
|
2457 for (i = 0; i < 30; i++) { |
|
2458 QString s = QString::number(i); |
|
2459 cache.insert(i, new QString(s), i); |
|
2460 if (i == 2) |
|
2461 two = s; |
|
2462 } |
|
2463 QVERIFY(!cache.contains(3)); |
|
2464 QVERIFY(cache.contains(2)); |
|
2465 } |
|
2466 { |
|
2467 QCache<int, int> cache(100); |
|
2468 cache.insert(2, new int(2)); |
|
2469 *cache[2] = 3; |
|
2470 QVERIFY(*cache.object(2) == 3); |
|
2471 } |
|
2472 |
|
2473 QVERIFY(CacheFoo::counter == 0); |
|
2474 |
|
2475 } |
|
2476 |
|
2477 void tst_Collections::regexp() |
|
2478 { |
|
2479 QRegExp rx("^\\d\\d?$"); |
|
2480 QVERIFY(rx.indexIn("123") == -1); |
|
2481 QVERIFY(rx.indexIn("-6") == -1); |
|
2482 QVERIFY(rx.indexIn("6") == 0) ; |
|
2483 } |
|
2484 |
|
2485 void tst_Collections::pair() |
|
2486 { |
|
2487 QPair<double, int> p; |
|
2488 QVERIFY(p.first == 0.0); |
|
2489 QVERIFY(p.second == 0); |
|
2490 |
|
2491 QPair<int, QString> a(1, "Zebra"), b(2, "Ying"), c(3, "Yang"), d(3, "Ying"), e(5, "Alabama"); |
|
2492 QVERIFY(a.first == 1); |
|
2493 QVERIFY(a.second == "Zebra"); |
|
2494 QVERIFY(a == qMakePair(1, QString("Zebra"))); |
|
2495 |
|
2496 QVERIFY(a == a && b == b && c == c && d == d && e == e); |
|
2497 QVERIFY(a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e |
|
2498 && d != e); |
|
2499 |
|
2500 QVERIFY(a < b && b < c && c < d && d < e); |
|
2501 QVERIFY(a <= b && b <= c && c <= d && d <= e); |
|
2502 |
|
2503 QVERIFY(e > d && d > c && c > b && b > a); |
|
2504 QVERIFY(e >= d && d >= c && c >= b && b >= a); |
|
2505 |
|
2506 QVERIFY(!(a > b || b > c || c > d || d > e)); |
|
2507 QVERIFY(!(a >= b || b >= c || c >= d || d >= e)); |
|
2508 |
|
2509 QVERIFY(!(e < d || d < c || c < b || b < a)); |
|
2510 QVERIFY(!(e <= d || d <= c || c <= b || b <= a)); |
|
2511 |
|
2512 QVERIFY(a <= a && b <= b && c <= c && d <= d && e <= e); |
|
2513 QVERIFY(!(a < a || b < b || c < c || d < d || e < e)); |
|
2514 |
|
2515 QVERIFY(a >= a && b >= b && c >= c && d >= d && e >= e); |
|
2516 QVERIFY(!(a > a || b > b || c > c || d > d || e > e)); |
|
2517 } |
|
2518 |
|
2519 void tst_Collections::cleanupHandler() |
|
2520 { |
|
2521 #ifdef QT3_SUPPORT |
|
2522 LargeStatic * f1 = 0; |
|
2523 LargeStatic * f2 = 0; |
|
2524 { |
|
2525 Q3CleanupHandler<LargeStatic> cleanup; |
|
2526 f1 = new LargeStatic; |
|
2527 f2 = new LargeStatic; |
|
2528 cleanup.add(&f1); |
|
2529 cleanup.add(&f2); |
|
2530 } |
|
2531 QVERIFY(f1 == 0 && f2 == 0); |
|
2532 #else |
|
2533 QSKIP("No Qt3 support", SkipAll); |
|
2534 #endif |
|
2535 } |
|
2536 |
|
2537 /* |
|
2538 These test that Java-style mutable iterators don't trash shared |
|
2539 copy (the setSharable() mechanism). |
|
2540 */ |
|
2541 |
|
2542 template <class Container> |
|
2543 void populate(Container &); |
|
2544 |
|
2545 template <> |
|
2546 void populate(QList<int> &container) |
|
2547 { |
|
2548 container << 1 << 2 << 4 << 8; |
|
2549 } |
|
2550 |
|
2551 template <> |
|
2552 void populate(QLinkedList<int> &container) |
|
2553 { |
|
2554 container << 1 << 2 << 4 << 8; |
|
2555 } |
|
2556 |
|
2557 template <> |
|
2558 void populate(QVector<int> &container) |
|
2559 { |
|
2560 container << 1 << 2 << 4 << 8; |
|
2561 } |
|
2562 |
|
2563 template <> |
|
2564 void populate(QMap<int, int> &container) |
|
2565 { |
|
2566 container.insert(1, 1); |
|
2567 container.insert(2, 2); |
|
2568 container.insert(4, 4); |
|
2569 container.insert(8, 8); |
|
2570 } |
|
2571 |
|
2572 template <> |
|
2573 void populate(QHash<int, int> &container) |
|
2574 { |
|
2575 container.insert(1, 1); |
|
2576 container.insert(2, 2); |
|
2577 container.insert(4, 4); |
|
2578 container.insert(8, 8); |
|
2579 } |
|
2580 |
|
2581 template <class Container> |
|
2582 bool isSharable(const Container &container) |
|
2583 { |
|
2584 Container copy = container; |
|
2585 return !container.isDetached(); |
|
2586 } |
|
2587 |
|
2588 template <class Container, class ContainerMutableIterator> |
|
2589 void testContainer() |
|
2590 { |
|
2591 /* |
|
2592 Verify that shared_null's 'sharable' flag is set to true. |
|
2593 */ |
|
2594 { |
|
2595 Container c1; |
|
2596 QVERIFY(!c1.isDetached()); |
|
2597 |
|
2598 Container c2 = c1; |
|
2599 QVERIFY(!c1.isDetached()); |
|
2600 QVERIFY(!c2.isDetached()); |
|
2601 } |
|
2602 |
|
2603 /* |
|
2604 Verify that the 'sharable' flag is true while no mutable |
|
2605 iterator is active. |
|
2606 */ |
|
2607 { |
|
2608 Container c1; |
|
2609 populate(c1); |
|
2610 QVERIFY(c1.size() == 4); |
|
2611 QVERIFY(c1.isDetached()); |
|
2612 |
|
2613 Container c2 = c1; |
|
2614 QVERIFY(c1.size() == 4); |
|
2615 QVERIFY(c2.size() == 4); |
|
2616 QVERIFY(!c1.isDetached()); |
|
2617 QVERIFY(!c2.isDetached()); |
|
2618 } |
|
2619 |
|
2620 /* |
|
2621 Verify that the 'sharable' flag is set to false by the |
|
2622 mutable iterator. |
|
2623 */ |
|
2624 { |
|
2625 Container c1; |
|
2626 populate(c1); |
|
2627 QVERIFY(c1.size() == 4); |
|
2628 QVERIFY(c1.isDetached()); |
|
2629 |
|
2630 ContainerMutableIterator i(c1); |
|
2631 i.next(); |
|
2632 |
|
2633 Container c2 = c1; |
|
2634 QVERIFY(c1.size() == 4); |
|
2635 QVERIFY(c2.size() == 4); |
|
2636 QVERIFY(c1.isDetached()); |
|
2637 QVERIFY(c2.isDetached()); |
|
2638 |
|
2639 i.remove(); |
|
2640 QVERIFY(c1.size() == 3); |
|
2641 QVERIFY(c2.size() == 4); |
|
2642 } |
|
2643 |
|
2644 /* |
|
2645 Verify that the 'sharable' flag is reset to true by the |
|
2646 mutable iterator's destructor. |
|
2647 */ |
|
2648 { |
|
2649 Container c1; |
|
2650 populate(c1); |
|
2651 QVERIFY(c1.size() == 4); |
|
2652 QVERIFY(c1.isDetached()); |
|
2653 |
|
2654 { |
|
2655 ContainerMutableIterator i(c1); |
|
2656 i.next(); |
|
2657 } |
|
2658 |
|
2659 Container c2 = c1; |
|
2660 QVERIFY(c1.size() == 4); |
|
2661 QVERIFY(c2.size() == 4); |
|
2662 QVERIFY(!c1.isDetached()); |
|
2663 QVERIFY(!c2.isDetached()); |
|
2664 } |
|
2665 |
|
2666 /* |
|
2667 Verify that the 'sharable' flag only affects the original |
|
2668 object, not the copies. |
|
2669 */ |
|
2670 { |
|
2671 Container c1; |
|
2672 populate(c1); |
|
2673 QVERIFY(c1.size() == 4); |
|
2674 QVERIFY(c1.isDetached()); |
|
2675 |
|
2676 Container c2 = c1; |
|
2677 QVERIFY(isSharable(c2)); |
|
2678 |
|
2679 ContainerMutableIterator i(c1); |
|
2680 QVERIFY(!isSharable(c1)); |
|
2681 QVERIFY(isSharable(c2)); |
|
2682 |
|
2683 Container c3 = c1; |
|
2684 QVERIFY(!isSharable(c1)); |
|
2685 QVERIFY(isSharable(c2)); |
|
2686 QVERIFY(isSharable(c3)); |
|
2687 QVERIFY(c1.isDetached()); |
|
2688 QVERIFY(c2.isDetached()); |
|
2689 QVERIFY(c3.isDetached()); |
|
2690 |
|
2691 Container c4; |
|
2692 c4 = c1; |
|
2693 QVERIFY(!isSharable(c1)); |
|
2694 QVERIFY(isSharable(c2)); |
|
2695 QVERIFY(isSharable(c4)); |
|
2696 QVERIFY(c1.isDetached()); |
|
2697 QVERIFY(c2.isDetached()); |
|
2698 QVERIFY(c4.isDetached()); |
|
2699 |
|
2700 c3 = c2; |
|
2701 QVERIFY(!isSharable(c1)); |
|
2702 QVERIFY(isSharable(c2)); |
|
2703 QVERIFY(isSharable(c3)); |
|
2704 QVERIFY(c1.isDetached()); |
|
2705 QVERIFY(!c2.isDetached()); |
|
2706 QVERIFY(!c3.isDetached()); |
|
2707 } |
|
2708 } |
|
2709 |
|
2710 #define TEST_SEQUENTIAL_CONTAINER(Container) \ |
|
2711 testContainer<Q##Container<int>, QMutable##Container##Iterator<int> >() |
|
2712 |
|
2713 #define TEST_ASSOCIATIVE_CONTAINER(Container) \ |
|
2714 testContainer<Q##Container<int, int>, QMutable##Container##Iterator<int, int> >() |
|
2715 |
|
2716 void tst_Collections::sharableQList() |
|
2717 { |
|
2718 TEST_SEQUENTIAL_CONTAINER(List); |
|
2719 } |
|
2720 |
|
2721 void tst_Collections::sharableQLinkedList() |
|
2722 { |
|
2723 TEST_SEQUENTIAL_CONTAINER(LinkedList); |
|
2724 } |
|
2725 |
|
2726 void tst_Collections::sharableQVector() |
|
2727 { |
|
2728 TEST_SEQUENTIAL_CONTAINER(Vector); |
|
2729 } |
|
2730 |
|
2731 void tst_Collections::sharableQMap() |
|
2732 { |
|
2733 TEST_ASSOCIATIVE_CONTAINER(Map); |
|
2734 } |
|
2735 |
|
2736 void tst_Collections::sharableQHash() |
|
2737 { |
|
2738 TEST_ASSOCIATIVE_CONTAINER(Hash); |
|
2739 } |
|
2740 |
|
2741 static int getList_calls = 0; |
|
2742 QList<int> getList() |
|
2743 { |
|
2744 ++getList_calls; |
|
2745 QList<int> list; |
|
2746 list << 1 << 2 << 3 << 4 << 5 << 6; |
|
2747 return list; |
|
2748 } |
|
2749 |
|
2750 |
|
2751 void tst_Collections::q_foreach() |
|
2752 { |
|
2753 QList<int> list; |
|
2754 list << -2 << -1 << 0 << 1 << 2; |
|
2755 |
|
2756 int sum = 0; |
|
2757 int j = 0; |
|
2758 foreach(int i, list) { |
|
2759 QCOMPARE(i, list.at(j)); |
|
2760 sum += i; |
|
2761 ++j; |
|
2762 } |
|
2763 QCOMPARE(sum, 0); |
|
2764 |
|
2765 // again, but without scope |
|
2766 foreach(int i, list) |
|
2767 sum += i; |
|
2768 QCOMPARE(sum, 0); |
|
2769 |
|
2770 foreach(int i, list) { |
|
2771 sum += i; |
|
2772 if (i == 0) |
|
2773 break; |
|
2774 } |
|
2775 QCOMPARE(sum, -3); |
|
2776 |
|
2777 sum = 0; |
|
2778 foreach(int i, list) { |
|
2779 if (i < 0) |
|
2780 continue; |
|
2781 sum += i; |
|
2782 } |
|
2783 QCOMPARE(sum, 3); |
|
2784 |
|
2785 sum = 0; |
|
2786 getList_calls = 0; |
|
2787 foreach(int i, getList()) |
|
2788 sum += i; |
|
2789 QCOMPARE(sum, 21); |
|
2790 QCOMPARE(getList_calls, 1); |
|
2791 } |
|
2792 |
|
2793 |
|
2794 void tst_Collections::conversions() |
|
2795 { |
|
2796 #define STUFF "A" << "C" << "B" << "A" |
|
2797 |
|
2798 { |
|
2799 QList<QString> list1; |
|
2800 list1 << STUFF; |
|
2801 |
|
2802 QVector<QString> vect1 = list1.toVector(); |
|
2803 QCOMPARE(list1.size(), 4); |
|
2804 QVERIFY(vect1 == (QVector<QString>() << STUFF)); |
|
2805 |
|
2806 QList<QString> list2 = vect1.toList(); |
|
2807 QCOMPARE(list2.size(), 4); |
|
2808 QVERIFY(list2 == (QList<QString>() << STUFF)); |
|
2809 |
|
2810 QSet<QString> set1 = list1.toSet(); |
|
2811 QCOMPARE(set1.size(), 3); |
|
2812 QVERIFY(set1.contains("A")); |
|
2813 QVERIFY(set1.contains("B")); |
|
2814 QVERIFY(set1.contains("C")); |
|
2815 QVERIFY(!set1.contains("D")); |
|
2816 |
|
2817 QList<QString> list3 = set1.toList(); |
|
2818 QCOMPARE(list3.size(), 3); |
|
2819 QVERIFY(list3.contains("A")); |
|
2820 QVERIFY(list3.contains("B")); |
|
2821 QVERIFY(list3.contains("C")); |
|
2822 QVERIFY(!list3.contains("D")); |
|
2823 |
|
2824 QVERIFY(QList<int>().toVector().isEmpty()); |
|
2825 QVERIFY(QList<int>().toSet().isEmpty()); |
|
2826 QVERIFY(QVector<int>().toList().isEmpty()); |
|
2827 QVERIFY(QSet<int>().toList().isEmpty()); |
|
2828 } |
|
2829 |
|
2830 { |
|
2831 QList<QString> list1; |
|
2832 list1 << STUFF; |
|
2833 |
|
2834 QVector<QString> vect1 = QVector<QString>::fromList(list1); |
|
2835 QCOMPARE(list1.size(), 4); |
|
2836 QVERIFY(vect1 == (QVector<QString>() << STUFF)); |
|
2837 |
|
2838 QList<QString> list2 = QList<QString>::fromVector(vect1); |
|
2839 QCOMPARE(list2.size(), 4); |
|
2840 QVERIFY(list2 == (QList<QString>() << STUFF)); |
|
2841 |
|
2842 QSet<QString> set1 = QSet<QString>::fromList(list1); |
|
2843 QCOMPARE(set1.size(), 3); |
|
2844 QVERIFY(set1.contains("A")); |
|
2845 QVERIFY(set1.contains("B")); |
|
2846 QVERIFY(set1.contains("C")); |
|
2847 QVERIFY(!set1.contains("D")); |
|
2848 |
|
2849 QList<QString> list3 = QList<QString>::fromSet(set1); |
|
2850 QCOMPARE(list3.size(), 3); |
|
2851 QVERIFY(list3.contains("A")); |
|
2852 QVERIFY(list3.contains("B")); |
|
2853 QVERIFY(list3.contains("C")); |
|
2854 QVERIFY(!list3.contains("D")); |
|
2855 |
|
2856 QVERIFY(QVector<int>::fromList(QList<int>()).isEmpty()); |
|
2857 QVERIFY(QSet<int>::fromList(QList<int>()).isEmpty()); |
|
2858 QVERIFY(QList<int>::fromVector(QVector<int>()).isEmpty()); |
|
2859 QVERIFY(QList<int>::fromSet(QSet<int>()).isEmpty()); |
|
2860 } |
|
2861 #undef STUFF |
|
2862 } |
|
2863 |
|
2864 void tst_Collections::javaStyleIterators() |
|
2865 { |
|
2866 QStringList list; |
|
2867 list << "a" << "b" << "c"; |
|
2868 QMutableStringListIterator i(list); |
|
2869 while (i.hasNext()) { |
|
2870 i.next(); |
|
2871 i.setValue(""); |
|
2872 } |
|
2873 while (i.hasPrevious()) { |
|
2874 i.previous(); |
|
2875 QVERIFY(i.value().isEmpty()); |
|
2876 i.value() = "x"; |
|
2877 QCOMPARE(i.value(), QString("x")); |
|
2878 } |
|
2879 } |
|
2880 |
|
2881 template <class Container> |
|
2882 void testLinkedListLikeStlIterators() |
|
2883 { |
|
2884 Container fake; |
|
2885 typename Container::value_type t; |
|
2886 fake << t; |
|
2887 |
|
2888 typename Container::iterator i1 = fake.begin(), i2 = i1 + 1; |
|
2889 typename Container::const_iterator c1 = i1, c2 = c1 + 1; |
|
2890 |
|
2891 QVERIFY(i1 == i1); |
|
2892 QVERIFY(i1 == c1); |
|
2893 QVERIFY(c1 == i1); |
|
2894 QVERIFY(c1 == c1); |
|
2895 QVERIFY(i2 == i2); |
|
2896 QVERIFY(i2 == c2); |
|
2897 QVERIFY(c2 == i2); |
|
2898 QVERIFY(c2 == c2); |
|
2899 |
|
2900 QVERIFY(i1 != i2); |
|
2901 QVERIFY(i1 != c2); |
|
2902 QVERIFY(c1 != i2); |
|
2903 QVERIFY(c1 != c2); |
|
2904 QVERIFY(i2 != i1); |
|
2905 QVERIFY(i2 != c1); |
|
2906 QVERIFY(c2 != i1); |
|
2907 QVERIFY(c2 != c1); |
|
2908 } |
|
2909 |
|
2910 template <class Container> |
|
2911 void testListLikeStlIterators() |
|
2912 { |
|
2913 testLinkedListLikeStlIterators<Container>(); |
|
2914 |
|
2915 Container fake; |
|
2916 typename Container::value_type t; |
|
2917 fake << t; |
|
2918 |
|
2919 typename Container::iterator i1 = fake.begin(), i2 = i1 + 1; |
|
2920 typename Container::const_iterator c1 = i1, c2 = c1 + 1; |
|
2921 |
|
2922 QVERIFY(i1 < i2); |
|
2923 QVERIFY(i1 < c2); |
|
2924 QVERIFY(c1 < i2); |
|
2925 QVERIFY(c1 < c2); |
|
2926 QVERIFY(!(i2 < i1)); |
|
2927 QVERIFY(!(i2 < c1)); |
|
2928 QVERIFY(!(c2 < i1)); |
|
2929 QVERIFY(!(c2 < c1)); |
|
2930 QVERIFY(!(i1 < i1)); |
|
2931 QVERIFY(!(i1 < c1)); |
|
2932 QVERIFY(!(c1 < i1)); |
|
2933 QVERIFY(!(c1 < c1)); |
|
2934 QVERIFY(!(i2 < i2)); |
|
2935 QVERIFY(!(i2 < c2)); |
|
2936 QVERIFY(!(c2 < i2)); |
|
2937 QVERIFY(!(c2 < c2)); |
|
2938 |
|
2939 QVERIFY(i2 > i1); |
|
2940 QVERIFY(i2 > c1); |
|
2941 QVERIFY(c2 > i1); |
|
2942 QVERIFY(c2 > c1); |
|
2943 QVERIFY(!(i1 > i2)); |
|
2944 QVERIFY(!(i1 > c2)); |
|
2945 QVERIFY(!(c1 > i2)); |
|
2946 QVERIFY(!(c1 > c2)); |
|
2947 QVERIFY(!(i1 > i1)); |
|
2948 QVERIFY(!(i1 > c1)); |
|
2949 QVERIFY(!(c1 > i1)); |
|
2950 QVERIFY(!(c1 > c1)); |
|
2951 QVERIFY(!(i2 > i2)); |
|
2952 QVERIFY(!(i2 > c2)); |
|
2953 QVERIFY(!(c2 > i2)); |
|
2954 QVERIFY(!(c2 > c2)); |
|
2955 |
|
2956 QVERIFY(!(i1 >= i2)); |
|
2957 QVERIFY(!(i1 >= c2)); |
|
2958 QVERIFY(!(c1 >= i2)); |
|
2959 QVERIFY(!(c1 >= c2)); |
|
2960 QVERIFY(i2 >= i1); |
|
2961 QVERIFY(i2 >= c1); |
|
2962 QVERIFY(c2 >= i1); |
|
2963 QVERIFY(c2 >= c1); |
|
2964 QVERIFY(i1 >= i1); |
|
2965 QVERIFY(i1 >= c1); |
|
2966 QVERIFY(c1 >= i1); |
|
2967 QVERIFY(c1 >= c1); |
|
2968 QVERIFY(i2 >= i2); |
|
2969 QVERIFY(i2 >= c2); |
|
2970 QVERIFY(c2 >= i2); |
|
2971 QVERIFY(c2 >= c2); |
|
2972 |
|
2973 QVERIFY(!(i2 <= i1)); |
|
2974 QVERIFY(!(i2 <= c1)); |
|
2975 QVERIFY(!(c2 <= i1)); |
|
2976 QVERIFY(!(c2 <= c1)); |
|
2977 QVERIFY(i1 <= i2); |
|
2978 QVERIFY(i1 <= c2); |
|
2979 QVERIFY(c1 <= i2); |
|
2980 QVERIFY(c1 <= c2); |
|
2981 QVERIFY(i1 <= i1); |
|
2982 QVERIFY(i1 <= c1); |
|
2983 QVERIFY(c1 <= i1); |
|
2984 QVERIFY(c1 <= c1); |
|
2985 QVERIFY(i2 <= i2); |
|
2986 QVERIFY(i2 <= c2); |
|
2987 QVERIFY(c2 <= i2); |
|
2988 QVERIFY(c2 <= c2); |
|
2989 } |
|
2990 |
|
2991 template <class Container> |
|
2992 void testMapLikeStlIterators() |
|
2993 { |
|
2994 Container fake; |
|
2995 QString k; |
|
2996 QString t; |
|
2997 fake.insert(k, t); |
|
2998 |
|
2999 typename Container::iterator i1 = fake.begin(), i2 = i1 + 1; |
|
3000 typename Container::const_iterator c1 = i1, c2 = c1 + 1; |
|
3001 |
|
3002 QVERIFY(i1 == i1); |
|
3003 QVERIFY(i1 == c1); |
|
3004 QVERIFY(c1 == i1); |
|
3005 QVERIFY(c1 == c1); |
|
3006 QVERIFY(i2 == i2); |
|
3007 QVERIFY(i2 == c2); |
|
3008 QVERIFY(c2 == i2); |
|
3009 QVERIFY(c2 == c2); |
|
3010 |
|
3011 QVERIFY(i1 != i2); |
|
3012 QVERIFY(i1 != c2); |
|
3013 QVERIFY(c1 != i2); |
|
3014 QVERIFY(c1 != c2); |
|
3015 QVERIFY(i2 != i1); |
|
3016 QVERIFY(i2 != c1); |
|
3017 QVERIFY(c2 != i1); |
|
3018 QVERIFY(c2 != c1); |
|
3019 } |
|
3020 |
|
3021 void tst_Collections::constAndNonConstStlIterators() |
|
3022 { |
|
3023 testListLikeStlIterators<QList<int> >(); |
|
3024 testListLikeStlIterators<QStringList >(); |
|
3025 testLinkedListLikeStlIterators<QLinkedList<int> >(); |
|
3026 testListLikeStlIterators<QVector<int> >(); |
|
3027 testMapLikeStlIterators<QMap<QString, QString> >(); |
|
3028 testMapLikeStlIterators<QMultiMap<QString, QString> >(); |
|
3029 testMapLikeStlIterators<QHash<QString, QString> >(); |
|
3030 testMapLikeStlIterators<QMultiHash<QString, QString> >(); |
|
3031 } |
|
3032 |
|
3033 #ifndef QT_NO_STL |
|
3034 void tst_Collections::vector_stl_data() |
|
3035 { |
|
3036 QTest::addColumn<QStringList>("elements"); |
|
3037 |
|
3038 QTest::newRow("empty") << QStringList(); |
|
3039 QTest::newRow("one") << (QStringList() << "Hei"); |
|
3040 QTest::newRow("two") << (QStringList() << "Hei" << "Hopp"); |
|
3041 QTest::newRow("three") << (QStringList() << "Hei" << "Hopp" << "Sann"); |
|
3042 } |
|
3043 |
|
3044 void tst_Collections::vector_stl() |
|
3045 { |
|
3046 QFETCH(QStringList, elements); |
|
3047 |
|
3048 QVector<QString> vector; |
|
3049 for (int i = 0; i < elements.count(); ++i) |
|
3050 vector << elements.at(i); |
|
3051 |
|
3052 std::vector<QString> stdVector = vector.toStdVector(); |
|
3053 |
|
3054 QCOMPARE(int(stdVector.size()), elements.size()); |
|
3055 |
|
3056 std::vector<QString>::const_iterator it = stdVector.begin(); |
|
3057 for (uint j = 0; j < stdVector.size() && it != stdVector.end(); ++j, ++it) |
|
3058 QCOMPARE(*it, vector[j]); |
|
3059 |
|
3060 QCOMPARE(QVector<QString>::fromStdVector(stdVector), vector); |
|
3061 } |
|
3062 |
|
3063 void tst_Collections::linkedlist_stl_data() |
|
3064 { |
|
3065 list_stl_data(); |
|
3066 } |
|
3067 |
|
3068 void tst_Collections::linkedlist_stl() |
|
3069 { |
|
3070 QFETCH(QStringList, elements); |
|
3071 |
|
3072 QLinkedList<QString> list; |
|
3073 for (int i = 0; i < elements.count(); ++i) |
|
3074 list << elements.at(i); |
|
3075 |
|
3076 std::list<QString> stdList = list.toStdList(); |
|
3077 |
|
3078 QCOMPARE(int(stdList.size()), elements.size()); |
|
3079 |
|
3080 std::list<QString>::const_iterator it = stdList.begin(); |
|
3081 QLinkedList<QString>::const_iterator it2 = list.constBegin(); |
|
3082 for (uint j = 0; j < stdList.size(); ++j, ++it, ++it2) |
|
3083 QCOMPARE(*it, *it2); |
|
3084 |
|
3085 QCOMPARE(QLinkedList<QString>::fromStdList(stdList), list); |
|
3086 } |
|
3087 |
|
3088 void tst_Collections::list_stl_data() |
|
3089 { |
|
3090 QTest::addColumn<QStringList>("elements"); |
|
3091 |
|
3092 QTest::newRow("empty") << QStringList(); |
|
3093 QTest::newRow("one") << (QStringList() << "Hei"); |
|
3094 QTest::newRow("two") << (QStringList() << "Hei" << "Hopp"); |
|
3095 QTest::newRow("three") << (QStringList() << "Hei" << "Hopp" << "Sann"); |
|
3096 } |
|
3097 |
|
3098 void tst_Collections::list_stl() |
|
3099 { |
|
3100 QFETCH(QStringList, elements); |
|
3101 |
|
3102 QList<QString> list; |
|
3103 for (int i = 0; i < elements.count(); ++i) |
|
3104 list << elements.at(i); |
|
3105 |
|
3106 std::list<QString> stdList = list.toStdList(); |
|
3107 |
|
3108 QCOMPARE(int(stdList.size()), elements.size()); |
|
3109 |
|
3110 std::list<QString>::const_iterator it = stdList.begin(); |
|
3111 for (uint j = 0; j < stdList.size() && it != stdList.end(); ++j, ++it) |
|
3112 QCOMPARE(*it, list[j]); |
|
3113 |
|
3114 QCOMPARE(QList<QString>::fromStdList(stdList), list); |
|
3115 } |
|
3116 #endif |
|
3117 |
|
3118 template <typename T> |
|
3119 T qtInit(T * = 0) |
|
3120 { |
|
3121 return T(); |
|
3122 } |
|
3123 |
|
3124 void tst_Collections::q_init() |
|
3125 { |
|
3126 QCOMPARE(qtInit<int>(), 0); |
|
3127 QCOMPARE(qtInit<double>(), 0.0); |
|
3128 QCOMPARE(qtInit<QString>(), QString()); |
|
3129 QCOMPARE(qtInit<int *>(), static_cast<int *>(0)); |
|
3130 QCOMPARE(qtInit<double *>(), static_cast<double *>(0)); |
|
3131 QCOMPARE(qtInit<QString *>(), static_cast<QString *>(0)); |
|
3132 QCOMPARE(qtInit<Pod>().i1, 0); |
|
3133 QCOMPARE(qtInit<Pod>().i2, 0); |
|
3134 } |
|
3135 |
|
3136 void tst_Collections::pointersize() |
|
3137 { |
|
3138 QCOMPARE(int(sizeof(void *)), QT_POINTER_SIZE); |
|
3139 } |
|
3140 |
|
3141 class LessThanComparable |
|
3142 { |
|
3143 public: |
|
3144 bool operator<(const LessThanComparable &) const { return true; } |
|
3145 }; |
|
3146 |
|
3147 class EqualsComparable |
|
3148 { |
|
3149 public: |
|
3150 bool operator==(const EqualsComparable &) const { return true; } |
|
3151 }; |
|
3152 |
|
3153 uint qHash(const EqualsComparable &) |
|
3154 { |
|
3155 return 0; |
|
3156 } |
|
3157 |
|
3158 /* |
|
3159 The following functions instatiates every member functions in the |
|
3160 Qt containers that requires either operator== or operator<. |
|
3161 They are ordered in a concept inheritance tree: |
|
3162 |
|
3163 Container |
|
3164 MutableIterationContainer |
|
3165 Sequence (QLinkedList) |
|
3166 Random Access (QVector, QList, QQueue, QStack) |
|
3167 Pair Associative (QHash, QMap) |
|
3168 Associative (QSet) |
|
3169 */ |
|
3170 template <typename ContainerType, typename ValueType> |
|
3171 void instantiateContainer() |
|
3172 { |
|
3173 const ValueType value = ValueType(); |
|
3174 ContainerType container; |
|
3175 const ContainerType constContainer(container); |
|
3176 |
|
3177 #ifndef QT_NO_STL |
|
3178 typename ContainerType::const_iterator constIt; |
|
3179 constIt = constContainer.begin(); |
|
3180 container.constBegin(); |
|
3181 |
|
3182 constIt = constContainer.end(); |
|
3183 container.constEnd(); |
|
3184 #endif |
|
3185 container.clear(); |
|
3186 container.contains(value); |
|
3187 container.count(); |
|
3188 container.empty(); |
|
3189 container.isEmpty(); |
|
3190 container.size(); |
|
3191 |
|
3192 container != constContainer; |
|
3193 container == constContainer; |
|
3194 container = constContainer; |
|
3195 } |
|
3196 |
|
3197 template <typename ContainerType, typename ValueType> |
|
3198 void instantiateMutableIterationContainer() |
|
3199 { |
|
3200 instantiateContainer<ContainerType, ValueType>(); |
|
3201 ContainerType container; |
|
3202 |
|
3203 #ifndef QT_NO_STL |
|
3204 typename ContainerType::iterator it; |
|
3205 it = container.begin(); |
|
3206 it = container.end(); |
|
3207 #endif |
|
3208 |
|
3209 // QSet lacks count(T). |
|
3210 const ValueType value = ValueType(); |
|
3211 container.count(value); |
|
3212 } |
|
3213 |
|
3214 template <typename ContainerType, typename ValueType> |
|
3215 void instantiateSequence() |
|
3216 { |
|
3217 instantiateMutableIterationContainer<ContainerType, ValueType>(); |
|
3218 |
|
3219 // QVector lacks removeAll(T) |
|
3220 // ValueType value = ValueType(); |
|
3221 // ContainerType container; |
|
3222 // container.removeAll(value); |
|
3223 } |
|
3224 |
|
3225 template <typename ContainerType, typename ValueType> |
|
3226 void instantiateRandomAccess() |
|
3227 { |
|
3228 instantiateSequence<ContainerType, ValueType>(); |
|
3229 |
|
3230 ValueType value = ValueType(); |
|
3231 ContainerType container; |
|
3232 container.indexOf(value); |
|
3233 container.lastIndexOf(value); |
|
3234 } |
|
3235 |
|
3236 template <typename ContainerType, typename ValueType> |
|
3237 void instantiateAssociative() |
|
3238 { |
|
3239 instantiateContainer<ContainerType, ValueType>(); |
|
3240 |
|
3241 const ValueType value = ValueType(); |
|
3242 ContainerType container; |
|
3243 const ContainerType constContainer(container); |
|
3244 |
|
3245 container.reserve(1); |
|
3246 container.capacity(); |
|
3247 container.squeeze(); |
|
3248 |
|
3249 container.remove(value); |
|
3250 container.values(); |
|
3251 |
|
3252 container.unite(constContainer); |
|
3253 container.intersect(constContainer); |
|
3254 container.subtract(constContainer); |
|
3255 |
|
3256 container != constContainer; |
|
3257 container == constContainer; |
|
3258 container & constContainer; |
|
3259 container &= constContainer; |
|
3260 container &= value; |
|
3261 container + constContainer; |
|
3262 container += constContainer; |
|
3263 container += value; |
|
3264 container - constContainer; |
|
3265 container -= constContainer; |
|
3266 container -= value; |
|
3267 container | constContainer; |
|
3268 container |= constContainer; |
|
3269 container |= value; |
|
3270 } |
|
3271 |
|
3272 template <typename ContainerType, typename KeyType, typename ValueType> |
|
3273 void instantiatePairAssociative() |
|
3274 { |
|
3275 instantiateMutableIterationContainer<ContainerType, KeyType>(); |
|
3276 |
|
3277 typename ContainerType::iterator it; |
|
3278 typename ContainerType::const_iterator constIt; |
|
3279 const KeyType key = KeyType(); |
|
3280 const ValueType value = ValueType(); |
|
3281 ContainerType container; |
|
3282 const ContainerType constContainer(container); |
|
3283 |
|
3284 it = container.insert(key, value); |
|
3285 container.erase(it); |
|
3286 container.find(key); |
|
3287 container.constFind(key); |
|
3288 constContainer.find(key); |
|
3289 |
|
3290 container.key(value); |
|
3291 container.keys(); |
|
3292 constContainer.keys(); |
|
3293 container.remove(key); |
|
3294 container.take(key); |
|
3295 container.unite(constContainer); |
|
3296 container.value(key); |
|
3297 container.value(key, value); |
|
3298 container.values(); |
|
3299 container.values(key); |
|
3300 container[key]; |
|
3301 const int foo = constContainer[key]; |
|
3302 Q_UNUSED(foo); |
|
3303 } |
|
3304 |
|
3305 /* |
|
3306 Instantiate all Qt containers using a datatype that |
|
3307 defines the minimum amount of operators. |
|
3308 */ |
|
3309 void tst_Collections::containerInstantiation() |
|
3310 { |
|
3311 // Instantiate QHash member functions. |
|
3312 typedef QHash<EqualsComparable, int> Hash; |
|
3313 instantiatePairAssociative<Hash, EqualsComparable, int>(); |
|
3314 |
|
3315 Hash hash; |
|
3316 hash.reserve(1); |
|
3317 hash.capacity(); |
|
3318 hash.squeeze(); |
|
3319 |
|
3320 // Instantiate QMap member functions. |
|
3321 typedef QMap<LessThanComparable, int> Map; |
|
3322 instantiatePairAssociative<Map, LessThanComparable, int>(); |
|
3323 |
|
3324 // Instantiate QSet member functions. |
|
3325 typedef QSet<EqualsComparable> Set; |
|
3326 instantiateAssociative<Set, EqualsComparable>(); |
|
3327 |
|
3328 //Instantiate QLinkedList member functions. |
|
3329 typedef QLinkedList<EqualsComparable> LinkedList; |
|
3330 instantiateSequence<LinkedList, EqualsComparable> (); |
|
3331 { |
|
3332 EqualsComparable value; |
|
3333 LinkedList list; |
|
3334 list.removeAll(value); |
|
3335 } |
|
3336 |
|
3337 //Instantiate QList member functions. |
|
3338 typedef QList<EqualsComparable> List; |
|
3339 instantiateRandomAccess<List, EqualsComparable>(); |
|
3340 { |
|
3341 EqualsComparable value; |
|
3342 List list; |
|
3343 list.removeAll(value); |
|
3344 } |
|
3345 |
|
3346 //Instantiate QVector member functions. |
|
3347 typedef QVector<EqualsComparable> Vector; |
|
3348 instantiateRandomAccess<Vector, EqualsComparable>(); |
|
3349 |
|
3350 //Instantiate QQueue member functions. |
|
3351 typedef QQueue<EqualsComparable> Queue; |
|
3352 instantiateRandomAccess<Queue, EqualsComparable>(); |
|
3353 |
|
3354 //Instantiate QStack member functions. |
|
3355 typedef QStack<EqualsComparable> Stack; |
|
3356 instantiateRandomAccess<Stack, EqualsComparable>(); |
|
3357 } |
|
3358 |
|
3359 void tst_Collections::qtimerList() |
|
3360 { |
|
3361 QList<double> foo; |
|
3362 const int N = 10000; |
|
3363 |
|
3364 foo.append(99.9); |
|
3365 foo.append(99.9); |
|
3366 foo.append(99.9); |
|
3367 |
|
3368 for(int i = 0; i < N; i++) { |
|
3369 foo.removeFirst(); |
|
3370 foo.insert(1, 99.9); |
|
3371 } |
|
3372 |
|
3373 QList<double>::Iterator end = foo.end(); |
|
3374 for (int i = 0; i < (N / 2) - 10; ++i) { |
|
3375 foo.prepend(99.9); |
|
3376 if (foo.end() != end) |
|
3377 return; |
|
3378 } |
|
3379 QFAIL("QList preallocates too much memory"); |
|
3380 } |
|
3381 |
|
3382 template <typename Container> |
|
3383 void testContainerTypedefs(Container container) |
|
3384 { |
|
3385 Q_UNUSED(container) |
|
3386 { typedef typename Container::value_type Foo; } |
|
3387 { typedef typename Container::iterator Foo; } |
|
3388 { typedef typename Container::const_iterator Foo; } |
|
3389 { typedef typename Container::reference Foo; } |
|
3390 { typedef typename Container::const_reference Foo; } |
|
3391 { typedef typename Container::pointer Foo; } |
|
3392 { typedef typename Container::difference_type Foo; } |
|
3393 { typedef typename Container::size_type Foo; } |
|
3394 } |
|
3395 |
|
3396 template <typename Container> |
|
3397 void testPairAssociativeContainerTypedefs(Container container) |
|
3398 { |
|
3399 Q_UNUSED(container) |
|
3400 |
|
3401 // TODO: Not sure how to define value_type for our associative containers |
|
3402 // { typedef typename Container::value_type Foo; } |
|
3403 // { typedef typename Container::const_iterator Foo; } |
|
3404 // { typedef typename Container::reference Foo; } |
|
3405 // { typedef typename Container::const_reference Foo; } |
|
3406 // { typedef typename Container::pointer Foo; } |
|
3407 |
|
3408 { typedef typename Container::difference_type Foo; } |
|
3409 { typedef typename Container::size_type Foo; } |
|
3410 { typedef typename Container::iterator Foo; } |
|
3411 { typedef typename Container::key_type Foo; } |
|
3412 { typedef typename Container::mapped_type Foo; } |
|
3413 // TODO |
|
3414 // { typedef typename Container::key_compare Foo; } |
|
3415 // { typedef typename Container::value_comare Foo; } |
|
3416 } |
|
3417 |
|
3418 template <typename Container> |
|
3419 void testSetContainerTypedefs(Container container) |
|
3420 { |
|
3421 Q_UNUSED(container) |
|
3422 { typedef typename Container::iterator Foo; } |
|
3423 { typedef typename Container::const_iterator Foo; } |
|
3424 { typedef typename Container::reference Foo; } |
|
3425 { typedef typename Container::const_reference Foo; } |
|
3426 { typedef typename Container::pointer Foo; } |
|
3427 { typedef typename Container::difference_type Foo; } |
|
3428 { typedef typename Container::size_type Foo; } |
|
3429 { typedef typename Container::key_type Foo; } |
|
3430 } |
|
3431 |
|
3432 /* |
|
3433 Compile-time test that verifies that the Qt containers |
|
3434 have STL-compatable typedefs. |
|
3435 */ |
|
3436 void tst_Collections::containerTypedefs() |
|
3437 { |
|
3438 testContainerTypedefs(QVector<int>()); |
|
3439 testContainerTypedefs(QStack<int>()); |
|
3440 testContainerTypedefs(QList<int>()); |
|
3441 testContainerTypedefs(QLinkedList<int>()); |
|
3442 testContainerTypedefs(QQueue<int>()); |
|
3443 |
|
3444 testPairAssociativeContainerTypedefs(QMap<int, int>()); |
|
3445 testPairAssociativeContainerTypedefs(QMultiMap<int, int>()); |
|
3446 testPairAssociativeContainerTypedefs(QHash<int, int>()); |
|
3447 |
|
3448 testSetContainerTypedefs(QSet<int>()); |
|
3449 } |
|
3450 |
|
3451 #if defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET) |
|
3452 class Key1 |
|
3453 {}; |
|
3454 class T1 |
|
3455 {}; |
|
3456 class T2 |
|
3457 {}; |
|
3458 #else |
|
3459 class Key1; |
|
3460 class T1; |
|
3461 class T2; |
|
3462 #endif |
|
3463 |
|
3464 void tst_Collections::forwardDeclared() |
|
3465 { |
|
3466 { typedef QHash<Key1, T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } |
|
3467 { typedef QMultiHash<Key1, T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } |
|
3468 #if !defined(Q_CC_MSVC_NET) || _MSC_VER >= 1310 |
|
3469 { typedef QMap<Key1, T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } |
|
3470 { typedef QMultiMap<Key1, T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } |
|
3471 #endif |
|
3472 #if !defined(Q_CC_RVCT) |
|
3473 // RVCT can't handle forward declared template parameters if those are used to declare |
|
3474 // class members inside templated class. |
|
3475 { typedef QPair<T1, T2> C; C *x = 0; Q_UNUSED(x) } |
|
3476 #endif |
|
3477 { typedef QList<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } |
|
3478 { typedef QLinkedList<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } |
|
3479 { typedef QVector<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) Q_UNUSED(i) Q_UNUSED(j) } |
|
3480 { typedef QStack<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) Q_UNUSED(i) Q_UNUSED(j) } |
|
3481 { typedef QQueue<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) } |
|
3482 { typedef QSet<T1> C; C *x = 0; /* C::iterator i; */ C::const_iterator j; Q_UNUSED(x) } |
|
3483 } |
|
3484 |
|
3485 #if defined(Q_ALIGNOF) && defined(Q_DECL_ALIGN) |
|
3486 |
|
3487 class Q_DECL_ALIGN(4) Aligned4 |
|
3488 { |
|
3489 char i; |
|
3490 public: |
|
3491 Aligned4(int i = 0) : i(i) {} |
|
3492 bool checkAligned() const |
|
3493 { |
|
3494 return (quintptr(this) & 3) == 0; |
|
3495 } |
|
3496 |
|
3497 inline bool operator==(const Aligned4 &other) const { return i == other.i; } |
|
3498 inline bool operator<(const Aligned4 &other) const { return i < other.i; } |
|
3499 friend inline int qHash(const Aligned4 &a) { return qHash(a.i); } |
|
3500 }; |
|
3501 |
|
3502 class Q_DECL_ALIGN(128) Aligned128 |
|
3503 { |
|
3504 char i; |
|
3505 public: |
|
3506 Aligned128(int i = 0) : i(i) {} |
|
3507 bool checkAligned() const |
|
3508 { |
|
3509 return (quintptr(this) & 127) == 0; |
|
3510 } |
|
3511 |
|
3512 inline bool operator==(const Aligned128 &other) const { return i == other.i; } |
|
3513 inline bool operator<(const Aligned128 &other) const { return i < other.i; } |
|
3514 friend inline int qHash(const Aligned128 &a) { return qHash(a.i); } |
|
3515 }; |
|
3516 |
|
3517 template<typename C> |
|
3518 void testVectorAlignment() |
|
3519 { |
|
3520 typedef typename C::value_type Aligned; |
|
3521 C container; |
|
3522 container.append(Aligned()); |
|
3523 QVERIFY(container[0].checkAligned()); |
|
3524 |
|
3525 for (int i = 0; i < 200; ++i) |
|
3526 container.append(Aligned()); |
|
3527 |
|
3528 for (int i = 0; i < container.size(); ++i) |
|
3529 QVERIFY(container.at(i).checkAligned()); |
|
3530 } |
|
3531 |
|
3532 template<typename C> |
|
3533 void testContiguousCacheAlignment() |
|
3534 { |
|
3535 typedef typename C::value_type Aligned; |
|
3536 C container(150); |
|
3537 container.append(Aligned()); |
|
3538 QVERIFY(container[container.firstIndex()].checkAligned()); |
|
3539 |
|
3540 for (int i = 0; i < 200; ++i) |
|
3541 container.append(Aligned()); |
|
3542 |
|
3543 for (int i = container.firstIndex(); i < container.lastIndex(); ++i) |
|
3544 QVERIFY(container.at(i).checkAligned()); |
|
3545 } |
|
3546 |
|
3547 template<typename C> |
|
3548 void testAssociativeContainerAlignment() |
|
3549 { |
|
3550 typedef typename C::key_type Key; |
|
3551 typedef typename C::mapped_type Value; |
|
3552 C container; |
|
3553 container.insert(Key(), Value()); |
|
3554 |
|
3555 typename C::const_iterator it = container.constBegin(); |
|
3556 QVERIFY(it.key().checkAligned()); |
|
3557 QVERIFY(it.value().checkAligned()); |
|
3558 |
|
3559 // add some more elements |
|
3560 for (int i = 0; i < 200; ++i) |
|
3561 container.insert(Key(i), Value(i)); |
|
3562 |
|
3563 it = container.constBegin(); |
|
3564 for ( ; it != container.constEnd(); ++it) { |
|
3565 QVERIFY(it.key().checkAligned()); |
|
3566 QVERIFY(it.value().checkAligned()); |
|
3567 } |
|
3568 } |
|
3569 |
|
3570 void tst_Collections::alignment() |
|
3571 { |
|
3572 testVectorAlignment<QVector<Aligned4> >(); |
|
3573 testVectorAlignment<QVector<Aligned128> >(); |
|
3574 testContiguousCacheAlignment<QContiguousCache<Aligned4> >(); |
|
3575 testContiguousCacheAlignment<QContiguousCache<Aligned128> >(); |
|
3576 testAssociativeContainerAlignment<QMap<Aligned4, Aligned4> >(); |
|
3577 testAssociativeContainerAlignment<QMap<Aligned4, Aligned128> >(); |
|
3578 testAssociativeContainerAlignment<QMap<Aligned128, Aligned4> >(); |
|
3579 testAssociativeContainerAlignment<QMap<Aligned128, Aligned128> >(); |
|
3580 testAssociativeContainerAlignment<QHash<Aligned4, Aligned4> >(); |
|
3581 testAssociativeContainerAlignment<QHash<Aligned4, Aligned128> >(); |
|
3582 testAssociativeContainerAlignment<QHash<Aligned128, Aligned4> >(); |
|
3583 testAssociativeContainerAlignment<QHash<Aligned128, Aligned128> >(); |
|
3584 } |
|
3585 |
|
3586 #else |
|
3587 void tst_Collections::alignment() |
|
3588 { |
|
3589 QSKIP("Compiler doesn't support necessary extension keywords", SkipAll); |
|
3590 } |
|
3591 #endif |
|
3592 |
|
3593 QTEST_APPLESS_MAIN(tst_Collections) |
|
3594 #include "tst_collections.moc" |