Skip to content

Commit

Permalink
util: Add test for MemBlockBuilder::release(), which was not working …
Browse files Browse the repository at this point in the history
…(discovered in envoyproxy#8779). (envoyproxy#9360)

* Add test for release(), which was not working (discovered in envoyproxy#8779).

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Prakhar <prakhar_au@yahoo.com>
  • Loading branch information
jmarantz authored and prakhag1 committed Jan 3, 2020
1 parent d18d7dd commit 1f526f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
16 changes: 9 additions & 7 deletions source/common/common/mem_block_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ template <class T> class MemBlockBuilder {
MemBlockBuilder() {}

/**
* Populates (or repopulates) the MemBlockBuilder to make it the specified
* capacity. This does not have resize semantics; when populate() is called any
* previous contents are erased.
* Allocates (or reallocates) memory for the MemBlockBuilder to make it the
* specified capacity. This does not have resize semantics; when setCapacity()
* is called any previous contents are erased.
*
* @param capacity The number of memory elements to allocate.
*/
void populate(uint64_t capacity) { populateHelper(capacity, std::make_unique<T[]>(capacity)); }
void setCapacity(uint64_t capacity) {
setCapacityHelper(capacity, std::make_unique<T[]>(capacity));
}

/**
* @return the capacity.
Expand Down Expand Up @@ -88,15 +90,15 @@ template <class T> class MemBlockBuilder {
/**
* Empties the contents of this.
*/
void reset() { populateHelper(0, std::unique_ptr<T[]>(nullptr)); }
void reset() { setCapacityHelper(0, std::unique_ptr<T[]>(nullptr)); }

/**
* Returns the underlying storage as a unique pointer, clearing this.
*
* @return the transferred storage.
*/
std::unique_ptr<T[]> release() {
write_span_.reset();
write_span_ = absl::MakeSpan(static_cast<T*>(nullptr), 0);
return std::move(data_);
}

Expand All @@ -111,7 +113,7 @@ template <class T> class MemBlockBuilder {
uint64_t size() const { return write_span_.data() - data_.get(); }

private:
void populateHelper(uint64_t capacity, std::unique_ptr<T[]> data) {
void setCapacityHelper(uint64_t capacity, std::unique_ptr<T[]> data) {
data_ = std::move(data);
write_span_ = absl::MakeSpan(data_.get(), capacity);
}
Expand Down
13 changes: 9 additions & 4 deletions test/common/common/mem_block_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ TEST(MemBlockBuilderTest, AppendUint8) {

MemBlockBuilder<uint8_t> append;
EXPECT_EQ(0, append.capacity());
append.populate(7);
append.setCapacity(7);
EXPECT_EQ(7, append.capacity());
append.appendOne(8);
append.appendOne(9);
Expand All @@ -28,7 +28,10 @@ TEST(MemBlockBuilderTest, AppendUint8) {

append.appendBlock(mem_block);
EXPECT_EQ(0, append.capacityRemaining());
EXPECT_EQ((std::vector<uint8_t>{8, 9, 5, 6, 7, 8, 9}), append.span());
uint64_t size = append.size();
std::unique_ptr<uint8_t[]> data = append.release();
EXPECT_EQ((std::vector<uint8_t>{8, 9, 5, 6, 7, 8, 9}),
std::vector<uint8_t>(data.get(), data.get() + size));

mem_block.reset();
EXPECT_EQ(0, mem_block.capacity());
Expand All @@ -45,7 +48,7 @@ TEST(MemBlockBuilderTest, AppendUint32) {

MemBlockBuilder<uint32_t> append;
EXPECT_EQ(0, append.capacity());
append.populate(7);
append.setCapacity(7);
EXPECT_EQ(7, append.capacity());
append.appendOne(100008);
append.appendOne(100009);
Expand All @@ -56,8 +59,10 @@ TEST(MemBlockBuilderTest, AppendUint32) {

append.appendBlock(mem_block);
EXPECT_EQ(0, append.capacityRemaining());
uint64_t size = append.size();
std::unique_ptr<uint32_t[]> data = append.release();
EXPECT_EQ((std::vector<uint32_t>{100008, 100009, 100005, 100006, 100007, 100008, 100009}),
append.span());
std::vector<uint32_t>(data.get(), data.get() + size));

mem_block.reset();
EXPECT_EQ(0, mem_block.capacity());
Expand Down

0 comments on commit 1f526f1

Please sign in to comment.