| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2009 Max Howell <max@last.fm> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify * |
| 5 |
* it under the terms of the GNU General Public License as published by * |
| 6 |
* the Free Software Foundation; either version 2 of the License, or * |
| 7 |
* (at your option) any later version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, * |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 |
* GNU General Public License for more details. * |
| 13 |
* * |
| 14 |
* You should have received a copy of the GNU General Public License * |
| 15 |
* along with this program; if not, write to the * |
| 16 |
* Free Software Foundation, Inc., * |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
| 18 |
***************************************************************************/ |
| 19 |
|
| 20 |
#include "../src/SmartPointerList.h" |
| 21 |
#include <QtTest> |
| 22 |
|
| 23 |
// use a macro, as we don't want to test copy ctor early |
| 24 |
#define THREE_TIMERS( x ) SmartPointerList<QTimer> x; x << new QTimer << new QTimer << new QTimer |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class TestSmartPointerList : public QObject |
| 29 |
{ |
| 30 |
Q_OBJECT |
| 31 |
|
| 32 |
private slots: |
| 33 |
void testCount() |
| 34 |
{ |
| 35 |
THREE_TIMERS( objects ); |
| 36 |
QCOMPARE( objects.count(), 3 ); |
| 37 |
} |
| 38 |
|
| 39 |
void testCopy() |
| 40 |
{ |
| 41 |
THREE_TIMERS( objects1 ); |
| 42 |
SmartPointerList<QTimer> objects2 = objects1; |
| 43 |
|
| 44 |
for (int x = 0; x < 3; ++x) |
| 45 |
QVERIFY( objects1[x] == objects2[x] ); |
| 46 |
|
| 47 |
QCOMPARE( objects1.count(), 3 ); |
| 48 |
QCOMPARE( objects2.count(), 3 ); |
| 49 |
delete objects1.last(); |
| 50 |
QCOMPARE( objects1.count(), 2 ); |
| 51 |
QCOMPARE( objects2.count(), 2 ); |
| 52 |
} |
| 53 |
|
| 54 |
void testCopyAndThenDelete() |
| 55 |
{ |
| 56 |
THREE_TIMERS( os1 ); |
| 57 |
SmartPointerList<QTimer>* os2 = new SmartPointerList<QTimer>( os1 ); |
| 58 |
SmartPointerList<QTimer> os3( *os2 ); |
| 59 |
|
| 60 |
delete os2; |
| 61 |
|
| 62 |
QCOMPARE( os1.count(), 3 ); |
| 63 |
QCOMPARE( os3.count(), 3 ); |
| 64 |
|
| 65 |
delete os1[1]; |
| 66 |
|
| 67 |
QCOMPARE( os1.count(), 2 ); |
| 68 |
QCOMPARE( os3.count(), 2 ); |
| 69 |
} |
| 70 |
|
| 71 |
void testRemove() |
| 72 |
{ |
| 73 |
THREE_TIMERS( objects ); |
| 74 |
delete objects.last(); |
| 75 |
QCOMPARE( objects.count(), 2 ); |
| 76 |
} |
| 77 |
|
| 78 |
void testRemoveAt() |
| 79 |
{ |
| 80 |
THREE_TIMERS( os ); |
| 81 |
QTimer* t = os[1]; |
| 82 |
os.removeAt( 1 ); |
| 83 |
os << t; |
| 84 |
QCOMPARE( os.count(), 3 ); |
| 85 |
delete t; |
| 86 |
QCOMPARE( os.count(), 2 ); |
| 87 |
} |
| 88 |
|
| 89 |
void testMultipleOrgasms() |
| 90 |
{ |
| 91 |
THREE_TIMERS( os ); |
| 92 |
for (int x = 0; x < 10; ++x) |
| 93 |
os << os.last(); |
| 94 |
QCOMPARE( os.count(), 13 ); |
| 95 |
delete os.last(); |
| 96 |
QCOMPARE( os.count(), 2 ); |
| 97 |
} |
| 98 |
|
| 99 |
void testForeach() |
| 100 |
{ |
| 101 |
THREE_TIMERS( objects ); |
| 102 |
int x = 0; |
| 103 |
foreach (QTimer* o, objects) { |
| 104 |
(void) o; |
| 105 |
x++; |
| 106 |
} |
| 107 |
QCOMPARE( x, 3 ); |
| 108 |
} |
| 109 |
|
| 110 |
void testOperatorPlus() |
| 111 |
{ |
| 112 |
THREE_TIMERS( os1 ); |
| 113 |
SmartPointerList<QTimer> os2 = os1; |
| 114 |
|
| 115 |
QCOMPARE( (os1 + os2).count(), 6 ); |
| 116 |
delete os1.last(); |
| 117 |
QCOMPARE( (os1 + os2).count(), 4 ); |
| 118 |
} |
| 119 |
|
| 120 |
void testOperatorPlusEquals() |
| 121 |
{ |
| 122 |
THREE_TIMERS( os ); |
| 123 |
os += os; |
| 124 |
os += os; |
| 125 |
QCOMPARE( os.count(), 12 ); |
| 126 |
QTimer* t = os.takeLast(); |
| 127 |
QCOMPARE( os.count(), 11 ); |
| 128 |
delete t; |
| 129 |
QCOMPARE( os.count(), 8 ); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
QTEST_MAIN( TestSmartPointerList ) |
| 135 |
#include "TestSmartPointerList.moc" |