diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/memory.h | 12 | ||||
-rw-r--r-- | src/common/memory_unittest.cc | 7 |
2 files changed, 18 insertions, 1 deletions
diff --git a/src/common/memory.h b/src/common/memory.h index a02ac578..e90bd52c 100644 --- a/src/common/memory.h +++ b/src/common/memory.h @@ -145,6 +145,18 @@ class wasteful_vector { used_(0) { } + T& back() { + return a_[used_ - 1]; + } + + const T& back() const { + return a_[used_ - 1]; + } + + bool empty() const { + return used_ == 0; + } + void push_back(const T& new_element) { if (used_ == allocated_) Realloc(allocated_ * 2); diff --git a/src/common/memory_unittest.cc b/src/common/memory_unittest.cc index 8b2ec410..d580c1fe 100644 --- a/src/common/memory_unittest.cc +++ b/src/common/memory_unittest.cc @@ -69,6 +69,7 @@ typedef testing::Test WastefulVectorTest; TEST(WastefulVectorTest, Setup) { PageAllocator allocator_; wasteful_vector<int> v(&allocator_); + ASSERT_TRUE(v.empty()); ASSERT_EQ(v.size(), 0u); } @@ -76,8 +77,12 @@ TEST(WastefulVectorTest, Simple) { PageAllocator allocator_; wasteful_vector<unsigned> v(&allocator_); - for (unsigned i = 0; i < 256; ++i) + for (unsigned i = 0; i < 256; ++i) { v.push_back(i); + ASSERT_EQ(i, v.back()); + ASSERT_EQ(&v.back(), &v[i]); + } + ASSERT_FALSE(v.empty()); ASSERT_EQ(v.size(), 256u); for (unsigned i = 0; i < 256; ++i) ASSERT_EQ(v[i], i); |