From fb7404e569b5c69dd9e1c6eab3157826442872e8 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Wed, 14 Dec 2011 17:49:35 +0100 Subject: [PATCH] Implement (and unit test) simple QVarLengthArray::first()/last(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Pure syntactical sugar, to match up with what the other container classes offer. Change-Id: I0f97de011923d9d204cca0fa906b059dc5054a89 Reviewed-by: João Abecasis Reviewed-by: Bradley T. Hughes --- src/corelib/tools/qvarlengtharray.h | 4 ++ src/corelib/tools/qvarlengtharray.qdoc | 30 ++++++++++++++ .../tools/qvarlengtharray/tst_qvarlengtharray.cpp | 46 ++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index fd8c26c..48b14e6 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -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); } diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index a932e9f..032521d 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -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. diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp index 54bab55..105b7f7 100644 --- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp @@ -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 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 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" -- 2.7.4