diff options
author | Niko Catania <niko@google.com> | 2010-02-11 10:35:15 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2010-02-11 10:35:15 -0800 |
commit | be8a5178d184c6ebfe11ba5ff3b37b5ca7a4bb60 (patch) | |
tree | 3ff24a4a9cffb1e69e92342cb3556a4ff90c890c | |
parent | 0d0613671834f13e40436d6becddab8f426de289 (diff) | |
parent | 77af30859da3429ae4a13ae5cea2face80c35fa7 (diff) | |
download | astl-be8a5178d184c6ebfe11ba5ff3b37b5ca7a4bb60.tar.gz |
Merge "Added at() method to vector. We use a dummy static field as the returned reference when the index is out of bound."
-rw-r--r-- | include/vector | 12 | ||||
-rw-r--r-- | tests/test_vector.cpp | 9 |
2 files changed, 21 insertions, 0 deletions
diff --git a/include/vector b/include/vector index 051da1d..b17cc70 100644 --- a/include/vector +++ b/include/vector @@ -132,6 +132,13 @@ class vector // @return A reference to the element. reference operator[](size_type index) { return *(mBegin + index); } + // 'at' is similar to operator[] except that it does check bounds. + const_reference at(const size_type index) const + { return index < mLength ? *( mBegin + index) : sDummy; } + + reference at(const size_type index) + { return index < mLength ? *( mBegin + index) : sDummy; } + iterator begin() { return iterator(mBegin); } iterator end() { return iterator(mBegin + mLength); } @@ -217,6 +224,7 @@ class vector pointer mBegin; size_type mCapacity; size_type mLength; + static value_type sDummy; // at() doen't throw exception and returns mDummy. static const size_type kExponentialFactor = 2; static const size_type kExponentialLimit = 256; static const size_type kLinearIncrement = 256; @@ -234,6 +242,7 @@ class vector // // Invariant: mLength <= mCapacity <= max_size() + template<typename _T> vector<_T>::vector() :mBegin(NULL), mCapacity(0), mLength(0) { } @@ -490,6 +499,9 @@ void vector<_T>::deallocate() free(mBegin); } +// Dummy element returned when at() is out of bound. +template<typename _T> _T vector<_T>::sDummy; + } // namespace std #endif // ANDROID_ASTL_VECTOR__ diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index 8b85b97..95de15e 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -641,6 +641,14 @@ bool testEraseRange() return true; } +// Valgrind should not barf when we access element out of bound. +bool testAt() { + vector<int> vec; + + vec.at(1000) = 0xdeadbeef; + EXPECT_TRUE(vec.at(1000) == 0xdeadbeef); + return true; +} } // namespace android int main(int argc, char **argv) @@ -659,5 +667,6 @@ int main(int argc, char **argv) FAIL_UNLESS(testCtorDtorForNonPod); FAIL_UNLESS(testEraseElt); FAIL_UNLESS(testEraseRange); + FAIL_UNLESS(testAt); return kPassed; } |