0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
|
|
45 |
|
|
46 |
#include "q3valuevector.h"
|
|
47 |
|
|
48 |
#include <qiodevice.h>
|
|
49 |
#include <q3valuevector.h>
|
|
50 |
|
|
51 |
//TESTED_CLASS=
|
|
52 |
//TESTED_FILES=
|
|
53 |
|
|
54 |
class tst_Q3ValueVector : public QObject
|
|
55 |
{
|
|
56 |
Q_OBJECT
|
|
57 |
|
|
58 |
public:
|
|
59 |
tst_Q3ValueVector();
|
|
60 |
virtual ~tst_Q3ValueVector();
|
|
61 |
|
|
62 |
|
|
63 |
public slots:
|
|
64 |
void init();
|
|
65 |
void cleanup();
|
|
66 |
private slots:
|
|
67 |
void acc_01();
|
|
68 |
|
|
69 |
void operator_equalEqual();
|
|
70 |
void operator_assign();
|
|
71 |
// a test for assignment from a std::list has been omitted
|
|
72 |
void size();
|
|
73 |
void empty();
|
|
74 |
void capacity_data();
|
|
75 |
void capacity();
|
|
76 |
// ommiting test for const iterators (for begin() and end())
|
|
77 |
void begin();
|
|
78 |
void end();
|
|
79 |
// omitting test for const reference
|
|
80 |
void at();
|
|
81 |
// omitting test for const reference
|
|
82 |
void operator_index();
|
|
83 |
void front();
|
|
84 |
void back();
|
|
85 |
void push_back();
|
|
86 |
void pop_back();
|
|
87 |
void insert();
|
|
88 |
void reserve_data();
|
|
89 |
void reserve();
|
|
90 |
void resize();
|
|
91 |
void clear();
|
|
92 |
};
|
|
93 |
|
|
94 |
|
|
95 |
static QByteArray toBA( const Q3ValueVector<int> &vv )
|
|
96 |
{
|
|
97 |
QByteArray ba;
|
|
98 |
QDataStream ds( &ba, IO_ReadWrite );
|
|
99 |
(void)vv;
|
|
100 |
ds << vv;
|
|
101 |
return ba;
|
|
102 |
}
|
|
103 |
|
|
104 |
tst_Q3ValueVector::tst_Q3ValueVector()
|
|
105 |
{
|
|
106 |
}
|
|
107 |
|
|
108 |
tst_Q3ValueVector::~tst_Q3ValueVector()
|
|
109 |
{
|
|
110 |
|
|
111 |
}
|
|
112 |
|
|
113 |
void tst_Q3ValueVector::init()
|
|
114 |
{
|
|
115 |
// TODO: Add initialization code here.
|
|
116 |
// This will be executed immediately before each test is run.
|
|
117 |
}
|
|
118 |
|
|
119 |
void tst_Q3ValueVector::cleanup()
|
|
120 |
{
|
|
121 |
// TODO: Add cleanup code here.
|
|
122 |
// This will be executed immediately after each test is run.
|
|
123 |
}
|
|
124 |
|
|
125 |
void tst_Q3ValueVector::reserve_data()
|
|
126 |
{
|
|
127 |
capacity_data();
|
|
128 |
}
|
|
129 |
|
|
130 |
void tst_Q3ValueVector::capacity_data()
|
|
131 |
{
|
|
132 |
QTest::addColumn<QByteArray>("ba");
|
|
133 |
|
|
134 |
Q3ValueVector<int> a;
|
|
135 |
QTest::newRow( "data0" ) << toBA( a );
|
|
136 |
|
|
137 |
Q3ValueVector<int> b;
|
|
138 |
b.push_back( 1 );
|
|
139 |
b.push_back( 2 );
|
|
140 |
QTest::newRow( "data1" ) << toBA( b );
|
|
141 |
|
|
142 |
Q3ValueVector<int> c;
|
|
143 |
|
|
144 |
// let's try one that's rather large
|
|
145 |
|
|
146 |
for(int i = 0; i < 10000; i++)
|
|
147 |
c.push_back( i );
|
|
148 |
|
|
149 |
QTest::newRow( "data2" ) << toBA( c );
|
|
150 |
}
|
|
151 |
|
|
152 |
void tst_Q3ValueVector::acc_01()
|
|
153 |
{
|
|
154 |
// vectors
|
|
155 |
Q3ValueVector<int> v1;
|
|
156 |
QVERIFY( v1.empty() );
|
|
157 |
QVERIFY( v1.size() == 0 );
|
|
158 |
QVERIFY( v1.capacity() >= v1.size() );
|
|
159 |
|
|
160 |
Q3ValueVector<int> v2( v1 );
|
|
161 |
QVERIFY( v2.empty() );
|
|
162 |
QVERIFY( v2.size() == 0 );
|
|
163 |
QVERIFY( v2.capacity() >= v1.size() );
|
|
164 |
|
|
165 |
Q3ValueVector<int> v5( 5 );
|
|
166 |
QVERIFY( !v5.empty() );
|
|
167 |
QVERIFY( v5.size() == 5 );
|
|
168 |
QVERIFY( v5.capacity() >= 5 );
|
|
169 |
|
|
170 |
//operator=
|
|
171 |
Q3ValueVector<int> v4 = v2;
|
|
172 |
QVERIFY( v4.empty() );
|
|
173 |
QVERIFY( v4.size() == 0 );
|
|
174 |
QVERIFY( (int)v4.capacity() >= 0 );
|
|
175 |
|
|
176 |
// adding elements
|
|
177 |
v4.push_back( 1 );
|
|
178 |
v4.push_back( 2 );
|
|
179 |
v4.push_back( 3 );
|
|
180 |
QVERIFY( !v4.empty() );
|
|
181 |
QVERIFY( v2.empty() ); // should have detached
|
|
182 |
QVERIFY( v4.size() == 3 );
|
|
183 |
QVERIFY( v4.capacity() >= v4.size() );
|
|
184 |
v4.insert( v4.end(), 4 );
|
|
185 |
v4.insert( v4.begin(), 0 );
|
|
186 |
QVERIFY( !v4.empty() );
|
|
187 |
QVERIFY( v4.size() == 5 );
|
|
188 |
QVERIFY( v4.capacity() >= v4.size() );
|
|
189 |
|
|
190 |
QVERIFY( v4[0] == 0);
|
|
191 |
QVERIFY( v4[1] == 1);
|
|
192 |
QVERIFY( v4[2] == 2);
|
|
193 |
QVERIFY( v4[3] == 3);
|
|
194 |
QVERIFY( v4[4] == 4);
|
|
195 |
|
|
196 |
// swap
|
|
197 |
Q3ValueVector<int> tmp = v2;
|
|
198 |
v2 = v4;
|
|
199 |
v4 = tmp;
|
|
200 |
// v4.swap( v2 );
|
|
201 |
QVERIFY( v4.empty() );
|
|
202 |
QVERIFY( !v2.empty() );
|
|
203 |
QVERIFY( v2.size() == 5 );
|
|
204 |
QVERIFY( v2.capacity() >= v2.size() );
|
|
205 |
QVERIFY( v2[0] == 0);
|
|
206 |
QVERIFY( v2[1] == 1);
|
|
207 |
QVERIFY( v2[2] == 2);
|
|
208 |
QVERIFY( v2[3] == 3);
|
|
209 |
QVERIFY( v2[4] == 4);
|
|
210 |
// v2 Should contain 5 elements: 0,1,2,3,4
|
|
211 |
|
|
212 |
QVERIFY(v4.size() == 0); //Should contain no elements
|
|
213 |
|
|
214 |
// element access
|
|
215 |
Q3ValueVector<int> v3( 5 );
|
|
216 |
v3[0] = 0;
|
|
217 |
v3[1] = 1;
|
|
218 |
v3[2] = 2;
|
|
219 |
v3[3] = 3;
|
|
220 |
v3[4] = 4;
|
|
221 |
QVERIFY( v3[0] == 0 );
|
|
222 |
QVERIFY( v3[1] == 1 );
|
|
223 |
QVERIFY( v3[2] == 2 );
|
|
224 |
QVERIFY( v3[3] == 3 );
|
|
225 |
QVERIFY( v3[4] == 4 );
|
|
226 |
bool ok = FALSE;
|
|
227 |
(void) v3.at( 1000, &ok );
|
|
228 |
QVERIFY( !ok );
|
|
229 |
int& j = v3.at( 2, &ok );
|
|
230 |
QVERIFY( ok );
|
|
231 |
QVERIFY( j == 2 );
|
|
232 |
|
|
233 |
// iterators
|
|
234 |
Q3ValueVector<int>::iterator it = v3.begin();
|
|
235 |
int k = 0;
|
|
236 |
for ( ; k < 5; ++k, ++it )
|
|
237 |
QVERIFY( *it == k );
|
|
238 |
QVERIFY( it == v3.end() );
|
|
239 |
--it;
|
|
240 |
for ( k = 4; k >= 0; --k, --it )
|
|
241 |
QVERIFY( *it == k );
|
|
242 |
|
|
243 |
QVERIFY( v3.front() == 0 );
|
|
244 |
QVERIFY( v3.back() == 4 );
|
|
245 |
|
|
246 |
// capacity stuff
|
|
247 |
v3.resize( 5 );
|
|
248 |
// Should contain 5 elements: 0,1,2,3,4
|
|
249 |
QVERIFY( v3.size() == 5 );
|
|
250 |
QVERIFY( v3[0] == 0 );
|
|
251 |
QVERIFY( v3[1] == 1 );
|
|
252 |
QVERIFY( v3[2] == 2 );
|
|
253 |
QVERIFY( v3[3] == 3 );
|
|
254 |
QVERIFY( v3[4] == 4 );
|
|
255 |
|
|
256 |
v3.resize( 6 );
|
|
257 |
// Should now contain 6 elements: 0,1,2,3,4,0
|
|
258 |
QVERIFY( v3[0] == 0 );
|
|
259 |
QVERIFY( v3[1] == 1 );
|
|
260 |
QVERIFY( v3[2] == 2 );
|
|
261 |
QVERIFY( v3[3] == 3 );
|
|
262 |
QVERIFY( v3[4] == 4 );
|
|
263 |
QVERIFY( v3[5] == 0 );
|
|
264 |
|
|
265 |
v3.reserve( 1000 );
|
|
266 |
QVERIFY( v3.size() == 6 );
|
|
267 |
QVERIFY( v3[0] == 0 );
|
|
268 |
QVERIFY( v3[1] == 1 );
|
|
269 |
QVERIFY( v3[2] == 2 );
|
|
270 |
QVERIFY( v3[3] == 3 );
|
|
271 |
QVERIFY( v3[4] == 4 );
|
|
272 |
QVERIFY( v3.capacity() >= 1000 );
|
|
273 |
|
|
274 |
v3.back() = 5;
|
|
275 |
// Should contain 5 elements: 0,1,2,3,4
|
|
276 |
QVERIFY( v3.back() == 5 );
|
|
277 |
QVERIFY( v3[0] == 0 );
|
|
278 |
QVERIFY( v3[1] == 1 );
|
|
279 |
QVERIFY( v3[2] == 2 );
|
|
280 |
QVERIFY( v3[3] == 3 );
|
|
281 |
QVERIFY( v3[4] == 4 );
|
|
282 |
|
|
283 |
v3.resize( 5 );
|
|
284 |
// Should contain 5 elements: 0,1,2,3,4
|
|
285 |
QVERIFY( v3.size() == 5 );
|
|
286 |
QVERIFY( v3[0] == 0 );
|
|
287 |
QVERIFY( v3[1] == 1 );
|
|
288 |
QVERIFY( v3[2] == 2 );
|
|
289 |
QVERIFY( v3[3] == 3 );
|
|
290 |
QVERIFY( v3[4] == 4 );
|
|
291 |
QVERIFY( v3.capacity() >= 1000 );
|
|
292 |
|
|
293 |
it = v3.end();
|
|
294 |
v3.erase( --it );
|
|
295 |
// Should contain 4 elements: 0,1,2,3
|
|
296 |
QVERIFY( v3.size() == 4 );
|
|
297 |
QVERIFY( v3[0] == 0 );
|
|
298 |
QVERIFY( v3[1] == 1 );
|
|
299 |
QVERIFY( v3[2] == 2 );
|
|
300 |
QVERIFY( v3[3] == 3 );
|
|
301 |
QVERIFY( v3.capacity() >= 1000 );
|
|
302 |
|
|
303 |
it = v3.begin();
|
|
304 |
Q3ValueVector<int>::iterator it2 = v3.end();
|
|
305 |
v3.erase( ++it, --it2 );
|
|
306 |
// Should contain 2 elements: 0,3
|
|
307 |
QVERIFY( v3.size() == 2 );
|
|
308 |
QVERIFY( v3[0] == 0 );
|
|
309 |
QVERIFY( v3[1] == 3 );
|
|
310 |
QVERIFY( v3.capacity() >= 1000 );
|
|
311 |
|
|
312 |
it = v3.begin();
|
|
313 |
v3.insert( ++it, 9 );
|
|
314 |
// Should contain 3 elements: 0,9,3
|
|
315 |
QVERIFY( v3.size() == 3 );
|
|
316 |
QVERIFY( v3[0] == 0 );
|
|
317 |
QVERIFY( v3[1] == 9 );
|
|
318 |
QVERIFY( v3[2] == 3 );
|
|
319 |
QVERIFY( v3.capacity() >= 1000 );
|
|
320 |
|
|
321 |
it = v3.begin();
|
|
322 |
v3.insert( ++it, 4, 4 );
|
|
323 |
// Should contain 7 elements: 0,4,4,4,4,9,3
|
|
324 |
QVERIFY( v3.size() == 7 );
|
|
325 |
QVERIFY( v3[0] == 0 );
|
|
326 |
QVERIFY( v3[1] == 4 );
|
|
327 |
QVERIFY( v3[2] == 4 );
|
|
328 |
QVERIFY( v3[3] == 4 );
|
|
329 |
QVERIFY( v3[4] == 4 );
|
|
330 |
QVERIFY( v3[5] == 9 );
|
|
331 |
QVERIFY( v3[6] == 3 );
|
|
332 |
QVERIFY( v3.capacity() >= 1000 );
|
|
333 |
|
|
334 |
it = v3.begin();
|
|
335 |
v3.insert( ++it, 2000, 2 );
|
|
336 |
// Should contain 2007 elements: 0,2,2,...2,4,4,4,4,9,3
|
|
337 |
QVERIFY( v3.size() == 2007 );
|
|
338 |
|
|
339 |
it = qFind( v3.begin(), v3.end(), 3 );
|
|
340 |
it2 = v3.end();
|
|
341 |
QVERIFY( it == --it2 );
|
|
342 |
|
|
343 |
v3.resize( 4 );
|
|
344 |
// Should contain 4 elements: 0,2,2,2
|
|
345 |
QVERIFY( v3.size() == 4 );
|
|
346 |
QVERIFY( v3[0] == 0 );
|
|
347 |
QVERIFY( v3[1] == 2 );
|
|
348 |
QVERIFY( v3[2] == 2 );
|
|
349 |
QVERIFY( v3[3] == 2 );
|
|
350 |
QVERIFY( v3.capacity() >= 2007 );
|
|
351 |
|
|
352 |
it = v3.begin();
|
|
353 |
v3.insert( ++it, 2000, 2 );
|
|
354 |
v3.push_back( 9 );
|
|
355 |
v3.push_back( 3 );
|
|
356 |
it = v3.begin();
|
|
357 |
it2 = v3.end();
|
|
358 |
v3.erase( ++it, ----it2 );
|
|
359 |
// Should contain 3 elements: 0,9,3
|
|
360 |
QVERIFY( v3.size() == 3 );
|
|
361 |
|
|
362 |
v3.pop_back();
|
|
363 |
// Should contain 2 elements: 0,9
|
|
364 |
QVERIFY( v3.size() == 2 );
|
|
365 |
|
|
366 |
/*
|
|
367 |
// instantiate other member functions
|
|
368 |
Q3ValueVector<int>::const_iterator cit = v3.begin();
|
|
369 |
cit = v3.end();
|
|
370 |
Q3ValueVector<int>::size_type max_size = v3.max_size();
|
|
371 |
std::cout << "max size of vector:" << max_size << std::endl;
|
|
372 |
const int& ci = v3.at( 1 );
|
|
373 |
const int& ci2 = v3[1];
|
|
374 |
const int& ci3 = v3.front();
|
|
375 |
const int& ci4 = v3.back();
|
|
376 |
v3.clear();
|
|
377 |
|
|
378 |
QStringList l1, l2;
|
|
379 |
l1 << "Weis" << "Ettrich" << "Arnt" << "Sue";
|
|
380 |
l2 << "Torben" << "Matthias";
|
|
381 |
qCopy( l2.begin(), l2.end(), l1.begin() );
|
|
382 |
|
|
383 |
Q3ValueVector<QString> v( l1.size(), "Dave" );
|
|
384 |
qCopy( l2.begin(), l2.end(), v.begin() );
|
|
385 |
std::for_each( v.begin(), v.end(), qDebug );
|
|
386 |
|
|
387 |
std::vector<int> stdvec( 5, 100 );
|
|
388 |
Q3ValueVector<int> cvec( stdvec );
|
|
389 |
std::cout << "Should contain 5 elements: 100,100,100,100,100" << std::endl;
|
|
390 |
print( cvec );
|
|
391 |
Q3ValueVector<int> cvec2 = stdvec;
|
|
392 |
std::cout << "Should contain 5 elements: 100,100,100,100,100" << std::endl;
|
|
393 |
print( cvec2 );
|
|
394 |
|
|
395 |
QFile f( "file.dta" );
|
|
396 |
f.open( QIODevice::WriteOnly );
|
|
397 |
QDataStream s( &f );
|
|
398 |
s << cvec2;
|
|
399 |
f.close();
|
|
400 |
|
|
401 |
f.open( QIODevice::ReadOnly );
|
|
402 |
Q3ValueVector<int> in;
|
|
403 |
s >> in;
|
|
404 |
std::cout << "Should contain 5 elements: 100,100,100,100,100" << std::endl;
|
|
405 |
print( in );
|
|
406 |
*/
|
|
407 |
}
|
|
408 |
|
|
409 |
void tst_Q3ValueVector::operator_equalEqual()
|
|
410 |
{
|
|
411 |
Q3ValueVector<int> a;
|
|
412 |
a.push_back( 1 );
|
|
413 |
a.push_back( 10 );
|
|
414 |
a.push_back( 100 );
|
|
415 |
|
|
416 |
Q3ValueVector<int> b;
|
|
417 |
b.push_back( 1 );
|
|
418 |
b.push_back( 10 );
|
|
419 |
b.push_back( 100 );
|
|
420 |
|
|
421 |
QVERIFY( a == b );
|
|
422 |
QVERIFY( b == a );
|
|
423 |
|
|
424 |
a.push_back( 1000 );
|
|
425 |
QVERIFY( !(a == b) );
|
|
426 |
QVERIFY( !(b == a) );
|
|
427 |
}
|
|
428 |
|
|
429 |
void tst_Q3ValueVector::operator_assign()
|
|
430 |
{
|
|
431 |
// test assignment using an int
|
|
432 |
|
|
433 |
Q3ValueVector<int> a;
|
|
434 |
a.push_back( 1 );
|
|
435 |
a.push_back( 2 );
|
|
436 |
Q3ValueVector<int> b = a;
|
|
437 |
QVERIFY( (a == b) );
|
|
438 |
}
|
|
439 |
|
|
440 |
void tst_Q3ValueVector::size()
|
|
441 |
{
|
|
442 |
Q3ValueVector<int> a;
|
|
443 |
a.push_back( 1 );
|
|
444 |
a.push_back( 2 );
|
|
445 |
QCOMPARE( (int)a.size(), 2 );
|
|
446 |
}
|
|
447 |
|
|
448 |
void tst_Q3ValueVector::empty()
|
|
449 |
{
|
|
450 |
Q3ValueVector<int> a;
|
|
451 |
QVERIFY( a.empty() );
|
|
452 |
}
|
|
453 |
|
|
454 |
void tst_Q3ValueVector::capacity()
|
|
455 |
{
|
|
456 |
QFETCH( QByteArray, ba );
|
|
457 |
Q3ValueVector<int> vector;
|
|
458 |
|
|
459 |
QDataStream ds( &ba, IO_ReadWrite );
|
|
460 |
ds >> vector;
|
|
461 |
|
|
462 |
QVERIFY( vector.capacity() >= vector.size() );
|
|
463 |
}
|
|
464 |
|
|
465 |
void tst_Q3ValueVector::begin()
|
|
466 |
{
|
|
467 |
Q3ValueVector<int> a;
|
|
468 |
a.push_back( 1 );
|
|
469 |
a.push_back( 2 );
|
|
470 |
|
|
471 |
Q3ValueVector<int>::iterator it_a = a.begin();
|
|
472 |
QCOMPARE( (*it_a), 1 );
|
|
473 |
|
|
474 |
// now try it for an empty vector; per the documentation begin() should equal end()
|
|
475 |
|
|
476 |
Q3ValueVector<int> b;
|
|
477 |
Q3ValueVector<int>::iterator it_b = b.begin();
|
|
478 |
QVERIFY( it_b == b.end() );
|
|
479 |
}
|
|
480 |
|
|
481 |
void tst_Q3ValueVector::end()
|
|
482 |
{
|
|
483 |
Q3ValueVector<int> a;
|
|
484 |
a.push_back( 1 );
|
|
485 |
a.push_back( 2 );
|
|
486 |
|
|
487 |
Q3ValueVector<int>::iterator it_a = a.end();
|
|
488 |
QCOMPARE( (*(--it_a)), 2 );
|
|
489 |
|
|
490 |
Q3ValueVector<int> b;
|
|
491 |
Q3ValueVector<int>::iterator it_b = b.end();
|
|
492 |
QVERIFY( it_b == b.begin() );
|
|
493 |
}
|
|
494 |
|
|
495 |
void tst_Q3ValueVector::at()
|
|
496 |
{
|
|
497 |
Q3ValueVector<int> a;
|
|
498 |
a.push_back( 1 );
|
|
499 |
a.push_back( 2 );
|
|
500 |
|
|
501 |
bool ok;
|
|
502 |
QVERIFY( a.at( 0, &ok ) == 1 && ok == true );
|
|
503 |
QVERIFY( a.at( 1, &ok ) == 2 && ok == true );
|
|
504 |
(void)a.at( 3, &ok );
|
|
505 |
QCOMPARE( ok, false );
|
|
506 |
}
|
|
507 |
|
|
508 |
void tst_Q3ValueVector::operator_index()
|
|
509 |
{
|
|
510 |
Q3ValueVector<int> a;
|
|
511 |
a.push_back( 1 );
|
|
512 |
a.push_back( 2 );
|
|
513 |
|
|
514 |
QCOMPARE( a[0], 1 );
|
|
515 |
QCOMPARE( a[1], 2 );
|
|
516 |
}
|
|
517 |
|
|
518 |
void tst_Q3ValueVector::front()
|
|
519 |
{
|
|
520 |
Q3ValueVector<int> a;
|
|
521 |
a.push_back( 1 );
|
|
522 |
a.push_back( 2 );
|
|
523 |
|
|
524 |
QCOMPARE(a.front(), 1 );
|
|
525 |
}
|
|
526 |
|
|
527 |
void tst_Q3ValueVector::back()
|
|
528 |
{
|
|
529 |
Q3ValueVector<int> a;
|
|
530 |
a.push_back( 1 );
|
|
531 |
a.push_back( 2 );
|
|
532 |
|
|
533 |
QCOMPARE(a.back(), 2 );
|
|
534 |
}
|
|
535 |
|
|
536 |
void tst_Q3ValueVector::push_back()
|
|
537 |
{
|
|
538 |
Q3ValueVector<int> a;
|
|
539 |
|
|
540 |
a.push_back( 1 );
|
|
541 |
QVERIFY( a.back() == 1 && a.size() == 1 );
|
|
542 |
|
|
543 |
a.push_back( 2 );
|
|
544 |
QVERIFY( a.back() == 2 && a.size() == 2 );
|
|
545 |
}
|
|
546 |
|
|
547 |
void tst_Q3ValueVector::pop_back()
|
|
548 |
{
|
|
549 |
Q3ValueVector<int> a;
|
|
550 |
a.push_back( 1 );
|
|
551 |
a.push_back( 2 );
|
|
552 |
|
|
553 |
a.pop_back();
|
|
554 |
QVERIFY( a.back() == 1 && a.size() == 1 );
|
|
555 |
|
|
556 |
a.pop_back();
|
|
557 |
QVERIFY( a.empty() );
|
|
558 |
}
|
|
559 |
|
|
560 |
void tst_Q3ValueVector::insert()
|
|
561 |
{
|
|
562 |
// insert at the beginning
|
|
563 |
Q3ValueVector<int> a;
|
|
564 |
a.insert( a.begin(), 1 );
|
|
565 |
QCOMPARE( a[0], 1 );
|
|
566 |
|
|
567 |
// insert at the end
|
|
568 |
a.insert( a.end(), 2 );
|
|
569 |
QCOMPARE( a[1], 2 );
|
|
570 |
// insert in the middle
|
|
571 |
Q3ValueVector<int>::iterator it_a = a.begin();
|
|
572 |
a.insert( ++it_a, 3 );
|
|
573 |
QCOMPARE( a[1], 3 );
|
|
574 |
|
|
575 |
// now testing the overloaded insert() which takes an
|
|
576 |
// argument for the number of items to insert
|
|
577 |
|
|
578 |
// we'll insert two of each value
|
|
579 |
|
|
580 |
Q3ValueVector<int> b;
|
|
581 |
b.insert( b.begin(), 2, 1 );
|
|
582 |
QCOMPARE( b[0], 1 );
|
|
583 |
QCOMPARE( b[1], 1 );
|
|
584 |
|
|
585 |
// insert at the end
|
|
586 |
b.insert( b.end(), 2, 2 );
|
|
587 |
QCOMPARE( b[2], 2 );
|
|
588 |
QCOMPARE( b[3], 2 );
|
|
589 |
|
|
590 |
// insert in the middle
|
|
591 |
Q3ValueVector<int>::iterator it_b = b.begin();
|
|
592 |
b.insert( ++++it_b, 2, 3 );
|
|
593 |
QCOMPARE( b[2], 3 );
|
|
594 |
QCOMPARE( b[3], 3 );
|
|
595 |
}
|
|
596 |
|
|
597 |
void tst_Q3ValueVector::reserve()
|
|
598 |
{
|
|
599 |
QFETCH( QByteArray, ba );
|
|
600 |
Q3ValueVector<int> vector;
|
|
601 |
|
|
602 |
QDataStream ds( &ba, IO_ReadWrite );
|
|
603 |
ds >> vector;
|
|
604 |
|
|
605 |
Q3ValueVector<int>::size_type cap = vector.capacity();
|
|
606 |
|
|
607 |
// should do nothing
|
|
608 |
if( cap > 5 )
|
|
609 |
vector.reserve( vector.capacity() - 5 );
|
|
610 |
else
|
|
611 |
vector.reserve( 0 );
|
|
612 |
|
|
613 |
QVERIFY( vector.capacity() == cap );
|
|
614 |
|
|
615 |
// should make capacity() grow
|
|
616 |
vector.reserve( vector.capacity() + 5 );
|
|
617 |
QVERIFY( cap < vector.capacity() );
|
|
618 |
}
|
|
619 |
|
|
620 |
void tst_Q3ValueVector::resize()
|
|
621 |
{
|
|
622 |
Q3ValueVector<int> a;
|
|
623 |
a.resize( 2 );
|
|
624 |
|
|
625 |
QVERIFY( a.size() == 2 );
|
|
626 |
|
|
627 |
Q3ValueVector<int> b;
|
|
628 |
b.resize( 2, 42 );
|
|
629 |
|
|
630 |
QVERIFY( b.size() == 2 );
|
|
631 |
QCOMPARE( b[0], 42 );
|
|
632 |
QCOMPARE( b[1], 42 );
|
|
633 |
|
|
634 |
b.resize( 1 );
|
|
635 |
QVERIFY( b.size() == 1 );
|
|
636 |
|
|
637 |
b.resize( 4, 21 );
|
|
638 |
QCOMPARE( b[0], 42 );
|
|
639 |
QCOMPARE( b[1], 21 );
|
|
640 |
QCOMPARE( b[2], 21 );
|
|
641 |
QCOMPARE( b[3], 21 );
|
|
642 |
|
|
643 |
b.resize( 0 );
|
|
644 |
QVERIFY( b.empty() );
|
|
645 |
|
|
646 |
}
|
|
647 |
|
|
648 |
void tst_Q3ValueVector::clear()
|
|
649 |
{
|
|
650 |
Q3ValueVector<int> a;
|
|
651 |
a.clear();
|
|
652 |
QVERIFY( a.empty() );
|
|
653 |
|
|
654 |
a.push_back( 1 );
|
|
655 |
a.push_back( 2 );
|
|
656 |
|
|
657 |
a.clear();
|
|
658 |
QVERIFY( a.empty() );
|
|
659 |
}
|
|
660 |
|
|
661 |
QTEST_APPLESS_MAIN(tst_Q3ValueVector)
|
|
662 |
#include "tst_q3valuevector.moc"
|