Implement (and unit test) simple QVarLengthArray::first()/last().

Pure syntactical sugar, to match up with what the other container
classes offer.

Change-Id: I0f97de011923d9d204cca0fa906b059dc5054a89
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
This commit is contained in:
Robin Burchell 2011-12-14 17:49:35 +01:00 committed by Qt by Nokia
parent 467a000089
commit fb7404e569
3 changed files with 80 additions and 0 deletions

View File

@ -96,6 +96,10 @@ public:
inline int size() const { return s; }
inline int count() const { return s; }
inline int length() const { return s; }
inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
inline const T& first() const { Q_ASSERT(!isEmpty()); return *begin(); }
T& last() { Q_ASSERT(!isEmpty()); return *(end() - 1); }
const T& last() const { Q_ASSERT(!isEmpty()); return *(end() - 1); }
inline bool isEmpty() const { return (s == 0); }
inline void resize(int size);
inline void clear() { resize(0); }

View File

@ -125,6 +125,36 @@
\sa isEmpty(), resize()
*/
/*! \fn T& QVarLengthArray::first()
Returns a reference to the first item in the array. The array must
not be empty. If the array can be empty, check isEmpty() before
calling this function.
\sa last(), isEmpty()
*/
/*! \fn const T& QVarLengthArray::first() const
\overload
*/
/*! \fn T& QVarLengthArray::last()
Returns a reference to the last item in the array. The array must
not be empty. If the array can be empty, check isEmpty() before
calling this function.
\sa first(), isEmpty()
*/
/*! \fn const T& QVarLengthArray::last() const
\overload
*/
/*! \fn bool QVarLengthArray::isEmpty() const
Returns true if the array has size 0; otherwise returns false.

View File

@ -56,6 +56,8 @@ private slots:
void resize();
void realloc();
void count();
void first();
void last();
};
int fooCtor = 0;
@ -655,5 +657,49 @@ void tst_QVarLengthArray::count()
}
}
void tst_QVarLengthArray::first()
{
// append some items, make sure it stays sane
QVarLengthArray<int> list;
list.append(27);
QCOMPARE(list.first(), 27);
list.append(4);
QCOMPARE(list.first(), 27);
list.append(1987);
QCOMPARE(list.first(), 27);
QCOMPARE(list.length(), 3);
// remove some, make sure it stays sane
list.removeLast();
QCOMPARE(list.first(), 27);
QCOMPARE(list.length(), 2);
list.removeLast();
QCOMPARE(list.first(), 27);
QCOMPARE(list.length(), 1);
}
void tst_QVarLengthArray::last()
{
// append some items, make sure it stays sane
QVarLengthArray<int> list;
list.append(27);
QCOMPARE(list.last(), 27);
list.append(4);
QCOMPARE(list.last(), 4);
list.append(1987);
QCOMPARE(list.last(), 1987);
QCOMPARE(list.length(), 3);
// remove some, make sure it stays sane
list.removeLast();
QCOMPARE(list.last(), 4);
QCOMPARE(list.length(), 2);
list.removeLast();
QCOMPARE(list.last(), 27);
QCOMPARE(list.length(), 1);
}
QTEST_APPLESS_MAIN(tst_QVarLengthArray)
#include "tst_qvarlengtharray.moc"