summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Burchell <robin.burchell@collabora.com>2011-12-14 17:49:35 +0100
committerQt by Nokia <qt-info@nokia.com>2012-01-02 15:08:19 +0100
commitfb7404e569b5c69dd9e1c6eab3157826442872e8 (patch)
tree70dc14d55cb8784145da0954ad5a82eda71c837d
parent467a000089f741dd6def17dacb7f1bcc98a9249a (diff)
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>
-rw-r--r--src/corelib/tools/qvarlengtharray.h4
-rw-r--r--src/corelib/tools/qvarlengtharray.qdoc30
-rw-r--r--tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp46
3 files changed, 80 insertions, 0 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h
index fd8c26c128..48b14e6816 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 a932e9fbcb..032521d1c2 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 54bab55360..105b7f7314 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<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"